KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > layout > LayoutDefinitionManager


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.layout;
24
25 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutDefinition;
26 import com.sun.enterprise.tools.jsfext.util.Util;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.faces.context.FacesContext;
34
35
36 /**
37  * <p> This abstract class provides the base functionality for all
38  * <code>LayoutDefinitionManager</code> implementations. It provides an
39  * static method used to obtain an instance of a concrete
40  * <code>LayoutDefinitionManager</code>: {@link #getManager(FacesContext)}.
41  * It also provides another version of this method which allows a specific
42  * instance to be specified by classname:
43  * {@link #getManager(String className)} (typically not used, the
44  * environment should be setup to provide the correct
45  * <code>LayoutDefinitionManager</code>). Once an instance is obtained,
46  * the {@link #getLayoutDefinition(String key)} method can be invoked to
47  * obtain a {@link LayoutDefinition}.
48  * </p>
49  *
50  * @author Ken Paulsen (ken.paulsen@sun.com)
51  */

52 public abstract class LayoutDefinitionManager {
53
54
55     /**
56      * <p> Constructor.</p>
57      */

58     protected LayoutDefinitionManager() {
59     super();
60     }
61
62
63     /**
64      * <p> This method is responsible for finding/creating the requested
65      * {@link LayoutDefinition}.</p>
66      *
67      * @param key The key used to identify the requested
68      * {@link LayoutDefintion}.
69      */

70     public abstract LayoutDefinition getLayoutDefinition(String JavaDoc key) throws IOException JavaDoc;
71
72
73     /**
74      * <p> This is a factory method for obtaining the
75      * {@link LayoutDefinitionManager}. This implementation uses the
76      * external context's initParams to look for the
77      * {@link LayoutDefinitionManager} class. If it exists, the specified
78      * concrete {@link LayoutDefinitionManager} class will be used.
79      * Otherwise, the default {@link LayoutDefinitionManager} will be
80      * used. The initParam key is:
81      * {@link #LAYOUT_DEFINITION_MANAGER_KEY}.</p>
82      *
83      * @param context The <code>FacesContext</code>.
84      *
85      * @see #LAYOUT_DEFINITION_MANAGER_KEY
86      */

87     public static LayoutDefinitionManager getManager(FacesContext context) {
88 // FIXME: Decide how to define the LAYOUT_DEFINITION_MANAGER
89
// FIXME: Properties should be settable on the LDM, such as entity resolvers...
90
Map JavaDoc initParams = context.getExternalContext().getInitParameterMap();
91     String JavaDoc className = DEFAULT_LAYOUT_DEFINITION_MANAGER_IMPL;
92     if (initParams.containsKey(LAYOUT_DEFINITION_MANAGER_KEY)) {
93         className = (String JavaDoc) initParams.get(LAYOUT_DEFINITION_MANAGER_KEY);
94     }
95     return getManager(className);
96     }
97
98
99     /**
100      * <p> This method is a singleton factory method for obtaining an instance
101      * of a <code>LayoutDefintionManager</code>. It is possible that
102      * multiple different implementations of
103      * <code>LayoutDefinitionManager</code>s will be used within the same
104      * JVM. This is OK, the purpose of the
105      * <code>LayoutDefinitionManager</code> is primarily performance.
106      * Someone may provide a different <code>LayoutDefinitionManager</code>
107      * to locate {@link LayoutDefiniton}'s in a different way (XML,
108      * database, file, java code, etc.).</p>
109      */

110     public static LayoutDefinitionManager getManager(String JavaDoc className) {
111     LayoutDefinitionManager ldm =
112         (LayoutDefinitionManager) _instances.get(className);
113     if (ldm == null) {
114         try {
115         ClassLoader JavaDoc loader = Util.getClassLoader(className);
116         ldm = (LayoutDefinitionManager) loader.loadClass(className).
117             getMethod("getInstance", (Class JavaDoc []) null).
118             invoke((Object JavaDoc) null, (Object JavaDoc []) null);
119         } catch (ClassNotFoundException JavaDoc ex) {
120         throw new RuntimeException JavaDoc(ex);
121         } catch (NoSuchMethodException JavaDoc ex) {
122         throw new RuntimeException JavaDoc(ex);
123         } catch (IllegalAccessException JavaDoc ex) {
124         throw new RuntimeException JavaDoc(ex);
125         } catch (InvocationTargetException JavaDoc ex) {
126         throw new RuntimeException JavaDoc(ex);
127         } catch (NullPointerException JavaDoc ex) {
128         throw new RuntimeException JavaDoc(ex);
129         } catch (ClassCastException JavaDoc ex) {
130         throw new RuntimeException JavaDoc(ex);
131         }
132         _instances.put(className, ldm);
133     }
134     return ldm;
135     }
136
137
138     /**
139      * <p> Retrieve an attribute by key.</p>
140      *
141      * @param key The key used to retrieve the attribute
142      *
143      * @return The requested attribute or null
144      */

145     public Object JavaDoc getAttribute(String JavaDoc key) {
146     return _attributes.get(key);
147     }
148
149
150     /**
151      * <p> Associate the given key with the given Object as an attribute.</p>
152      *
153      * @param key The key associated with the given object (if this key
154      * is already in use, it will replace the previously set attribute
155      * object).
156      *
157      * @param value The Object to store.
158      */

159     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
160     _attributes.put(key, value);
161     }
162
163
164     /**
165      * <p> This map contains sub-class specific attributes that may be needed
166      * by specific implementations of
167      * <code>LayoutDefinitionManager</code>s. For example, setting an
168      * <code>EntityResolver</code> on a
169      * <code>LayoutDefinitionManager</code> that creates
170      * <code>LayoutDefinitions</code> from XML files.</p>
171      */

172     private Map JavaDoc _attributes = new HashMap JavaDoc();
173
174
175 // FIXME: Rethink this... since I am allowing LDM's to be parameterized via
176
// FIXME: attributes, it is not enough to have 1 LDM... we will need 1 per
177
// FIXME: application. Or... I need to move the attributes.
178
/**
179      * <p> Static map of <code>LayoutDefinitionManager</code s. Normally
180      * this will only contain the default <code>LayoutManager</code>.</p>
181      */

182     private static Map JavaDoc _instances = new HashMap JavaDoc(2);
183
184
185     /**
186      * <p> This constant defines the default
187      * <code>LayoutDefinitionManager</code> implementation class name.</p>
188      */

189     public static final String JavaDoc DEFAULT_LAYOUT_DEFINITION_MANAGER_IMPL =
190     "com.sun.enterprise.tools.jsfext.layout.xml.XMLLayoutDefinitionManager";
191
192
193     /**
194      * <p> This constant defines the <code>LayoutDefinitionManager</code>
195      * implementation key for initParams. ("layoutManagerImpl")</p>
196      */

197     public static final String JavaDoc LAYOUT_DEFINITION_MANAGER_KEY =
198     "layoutManagerImpl";
199 }
200
Popular Tags