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.PostValidateEvent;
28  import javax.faces.event.PreRenderComponentEvent;
29  import javax.faces.event.PreRenderViewEvent;
30  import javax.faces.event.PreValidateEvent;
31  
32  /**
33   * The NamedEventManager class is used to keep map a short name to ComponentSystemEvent classes
34   * annotated with @NamedEvent.
35   */
36  
37  public class NamedEventManager
38  {
39      //private static final NamedEventManager instance = new NamedEventManager();
40      
41      private HashMap<String, Collection<Class<? extends ComponentSystemEvent>>> events;
42      
43      public NamedEventManager ()
44      {
45          events = new HashMap<String, Collection<Class<? extends ComponentSystemEvent>>>();
46          
47          // Special spec-defined values.
48          
49          addNamedEvent ("postAddToView", PostAddToViewEvent.class);
50          addNamedEvent ("preRenderView", PreRenderViewEvent.class);
51          addNamedEvent ("preRenderComponent", PreRenderComponentEvent.class);
52          addNamedEvent ("preValidate", PreValidateEvent.class);
53          addNamedEvent ("postValidate", PostValidateEvent.class);
54      }
55      
56      //public static NamedEventManager getInstance ()
57      //{
58      //    return instance;
59      //}
60      
61      /**
62       * Registers a named event.
63       * 
64       * @param shortName a String containing the short name for the event, from the @NamedEvent.shortName()
65       *        attribute.
66       * @param cls the event class to register.
67       */
68      
69      public void addNamedEvent (String shortName, Class<? extends ComponentSystemEvent> cls)
70      {
71          String key = shortName;
72          Collection<Class<? extends ComponentSystemEvent>> eventList;
73          
74          // Per the spec, if the short name is missing, generate one.
75          
76          if (shortName == null)
77          {
78              key = getFixedName (cls);
79          }
80          
81          eventList = events.get (key);
82          
83          if (eventList == null)
84          {
85              // First event registered to this short name.
86              
87              eventList = new LinkedList<Class<? extends ComponentSystemEvent>>();
88              
89              events.put (key, eventList);
90          }
91          
92          eventList.add (cls);
93      }
94      
95      /**
96       * Retrieves a collection of system event classes based on their short name.
97       * 
98       * @param shortName the short name to look up.
99       * @return a Collection of Class objects containing the system event classes registered to
100      *         the given short name.
101      */
102     
103     public Collection<Class<? extends ComponentSystemEvent>> getNamedEvent (String shortName)
104     {
105         return events.get (shortName);
106     }
107     
108     /**
109      * Retrieves the short name for an event class, according to spec rules.
110      * 
111      * @param cls the class to find the short name for.
112      * @return a String containing the short name for the given class.
113      */
114     
115     private String getFixedName (Class<? extends ComponentSystemEvent> cls)
116     {
117         StringBuilder result = new StringBuilder();
118         String className;
119         
120         // Get the unqualified class name.
121         
122         className = cls.getSimpleName();
123         
124         // Strip the trailing "event" off the class name if present.
125         
126         if (className.toLowerCase().endsWith ("event"))
127         {
128             className = className.substring (0, result.length() - 5);
129         }
130         
131         // Prepend the package name.
132         
133         if (cls.getPackage() != null)
134         {
135             result.append (cls.getPackage().getName());
136             result.append ('.');
137         }
138         
139         result.append (className);
140         
141         return result.toString();
142     }
143 }