Search in sources :

Example 16 with StrutsException

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

the class Dispatcher method init.

/**
 * Load configurations, including both XML and zero-configuration strategies,
 * and update optional settings, including whether to reload configurations and resource files.
 */
public void init() {
    if (configurationManager == null) {
        configurationManager = createConfigurationManager(Container.DEFAULT_NAME);
    }
    try {
        init_FileManager();
        // [1]
        init_DefaultProperties();
        // [2]
        init_TraditionalXmlConfigurations();
        init_JavaConfigurations();
        // [3]
        init_LegacyStrutsProperties();
        // [5]
        init_CustomConfigurationProviders();
        // [6]
        init_FilterInitParameters();
        // [7]
        init_AliasStandardObjects();
        Container container = init_PreloadConfiguration();
        container.inject(this);
        init_CheckWebLogicWorkaround(container);
        if (!dispatcherListeners.isEmpty()) {
            for (DispatcherListener l : dispatcherListeners) {
                l.dispatcherInitialized(this);
            }
        }
        errorHandler.init(servletContext);
        if (servletContext.getAttribute(StrutsStatics.SERVLET_DISPATCHER) == null) {
            servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, this);
        }
    } catch (Exception ex) {
        LOG.error("Dispatcher initialization failed", ex);
        throw new StrutsException(ex);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) Container(com.opensymphony.xwork2.inject.Container) ServletException(javax.servlet.ServletException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException)

Example 17 with StrutsException

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

the class DefaultTheme method renderTag.

public void renderTag(String tagName, TemplateRenderingContext context) {
    if (tagName.endsWith(".java")) {
        tagName = tagName.substring(0, tagName.length() - ".java".length());
    }
    List<TagHandler> handlers = new ArrayList<TagHandler>();
    List<TagHandlerFactory> factories = handlerFactories.get(tagName);
    if (factories == null) {
        throw new StrutsException("Unable to find handlers for tag " + tagName);
    }
    TagHandler prev = null;
    for (int x = factories.size() - 1; x >= 0; x--) {
        prev = factories.get(x).create(prev);
        prev.setup(context);
        handlers.add(0, prev);
    }
    // TagSerializer ser = (TagSerializer) handlers.get(handlers.size() - 1);
    TagGenerator gen = (TagGenerator) handlers.get(0);
    try {
        LOG.trace("Rendering tag [{}]", tagName);
        gen.generate();
    } catch (IOException ex) {
        throw new StrutsException("Unable to write tag: " + tagName, ex);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 18 with StrutsException

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

the class JavaTemplateEngine method renderTemplate.

public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
    Template t = templateContext.getTemplate();
    Theme theme = themes.get(t.getTheme());
    if (theme == null) {
        // Theme not supported, so do what struts would have done if we were not here.
        LOG.debug("Theme not found [{}] trying default template engine using template type [{}]", t.getTheme(), defaultTemplateType);
        final TemplateEngine engine = templateEngineManager.getTemplateEngine(templateContext.getTemplate(), defaultTemplateType);
        if (engine == null) {
            // May be the default template has changed?
            throw new ConfigurationException("Unable to find a TemplateEngine for template type '" + defaultTemplateType + "' whilst trying to render template " + templateContext.getTemplate());
        } else {
            try {
                // Retry render
                engine.renderTemplate(templateContext);
            } catch (Exception e) {
                // Give up and throw a new StrutsException(e);
                throw new StrutsException("Cannot render tag [" + t.getName() + "] because theme [" + t.getTheme() + "] was not found.", e);
            }
        }
    } else {
        // Render our template
        theme.renderTag(t.getName(), templateContext);
    }
}
Also used : TemplateEngine(org.apache.struts2.components.template.TemplateEngine) BaseTemplateEngine(org.apache.struts2.components.template.BaseTemplateEngine) StrutsException(org.apache.struts2.StrutsException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) SimpleTheme(org.apache.struts2.views.java.simple.SimpleTheme) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) Template(org.apache.struts2.components.template.Template)

Example 19 with StrutsException

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

the class XmlConfigurationProvider method loadConfigurationFiles.

private List<Document> loadConfigurationFiles(String fileName, Element includeElement) {
    List<Document> docs = new ArrayList<>();
    List<Document> finalDocs = new ArrayList<>();
    if (!includedFileNames.contains(fileName)) {
        LOG.debug("Loading action configurations from: {}", fileName);
        includedFileNames.add(fileName);
        Iterator<URL> urls = null;
        InputStream is = null;
        IOException ioException = null;
        try {
            urls = getConfigurationUrls(fileName);
        } catch (IOException ex) {
            ioException = ex;
        }
        if (urls == null || !urls.hasNext()) {
            LOG.debug("Ignoring file that does not exist: " + fileName, ioException);
            return docs;
        }
        URL url = null;
        while (urls.hasNext()) {
            try {
                url = urls.next();
                is = fileManager.loadFile(url);
                InputSource in = new InputSource(is);
                in.setSystemId(url.toString());
                Document helperDoc = DomHelper.parse(in, dtdMappings);
                if (helperDoc != null) {
                    docs.add(helperDoc);
                }
                loadedFileUrls.add(url.toString());
            } catch (StrutsException e) {
                if (includeElement != null) {
                    throw new ConfigurationException("Unable to load " + url, e, includeElement);
                } else {
                    throw new ConfigurationException("Unable to load " + url, e);
                }
            } catch (Exception e) {
                throw new ConfigurationException("Caught exception while loading file " + fileName, e, includeElement);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        LOG.error("Unable to close input stream", e);
                    }
                }
            }
        }
        // sort the documents, according to the "order" attribute
        Collections.sort(docs, new Comparator<Document>() {

            public int compare(Document doc1, Document doc2) {
                return XmlHelper.getLoadOrder(doc1).compareTo(XmlHelper.getLoadOrder(doc2));
            }
        });
        for (Document doc : docs) {
            Element rootElement = doc.getDocumentElement();
            NodeList children = rootElement.getChildNodes();
            int childSize = children.getLength();
            for (int i = 0; i < childSize; i++) {
                Node childNode = children.item(i);
                if (childNode instanceof Element) {
                    Element child = (Element) childNode;
                    final String nodeName = child.getNodeName();
                    if ("include".equals(nodeName)) {
                        String includeFileName = child.getAttribute("file");
                        if (includeFileName.indexOf('*') != -1) {
                            // handleWildCardIncludes(includeFileName, docs, child);
                            ClassPathFinder wildcardFinder = new ClassPathFinder();
                            wildcardFinder.setPattern(includeFileName);
                            Vector<String> wildcardMatches = wildcardFinder.findMatches();
                            for (String match : wildcardMatches) {
                                finalDocs.addAll(loadConfigurationFiles(match, child));
                            }
                        } else {
                            finalDocs.addAll(loadConfigurationFiles(includeFileName, child));
                        }
                    }
                }
            }
            finalDocs.add(doc);
        }
        LOG.debug("Loaded action configuration from: {}", fileName);
    }
    return finalDocs;
}
Also used : StrutsException(org.apache.struts2.StrutsException) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) ClassPathFinder(com.opensymphony.xwork2.util.ClassPathFinder) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 20 with StrutsException

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

the class CompoundRootAccessor method setProperty.

public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
    CompoundRoot root = (CompoundRoot) target;
    OgnlContext ognlContext = (OgnlContext) context;
    for (Object o : root) {
        if (o == null) {
            continue;
        }
        try {
            if (OgnlRuntime.hasSetProperty(ognlContext, o, name)) {
                OgnlRuntime.setProperty(ognlContext, o, name, value);
                return;
            } else if (o instanceof Map) {
                @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) o;
                try {
                    map.put(name, value);
                    return;
                } catch (UnsupportedOperationException e) {
                // This is an unmodifiable Map, so move on to the next element in the stack
                }
            }
        // } catch (OgnlException e) {
        // if (e.getReason() != null) {
        // final String msg = "Caught an Ognl exception while setting property " + name;
        // log.error(msg, e);
        // throw new RuntimeException(msg, e.getReason());
        // }
        } catch (IntrospectionException e) {
        // this is OK if this happens, we'll just keep trying the next
        }
    }
    boolean reportError = toBoolean((Boolean) context.get(ValueStack.REPORT_ERRORS_ON_NO_PROP));
    if (reportError || devMode) {
        final String msg = format("No object in the CompoundRoot has a publicly accessible property named '%s' " + "(no setter could be found).", name);
        if (reportError) {
            throw new StrutsException(msg);
        } else {
            LOG.warn(msg);
        }
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) IntrospectionException(java.beans.IntrospectionException) CompoundRoot(com.opensymphony.xwork2.util.CompoundRoot) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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