KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > BaseRoot


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.share.configbean;
20
21 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
22 import javax.enterprise.deploy.spi.DConfigBeanRoot JavaDoc;
23 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
24 import javax.enterprise.deploy.model.DDBean JavaDoc;
25 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
26
27 import org.netbeans.modules.j2ee.sun.share.config.DDRoot;
28 import org.openide.ErrorManager;
29
30
31 /**
32  *
33  * @author Vince Kraemer
34  * @author Peter Williams
35  */

36 public abstract class BaseRoot extends Base implements DConfigBeanRoot JavaDoc {
37
38     private SunONEDeploymentConfiguration dc;
39     private DDBean JavaDoc uri;
40     private DDBean JavaDoc displayNameDD;
41
42     public SunONEDeploymentConfiguration getConfig() {
43         if(null == getParent()) {
44             return this.dc;
45         } else {
46             return getParent().getConfig();
47         }
48     }
49
50     protected void setConfig(SunONEDeploymentConfiguration dc) {
51         this.dc = dc;
52     }
53
54     public BaseRoot() {
55     }
56
57     protected void init(DDBeanRoot JavaDoc dDBeanRoot, SunONEDeploymentConfiguration dc, DDBean JavaDoc ddbExtra) throws ConfigurationException JavaDoc {
58         super.init(dDBeanRoot, null);
59         this.dc = dc;
60         this.uri = ddbExtra;
61         
62         // initialize reference if we can
63
findRefDCB(dDBeanRoot);
64     }
65     
66     /** Retrieve a DConfigBeanRoot bound to a secondary descriptor file, e.g.
67      * webservices.xml. This method is overloaded in EjbJarRoot and WebAppRoot
68      * to handle creating the WebServices DConfigBeanRoot. No other secondary
69      * descriptors are supported (or even exist).
70      *
71      */

72     public DConfigBean JavaDoc getDConfigBean(DDBeanRoot JavaDoc dDBeanRoot) {
73         return null;
74     }
75     
76     protected BaseRoot createWebServicesRoot(DDBeanRoot JavaDoc dDBeanRoot) {
77         if(null == dDBeanRoot) {
78             throw new IllegalArgumentException JavaDoc(bundle.getString("ERR_DDBeanIsNull"));
79         }
80         
81         if(null == dDBeanRoot.getXpath()) {
82             throw new IllegalArgumentException JavaDoc(bundle.getString("ERR_DDBeanHasNullXpath"));
83         }
84
85         BaseRoot rootDCBean = null;
86         
87         if(dDBeanRoot.getXpath().equals("/webservices")) {
88             SunONEDeploymentConfiguration config = getConfig();
89
90             // If DDBean is not from our internal tree, normalize it to one that is.
91
if(!(dDBeanRoot instanceof DDRoot)) {
92                 // If the root cache is empty, then it is likely that
93
assert config.getDCBRootCache().entrySet().size() > 0 : "No DDBeanRoots have been cached. No way to normalize " + dDBeanRoot;
94                 dDBeanRoot = config.getStorage().normalizeDDBeanRoot(dDBeanRoot);
95             }
96
97             rootDCBean = (BaseRoot) config.getDCBRootCache().get(dDBeanRoot);
98
99             if(null == rootDCBean) {
100                 try {
101                     rootDCBean = new WebServices();
102                     rootDCBean.init(dDBeanRoot, config, dDBeanRoot);
103                     config.getDCBCache().put(dDBeanRoot, rootDCBean);
104                     config.getDCBRootCache().put(dDBeanRoot, rootDCBean);
105                 } catch(ConfigurationException JavaDoc ex) {
106                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
107                     rootDCBean = null;
108                 }
109             }
110         }
111         
112         return rootDCBean;
113     }
114
115     public String JavaDoc getComponentName() {
116         String JavaDoc name = getUriText();
117         
118         // This method should return null if there is no name, while getUriText
119
// must always return non-null.
120
if(!Utils.notEmpty(name)) {
121             name = null;
122         }
123
124         return name;
125     }
126
127     public String JavaDoc getUriText() {
128         BaseModuleRef ref = getReference();
129         
130         if(ref != null) {
131             return ref.getModuleUri();
132         }
133
134         // No reference, no URI. This should only happen with solo modules
135
// such as webapp modules and ejbjar modules.
136
return ""; // NOI18N
137
}
138     
139     /** Get the J2EE version of this module. The derived class must implement
140      * this method and return the correct J2EEBaseVersion derivative representing
141      * the version of the module, e.g. ServletVersion.SERVLET_X_Y for web-apps,
142      *
143      * This method MUST be overridden in derived root classes (e.g. WebAppRoot, etc.)
144      * It is not declared abstract because there is a real implementation in
145      * Base.java that is used by the child beans and I'm not sure if java will
146      * allow turning a concrete function to abstract for a portion of the hierarchy.
147      * It's a sketchy concept at best, even if allowed.
148      *
149      * @return J2EEVersion enum for the version of this module.
150      */

151     public J2EEBaseVersion getJ2EEModuleVersion() {
152         // This implementation should never be called.
153
assert false : this.getClass().getName() + " does not override getJ2EEModuleVersion!!!"; // NOI18N
154

155         // Prevent other runtime issues, if something slips past by returning
156
// something reasonable, but likely benign.
157
return J2EEVersion.J2EE_1_4;
158     }
159
160     /** Get the AppServer version to be used for saving deployment descriptors.
161      * Note that this is different than the version of the connected target
162      * application server (which can be queried by the appropriate method on
163      * SunONEDeploymentConfiguration.)
164      *
165      * @return ASDDVersion enum for the appserver version
166      */

167     public ASDDVersion getAppServerVersion() {
168         return getConfig().getAppServerVersion();
169     }
170         
171     /** Set the AppServer version to be used for saving deployment descriptors.
172      *
173      * @param asVersion enum for the appserver version
174      */

175     public void setAppServerVersion(ASDDVersion asVersion) {
176         getConfig().setAppServerVersion(asVersion);
177     }
178     
179     /** Generate a DOCTYPE string for the specified version (which may be different
180      * than the current version of the tree
181      */

182     public abstract String JavaDoc generateDocType(ASDDVersion version);
183
184     protected String JavaDoc generateDocType(String JavaDoc docroot, String JavaDoc publicId, String JavaDoc systemId) {
185         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(192);
186         buffer.append("<DOCTYPE "); // NOI18N
187
buffer.append(docroot);
188         buffer.append(" PUBLIC \n\t\""); // NOI18N
189
buffer.append(publicId);
190         buffer.append("\" \n\t\""); // NOI18N
191
buffer.append(systemId);
192         buffer.append("\">"); // NOI18N
193
return buffer.toString();
194     }
195     
196     
197     /** -----------------------------------------------------------------------
198      * Properties
199      */

200     
201     /** Getter for property refIdentity.
202      * @return Value of property refIdentity.
203      *
204      */

205     public String JavaDoc getRefIdentity() {
206         String JavaDoc result = "(null)"; // NOI18N
207
if(getReference() != null) {
208             result = getReference().getIdentity();
209         }
210         
211         return result;
212     }
213     
214     /** -----------------------------------------------------------------------
215      * Reference support
216      */

217     private BaseModuleRef rootReference = null;
218     
219     protected BaseModuleRef getReference() {
220         return rootReference;
221     }
222     
223     protected void setReference(BaseModuleRef ref) {
224         rootReference = ref;
225         this.uri = ref.getDDBean();
226     }
227     
228     protected void findRefDCB(DDBeanRoot JavaDoc ddbRoot) {
229         // For this direction, check patch cache to see if there is an unpatched
230
// reference bean under this key.
231
//
232
BaseModuleRef ref = (BaseModuleRef) dc.getPatchList().get(ddbRoot);
233         
234         if(ref != null) {
235             setReference(ref);
236             ref.setReference(this);
237             
238             // Since we found and patched the reference, remove it from the
239
// patch list.
240
dc.getPatchList().remove(ddbRoot);
241         }
242     }
243     
244     /** -----------------------------------------------------------------------
245      * Persistence support helper objects
246      */

247     protected abstract ConfigParser getParser();
248     
249 /* !PW not currently used. I didn't like the additional runtime issues that
250  * using introspection added to the code.
251  *
252     public static class SimpleRootParser implements ConfigParser {
253         private Class rootBaseBeanClass;
254         private Method createGraphMethod;
255         private Method createGraphFromStreamMethod;
256
257         public SimpleRootParser(Class rootTargetClass) {
258             rootBaseBeanClass = rootTargetClass;
259
260             try {
261                 createGraphMethod = rootBaseBeanClass.getMethod("createGraph", null); // NOI18N
262
263                 Class [] paramTypes = new Class[1];
264                 paramTypes[0] = java.io.InputStream.class;
265                 createGraphFromStreamMethod = rootBaseBeanClass.getMethod("createGraph", paramTypes); // NOI18N
266             } catch(NoSuchMethodException ex) {
267                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
268             } catch(SecurityException ex) {
269                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
270             }
271         }
272
273         public Object parse(java.io.InputStream stream) {
274             Object result = null;
275
276             try {
277                 if(null == stream) {
278                     // call <createGraph>();
279                     result = createGraphMethod.invoke(null, null);
280                 } else {
281                     try {
282                         // call <createGraph>(stream);
283                         Object [] params = new Object[1];
284                         params[0] = stream;
285                         createGraphFromStreamMethod.invoke(null, params);
286                     } catch(Exception ex) {
287                         jsr88Logger.severe("invalid stream for " + rootBaseBeanClass.getName());
288                         // call <createGraph>();
289                         result = createGraphMethod.invoke(null, null);
290                     }
291                 }
292             } catch(Exception ex) {
293                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
294             }
295
296             return result;
297         }
298     }
299 */

300     
301     public static class SimpleRootFinder implements ConfigFinder {
302         private Class JavaDoc rootBaseBeanClass;
303         
304         public SimpleRootFinder(Class JavaDoc rootTargetClass) {
305             rootBaseBeanClass = rootTargetClass;
306         }
307         
308         public Object JavaDoc find(Object JavaDoc obj) {
309             Object JavaDoc result = null;
310             if(rootBaseBeanClass.equals(obj.getClass())) {
311                 result = obj;
312             }
313             return result;
314         }
315     }
316 }
317
Popular Tags