Search in sources :

Example 1 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class DefaultUnknownHandlerManager method build.

/**
 * Builds a list of UnknownHandlers in the order specified by the configured "unknown-handler-stack".
 * If "unknown-handler-stack" was not configured, all UnknownHandlers will be returned, in no specific order
 *
 * @throws Exception in case of any error
 */
protected void build() throws Exception {
    Configuration configuration = container.getInstance(Configuration.class);
    ObjectFactory factory = container.getInstance(ObjectFactory.class);
    if (configuration != null && container != null) {
        List<UnknownHandlerConfig> unkownHandlerStack = configuration.getUnknownHandlerStack();
        unknownHandlers = new ArrayList<>();
        if (unkownHandlerStack != null && !unkownHandlerStack.isEmpty()) {
            // get UnknownHandlers in the specified order
            for (UnknownHandlerConfig unknownHandlerConfig : unkownHandlerStack) {
                UnknownHandler uh = factory.buildUnknownHandler(unknownHandlerConfig.getName(), new HashMap<String, Object>());
                unknownHandlers.add(uh);
            }
        } else {
            // add all available UnknownHandlers
            Set<String> unknownHandlerNames = container.getInstanceNames(UnknownHandler.class);
            for (String unknownHandlerName : unknownHandlerNames) {
                UnknownHandler uh = container.getInstance(UnknownHandler.class, unknownHandlerName);
                unknownHandlers.add(uh);
            }
        }
    }
}
Also used : UnknownHandlerConfig(com.opensymphony.xwork2.config.entities.UnknownHandlerConfig) Configuration(com.opensymphony.xwork2.config.Configuration)

Example 2 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class DomHelper method parse.

/**
 * Creates a W3C Document that remembers the location of each element in
 * the source file. The location of element nodes can then be retrieved
 * using the {@link #getLocationObject(Element)} method.
 *
 * @param inputSource the inputSource to read the document from
 * @param dtdMappings a map of DTD names and public ids
 *
 * @return the W3C Document
 */
public static Document parse(InputSource inputSource, Map<String, String> dtdMappings) {
    SAXParserFactory factory = null;
    String parserProp = System.getProperty("xwork.saxParserFactory");
    if (parserProp != null) {
        try {
            ObjectFactory objectFactory = ActionContext.getContext().getContainer().getInstance(ObjectFactory.class);
            Class clazz = objectFactory.getClassInstance(parserProp);
            factory = (SAXParserFactory) clazz.newInstance();
        } catch (Exception e) {
            LOG.error("Unable to load saxParserFactory set by system property 'xwork.saxParserFactory': {}", parserProp, e);
        }
    }
    if (factory == null) {
        factory = SAXParserFactory.newInstance();
    }
    factory.setValidating((dtdMappings != null));
    factory.setNamespaceAware(true);
    SAXParser parser;
    try {
        parser = factory.newSAXParser();
    } catch (Exception ex) {
        throw new StrutsException("Unable to create SAX parser", ex);
    }
    DOMBuilder builder = new DOMBuilder();
    // Enhance the sax stream with location information
    ContentHandler locationHandler = new LocationAttributes.Pipe(builder);
    try {
        parser.parse(inputSource, new StartHandler(locationHandler, dtdMappings));
    } catch (Exception ex) {
        throw new StrutsException(ex);
    }
    return builder.getDocument();
}
Also used : StrutsException(org.apache.struts2.StrutsException) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) SAXParser(javax.xml.parsers.SAXParser) StrutsException(org.apache.struts2.StrutsException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class AbstractBeanSelectionProvider method alias.

protected void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
    if (!builder.contains(type, Container.DEFAULT_NAME)) {
        String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
        if (builder.contains(type, foundName)) {
            LOG.trace("Choosing bean ({}) for ({})", foundName, type.getName());
            builder.alias(type, foundName, Container.DEFAULT_NAME);
        } else {
            try {
                Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
                LOG.trace("Choosing bean ({}) for ({})", cls.getName(), type.getName());
                builder.factory(type, cls, scope);
            } catch (ClassNotFoundException ex) {
                // Perhaps a spring bean id, so we'll delegate to the object factory at runtime
                LOG.trace("Choosing bean ({}) for ({}) to be loaded from the ObjectFactory", foundName, type.getName());
                if (DEFAULT_BEAN_NAME.equals(foundName)) {
                // Probably an optional bean, will ignore
                } else {
                    if (ObjectFactory.class != type) {
                        builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
                    } else {
                        throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: " + foundName);
                    }
                }
            }
        }
    } else {
        LOG.warn("Unable to alias bean type ({}), default mapping already assigned.", type.getName());
    }
}
Also used : ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 4 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class DefaultValidatorFileParser method addValidatorConfigs.

private void addValidatorConfigs(ValidatorFactory factory, NodeList validatorNodes, Map<String, Object> extraParams, List<ValidatorConfig> validatorCfgs) {
    for (int j = 0; j < validatorNodes.getLength(); j++) {
        Element validatorElement = (Element) validatorNodes.item(j);
        String validatorType = validatorElement.getAttribute("type");
        Map<String, Object> params = new HashMap<String, Object>(extraParams);
        params.putAll(XmlHelper.getParams(validatorElement));
        // ensure that the type is valid...
        try {
            factory.lookupRegisteredValidatorType(validatorType);
        } catch (IllegalArgumentException ex) {
            throw new ConfigurationException("Invalid validation type: " + validatorType, validatorElement);
        }
        ValidatorConfig.Builder vCfg = new ValidatorConfig.Builder(validatorType).addParams(params).location(DomHelper.getLocationObject(validatorElement)).shortCircuit(Boolean.valueOf(validatorElement.getAttribute("short-circuit")));
        NodeList messageNodes = validatorElement.getElementsByTagName("message");
        Element messageElement = (Element) messageNodes.item(0);
        final Node defaultMessageNode = messageElement.getFirstChild();
        String defaultMessage = (defaultMessageNode == null) ? "" : defaultMessageNode.getNodeValue();
        vCfg.defaultMessage(defaultMessage);
        Map<String, String> messageParams = XmlHelper.getParams(messageElement);
        String key = messageElement.getAttribute("key");
        if ((key != null) && (key.trim().length() > 0)) {
            vCfg.messageKey(key);
            if (messageParams.containsKey("defaultMessage")) {
                vCfg.defaultMessage(messageParams.get("defaultMessage"));
            }
            // Sort the message param. those with keys as '1', '2', '3' etc. (numeric values)
            // are i18n message parameter, others are excluded.
            TreeMap<Integer, String> sortedMessageParameters = new TreeMap<Integer, String>();
            for (Map.Entry<String, String> messageParamEntry : messageParams.entrySet()) {
                try {
                    int _order = Integer.parseInt(messageParamEntry.getKey());
                    sortedMessageParameters.put(_order, messageParamEntry.getValue());
                } catch (NumberFormatException e) {
                // ignore if its not numeric.
                }
            }
            vCfg.messageParams(sortedMessageParameters.values().toArray(new String[sortedMessageParameters.values().size()]));
        } else {
            if (messageParams != null && (messageParams.size() > 0)) {
                // let's warn the user.
                if (LOG.isWarnEnabled()) {
                    LOG.warn("validator of type [" + validatorType + "] have i18n message parameters defined but no i18n message key, it's parameters will be ignored");
                }
            }
        }
        validatorCfgs.add(vCfg.build());
    }
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 5 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class ValidateAction method createValueStackFactory.

private ValueStackFactory createValueStackFactory(final Map<String, Object> context) {
    OgnlValueStackFactory factory = new OgnlValueStackFactory() {

        @Override
        public ValueStack createValueStack(ValueStack stack) {
            return createStubValueStack(context);
        }
    };
    container.inject(factory);
    return factory;
}
Also used : OgnlValueStackFactory(com.opensymphony.xwork2.ognl.OgnlValueStackFactory) OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Aggregations

ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)10 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)7 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)5 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)4 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 StrutsException (org.apache.struts2.StrutsException)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 DefaultActionProxyFactory (com.opensymphony.xwork2.DefaultActionProxyFactory)3 FileManager (com.opensymphony.xwork2.FileManager)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 Factory (com.opensymphony.xwork2.inject.Factory)3 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)3 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3