View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.config;
20  
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.LinkedList;
24  
25  import javax.faces.event.ComponentSystemEvent;
26  import javax.faces.event.PostAddToViewEvent;
27  import javax.faces.event.PostRenderViewEvent;
28  import javax.faces.event.PostValidateEvent;
29  import javax.faces.event.PreRenderComponentEvent;
30  import javax.faces.event.PreRenderViewEvent;
31  import javax.faces.event.PreValidateEvent;
32  
33  /**
34   * The NamedEventManager class is used to keep map a short name to ComponentSystemEvent classes
35   * annotated with @NamedEvent.
36   */
37  public class NamedEventManager
38  {
39      private HashMap<String, Collection<Class<? extends ComponentSystemEvent>>> events;
40      
41      public NamedEventManager ()
42      {
43          events = new HashMap<String, Collection<Class<? extends ComponentSystemEvent>>>();
44          
45          // Special spec-defined values.
46          
47          addNamedEvent("postAddToView", PostAddToViewEvent.class);
48          addNamedEvent("preRenderView", PreRenderViewEvent.class);
49          addNamedEvent("preRenderComponent", PreRenderComponentEvent.class);
50          addNamedEvent("preValidate", PreValidateEvent.class);
51          addNamedEvent("postValidate", PostValidateEvent.class);
52          addNamedEvent("postRenderView", PostRenderViewEvent.class);
53      }
54  
55      /**
56       * Registers a named event.
57       * 
58       * @param shortName a String containing the short name for the event, from the @NamedEvent.shortName()
59       *        attribute.
60       * @param cls the event class to register.
61       */
62      public void addNamedEvent(String shortName, Class<? extends ComponentSystemEvent> cls)
63      {
64          String key = shortName;
65          Collection<Class<? extends ComponentSystemEvent>> eventList;
66          
67          // Per the spec, if the short name is missing, generate one.
68          
69          if (shortName == null)
70          {
71              key = getFixedName (cls);
72          }
73          
74          eventList = events.get (key);
75          
76          if (eventList == null)
77          {
78              // First event registered to this short name.
79              
80              eventList = new LinkedList<Class<? extends ComponentSystemEvent>>();
81              
82              events.put (key, eventList);
83          }
84          
85          eventList.add (cls);
86      }
87      
88      /**
89       * Retrieves a collection of system event classes based on their short name.
90       * 
91       * @param shortName the short name to look up.
92       * @return a Collection of Class objects containing the system event classes registered to
93       *         the given short name.
94       */
95      public Collection<Class<? extends ComponentSystemEvent>> getNamedEvent(String shortName)
96      {
97          return events.get(shortName);
98      }
99      
100     /**
101      * Retrieves the short name for an event class, according to spec rules.
102      * 
103      * @param cls the class to find the short name for.
104      * @return a String containing the short name for the given class.
105      */
106     private String getFixedName(Class<? extends ComponentSystemEvent> cls)
107     {
108         StringBuilder result = new StringBuilder();
109         String className;
110         
111         // Get the unqualified class name.
112         
113         className = cls.getSimpleName();
114         
115         // Strip the trailing "event" off the class name if present.
116         
117         if (className.toLowerCase().endsWith("event"))
118         {
119             className = className.substring(0, result.length() - 5);
120         }
121         
122         // Prepend the package name.
123         
124         if (cls.getPackage() != null)
125         {
126             result.append(cls.getPackage().getName());
127             result.append('.');
128         }
129         
130         result.append(className);
131         
132         return result.toString();
133     }
134 }