You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sl...@apache.org on 2009/04/09 00:38:27 UTC

svn commit: r763434 - in /myfaces/core/branches/2_0_0: api/src/main/java/javax/faces/application/Application.java impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java

Author: slessard
Date: Wed Apr  8 22:38:27 2009
New Revision: 763434

URL: http://svn.apache.org/viewvc?rev=763434&view=rev
Log:
Implemented new Application methods.

Modified:
    myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/application/Application.java
    myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java

Modified: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/application/Application.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/application/Application.java?rev=763434&r1=763433&r2=763434&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/application/Application.java (original)
+++ myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/application/Application.java Wed Apr  8 22:38:27 2009
@@ -150,8 +150,6 @@
      */
     public void addDefaultValidatorId(String validatorId)
     {
-        // TODO: IMPLEMENT IMPL
-        throw new UnsupportedOperationException();
     }
 
     /**
@@ -251,7 +249,6 @@
      */
     public UIComponent createComponent(FacesContext context, String componentType, String rendererType)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -334,7 +331,6 @@
     public UIComponent createComponent(ValueExpression componentExpression, FacesContext context, String componentType,
                                        String rendererType)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -543,7 +539,6 @@
      */
     public Map<String, String> getDefaultValidatorInfo()
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -664,7 +659,6 @@
      */
     public ProjectStage getProjectStage()
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -725,7 +719,6 @@
      */
     public ResourceHandler getResourceHandler()
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -774,7 +767,6 @@
      */
     public void publishEvent(Class<? extends SystemEvent> systemEventClass, Class<?> sourceBaseType, Object source)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -843,7 +835,6 @@
      */
     public void publishEvent(Class<? extends SystemEvent> systemEventClass, Object source)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -937,7 +928,6 @@
      */
     public void setResourceHandler(ResourceHandler resourceHandler)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -1003,7 +993,6 @@
     public void subscribeToEvent(Class<? extends SystemEvent> systemEventClass, Class<?> sourceClass,
                                  SystemEventListener listener)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 
@@ -1030,7 +1019,6 @@
     public void unsubscribeFromEvent(Class<? extends SystemEvent> systemEventClass, Class<?> sourceClass,
                                      SystemEventListener listener)
     {
-        // TODO: IMPLEMENT IMPL
         throw new UnsupportedOperationException();
     }
 

Modified: myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java?rev=763434&r1=763433&r2=763434&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java (original)
+++ myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java Wed Apr  8 22:38:27 2009
@@ -18,6 +18,8 @@
  */
 package org.apache.myfaces.application;
 
+import java.beans.BeanDescriptor;
+import java.beans.BeanInfo;
 import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -42,12 +44,14 @@
 import javax.faces.application.Application;
 import javax.faces.application.NavigationHandler;
 import javax.faces.application.ProjectStage;
+import javax.faces.application.Resource;
 import javax.faces.application.ResourceDependencies;
 import javax.faces.application.ResourceDependency;
 import javax.faces.application.ResourceHandler;
 import javax.faces.application.StateManager;
 import javax.faces.application.ViewHandler;
 import javax.faces.component.UIComponent;
+import javax.faces.component.UINamingContainer;
 import javax.faces.component.UIOutput;
 import javax.faces.component.UIViewRoot;
 import javax.faces.component.behavior.ClientBehavior;
@@ -94,7 +98,7 @@
 
 /**
  * DOCUMENT ME!
- *
+ * 
  * @author Manfred Geiler (latest modification by $Author$)
  * @author Anton Koinov
  * @author Thomas Spiegl
@@ -144,6 +148,8 @@
 
     private final Map<Class<? extends SystemEvent>, SystemListenerEntry> _systemEventListenerClassMap = new ConcurrentHashMap<Class<? extends SystemEvent>, SystemListenerEntry>();
 
+    private Map<String, String> _defaultValidatorsIds = new ConcurrentHashMap<String, String>();
+
     private final RuntimeConfig _runtimeConfig;
 
     private ELResolver elResolver;
@@ -236,6 +242,21 @@
     }
 
     @Override
+    public void addDefaultValidatorId(String validatorId)
+    {
+        if (_validatorClassMap.containsKey(validatorId))
+        {
+            _defaultValidatorsIds.put(validatorId, _validatorClassMap.get(validatorId).getName());
+        }
+    }
+
+    @Override
+    public Map<String, String> getDefaultValidatorInfo()
+    {
+        return Collections.unmodifiableMap(_defaultValidatorsIds);
+    }
+
+    @Override
     public final ELResolver getELResolver()
     {
         // we don't need synchronization here since it is ok to have multiple
@@ -405,7 +426,7 @@
 
         ExpressionFactory factory = getExpressionFactory();
 
-        return (T)factory.createValueExpression(elContext, expression, expectedType).getValue(elContext);
+        return (T) factory.createValueExpression(elContext, expression, expectedType).getValue(elContext);
     }
 
     @Override
@@ -431,10 +452,11 @@
                 SystemEventListenerHolder holder = (SystemEventListenerHolder) source;
 
                 // If the source argument implements SystemEventListenerHolder, call
-                // SystemEventListenerHolder.getListenersForEventClass(java.lang.Class) on it, passing the systemEventClass
+                // SystemEventListenerHolder.getListenersForEventClass(java.lang.Class) on it, passing the
+                // systemEventClass
                 // argument. If the list is not empty, perform algorithm traverseListenerList on the list.
-                event = _traverseListenerList(holder.getListenersForEventClass(systemEventClass), systemEventClass, source,
-                                              event);
+                event = _traverseListenerList(holder.getListenersForEventClass(systemEventClass), systemEventClass,
+                                              source, event);
             }
 
             SystemListenerEntry systemListenerEntry = _systemEventListenerClassMap.get(systemEventClass);
@@ -899,6 +921,145 @@
     }
 
     @Override
+    public UIComponent createComponent(FacesContext context, Resource componentResource)
+    {
+        UIComponent component = null;
+        Resource resource;
+        String fqcn;
+        Class<? extends UIComponent> componentClass = null;
+
+        /*
+         * Obtain a reference to the ViewDeclarationLanguage for this Application instance by calling
+         * ViewHandler.getViewDeclarationLanguage(javax.faces.context.FacesContext, java.lang.String), passing the
+         * viewId found by calling UIViewRoot.getViewId() on the UIViewRoot in the argument FacesContext.
+         */
+        UIViewRoot view = context.getViewRoot();
+        ViewDeclarationLanguage vdl = getViewHandler().getViewDeclarationLanguage(context, view.getViewId());
+
+        /*
+         * Obtain a reference to the composite component metadata for this composite component by calling
+         * ViewDeclarationLanguage.getComponentMetadata(javax.faces.context.FacesContext,
+         * javax.faces.application.Resource), passing the facesContext and componentResource arguments to this method.
+         * This version of JSF specification uses JavaBeans as the API to the component metadata.
+         */
+        BeanInfo metadata = vdl.getComponentMetadata(context, componentResource);
+
+        /*
+         * Determine if the component author declared a component-type for this component instance by obtaining the
+         * BeanDescriptor from the component metadata and calling its getValue() method, passing
+         * UIComponent.COMPOSITE_COMPONENT_TYPE_KEY as the argument. If non-null, the result must be a ValueExpression
+         * whose value is the component-type of the UIComponent to be created for this Resource component. Call through
+         * to createComponent(java.lang.String) to create the component.
+         */
+        BeanDescriptor descriptor = metadata.getBeanDescriptor();
+        ValueExpression componentType = (ValueExpression) descriptor.getValue(UIComponent.COMPOSITE_COMPONENT_TYPE_KEY);
+
+        if (componentType != null)
+        {
+            component = createComponent((String) componentType.getValue(context.getELContext()));
+        }
+        else
+        {
+            /*
+             * Otherwise, determine if a script based component for this Resource can be found by calling
+             * ViewDeclarationLanguage.getScriptComponentResource(javax.faces.context.FacesContext,
+             * javax.faces.application.Resource). If the result is non-null, and is a script written in one of the
+             * languages listed in JSF 4.3 of the specification prose document, create a UIComponent instance from the
+             * script resource.
+             */
+            resource = vdl.getScriptComponentResource(context, componentResource);
+            if (resource != null)
+            {
+                String name = resource.getResourceName();
+                String className = name.substring(0, name.lastIndexOf('.'));
+
+                component = (UIComponent)ClassUtils.newInstance(className);
+                component.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
+            }
+            else
+            {
+                /*
+                 * Otherwise, let library-name be the return from calling Resource.getLibraryName() on the argument
+                 * componentResource and resource-name be the return from calling Resource.getResourceName() on the
+                 * argument componentResource. Create a fully qualified Java class name by removing any file extension
+                 * from resource-name and let fqcn be library-name + "." + resource-name. If a class with the name of
+                 * fqcn cannot be found, take no action and continue to the next step. If any of InstantiationException,
+                 * IllegalAccessException, or ClassCastException are thrown, wrap the exception in a FacesException and
+                 * re-throw it. If any other exception is thrown, log the exception and continue to the next step.
+                 */
+
+                fqcn = componentResource.getLibraryName() + "." + componentResource.getResourceName();
+
+                try
+                {
+                    componentClass = ClassUtils.classForName(fqcn);
+                }
+                catch (ClassNotFoundException e)
+                {
+                }
+
+                if (componentClass != null)
+                {
+                    try
+                    {
+                        component = componentClass.newInstance();
+                    }
+                    catch (InstantiationException e)
+                    {
+                        log.error("Could not instantiate component class name = " + fqcn, e);
+                        throw new FacesException("Could not instantiate component class name = " + fqcn, e);
+                    }
+                    catch (IllegalAccessException e)
+                    {
+                        log.error("Could not instantiate component class name = " + fqcn, e);
+                        throw new FacesException("Could not instantiate component class name = " + fqcn, e);
+                    }
+                    catch (Exception e)
+                    {
+                        log.error("Could not instantiate component class name = " + fqcn, e);
+                    }
+                }
+
+                /*
+                 * If none of the previous steps have yielded a UIComponent instance, call
+                 * createComponent(java.lang.String) passing "javax.faces.NamingContainer" as the argument.
+                 */
+                if (component == null)
+                {
+                    createComponent(UINamingContainer.COMPONENT_TYPE);
+                }
+            }
+        }
+
+        /*
+         * Call UIComponent.setRendererType(java.lang.String) on the UIComponent instance, passing
+         * "javax.faces.Composite" as the argument.
+         */
+        component.setRendererType("javax.faces.Composite");
+
+        /*
+         * Store the argument Resource in the attributes Map of the UIComponent under the key,
+         * Resource.COMPONENT_RESOURCE_KEY.
+         */
+        component.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
+
+        /*
+         * Store composite component metadata in the attributes Map of the UIComponent under the key,
+         * UIComponent.BEANINFO_KEY.
+         */
+        component.getAttributes().put(UIComponent.BEANINFO_KEY, metadata);
+
+        /*
+         * Before the component instance is returned, it must be inspected for the presence of a ListenerFor annotation.
+         * If this annotation is present, the action listed in ListenerFor must be taken on the component, before it is
+         * returned from this method.
+         */
+        _handleAnnotations(context, component, component);
+
+        return component;
+    }
+
+    @Override
     public UIComponent createComponent(FacesContext context, String componentType, String rendererType)
     {
         checkNull(context, "context");
@@ -1287,15 +1448,18 @@
             {
                 Constructor<?>[] constructors = systemEventClass.getConstructors();
                 Constructor<? extends SystemEvent> constructor = null;
-                for (Constructor<?> c : constructors) {
-                    if (c.getParameterTypes().length == 1) {
+                for (Constructor<?> c : constructors)
+                {
+                    if (c.getParameterTypes().length == 1)
+                    {
                         // Safe cast, since the constructor belongs
                         // to a class of type SystemEvent
                         constructor = (Constructor<? extends SystemEvent>) c;
                         break;
                     }
                 }
-                if (constructor != null) {
+                if (constructor != null)
+                {
                     event = constructor.newInstance(source);
                 }
 
@@ -1322,13 +1486,13 @@
             }
         }
 
-        _handleResourceDependency(context, inspected.getClass().getAnnotation(ResourceDependency.class));
+        _handleResourceDependency(context, component, inspected.getClass().getAnnotation(ResourceDependency.class));
         ResourceDependencies dependencies = inspected.getClass().getAnnotation(ResourceDependencies.class);
         if (dependencies != null)
         {
             for (ResourceDependency dependency : dependencies.value())
             {
-                _handleResourceDependency(context, dependency);
+                _handleResourceDependency(context, component, dependency);
             }
         }
     }
@@ -1394,7 +1558,7 @@
         }
     }
 
-    private void _handleResourceDependency(FacesContext context, ResourceDependency annotation)
+    private void _handleResourceDependency(FacesContext context, UIComponent component, ResourceDependency annotation)
     {
         // If this annotation is not present on the class in question, no action must be taken.
         if (annotation != null)
@@ -1425,7 +1589,19 @@
             if (library != null && library.length() > 0)
             {
                 // If library is non-null, store it under the key "library".
-                attributes.put("library", library);
+                if ("this".equals(library))
+                {
+                    // Special "this" behavior
+                    Resource resource = (Resource)component.getAttributes().get(Resource.COMPONENT_RESOURCE_KEY);
+                    if (resource != null)
+                    {
+                        attributes.put("library", resource.getLibraryName());
+                    }
+                }
+                else
+                {
+                    attributes.put("library", library);
+                }
             }
 
             // If target is the empty string, let target be null.
@@ -1448,9 +1624,8 @@
     {
         /*
          * The Renderer instance to inspect must be obtained by calling FacesContext.getRenderKit() and calling
-         * RenderKit.getRenderer(java.lang.String, java.lang.String) on the result, passing the argument
-         * componentFamily of the newly created component as the first argument and the argument rendererType as
-         * the second argument.
+         * RenderKit.getRenderer(java.lang.String, java.lang.String) on the result, passing the argument componentFamily
+         * of the newly created component as the first argument and the argument rendererType as the second argument.
          */
         Renderer renderer = context.getRenderKit().getRenderer(component.getFamily(), rendererType);
         if (renderer == null)
@@ -1591,7 +1766,7 @@
             {
                 /*
                  * TODO: Check if modification occurs often or not, might have to use a synchronized list instead.
-                 *
+                 * 
                  * Registrations found:
                  */
                 _lstSystemEventListener = new CopyOnWriteArrayList<SystemEventListener>();
@@ -1612,7 +1787,7 @@
             {
                 /*
                  * TODO: Check if modification occurs often or not, might have to use a synchronized list instead.
-                 *
+                 * 
                  * Registrations found:
                  */
                 list = new CopyOnWriteArrayList<SystemEventListener>();