KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > event > handlers > OutputTypeManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.event.handlers;
24
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.faces.context.FacesContext;
30
31
32 /**
33  * <p> <code>OutputTypeManager</code> manages the various {@link OutputType}s
34  * that can be used. The {@link OutputType}s are managed statically.</p>
35  *
36  * @author Ken Paulsen (ken.paulsen@sun.com)
37  */

38 public class OutputTypeManager {
39
40
41     /**
42      * Constructor.
43      */

44     protected OutputTypeManager() {
45     }
46
47     /**
48      *
49      */

50     public static OutputTypeManager getInstance() {
51     return _defaultInstance;
52     }
53
54     /**
55      * <p> This is a factory method for obtaining an OutputTypeManager
56      * instance. This implementation uses the external context's
57      * initParams to look for the OutputTypeManager class. If it
58      * exists, the specified concrete OutputTypeManager class will
59      * be used. Otherwise, the default will be used -- which is an
60      * instance of this class. The initParam key is:
61      * {@link #OUTPUT_TYPE_MANAGER_KEY}.</p>
62      *
63      * @param context The FacesContext
64      *
65      * @see #OUTPUT_TYPE_MANAGER_KEY
66      */

67     public static OutputTypeManager getManager(FacesContext context) {
68     if (context == null) {
69         return _defaultInstance;
70     }
71
72     // If the context is non-null, check for init parameter specifying
73
// the Manager
74
String JavaDoc className = null;
75     Map JavaDoc initParams = context.getExternalContext().getInitParameterMap();
76     if (initParams.containsKey(OUTPUT_TYPE_MANAGER_KEY)) {
77         className = (String JavaDoc) initParams.get(OUTPUT_TYPE_MANAGER_KEY);
78     }
79     return getManager(className);
80     }
81
82
83     /**
84      * This method is a singleton factory method for obtaining an instance of
85      * a OutputTypeManager. It is possible that multiple different
86      * implementations of OutputTypeManagers will be used within the
87      * same JVM. This is OK, the purpose of the OutputTypeManager is
88      * primarily performance. Someone may provide a different
89      * OutputTypeManager to locate OutputTypeManager's in a different way
90      * (XML, database, file, java code, etc.).
91      */

92     public static OutputTypeManager getManager(String JavaDoc className) {
93     if (className == null) {
94         // Default case...
95
return _defaultInstance;
96     }
97
98     OutputTypeManager ldm =
99         (OutputTypeManager) _instances.get(className);
100     if (ldm == null) {
101         try {
102         ldm = (OutputTypeManager) Class.forName(className).
103             getMethod("getInstance", (Class JavaDoc []) null).
104             invoke((Object JavaDoc) null, (Object JavaDoc []) null);
105         } catch (ClassNotFoundException JavaDoc ex) {
106         throw new RuntimeException JavaDoc(ex);
107         } catch (NoSuchMethodException JavaDoc ex) {
108         throw new RuntimeException JavaDoc(ex);
109         } catch (IllegalAccessException JavaDoc ex) {
110         throw new RuntimeException JavaDoc(ex);
111         } catch (InvocationTargetException JavaDoc ex) {
112         throw new RuntimeException JavaDoc(ex);
113         } catch (NullPointerException JavaDoc ex) {
114         throw new RuntimeException JavaDoc(ex);
115         } catch (ClassCastException JavaDoc ex) {
116         throw new RuntimeException JavaDoc(ex);
117         }
118         _instances.put(className, ldm);
119     }
120     return ldm;
121     }
122
123     /**
124      * <p> This method retrieves an OutputType.</p>
125      *
126      * @param name The name of the OutputType.
127      *
128      * @return The requested OutputType.
129      */

130     public OutputType getOutputType(String JavaDoc name) {
131     return (OutputType) _outputTypes.get(name);
132     }
133
134     /**
135      * <p> This method sets an OutputType.</p>
136      *
137      * @param name The name of the OutputType.
138      * @param outputType The OutputType.
139      */

140     public void setOutputType(String JavaDoc name, OutputType outputType) {
141     _outputTypes.put(name, outputType);
142     }
143
144     /**
145      * <p> Cache different subclasses. </p>
146      */

147     private static Map JavaDoc _outputTypes = new HashMap JavaDoc(8);
148
149     /**
150      * <p> Cache different subclasses. </p>
151      */

152     private static Map JavaDoc _instances = new HashMap JavaDoc(2);
153
154     /**
155      * <p> This is the default implementation of the OutputTypeManager, which
156      * happens to be an instance of this class (because I'm too lazy to
157      * do this right).</p>
158      */

159     private static OutputTypeManager _defaultInstance =
160     new OutputTypeManager();
161
162
163     /**
164      * <p> This constant defines the layout definition manager implementation
165      * key for initParams. The value for this initParam should be the
166      * full class name of an {@link OutputTypeManager}.
167      * ("outputTypeManagerImpl")</p>
168      */

169     public static final String JavaDoc OUTPUT_TYPE_MANAGER_KEY =
170     "outputTypeManagerImpl";
171
172     public static final String JavaDoc REQUEST_ATTRIBUTE_TYPE = "attribute";
173     public static final String JavaDoc SESSION_ATTRIBUTE_TYPE = "session";
174
175     static {
176     _outputTypes.put(REQUEST_ATTRIBUTE_TYPE,
177         new RequestAttributeOutputType());
178     _outputTypes.put(SESSION_ATTRIBUTE_TYPE,
179         new SessionAttributeOutputType());
180     }
181 }
182
Popular Tags