KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * Factory.java
21  *
22  * Created on May 2, 2003, 4:17 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean;
26
27 import java.util.Map JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30
31 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
32 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
34 import javax.enterprise.deploy.model.DDBean JavaDoc;
35 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
36 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
37
38 /**
39  *
40  * @author Peter Williams
41  */

42 public class DCBFactoryMgr {
43     
44     protected static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
45         "org.netbeans.modules.j2ee.sun.share.configbean.Bundle"); // NOI18N
46

47     private Map JavaDoc factoryMap;
48     private String JavaDoc beanBaseXpath;
49     
50     /** Creates a new instance of Factory
51      */

52     public DCBFactoryMgr(Map JavaDoc xPathFactoryMap, String JavaDoc baseXpath) {
53         factoryMap = xPathFactoryMap;
54         beanBaseXpath = baseXpath;
55         
56         if(!Utils.hasTrailingSlash(beanBaseXpath)) {
57             beanBaseXpath += "/"; // NOI18N
58
}
59     }
60     
61     public Base createDCB(DDBean JavaDoc ddBean, Base dcbParent) throws ConfigurationException JavaDoc {
62         Base dcbResult = null;
63         
64         // TODO is bean waiting to be loaded from persistent storage?
65
// [can this even go here if we use a generic factory manager?]
66

67         // If we can't load it, it's a new bean so we create it from scratch.
68
if(dcbResult == null) {
69             String JavaDoc ddBeanRelativeXPath = makeRelative(ddBean.getXpath());
70             DCBFactory factory = (DCBFactory) factoryMap.get(ddBeanRelativeXPath);
71             if(factory != null) {
72                 dcbResult = factory.createDCB(ddBean, dcbParent);
73                 if (null == dcbResult) {
74                     dcbResult = new Error JavaDoc();
75                     dcbResult.init(ddBean, dcbParent);
76                 }
77             } else {
78                 Object JavaDoc [] args = new Object JavaDoc[1];
79                 args[0] = ddBean.getXpath();
80                 throw Utils.makeCE("ERR_BadFactoryMapping", args, null); // NOI18N
81
}
82         }
83
84         return dcbResult;
85     }
86     
87     public String JavaDoc [] getFactoryKeys() {
88         return (String JavaDoc []) factoryMap.keySet().toArray(new String JavaDoc[factoryMap.size()]);
89     }
90     
91     /** !PW This method works around an issue with NetBean's implementation of
92      * toolside JSR-88. We specify many child beans via relative xpaths in
93      * DConfigBean.getXpaths(). However, when the studio calls getDConfigBean
94      * to create those beans, they pass us an absolute xpath. (This is vague
95      * in JSR-88 1.0, but was clarified as a violation of JSR-88 1.1.)
96      *
97      * Anyway, this method allows us at runtime to determine what relative path
98      * we most likely passed them that caused the call to getDConfigBean now
99      * being processed. We need the relative xpath because that is how our
100      * factories are indexed.
101      */

102     private String JavaDoc makeRelative(String JavaDoc xpath) {
103         String JavaDoc result;
104         
105         if(xpath.startsWith(beanBaseXpath)) {
106             result = xpath.substring(beanBaseXpath.length());
107         } else if(!xpath.startsWith("/")) { // NOI18N
108
result = xpath;
109         } else {
110             result = xpath;
111         }
112         
113         return result;
114     }
115 }
116
Popular Tags