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-2006 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 org.netbeans.modules.j2ee.sun.share.configbean;
042:
043: import javax.enterprise.deploy.spi.DConfigBean;
044: import javax.enterprise.deploy.spi.DConfigBeanRoot;
045: import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
046: import javax.enterprise.deploy.model.DDBean;
047: import javax.enterprise.deploy.model.DDBeanRoot;
048: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
049: import org.netbeans.modules.j2ee.sun.dd.api.ASDDVersion;
050:
051: import org.netbeans.modules.j2ee.sun.share.config.DDRoot;
052: import org.openide.ErrorManager;
053:
054: /**
055: *
056: * @author Vince Kraemer
057: * @author Peter Williams
058: */
059: public abstract class BaseRoot extends Base implements DConfigBeanRoot {
060:
061: private SunONEDeploymentConfiguration dc;
062: private DDBean uri;
063: private Object uriObj;
064: private DDBean displayNameDD;
065:
066: public SunONEDeploymentConfiguration getConfig() {
067: if (null == getParent()) {
068: return this .dc;
069: } else {
070: return getParent().getConfig();
071: }
072: }
073:
074: protected void setConfig(SunONEDeploymentConfiguration dc) {
075: this .dc = dc;
076: }
077:
078: public BaseRoot() {
079: }
080:
081: protected void init(DDBeanRoot dDBeanRoot,
082: SunONEDeploymentConfiguration dc, DDBean ddbExtra)
083: throws ConfigurationException {
084: super .init(dDBeanRoot, null);
085: this .dc = dc;
086: this .uri = ddbExtra;
087:
088: // initialize reference if we can
089: findRefDCB(dDBeanRoot);
090: }
091:
092: void init(
093: J2eeModule mod,
094: SunONEDeploymentConfiguration sunONEDeploymentConfiguration,
095: Object uriObj) {
096: super .init(mod, null);
097: this .dc = dc;
098: this .uriObj = uriObj;
099:
100: // initialize reference if we can
101: // findRefDCB(mod);
102: }
103:
104: /** Retrieve a DConfigBeanRoot bound to a secondary descriptor file, e.g.
105: * webservices.xml. This method is overloaded in EjbJarRoot and WebAppRoot
106: * to handle creating the WebServices DConfigBeanRoot. No other secondary
107: * descriptors are supported (or even exist).
108: *
109: */
110: public DConfigBean getDConfigBean(DDBeanRoot dDBeanRoot) {
111: return null;
112: }
113:
114: protected BaseRoot createWebServicesRoot(DDBeanRoot dDBeanRoot) {
115: if (null == dDBeanRoot) {
116: throw new IllegalArgumentException(bundle
117: .getString("ERR_DDBeanIsNull"));
118: }
119:
120: if (null == dDBeanRoot.getXpath()) {
121: throw new IllegalArgumentException(bundle
122: .getString("ERR_DDBeanHasNullXpath"));
123: }
124:
125: BaseRoot rootDCBean = null;
126:
127: if (dDBeanRoot.getXpath().equals("/webservices")) {
128: SunONEDeploymentConfiguration config = getConfig();
129:
130: // If DDBean is not from our internal tree, normalize it to one that is.
131: if (!(dDBeanRoot instanceof DDRoot)) {
132: // If the root cache is empty, then it is likely that
133: assert config.getDCBRootCache().entrySet().size() > 0 : "No DDBeanRoots have been cached. No way to normalize "
134: + dDBeanRoot;
135: dDBeanRoot = config.getStorage().normalizeDDBeanRoot(
136: dDBeanRoot);
137: }
138:
139: rootDCBean = (BaseRoot) config.getDCBRootCache().get(
140: dDBeanRoot);
141:
142: if (null == rootDCBean) {
143: try {
144: rootDCBean = new WebServices();
145: rootDCBean.init(dDBeanRoot, config, dDBeanRoot);
146: config.getDCBCache().put(dDBeanRoot, rootDCBean);
147: config.getDCBRootCache()
148: .put(dDBeanRoot, rootDCBean);
149: } catch (ConfigurationException ex) {
150: ErrorManager.getDefault().notify(
151: ErrorManager.INFORMATIONAL, ex);
152: rootDCBean = null;
153: }
154: }
155: }
156:
157: return rootDCBean;
158: }
159:
160: public String getComponentName() {
161: String name = getUriText();
162:
163: // This method should return null if there is no name, while getUriText
164: // must always return non-null.
165: if (!Utils.notEmpty(name)) {
166: name = null;
167: }
168:
169: return name;
170: }
171:
172: public String getUriText() {
173: BaseModuleRef ref = getReference();
174:
175: if (ref != null) {
176: return ref.getModuleUri();
177: }
178:
179: // No reference, no URI. This should only happen with solo modules
180: // such as webapp modules and ejbjar modules.
181: return ""; // NOI18N
182: }
183:
184: /** Get the J2EE version of this module. The derived class must implement
185: * this method and return the correct J2EEBaseVersion derivative representing
186: * the version of the module, e.g. ServletVersion.SERVLET_X_Y for web-apps,
187: *
188: * This method MUST be overridden in derived root classes (e.g. WebAppRoot, etc.)
189: * It is not declared abstract because there is a real implementation in
190: * Base.java that is used by the child beans and I'm not sure if java will
191: * allow turning a concrete function to abstract for a portion of the hierarchy.
192: * It's a sketchy concept at best, even if allowed.
193: *
194: * @return J2EEVersion enum for the version of this module.
195: */
196: public J2EEBaseVersion getJ2EEModuleVersion() {
197: // This implementation should never be called.
198: assert false : this .getClass().getName()
199: + " does not override getJ2EEModuleVersion!!!"; // NOI18N
200:
201: // Prevent other runtime issues, if something slips past by returning
202: // something reasonable, but likely benign.
203: return J2EEVersion.J2EE_1_4;
204: }
205:
206: /** Get the AppServer version to be used for saving deployment descriptors.
207: * Note that this is different than the version of the connected target
208: * application server (which can be queried by the appropriate method on
209: * SunONEDeploymentConfiguration.)
210: *
211: * @return ASDDVersion enum for the appserver version
212: */
213: public ASDDVersion getAppServerVersion() {
214: return getConfig().getAppServerVersion();
215: }
216:
217: /** Set the AppServer version to be used for saving deployment descriptors.
218: *
219: * @param asVersion enum for the appserver version
220: */
221: public void setAppServerVersion(ASDDVersion asVersion) {
222: getConfig().setAppServerVersion(asVersion);
223: }
224:
225: /** Generate a DOCTYPE string for the specified version (which may be different
226: * than the current version of the tree
227: */
228: public abstract String generateDocType(ASDDVersion version);
229:
230: protected String generateDocType(String docroot, String publicId,
231: String systemId) {
232: StringBuffer buffer = new StringBuffer(192);
233: buffer.append("<DOCTYPE "); // NOI18N
234: buffer.append(docroot);
235: buffer.append(" PUBLIC \n\t\""); // NOI18N
236: buffer.append(publicId);
237: buffer.append("\" \n\t\""); // NOI18N
238: buffer.append(systemId);
239: buffer.append("\">"); // NOI18N
240: return buffer.toString();
241: }
242:
243: /** -----------------------------------------------------------------------
244: * Properties
245: */
246:
247: /** Getter for property refIdentity.
248: * @return Value of property refIdentity.
249: *
250: */
251: public String getRefIdentity() {
252: String result = "(null)"; // NOI18N
253: if (getReference() != null) {
254: result = getReference().getIdentity();
255: }
256:
257: return result;
258: }
259:
260: /** -----------------------------------------------------------------------
261: * Reference support
262: */
263: private BaseModuleRef rootReference = null;
264:
265: protected BaseModuleRef getReference() {
266: return rootReference;
267: }
268:
269: protected void setReference(BaseModuleRef ref) {
270: rootReference = ref;
271: this .uri = ref.getDDBean();
272: }
273:
274: protected void findRefDCB(DDBeanRoot ddbRoot) {
275: // For this direction, check patch cache to see if there is an unpatched
276: // reference bean under this key.
277: //
278: BaseModuleRef ref = (BaseModuleRef) dc.getPatchList().get(
279: ddbRoot);
280:
281: if (ref != null) {
282: setReference(ref);
283: ref.setReference(this );
284:
285: // Since we found and patched the reference, remove it from the
286: // patch list.
287: dc.getPatchList().remove(ddbRoot);
288: }
289: }
290:
291: /** -----------------------------------------------------------------------
292: * Persistence support helper objects
293: */
294: protected abstract ConfigParser getParser();
295:
296: /* !PW not currently used. I didn't like the additional runtime issues that
297: * using introspection added to the code.
298: *
299: public static class SimpleRootParser implements ConfigParser {
300: private Class rootBaseBeanClass;
301: private Method createGraphMethod;
302: private Method createGraphFromStreamMethod;
303:
304: public SimpleRootParser(Class rootTargetClass) {
305: rootBaseBeanClass = rootTargetClass;
306:
307: try {
308: createGraphMethod = rootBaseBeanClass.getMethod("createGraph", null); // NOI18N
309:
310: Class [] paramTypes = new Class[1];
311: paramTypes[0] = java.io.InputStream.class;
312: createGraphFromStreamMethod = rootBaseBeanClass.getMethod("createGraph", paramTypes); // NOI18N
313: } catch(NoSuchMethodException ex) {
314: ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
315: } catch(SecurityException ex) {
316: ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
317: }
318: }
319:
320: public Object parse(java.io.InputStream stream) {
321: Object result = null;
322:
323: try {
324: if(null == stream) {
325: // call <createGraph>();
326: result = createGraphMethod.invoke(null, null);
327: } else {
328: try {
329: // call <createGraph>(stream);
330: Object [] params = new Object[1];
331: params[0] = stream;
332: createGraphFromStreamMethod.invoke(null, params);
333: } catch(Exception ex) {
334: jsr88Logger.severe("invalid stream for " + rootBaseBeanClass.getName());
335: // call <createGraph>();
336: result = createGraphMethod.invoke(null, null);
337: }
338: }
339: } catch(Exception ex) {
340: ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
341: }
342:
343: return result;
344: }
345: }
346: */
347:
348: public static class SimpleRootFinder implements ConfigFinder {
349: private Class rootBaseBeanClass;
350:
351: public SimpleRootFinder(Class rootTargetClass) {
352: rootBaseBeanClass = rootTargetClass;
353: }
354:
355: public Object find(Object obj) {
356: Object result = null;
357: if (rootBaseBeanClass.equals(obj.getClass())) {
358: result = obj;
359: }
360: return result;
361: }
362: }
363: }
|