001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.component.util.event;
042:
043: import java.lang.reflect.InvocationTargetException;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import javax.faces.context.FacesContext;
048:
049: /**
050: * <P> This class provides the functionality for the OutputTypeManager. The
051: * OutputTypeManager manages the various OutputTypes that can be used.
052: * The OutputTypes are managed statically.</P>
053: *
054: * @author Ken Paulsen (ken.paulsen@sun.com)
055: */
056: public class OutputTypeManager {
057:
058: /**
059: * Constructor.
060: */
061: protected OutputTypeManager() {
062: }
063:
064: /**
065: *
066: */
067: public static OutputTypeManager getInstance() {
068: return _defaultInstance;
069: }
070:
071: /**
072: * <P> This is a factory method for obtaining an OutputTypeManager
073: * instance. This implementation uses the external context's
074: * initParams to look for the OutputTypeManager class. If it
075: * exists, the specified concrete OutputTypeManager class will
076: * be used. Otherwise, the default will be used -- which is an
077: * instance of this class. The initParam key is:
078: * {@link #OUTPUT_TYPE_MANAGER_KEY}.</P>
079: *
080: * @param context The FacesContext
081: *
082: * @see #OUTPUT_TYPE_MANAGER_KEY
083: */
084: public static OutputTypeManager getManager(FacesContext context) {
085: if (context == null) {
086: return _defaultInstance;
087: }
088:
089: // If the context is non-null, check for init parameter specifying
090: // the Manager
091: String className = null;
092: Map initParams = context.getExternalContext()
093: .getInitParameterMap();
094: if (initParams.containsKey(OUTPUT_TYPE_MANAGER_KEY)) {
095: className = (String) initParams
096: .get(OUTPUT_TYPE_MANAGER_KEY);
097: }
098: return getManager(className);
099: }
100:
101: /**
102: * This method is a singleton factory method for obtaining an instance of
103: * a OutputTypeManager. It is possible that multiple different
104: * implementations of OutputTypeManagers will be used within the
105: * same JVM. This is OK, the purpose of the OutputTypeManager is
106: * primarily performance. Someone may provide a different
107: * OutputTypeManager to locate OutputTypeManager's in a different way
108: * (XML, database, file, java code, etc.).
109: */
110: public static OutputTypeManager getManager(String className) {
111: if (className == null) {
112: // Default case...
113: return _defaultInstance;
114: }
115:
116: OutputTypeManager ldm = (OutputTypeManager) _instances
117: .get(className);
118: if (ldm == null) {
119: try {
120: ldm = (OutputTypeManager) Class.forName(className)
121: .getMethod("getInstance", null).invoke(null,
122: null);
123: } catch (ClassNotFoundException ex) {
124: throw new RuntimeException(ex);
125: } catch (NoSuchMethodException ex) {
126: throw new RuntimeException(ex);
127: } catch (IllegalAccessException ex) {
128: throw new RuntimeException(ex);
129: } catch (InvocationTargetException ex) {
130: throw new RuntimeException(ex);
131: } catch (NullPointerException ex) {
132: throw new RuntimeException(ex);
133: } catch (ClassCastException ex) {
134: throw new RuntimeException(ex);
135: }
136: _instances.put(className, ldm);
137: }
138: return ldm;
139: }
140:
141: /**
142: * <P> This method retrieves an OutputType.</P>
143: *
144: * @param name The name of the OutputType.
145: *
146: * @return The requested OutputType.
147: */
148: public OutputType getOutputType(String name) {
149: return (OutputType) _outputTypes.get(name);
150: }
151:
152: /**
153: * <P> This method sets an OutputType.</P>
154: *
155: * @param name The name of the OutputType.
156: * @param outputType The OutputType.
157: */
158: public void setOutputType(String name, OutputType outputType) {
159: _outputTypes.put(name, outputType);
160: }
161:
162: /**
163: * <P> Cache different subclasses. </P>
164: */
165: private static Map _outputTypes = new HashMap(8);
166:
167: /**
168: * <P> Cache different subclasses. </P>
169: */
170: private static Map _instances = new HashMap(2);
171:
172: /**
173: * <P> This is the default implementation of the OutputTypeManager, which
174: * happens to be an instance of this class (because I'm too lazy to
175: * do this right).</P>
176: */
177: private static OutputTypeManager _defaultInstance = new OutputTypeManager();
178:
179: /**
180: * <P> This constant defines the layout definition manager implementation
181: * key for initParams. The value for this initParam should be the
182: * full class name of an {@link OutputTypeManager}.
183: * ("outputTypeManagerImpl")</P>
184: */
185: public static final String OUTPUT_TYPE_MANAGER_KEY = "outputTypeManagerImpl";
186:
187: public static final String REQUEST_ATTRIBUTE_TYPE = "attribute";
188: public static final String SESSION_ATTRIBUTE_TYPE = "session";
189:
190: static {
191: _outputTypes.put(REQUEST_ATTRIBUTE_TYPE,
192: new RequestAttributeOutputType());
193: _outputTypes.put(SESSION_ATTRIBUTE_TYPE,
194: new SessionAttributeOutputType());
195: }
196: }
|