Search in sources :

Example 1 with DefinitionsFactoryException

use of org.apache.struts.tiles.DefinitionsFactoryException in project sonar-java by SonarSource.

the class ReloadableDefinitionsFactory method createFactoryFromClassname.

/**
 * Create Definition factory from provided classname.
 * If a factory class name is provided, a factory of this class is created. Otherwise,
 * a default factory is created.
 * Factory must have a constructor taking ServletContext and Map as parameter.
 * @param classname Class name of the factory to create.
 * @param servletContext Servlet Context passed to newly created factory.
 * @param properties Map of name/property passed to newly created factory.
 * @return newly created factory.
 * @throws DefinitionsFactoryException If an error occur while initializing factory
 */
public ComponentDefinitionsFactory createFactoryFromClassname(ServletContext servletContext, Map properties, String classname) throws DefinitionsFactoryException {
    if (classname == null) {
        return createFactory(servletContext, properties);
    }
    // Try to create from classname
    try {
        Class factoryClass = RequestUtils.applicationClass(classname);
        ComponentDefinitionsFactory factory = (ComponentDefinitionsFactory) factoryClass.newInstance();
        factory.initFactory(servletContext, properties);
        return factory;
    } catch (ClassCastException ex) {
        // Bad classname
        throw new DefinitionsFactoryException("Error - createDefinitionsFactory : Factory class '" + classname + " must implements 'ComponentDefinitionsFactory'.", ex);
    } catch (ClassNotFoundException ex) {
        // Bad classname
        throw new DefinitionsFactoryException("Error - createDefinitionsFactory : Bad class name '" + classname + "'.", ex);
    } catch (InstantiationException ex) {
        // Bad constructor or error
        throw new DefinitionsFactoryException(ex);
    } catch (IllegalAccessException ex) {
        throw new DefinitionsFactoryException(ex);
    }
}
Also used : ComponentDefinitionsFactory(org.apache.struts.tiles.ComponentDefinitionsFactory) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException)

Example 2 with DefinitionsFactoryException

use of org.apache.struts.tiles.DefinitionsFactoryException in project sonar-java by SonarSource.

the class DefinitionDispatcherAction method execute.

/**
 * Process the specified HTTP request, and create the corresponding HTTP
 * response (or forward to another web component that will create it),
 * with provision for handling exceptions thrown by the business logic.
 *
 * @param mapping The ActionMapping used to select this instance
 * @param form The optional ActionForm bean for this request (if any)
 * @param request The HTTP request we are processing
 * @param response The HTTP response we are creating
 *
 * @exception Exception if the application business logic throws
 *  an exception
 * @since Struts 1.1
 */
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Identify the request parameter containing the method name
    // If none defined, use "def"
    String parameter = mapping.getParameter();
    if (parameter == null) {
        parameter = "def";
    }
    // Identify the method name to be dispatched to
    String name = request.getParameter(parameter);
    if (name == null) {
        log.error("Can't get parameter '" + parameter + "'.");
        return mapping.findForward("error");
    }
    // Try to dispatch to requested definition
    try {
        // Read definition from factory, but we can create it here.
        ComponentDefinition definition = TilesUtil.getDefinition(name, request, getServlet().getServletContext());
        if (log.isDebugEnabled()) {
            log.debug("Get Definition " + definition);
        }
        DefinitionsUtil.setActionDefinition(request, definition);
    } catch (FactoryNotFoundException e) {
        log.error("Can't get definition factory.", e);
        return mapping.findForward("error");
    } catch (NoSuchDefinitionException e) {
        log.error("Can't get definition '" + name + "'.", e);
        return mapping.findForward("error");
    } catch (DefinitionsFactoryException e) {
        log.error("General Factory error '" + e.getMessage() + "'.", e);
        return mapping.findForward("error");
    } catch (Exception e) {
        log.error("General error '" + e.getMessage() + "'.", e);
        return mapping.findForward("error");
    }
    return mapping.findForward("success");
}
Also used : FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) NoSuchDefinitionException(org.apache.struts.tiles.NoSuchDefinitionException) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) IOException(java.io.IOException) FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) NoSuchDefinitionException(org.apache.struts.tiles.NoSuchDefinitionException) ComponentDefinition(org.apache.struts.tiles.ComponentDefinition)

Example 3 with DefinitionsFactoryException

use of org.apache.struts.tiles.DefinitionsFactoryException in project sonar-java by SonarSource.

the class ReloadDefinitionsAction method execute.

/**
 * Process the specified HTTP request, and create the corresponding HTTP
 * response (or forward to another web component that will create it),
 * with provision for handling exceptions thrown by the business logic.
 *
 * @param mapping The ActionMapping used to select this instance
 * @param form The optional ActionForm bean for this request (if any)
 * @param request The HTTP request we are processing
 * @param response The HTTP response we are creating
 *
 * @exception Exception if the application business logic throws
 *  an exception
 * @since Struts 1.1
 */
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/plain");
    PrintWriter writer = response.getWriter();
    try {
        ServletContext context = getServlet().getServletContext();
        DefinitionsFactory factory = TilesUtil.getDefinitionsFactory(request, context);
        factory.setConfig(factory.getConfig(), context);
        writer.println("OK");
    } catch (ClassCastException e) {
        writer.println("FAIL - " + e.toString());
        getServlet().log("ReloadAction", e);
    } catch (DefinitionsFactoryException e) {
        writer.println("FAIL - " + e.toString());
        getServlet().log("ReloadAction", e);
    }
    writer.flush();
    writer.close();
    return (null);
}
Also used : DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) DefinitionsFactory(org.apache.struts.tiles.DefinitionsFactory) ServletContext(javax.servlet.ServletContext) PrintWriter(java.io.PrintWriter)

Example 4 with DefinitionsFactoryException

use of org.apache.struts.tiles.DefinitionsFactoryException in project sonar-java by SonarSource.

the class ComponentDefinitionsFactoryWrapper method createFactoryInstance.

/**
 * Create Definition factory from provided classname which must implement {@link ComponentDefinitionsFactory}.
 * Factory class must extend {@link DefinitionsFactory}.
 * @param classname Class name of the factory to create.
 * @return newly created factory.
 * @throws DefinitionsFactoryException If an error occur while initializing factory
 */
protected ComponentDefinitionsFactory createFactoryInstance(String classname) throws DefinitionsFactoryException {
    try {
        Class factoryClass = RequestUtils.applicationClass(classname);
        Object factory = factoryClass.newInstance();
        return (ComponentDefinitionsFactory) factory;
    } catch (ClassCastException ex) {
        // Bad classname
        throw new DefinitionsFactoryException("Error - createDefinitionsFactory : Factory class '" + classname + " must implement 'DefinitionsFactory'.", ex);
    } catch (ClassNotFoundException ex) {
        // Bad classname
        throw new DefinitionsFactoryException("Error - createDefinitionsFactory : Bad class name '" + classname + "'.", ex);
    } catch (InstantiationException ex) {
        // Bad constructor or error
        throw new DefinitionsFactoryException(ex);
    } catch (IllegalAccessException ex) {
        throw new DefinitionsFactoryException(ex);
    }
}
Also used : ComponentDefinitionsFactory(org.apache.struts.tiles.ComponentDefinitionsFactory) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException)

Example 5 with DefinitionsFactoryException

use of org.apache.struts.tiles.DefinitionsFactoryException in project sonar-java by SonarSource.

the class I18nFactorySet method parseXmlFile.

/**
 * Parse specified xml file and add definition to specified definitions set.
 * This method is used to load several description files in one instances list.
 * If filename exists and definition set is <code>null</code>, create a new set. Otherwise, return
 * passed definition set (can be <code>null</code>).
 * @param servletContext Current servlet context. Used to open file.
 * @param filename Name of file to parse.
 * @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions
 * set is created on request.
 * @return XmlDefinitionsSet The definitions set created or passed as parameter.
 * @throws DefinitionsFactoryException On errors parsing file.
 */
protected XmlDefinitionsSet parseXmlFile(ServletContext servletContext, String filename, XmlDefinitionsSet xmlDefinitions) throws DefinitionsFactoryException {
    try {
        InputStream input = servletContext.getResourceAsStream(filename);
        // Patch proposed Houston, Stephen (LIT) on 5 Apr 2002
        if (null == input) {
            try {
                input = new java.io.FileInputStream(servletContext.getRealPath(filename));
            } catch (Exception e) {
            }
        }
        // which allows the config files to be stored in a jar
        if (input == null) {
            input = getClass().getResourceAsStream(filename);
        }
        // If still nothing found, this mean no config file is associated
        if (input == null) {
            if (log.isDebugEnabled()) {
                log.debug("Can't open file '" + filename + "'");
            }
            return xmlDefinitions;
        }
        // if( xmlParser == null )
        if (true) {
            xmlParser = new XmlParser();
            xmlParser.setValidating(isValidatingParser);
        }
        // Check if definition set already exist.
        if (xmlDefinitions == null) {
            xmlDefinitions = new XmlDefinitionsSet();
        }
        xmlParser.parse(input, xmlDefinitions);
    } catch (SAXException ex) {
        if (log.isDebugEnabled()) {
            log.debug("Error while parsing file '" + filename + "'.");
            ex.printStackTrace();
        }
        throw new DefinitionsFactoryException("Error while parsing file '" + filename + "'. " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new DefinitionsFactoryException("IO Error while parsing file '" + filename + "'. " + ex.getMessage(), ex);
    }
    return xmlDefinitions;
}
Also used : DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) FileNotFoundException(java.io.FileNotFoundException) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Aggregations

DefinitionsFactoryException (org.apache.struts.tiles.DefinitionsFactoryException)8 IOException (java.io.IOException)3 DefinitionsFactory (org.apache.struts.tiles.DefinitionsFactory)3 ComponentDefinitionsFactory (org.apache.struts.tiles.ComponentDefinitionsFactory)2 FactoryNotFoundException (org.apache.struts.tiles.FactoryNotFoundException)2 SAXException (org.xml.sax.SAXException)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 ServletContext (javax.servlet.ServletContext)1 JspException (javax.servlet.jsp.JspException)1 ComponentDefinition (org.apache.struts.tiles.ComponentDefinition)1 DefinitionsFactoryConfig (org.apache.struts.tiles.DefinitionsFactoryConfig)1 NoSuchDefinitionException (org.apache.struts.tiles.NoSuchDefinitionException)1 TilesRequestProcessor (org.apache.struts.tiles.TilesRequestProcessor)1 XmlDefinitionsSet (org.apache.struts.tiles.xmlDefinition.XmlDefinitionsSet)1 XmlParser (org.apache.struts.tiles.xmlDefinition.XmlParser)1 OrderedPathMatchingResourcePatternResolver (org.jaffa.util.OrderedPathMatchingResourcePatternResolver)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1