Search in sources :

Example 1 with FacesConfig

use of org.apache.openejb.jee.FacesConfig in project tomee by apache.

the class DeploymentLoader method addFacesConfigs.

/**
     * Finds all faces configuration files and stores them in the WebModule
     *
     * @param webModule WebModule
     * @throws OpenEJBException
     */
private void addFacesConfigs(final WebModule webModule) throws OpenEJBException {
    //*************************IMPORTANT*******************************************
    // TODO : kmalhi :: Add support to scrape META-INF/faces-config.xml in jar files
    // look at section 10.4.2 of the JSF v1.2 spec, bullet 1 for details
    final Set<URL> facesConfigLocations = new HashSet<URL>();
    // web.xml contains faces config locations in the context parameter javax.faces.CONFIG_FILES
    final File warFile = new File(webModule.getJarLocation());
    final WebApp webApp = webModule.getWebApp();
    if (webApp != null) {
        final String foundContextParam = webApp.contextParamsAsMap().get("javax.faces.CONFIG_FILES");
        if (foundContextParam != null) {
            // the value is a comma separated list of config files
            final String commaDelimitedListOfFiles = foundContextParam.trim();
            final String[] configFiles = commaDelimitedListOfFiles.split(",");
            // trim any extra spaces in each file
            final String[] trimmedConfigFiles = new String[configFiles.length];
            for (int i = 0; i < configFiles.length; i++) {
                trimmedConfigFiles[i] = configFiles[i].trim();
            }
            // convert each file to a URL and add it to facesConfigLocations
            for (final String location : trimmedConfigFiles) {
                if (!location.startsWith("/")) {
                    logger.error("A faces configuration file should be context relative when specified in web.xml. Please fix the value of context parameter javax.faces.CONFIG_FILES for the file " + location);
                }
                try {
                    final File file = new File(warFile, location).getCanonicalFile().getAbsoluteFile();
                    final URL url = file.toURI().toURL();
                    facesConfigLocations.add(url);
                } catch (final IOException e) {
                    logger.error("Faces configuration file location bad: " + location, e);
                }
            }
        } else {
            logger.debug("faces config file is null");
        }
    }
    // Search for WEB-INF/faces-config.xml
    final File webInf = new File(warFile, "WEB-INF");
    if (webInf.isDirectory()) {
        File facesConfigFile = new File(webInf, "faces-config.xml");
        if (facesConfigFile.exists()) {
            try {
                facesConfigFile = facesConfigFile.getCanonicalFile().getAbsoluteFile();
                final URL url = facesConfigFile.toURI().toURL();
                facesConfigLocations.add(url);
            } catch (final IOException e) {
                // TODO: kmalhi:: Remove the printStackTrace after testing
                e.printStackTrace();
            }
        }
    }
    // flag an error and not allow the application to be deployed.
    for (final URL location : facesConfigLocations) {
        final FacesConfig facesConfig = ReadDescriptors.readFacesConfig(location);
        webModule.getFacesConfigs().add(facesConfig);
        if ("file".equals(location.getProtocol())) {
            webModule.getWatchedResources().add(URLs.toFilePath(location));
        }
    }
}
Also used : FacesConfig(org.apache.openejb.jee.FacesConfig) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) WebApp(org.apache.openejb.jee.WebApp)

Example 2 with FacesConfig

use of org.apache.openejb.jee.FacesConfig in project tomee by apache.

the class ReadDescriptors method readFacesConfig.

public static FacesConfig readFacesConfig(final URL url) throws OpenEJBException {
    try {
        final Source src = getSource(url);
        if (src == null) {
            return new FacesConfig();
        }
        final String content = IO.slurp(src.get());
        if (isEmpty(new ByteArrayInputStream(content.getBytes()), "faces-config")) {
            return new FacesConfig();
        }
        return FacesConfigXml.unmarshal(new ByteArrayInputStream(content.getBytes()));
    } catch (final SAXException e) {
        throw new OpenEJBException("Cannot parse the faces configuration file: " + url.toExternalForm(), e);
    } catch (final JAXBException e) {
        throw new OpenEJBException("Cannot unmarshall the faces configuration file: " + url.toExternalForm(), e);
    } catch (final IOException e) {
        throw new OpenEJBException("Cannot read the faces configuration file: " + url.toExternalForm(), e);
    } catch (final Exception e) {
        throw new OpenEJBException("Encountered unknown error parsing the faces configuration file: " + url.toExternalForm(), e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) FacesConfig(org.apache.openejb.jee.FacesConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) InputSource(org.xml.sax.InputSource) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with FacesConfig

use of org.apache.openejb.jee.FacesConfig in project tomee by apache.

the class JsfTest method testFacesConfig.

/**
     * This test requires that there are three managed beans in faces-config.xml. It will ask JaxbJavaee to load faces-config.xml
     * and then assert if it found the three managed beans and checks if the class names are correct
     *
     * @throws Exception
     */
public void testFacesConfig() throws Exception {
    final List<String> managedBeanClasses = new ArrayList<String>();
    managedBeanClasses.add("org.apache.openejb.faces.EmployeeBean");
    managedBeanClasses.add("org.apache.openejb.faces.OneBean");
    managedBeanClasses.add("org.apache.openejb.faces.TwoBean");
    final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jsf/faces-config.xml");
    final FacesConfig facesConfig = (FacesConfig) JaxbJavaee.unmarshalJavaee(FacesConfig.class, inputStream);
    final List<FacesManagedBean> managedBean = facesConfig.getManagedBean();
    for (final FacesManagedBean bean : managedBean) {
        assertTrue(managedBeanClasses.contains(bean.getManagedBeanClass().trim()));
    }
    assertEquals(3, managedBean.size());
    marshalAndUnmarshal(FacesConfig.class, "jsf/faces-config.xml", null);
}
Also used : FacesConfig(org.apache.openejb.jee.FacesConfig) FacesManagedBean(org.apache.openejb.jee.FacesManagedBean) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Example 4 with FacesConfig

use of org.apache.openejb.jee.FacesConfig in project tomee by apache.

the class JsfTest method test11.

public void test11() throws Exception {
    marshalAndUnmarshal(FacesConfig.class, "jsf/1_1_dtd/faces-config-simple-src.xml", "jsf/1_0_dtd/faces-config-simple-expected.xml");
    marshalAndUnmarshal(FacesConfig.class, "jsf/1_1_dtd/faces-config-moderate-src.xml", "jsf/1_0_dtd/faces-config-moderate-expected.xml");
    marshalAndUnmarshal(FacesConfig.class, "jsf/1_1_dtd/faces-config-complex-src.xml", "jsf/1_0_dtd/faces-config-complex-expected.xml");
    marshalAndUnmarshal(FacesConfig.class, "jsf/1_1_dtd/faces-config-empty-src.xml", "jsf/1_0_dtd/faces-config-empty-expected.xml");
    final FacesConfig f = marshalAndUnmarshal(FacesConfig.class, "jsf/1_1_dtd/faces-config.xml", null);
}
Also used : FacesConfig(org.apache.openejb.jee.FacesConfig)

Example 5 with FacesConfig

use of org.apache.openejb.jee.FacesConfig in project tomee by apache.

the class FacesConfig22Test method unmarshallFacesConfig22.

@Test
public void unmarshallFacesConfig22() throws Exception {
    final URL url = getClass().getClassLoader().getResource("a-faces-config-22.xml");
    final FacesConfig facesConfig = FacesConfigXml.unmarshal(url);
    assertNotNull(facesConfig);
}
Also used : FacesConfig(org.apache.openejb.jee.FacesConfig) URL(java.net.URL) Test(org.junit.Test)

Aggregations

FacesConfig (org.apache.openejb.jee.FacesConfig)5 IOException (java.io.IOException)2 URL (java.net.URL)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 JarFile (java.util.jar.JarFile)1 JAXBException (javax.xml.bind.JAXBException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 FacesManagedBean (org.apache.openejb.jee.FacesManagedBean)1 WebApp (org.apache.openejb.jee.WebApp)1 Test (org.junit.Test)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1