Search in sources :

Example 6 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class DefaultClassFinder method readClassDef.

private void readClassDef(String className) {
    if (!className.endsWith(".class")) {
        className = className.replace('.', '/') + ".class";
    }
    try {
        URL resource = classLoaderInterface.getResource(className);
        if (resource != null) {
            try (InputStream in = resource.openStream()) {
                ClassReader classReader = new ClassReader(in);
                classReader.accept(new InfoBuildingVisitor(this), ClassReader.SKIP_DEBUG);
            }
        } else {
            throw new StrutsException("Could not load " + className);
        }
    } catch (IOException e) {
        throw new StrutsException("Could not load " + className, e);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) URL(java.net.URL)

Example 7 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class VelocityManager method newVelocityEngine.

/**
 * <p>
 * Instantiates a new VelocityEngine.
 * </p>
 * <p>
 * The following is the default Velocity configuration
 * </p>
 *
 * <pre>
 *  resource.loader = file, class
 *  file.resource.loader.path = real path of webapp
 *  class.resource.loader.description = Velocity Classpath Resource Loader
 *  class.resource.loader.class = org.apache.struts2.views.velocity.StrutsResourceLoader
 * </pre>
 * <p>
 * this default configuration can be overridden by specifying a struts.velocity.configfile property in the
 * struts.properties file.  the specified config file will be searched for in the following order:
 * </p>
 *
 * <ul>
 * <li>relative to the servlet context path</li>
 * <li>relative to the WEB-INF directory</li>
 * <li>on the classpath</li>
 * </ul>
 *
 * @param context the current ServletContext. may <b>not</b> be null
 * @return the new velocity engine
 */
protected VelocityEngine newVelocityEngine(ServletContext context) {
    if (context == null) {
        String gripe = "Error attempting to create a new VelocityEngine from a null ServletContext!";
        LOG.error(gripe);
        throw new IllegalArgumentException(gripe);
    }
    Properties p = loadConfiguration(context);
    VelocityEngine velocityEngine = new VelocityEngine();
    // Set the velocity attribute for the servlet context
    // if this is not set the webapp loader WILL NOT WORK
    velocityEngine.setApplicationAttribute(ServletContext.class.getName(), context);
    try {
        velocityEngine.init(p);
    } catch (Exception e) {
        throw new StrutsException("Unable to instantiate VelocityEngine!", e);
    }
    return velocityEngine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StrutsException(org.apache.struts2.StrutsException) ServletContext(javax.servlet.ServletContext) Properties(java.util.Properties) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Example 8 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class AdapterFactory method constructAdapterInstance.

/**
 * Create an instance of an adapter dynamically and set its context via
 * the AdapterNode interface.
 *
 * @param adapterClass  adapter class
 * @param parent parent adapter node
 * @param propertyName the property name
 * @param propertyValue the property value
 *
 * @return the new node
 */
private Node constructAdapterInstance(Class adapterClass, AdapterNode parent, String propertyName, Object propertyValue) {
    // Check to see if the class has a no-args constructor
    try {
        adapterClass.getConstructor(new Class[] {});
    } catch (NoSuchMethodException e1) {
        throw new StrutsException("Adapter class: " + adapterClass + " does not have a no-args constructor.");
    }
    try {
        AdapterNode adapterNode = (AdapterNode) adapterClass.newInstance();
        adapterNode.setAdapterFactory(this);
        adapterNode.setParent(parent);
        adapterNode.setPropertyName(propertyName);
        adapterNode.setPropertyValue(propertyValue);
        return adapterNode;
    } catch (IllegalAccessException | InstantiationException e) {
        throw new StrutsException("Cannot adapt " + propertyValue + " (" + propertyName + ") :" + e.getMessage(), e);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException)

Example 9 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class I18n method start.

public boolean start(Writer writer) {
    boolean result = super.start(writer);
    try {
        String name = this.findString(this.name, "name", "Resource bundle name is required. Example: foo or foo_en");
        ResourceBundle bundle = defaultTextProvider.getTexts(name);
        if (bundle == null) {
            LocaleProvider localeProvider = localeProviderFactory.createLocaleProvider();
            bundle = localizedTextProvider.findResourceBundle(name, localeProvider.getLocale());
        }
        if (bundle != null) {
            textProvider = textProviderFactory.createInstance(bundle);
            getStack().push(textProvider);
            pushed = true;
        }
    } catch (Exception e) {
        throw new StrutsException("Could not find the bundle " + name, e);
    }
    return result;
}
Also used : StrutsException(org.apache.struts2.StrutsException) LocaleProvider(com.opensymphony.xwork2.LocaleProvider) ResourceBundle(java.util.ResourceBundle) StrutsException(org.apache.struts2.StrutsException)

Example 10 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class Debug method start.

public boolean start(Writer writer) {
    boolean result = super.start(writer);
    if (showDebug()) {
        ValueStack stack = getStack();
        Iterator iter = stack.getRoot().iterator();
        List stackValues = new ArrayList(stack.getRoot().size());
        while (iter.hasNext()) {
            Object o = iter.next();
            Map values;
            try {
                values = reflectionProvider.getBeanMap(o);
            } catch (Exception e) {
                throw new StrutsException("Caught an exception while getting the property values of " + o, e);
            }
            stackValues.add(new DebugMapEntry(o.getClass().getName(), values));
        }
        addParameter("stackValues", stackValues);
    }
    return result;
}
Also used : StrutsException(org.apache.struts2.StrutsException) ValueStack(com.opensymphony.xwork2.util.ValueStack) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) StrutsException(org.apache.struts2.StrutsException)

Aggregations

StrutsException (org.apache.struts2.StrutsException)46 IOException (java.io.IOException)17 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)9 ArrayList (java.util.ArrayList)7 InputStream (java.io.InputStream)6 URL (java.net.URL)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 List (java.util.List)4 ServletContext (javax.servlet.ServletContext)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 IntrospectionException (java.beans.IntrospectionException)3 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 CompoundRoot (com.opensymphony.xwork2.util.CompoundRoot)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2