You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2013/03/12 11:56:01 UTC

svn commit: r1455478 - in /tomee/tomee/trunk/tomee/tomee-myfaces/src/main: java/org/apache/tomee/myfaces/ resources/META-INF/services/

Author: rmannibucau
Date: Tue Mar 12 10:56:01 2013
New Revision: 1455478

URL: http://svn.apache.org/r1455478
Log:
avoid to fail even with an empty faces-config.xml

Added:
    tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProvider.java
    tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProviderFactory.java
    tomee/tomee/trunk/tomee/tomee-myfaces/src/main/resources/META-INF/services/org.apache.myfaces.spi.FacesConfigurationProviderFactory

Added: tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProvider.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProvider.java?rev=1455478&view=auto
==============================================================================
--- tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProvider.java (added)
+++ tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProvider.java Tue Mar 12 10:56:01 2013
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomee.myfaces;
+
+import org.apache.myfaces.config.DefaultFacesConfigurationProvider;
+import org.apache.myfaces.config.element.FacesConfig;
+import org.apache.openejb.loader.IO;
+
+import javax.faces.context.ExternalContext;
+import java.io.InputStream;
+
+public class TomEEFacesConfigurationProvider extends DefaultFacesConfigurationProvider {
+    @Override
+    public FacesConfig getWebAppFacesConfig(final ExternalContext ectx) {
+        final InputStream stream = ectx.getResourceAsStream("/WEB-INF/faces-config.xml");
+        if (stream != null && isEmpty(stream)) {
+            return new org.apache.myfaces.config.impl.digester.elements.FacesConfig();
+        }
+        // we can't just check the emptyness after the exception
+        // because otherwise an exception is logged because of the parser error handler
+        return super.getWebAppFacesConfig(ectx);
+    }
+
+    private static boolean isEmpty(final InputStream stream) {
+        try {
+            final String content = IO.slurp(stream);
+            return content.trim().length() == 0;
+        } catch (final Exception e) {
+            return false;
+        }
+    }
+}

Added: tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProviderFactory.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProviderFactory.java?rev=1455478&view=auto
==============================================================================
--- tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProviderFactory.java (added)
+++ tomee/tomee/trunk/tomee/tomee-myfaces/src/main/java/org/apache/tomee/myfaces/TomEEFacesConfigurationProviderFactory.java Tue Mar 12 10:56:01 2013
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomee.myfaces;
+
+import org.apache.myfaces.shared.util.ClassUtils;
+import org.apache.myfaces.spi.FacesConfigurationProvider;
+import org.apache.myfaces.spi.ServiceProviderFinderFactory;
+import org.apache.myfaces.spi.impl.DefaultFacesConfigurationProviderFactory;
+
+import javax.faces.FacesException;
+import javax.faces.context.ExternalContext;
+import java.lang.reflect.InvocationTargetException;
+import java.security.PrivilegedActionException;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+// fork to be able to not fail on empty faces-config.xml
+public class TomEEFacesConfigurationProviderFactory extends DefaultFacesConfigurationProviderFactory {
+    public static final String FACES_CONFIGURATION_PROVIDER = FacesConfigurationProvider.class.getName();
+    public static final String FACES_CONFIGURATION_PROVIDER_LIST = FacesConfigurationProvider.class.getName()+".LIST";
+    public static final String FACES_CONFIGURATION_PROVIDER_INSTANCE_KEY = FacesConfigurationProvider.class.getName() + ".INSTANCE";
+
+    private Logger getLogger() {
+        return Logger.getLogger(DefaultFacesConfigurationProviderFactory.class.getName());
+    }
+
+    @Override
+    public FacesConfigurationProvider getFacesConfigurationProvider(final ExternalContext externalContext) {
+        FacesConfigurationProvider returnValue = (FacesConfigurationProvider) externalContext.getApplicationMap().get(FACES_CONFIGURATION_PROVIDER_INSTANCE_KEY);
+        if (returnValue == null) {
+            final ExternalContext extContext = externalContext;
+            try {
+                returnValue = resolveFacesConfigurationProviderFromService(extContext);
+                externalContext.getApplicationMap().put(FACES_CONFIGURATION_PROVIDER_INSTANCE_KEY, returnValue);
+            } catch (final ClassNotFoundException e) {
+                // ignore
+            } catch (final NoClassDefFoundError e) {
+                // ignore
+            } catch (final InstantiationException e) {
+                getLogger().log(Level.SEVERE, "", e);
+            } catch (final IllegalAccessException e) {
+                getLogger().log(Level.SEVERE, "", e);
+            } catch (final InvocationTargetException e) {
+                getLogger().log(Level.SEVERE, "", e);
+            } catch (final PrivilegedActionException e) {
+                throw new FacesException(e);
+            }
+        }
+
+
+        return returnValue;
+    }
+
+    private FacesConfigurationProvider resolveFacesConfigurationProviderFromService(final ExternalContext externalContext)
+            throws ClassNotFoundException, NoClassDefFoundError, InstantiationException, IllegalAccessException, InvocationTargetException, PrivilegedActionException {
+        List<String> classList = (List<String>) externalContext.getApplicationMap().get(FACES_CONFIGURATION_PROVIDER_LIST);
+        if (classList == null) {
+            classList = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext).getServiceProviderList(FACES_CONFIGURATION_PROVIDER);
+            externalContext.getApplicationMap().put(FACES_CONFIGURATION_PROVIDER_LIST, classList);
+        }
+        return ClassUtils.buildApplicationObject(FacesConfigurationProvider.class, classList, new TomEEFacesConfigurationProvider());
+    }
+}

Added: tomee/tomee/trunk/tomee/tomee-myfaces/src/main/resources/META-INF/services/org.apache.myfaces.spi.FacesConfigurationProviderFactory
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tomee/tomee-myfaces/src/main/resources/META-INF/services/org.apache.myfaces.spi.FacesConfigurationProviderFactory?rev=1455478&view=auto
==============================================================================
--- tomee/tomee/trunk/tomee/tomee-myfaces/src/main/resources/META-INF/services/org.apache.myfaces.spi.FacesConfigurationProviderFactory (added)
+++ tomee/tomee/trunk/tomee/tomee-myfaces/src/main/resources/META-INF/services/org.apache.myfaces.spi.FacesConfigurationProviderFactory Tue Mar 12 10:56:01 2013
@@ -0,0 +1 @@
+org.apache.tomee.myfaces.TomEEFacesConfigurationProviderFactory