use of org.apache.struts2.StrutsException in project struts by apache.
the class StrutsConversionPropertiesProcessor method loadConversionProperties.
public void loadConversionProperties(String propsName, boolean require) {
try {
Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
while (resources.hasNext()) {
if (XWORK_CONVERSION_PROPERTIES.equals(propsName)) {
LOG.warn("Instead of using deprecated {} please use the new file name {}", XWORK_CONVERSION_PROPERTIES, STRUTS_CONVERSION_PROPERTIES);
}
URL url = resources.next();
Properties props = new Properties();
props.load(url.openStream());
LOG.debug("Processing conversion file [{}]", propsName);
for (Object o : props.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String key = (String) entry.getKey();
try {
TypeConverter typeConverter = converterCreator.createTypeConverter((String) entry.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("\t{}:{} [treated as TypeConverter {}]", key, entry.getValue(), typeConverter);
}
converterHolder.addDefaultMapping(key, typeConverter);
} catch (Exception e) {
LOG.error("Conversion registration error", e);
}
}
}
} catch (IOException ex) {
if (require) {
throw new StrutsException("Cannot load conversion properties file: " + propsName, ex);
} else {
LOG.debug("Cannot load conversion properties file: {}", propsName, ex);
}
}
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class Component method findValue.
/**
* <p>
* Evaluates the OGNL stack to find an Object value.
* </p>
*
* <p>
* Function just like <code>findValue(String)</code> except that if the
* given expression is <tt>null</tt> a error is logged and
* a <code>RuntimeException</code> is thrown constructed with a
* messaged based on the given field and errorMsg parameter.
* </p>
*
* @param expr OGNL expression.
* @param field field name used when throwing <code>RuntimeException</code>.
* @param errorMsg error message used when throwing <code>RuntimeException</code>.
* @return the Object found, is never <tt>null</tt>.
* @throws StrutsException is thrown in case of not found in the OGNL stack, or expression is <tt>null</tt>.
*/
protected Object findValue(String expr, String field, String errorMsg) {
if (expr == null) {
throw fieldError(field, errorMsg, null);
} else {
Object value = null;
Exception problem = null;
try {
value = findValue(expr);
} catch (Exception e) {
problem = e;
}
if (value == null) {
throw fieldError(field, errorMsg, problem);
}
return value;
}
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class PackageBasedActionConfigBuilder method buildConfiguration.
@SuppressWarnings("unchecked")
protected void buildConfiguration(Set<Class> classes) {
Map<String, PackageConfig.Builder> packageConfigs = new HashMap<>();
for (Class<?> actionClass : classes) {
Actions actionsAnnotation = actionClass.getAnnotation(Actions.class);
Action actionAnnotation = actionClass.getAnnotation(Action.class);
// Skip classes that can't be instantiated
if (cannotInstantiate(actionClass)) {
LOG.trace("Class [{}] did not pass the instantiation test and will be ignored", actionClass.getName());
continue;
}
if (eagerLoading) {
// Tell the ObjectFactory about this class
try {
objectFactory.getClassInstance(actionClass.getName());
} catch (ClassNotFoundException e) {
LOG.error("Object Factory was unable to load class [{}]", actionClass.getName(), e);
throw new StrutsException("Object Factory was unable to load class " + actionClass.getName(), e);
}
}
// Determine the action package
String actionPackage = actionClass.getPackage().getName();
LOG.debug("Processing class [{}] in package [{}]", actionClass.getName(), actionPackage);
Set<String> allowedMethods = getAllowedMethods(actionClass);
// Determine the default namespace and action name
List<String> namespaces = determineActionNamespace(actionClass);
for (String namespace : namespaces) {
String defaultActionName = determineActionName(actionClass);
PackageConfig.Builder defaultPackageConfig = getPackageConfig(packageConfigs, namespace, actionPackage, actionClass, null);
// Verify that the annotations have no errors and also determine if the default action
// configuration should still be built or not.
Map<String, List<Action>> map = getActionAnnotations(actionClass);
Set<String> actionNames = new HashSet<>();
boolean hasDefaultMethod = ReflectionTools.containsMethod(actionClass, DEFAULT_METHOD);
if (!map.containsKey(DEFAULT_METHOD) && hasDefaultMethod && actionAnnotation == null && actionsAnnotation == null && (alwaysMapExecute || map.isEmpty())) {
boolean found = false;
for (List<Action> actions : map.values()) {
for (Action action : actions) {
// Check if there are duplicate action names in the annotations.
String actionName = action.value().equals(Action.DEFAULT_VALUE) ? defaultActionName : action.value();
if (actionNames.contains(actionName)) {
throw new ConfigurationException("The action class [" + actionClass + "] contains two methods with an action name annotation whose value " + "is the same (they both might be empty as well).");
} else {
actionNames.add(actionName);
}
// Check this annotation is the default action
if (action.value().equals(Action.DEFAULT_VALUE)) {
found = true;
}
}
}
// Build the default
if (!found) {
createActionConfig(defaultPackageConfig, actionClass, defaultActionName, DEFAULT_METHOD, null, allowedMethods);
}
}
// Build the actions for the annotations
for (Map.Entry<String, List<Action>> entry : map.entrySet()) {
String method = entry.getKey();
List<Action> actions = entry.getValue();
for (Action action : actions) {
PackageConfig.Builder pkgCfg = defaultPackageConfig;
if (action.value().contains("/") && !slashesInActionNames) {
pkgCfg = getPackageConfig(packageConfigs, namespace, actionPackage, actionClass, action);
}
createActionConfig(pkgCfg, actionClass, defaultActionName, method, action, allowedMethods);
}
}
// where the action mapper is the one that finds the right method at runtime
if (map.isEmpty() && mapAllMatches && actionAnnotation == null && actionsAnnotation == null) {
createActionConfig(defaultPackageConfig, actionClass, defaultActionName, null, actionAnnotation, allowedMethods);
}
// if there are @Actions or @Action at the class level, create the mappings for them
String methodName = hasDefaultMethod ? DEFAULT_METHOD : null;
if (actionsAnnotation != null) {
List<Action> actionAnnotations = checkActionsAnnotation(actionsAnnotation);
for (Action actionAnnotation2 : actionAnnotations) createActionConfig(defaultPackageConfig, actionClass, defaultActionName, methodName, actionAnnotation2, allowedMethods);
} else if (actionAnnotation != null)
createActionConfig(defaultPackageConfig, actionClass, defaultActionName, methodName, actionAnnotation, allowedMethods);
}
}
buildIndexActions(packageConfigs);
// Add the new actions to the configuration
Set<String> packageNames = packageConfigs.keySet();
for (String packageName : packageNames) {
configuration.addPackageConfig(packageName, packageConfigs.get(packageName).build());
}
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class ConventionUnknownHandler method buildResult.
protected Result buildResult(String path, String resultCode, ResultTypeConfig config, ActionContext invocationContext) {
String resultClass = config.getClassName();
Map<String, String> params = new LinkedHashMap<>();
if (config.getParams() != null) {
params.putAll(config.getParams());
}
params.put(config.getDefaultResultParam(), path);
ResultConfig resultConfig = new ResultConfig.Builder(resultCode, resultClass).addParams(params).build();
try {
return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
} catch (Exception e) {
throw new StrutsException("Unable to build convention result", e, resultConfig);
}
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class AbstractAdapterNode method getChildBeforeOrAfter.
public Node getChildBeforeOrAfter(Node child, boolean before) {
log.debug("getChildBeforeOrAfter: ");
List adapters = getChildAdapters();
if (log.isDebugEnabled()) {
log.debug("childAdapters = {}", adapters);
log.debug("child = {}", child);
}
int index = adapters.indexOf(child);
if (index < 0)
throw new StrutsException(child + " is no child of " + this);
int siblingIndex = before ? index - 1 : index + 1;
return ((0 < siblingIndex) && (siblingIndex < adapters.size())) ? ((Node) adapters.get(siblingIndex)) : null;
}
Aggregations