KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
27 import javax.enterprise.deploy.model.DDBean JavaDoc;
28 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
29
30 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
31 import org.netbeans.modules.j2ee.sun.dd.api.RootInterface;
32 import org.netbeans.modules.j2ee.sun.dd.api.common.WebserviceEndpoint;
33 import org.netbeans.modules.j2ee.sun.dd.api.ejb.Ejb;
34 import org.netbeans.modules.j2ee.sun.dd.api.ejb.EnterpriseBeans;
35 import org.netbeans.modules.j2ee.sun.dd.api.ejb.SunEjbJar;
36 import org.netbeans.modules.j2ee.sun.dd.api.web.Servlet;
37 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
38 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet;
39
40 /** Property structure of WebServiceEndpoint, from DTD (sun-web.xml or sun-ejb-jar.xml):
41  *
42  * webservice-endpoint : WebserviceEndpoint[0,n]
43  * port-component-name : String
44  * endpoint-address-uri : String?
45  * login-config : LoginConfig?
46  * auth-method : String
47  * transport-guarantee : String?
48  * service-qname : ServiceQname? [not used - set by server]
49  * namespaceURI : String [not used - set by server]
50  * localpart : String [not used - set by server]
51  * tie-class : String? [not used - set by server]
52  * servlet-impl-class : String? [not used - set by server]
53  *
54  *
55  * @author Peter Williams
56  * @version %I%, %G%
57  */

58 public class WebServices extends BaseRoot {
59     
60     /** Differentiates Servlet vs Ejb webservice support */
61     private EndpointHelper helper;
62
63     /** Hold a map, indexed by port component name, of endpoints that we found
64      * in the descriptor file, but haven't been loaded yet. Note we find these
65      * under the host (servlet or ejb) so we don't know which web service they
66      * belong to.
67      */

68     private Map JavaDoc savedEndpoints;
69     
70     
71     /** Creates a new instance of WebServices */
72     public WebServices() {
73         setDescriptorElement(bundle.getString("BDN_WebServices")); // NOI18N
74
}
75     
76     protected void init(DDBeanRoot JavaDoc dDBeanRoot, SunONEDeploymentConfiguration parent, DDBean JavaDoc ddbExtra) throws ConfigurationException JavaDoc {
77         super.init(dDBeanRoot, parent, ddbExtra);
78 // dDBeanRoot.addXpathListener(dDBeanRoot.getXpath(), this);
79

80         BaseRoot masterRoot = parent.getMasterDCBRoot();
81         if(masterRoot instanceof WebAppRoot) {
82             helper = servletHelper;
83         } else if(masterRoot instanceof EjbJarRoot) {
84             helper = ejbHelper;
85         } else {
86             throw new ConfigurationException JavaDoc("Unexpected master DConfigBean type: " + masterRoot); // NOI18N
87
}
88         
89         loadFromPlanFile(parent);
90     }
91     
92     public String JavaDoc generateDocType(ASDDVersion version) {
93         return getMasterRootBean().generateDocType(version);
94     }
95     
96     
97     /** Getter for helpId property
98      * @return Help context ID for this DConfigBean
99      */

100     public String JavaDoc getHelpId() {
101         return "AS_CFG_WebServices";
102     }
103     
104     public J2EEBaseVersion getJ2EEModuleVersion() {
105         return getMasterRootBean().getJ2EEModuleVersion();
106     }
107     
108     /** The DDBean (or one of it's children) that this DConfigBean is bound to
109      * has changed.
110      *
111      * @param xpathEvent
112      */

113 // public void notifyDDChange(XpathEvent xpathEvent) {
114
// super.notifyDDChange(xpathEvent);
115
// dumpNotification("notifyDDChange", xpathEvent);
116
// }
117
//
118
// public void fireXpathEvent(XpathEvent xpathEvent) {
119
// super.fireXpathEvent(xpathEvent);
120
// dumpNotification("fireXpathEvent", xpathEvent);
121
// }
122

123     /* ------------------------------------------------------------------------
124      * Persistence support. Loads DConfigBeans from previously saved Deployment
125      * plan file.
126      */

127     Collection JavaDoc getSnippets() {
128         Collection JavaDoc snippets = new ArrayList JavaDoc();
129         Snippet snipOne = new DefaultSnippet() {
130             public CommonDDBean getDDSnippet() {
131 // // FIXME must return proper root (SunWebApp or SunEjbJar)
132
// DDProvider provider = DDProvider.getDefault();
133
// SunWebApp swa = (SunWebApp) provider.newGraph(SunWebApp.class);
134
// return swa;
135
return null;
136             }
137             
138             public CommonDDBean mergeIntoRovingDD(CommonDDBean ddParent) {
139                 throw new UnsupportedOperationException JavaDoc();
140             }
141             
142             public CommonDDBean mergeIntoRootDD(CommonDDBean ddParent) {
143                 // If we were had data to merge, it might look something like this...
144
// if(ddParent instanceof SunWebApp) {
145
// CommonDDBean snippet = getDDSnippet();
146
// ddParent.merge(snippet, CommonDDBean.MERGE_UNION);
147
// }
148
// Only data to save is any web service descriptions remaining
149
// in the datacache. Add those. We don't need a snippet for this.
150
if(ddParent instanceof RootInterface) {
151                     /* IZ 84549, etc - add remaining saved named beans here. All entries that are represented
152                      * by real DConfigBeans should have been removed by now. */

153                     RootInterface ddRoot = (RootInterface) ddParent;
154                     String JavaDoc version = ddRoot.getVersion().toString();
155                     restoreAllNamedBeans(ddRoot, version);
156                 }
157                 
158                 // It still needs to be returned here, as this is the SunWebApp/SunEjbJar
159
// root that will used by the children to save their data.
160
return ddParent;
161             }
162         };
163         
164         snippets.add(snipOne);
165         return snippets;
166     }
167     
168     public WebServiceDescriptor getWebServiceDescriptor(String JavaDoc wsName) {
169         WebServiceDescriptor result = null;
170         Iterator JavaDoc wsIter = getChildren().iterator();
171         while(wsIter.hasNext()) {
172             WebServiceDescriptor wsBean = (WebServiceDescriptor) wsIter.next();
173             if(wsBean.getWebServiceDescriptionName().equals(wsName)) {
174                 result = wsBean;
175                 break;
176             }
177         }
178         return result;
179     }
180     
181 // /** Calculate what the parent S2B bean should be for this child and return
182
// * that bean.
183
// */
184
// protected CommonDDBean processParentBean(CommonDDBean bean, DConfigBean child) {
185
// // If these services are in an ejb-jar, then we need to move the pointer
186
// // to enterprise beans.
187
// if(bean instanceof SunEjbJar) {
188
// return ((SunEjbJar) bean).getEnterpriseBeans();
189
// }
190
//
191
// // All other children require no translation.
192
// return bean;
193
// }
194

195     private class RootFinder implements ConfigFinder {
196         public Object JavaDoc find(Object JavaDoc obj) {
197             RootInterface result = null;
198             
199             if(obj instanceof RootInterface) {
200                 result = (RootInterface) obj;
201             }
202             
203             return result;
204         }
205     }
206     
207     protected ConfigParser getParser() {
208         return getMasterRootBean().getParser();
209     }
210     
211     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
212         String JavaDoc uriText = getUriText();
213         
214         RootInterface beanGraph = (RootInterface) config.getBeans(uriText,
215                 constructFileName(), getParser(), new RootFinder());
216         
217         clearProperties();
218         
219         if(beanGraph != null) {
220             // Load all endpoints that already have defined values and save them.
221
// This facilitates lazy loading of endpoint data when the port-component
222
// DDBeans are not available at the time the web-service-description
223
// DDBean is created.
224
CommonDDBean [] hosts = helper.getEndpointHosts(beanGraph);
225             if(hosts != null) {
226                 for(int i = 0; i < hosts.length; i++) {
227                     WebserviceEndpoint [] definedEndpoints =
228                             (WebserviceEndpoint []) hosts[i].getValues(helper.getEndpointProperty());
229                     if(definedEndpoints != null && definedEndpoints.length > 0) {
230                         String JavaDoc hostName = (String JavaDoc) hosts[i].getValue(helper.getHostNameProperty());
231                         for(int j = 0; j < definedEndpoints.length; j++) {
232                             saveEndpoint(hostName, definedEndpoints[j]);
233                         }
234                     }
235                 }
236             }
237             
238             // IZ 84549, etc - cache the data for all named beans.
239
saveAllNamedBeans(beanGraph);
240         } else {
241             setDefaultProperties();
242         }
243         
244         return (beanGraph != null);
245     }
246     
247     protected void clearProperties() {
248         savedEndpoints = new HashMap JavaDoc();
249     }
250     
251     protected void setDefaultProperties() {
252     }
253     
254     private static Collection JavaDoc webServiceBeanSpecs = new ArrayList JavaDoc();
255     
256     static {
257         webServiceBeanSpecs.add(new NamedBean(SunWebApp.WEBSERVICE_DESCRIPTION,
258                 org.netbeans.modules.j2ee.sun.dd.api.common.WebserviceDescription.WEBSERVICE_DESCRIPTION_NAME));
259     }
260     
261     protected Collection JavaDoc getNamedBeanSpecs() {
262         return webServiceBeanSpecs;
263     }
264     
265     protected String JavaDoc constructFileName() {
266         // Delegate to master DConfigBean which could be either WebAppRoot or EjbJarRoot.
267
return getMasterRootBean().constructFileName();
268     }
269     
270     private BaseRoot getMasterRootBean() {
271         return getConfig().getMasterDCBRoot();
272     }
273     
274     /* ------------------------------------------------------------------------
275      * XPath to Factory mapping support
276      */

277     private HashMap JavaDoc webServicesRootFactoryMap;
278     
279     /** Retrieve the XPathToFactory map for this DConfigBean. For AppRoot,
280      * this maps application xpaths to factories for other contained root
281      * objects plus a SecurityRoleModel factory
282      * @return
283      */

284     protected java.util.Map JavaDoc getXPathToFactoryMap() {
285         if(webServicesRootFactoryMap == null) {
286             webServicesRootFactoryMap = new HashMap JavaDoc(3);
287             webServicesRootFactoryMap.put("webservice-description", new DCBGenericFactory(WebServiceDescriptor.class)); // NOI18N
288
}
289         
290         return webServicesRootFactoryMap;
291     }
292     
293     /* ------------------------------------------------------------------------
294      * Property getter/setter support
295      */

296     
297     /** Saves the data in this particular endpoint in a cache in case the related
298      * port-component DDBean is created after the associated WebServiceDescriptor
299      * has already been constructed and loaded.
300      *
301      * Added to support new event model used by merged annotation-dd provider which
302      * creates and sends events for empty webservice-description entries and then
303      * creates the individual port-components, after we've already loaded the bean
304      * data. Saving the endpoint data for unrecognized ports here allows us to
305      * restore their data later, when the port-component events are received.
306      *
307      * Presumes port-component names are unique per module. Might have to save
308      * the servlet or ejb binding along with this.
309      */

310     public void saveEndpoint(final String JavaDoc hostName, final WebserviceEndpoint endpoint) {
311         String JavaDoc key = hostName + endpoint.getPortComponentName();
312         if(Utils.notEmpty(key) && savedEndpoints.get(key) == null) {
313             savedEndpoints.put(key, endpoint);
314         }
315     }
316     
317     public WebserviceEndpoint removeEndpoint(final String JavaDoc linkName, final String JavaDoc portName) {
318         WebserviceEndpoint result = null;
319         String JavaDoc key = linkName + portName;
320         if(savedEndpoints != null && savedEndpoints.size() > 0 && Utils.notEmpty(key)) {
321             result = (WebserviceEndpoint) savedEndpoints.remove(key);
322         }
323         return result;
324     }
325     
326     
327     /** !PW FIXME In favor of least entropy during high resistence mode, partial
328      * versions of the helper classes have been copied here from WebServiceDescriptor.
329      *
330      * These actually should be moved here in full and instances of WebServiceDescriptor
331      * should use their parent's helper instance. That change can wait until 6.0
332      */

333     private final EndpointHelper servletHelper = new ServletHelper();
334     private final EndpointHelper ejbHelper = new EjbHelper();
335     
336     private abstract class EndpointHelper {
337         
338         private final String JavaDoc hostNameProperty;
339         private final String JavaDoc endpointProperty;
340         
341         public EndpointHelper(String JavaDoc hnp, String JavaDoc epp) {
342             hostNameProperty = hnp;
343             endpointProperty = epp;
344         }
345         
346         public String JavaDoc getHostNameProperty() {
347             return hostNameProperty;
348         }
349         
350         public String JavaDoc getEndpointProperty() {
351             return endpointProperty;
352         }
353         
354         public abstract CommonDDBean [] getEndpointHosts(RootInterface root);
355         
356     }
357     
358     private class ServletHelper extends EndpointHelper {
359         public ServletHelper() {
360             super(Servlet.SERVLET_NAME, Servlet.WEBSERVICE_ENDPOINT);
361         }
362         
363         public CommonDDBean [] getEndpointHosts(RootInterface root) {
364             CommonDDBean [] result = (CommonDDBean []) root.getValues(SunWebApp.SERVLET);
365             return result;
366         }
367     }
368     
369     private class EjbHelper extends EndpointHelper {
370         public EjbHelper() {
371             super(Ejb.EJB_NAME, Ejb.WEBSERVICE_ENDPOINT);
372         }
373         
374         public CommonDDBean [] getEndpointHosts(RootInterface root) {
375             CommonDDBean [] result = null;
376             CommonDDBean enterpriseBeans = (CommonDDBean) root.getValue(SunEjbJar.ENTERPRISE_BEANS);
377             if(enterpriseBeans != null) {
378                 result = (CommonDDBean []) enterpriseBeans.getValues(EnterpriseBeans.EJB);
379             }
380             return result;
381         }
382     }
383 }
384
Popular Tags