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