KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.j2ee.sun.share.configbean;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
28 import javax.enterprise.deploy.model.DDBean JavaDoc;
29 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
30 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
31 import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
32 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
33
34 import org.openide.ErrorManager;
35
36 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
37 import org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException;
38 import org.netbeans.modules.j2ee.sun.dd.api.common.CallProperty;
39 import org.netbeans.modules.j2ee.sun.dd.api.common.MessageSecurityBinding;
40 import org.netbeans.modules.j2ee.sun.dd.api.common.PortInfo;
41 import org.netbeans.modules.j2ee.sun.dd.api.common.WsdlPort;
42 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
43
44 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet;
45
46
47 /** Property structure of ServiceRef from DTD:
48  *
49  * service-ref : ServiceRef[0,n]
50  * service-ref-name : String
51  * port-info : PortInfo[0,n]
52  * service-endpoint-interface : String?
53  * wsdl-port : WsdlPort?
54  * namespaceURI : String
55  * localpart : String
56  * stub-property : StubProperty[0,n]
57  * name : String
58  * value : String
59  * call-property : CallProperty[0,n]
60  * name : String
61  * value : String
62  * call-property : CallProperty[0,n]
63  * name : String
64  * value : String
65  * wsdl-override : String?
66  * service-impl-class : String?
67  * service-qname : ServiceQname?
68  * namespaceURI : String
69  * localpart : String
70  *
71  *
72  * @author Peter Williams
73  * @version %I%, %G%
74  */

75 public class ServiceRef extends Base {
76
77     /** property event names
78      */

79     public static final String JavaDoc SERVICE_REF_NAME = "serviceRefName"; // NOI18N
80

81     /** DDBean that refers to "service-ref-name" child of bound DDBean. */
82     private DDBean JavaDoc serviceRefNameDD;
83     
84     /** Holds value of property ports */
85     private List JavaDoc ports;
86
87     /** Holds value of property ports */
88     private List JavaDoc callProperties;
89     
90     /** Holds value of property wsdlOverride. */
91     private String JavaDoc wsdlOverride;
92     
93     /** Holds value of property serviceImplClass. */
94 // private String serviceImplClass; // derived during deployment
95

96     /** Holds value of property serviceQName. */
97 // private ServiceQname serviceQName; // derived during deployment
98

99     /** Creates a new instance of ServiceRef */
100     public ServiceRef() {
101         setDescriptorElement(bundle.getString("BDN_ServiceRef")); // NOI18N
102
}
103
104     /** Override init to enable grouping support for this bean and load name
105      * field from related DDBean.
106      * @param dDBean DDBean matching this bean
107      * @param parent Parent DConfigBean in the tree
108      */

109     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
110         super.init(dDBean, parent);
111         dDBean.addXpathListener(dDBean.getXpath(), this);
112 // !PW Disable grouping code for now, spec non-compliance.
113
// initGroup(dDBean, parent);
114

115         serviceRefNameDD = getNameDD("service-ref-name"); //NOI18N
116

117         updateNamedBeanCache(SunWebApp.SERVICE_REF);
118         
119         loadFromPlanFile(getConfig());
120     }
121     
122     protected String JavaDoc getComponentName() {
123         return getServiceRefName();
124     }
125     
126     /** Getter for helpId property
127      * @return Help context ID for this DConfigBean
128      */

129     public String JavaDoc getHelpId() {
130         return "AS_CFG_ServiceRefGeneral";
131     }
132     
133     /** The DDBean (or one of it's children) that this DConfigBean is bound to
134      * has changed.
135      *
136      * @param xpathEvent
137      */

138     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
139         super.notifyDDChange(xpathEvent);
140
141         if(serviceRefNameDD == xpathEvent.getBean()) {
142             // name changed...
143
getPCS().firePropertyChange(SERVICE_REF_NAME, "", getServiceRefName());
144             getPCS().firePropertyChange(DISPLAY_NAME, "", getDisplayName());
145
146             updateNamedBeanCache(SunWebApp.SERVICE_REF);
147         }
148     }
149
150     /** Getter for property service-ref-name.
151      * @return Value of property service-ref-name.
152      *
153      */

154     public String JavaDoc getServiceRefName() {
155         return cleanDDBeanText(serviceRefNameDD);
156     }
157     
158     /* ------------------------------------------------------------------------
159      * Persistence support. Loads DConfigBeans from previously saved Deployment
160      * plan file.
161      */

162     Collection JavaDoc getSnippets() {
163         Collection JavaDoc snippets = new ArrayList JavaDoc();
164         Snippet snipOne = new DefaultSnippet() {
165             public CommonDDBean getDDSnippet() {
166                 org.netbeans.modules.j2ee.sun.dd.api.common.ServiceRef serviceRef =
167                     getConfig().getStorageFactory().createServiceRef();
168                 String JavaDoc version = getAppServerVersion().getEjbJarVersionAsString();
169
170                 // write properties into Servlet bean
171
String JavaDoc sn = getServiceRefName();
172                 if(sn != null) {
173                     serviceRef.setServiceRefName(sn);
174                 }
175                 
176                 if(wsdlOverride != null && wsdlOverride.length() > 0) {
177                     serviceRef.setWsdlOverride(wsdlOverride);
178                 }
179
180                 PortInfo [] portInfos = (PortInfo [])
181                     Utils.listToArray(getPortInfos(), PortInfo.class, version);
182                 if(portInfos != null) {
183                     serviceRef.setPortInfo(portInfos);
184                 }
185                 
186                 CallProperty [] callProps = (CallProperty [])
187                     Utils.listToArray(getCallProperties(), CallProperty.class, version);
188                 if(callProps != null) {
189                     serviceRef.setCallProperty(callProps);
190                 }
191                 
192                 return serviceRef;
193             }
194             
195             public boolean hasDDSnippet() {
196                 if(wsdlOverride != null && wsdlOverride.length() > 0) {
197                     return true;
198                 }
199                 
200                 if(ports != null && ports.size() > 0) {
201                     return true;
202                 }
203                 
204                 if(callProperties != null && callProperties.size() > 0) {
205                     return true;
206                 }
207                 
208                 return false;
209             }
210             
211             public String JavaDoc getPropertyName() {
212                 return SunWebApp.SERVICE_REF;
213             }
214         };
215         
216         snippets.add(snipOne);
217         return snippets;
218     }
219     
220     private class ServiceRefFinder extends NameBasedFinder {
221         public ServiceRefFinder(String JavaDoc beanName) {
222             super(org.netbeans.modules.j2ee.sun.dd.api.common.ServiceRef.SERVICE_REF_NAME,
223                 beanName, org.netbeans.modules.j2ee.sun.dd.api.common.ServiceRef.class);
224         }
225     }
226     
227     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
228         String JavaDoc uriText = getUriText();
229
230         org.netbeans.modules.j2ee.sun.dd.api.common.ServiceRef beanGraph =
231             (org.netbeans.modules.j2ee.sun.dd.api.common.ServiceRef) config.getBeans(uriText,
232             constructFileName(), getParser(), new ServiceRefFinder(getServiceRefName()));
233         
234         clearProperties();
235         
236         if(beanGraph != null) {
237             wsdlOverride = beanGraph.getWsdlOverride();
238             ports = Utils.arrayToList(beanGraph.getPortInfo());
239             callProperties = Utils.arrayToList(beanGraph.getCallProperty());
240         } else {
241             setDefaultProperties();
242         }
243         
244         return (beanGraph != null);
245     }
246     
247     protected void clearProperties() {
248         wsdlOverride = null;
249         ports = null;
250         callProperties = null;
251     }
252     
253     protected void setDefaultProperties() {
254         setDefaultPorts();
255         getConfig().getMasterDCBRoot().setDirty();
256     }
257     
258     private void setDefaultPorts() {
259         List JavaDoc portInfoList = getDefaultPortInfos();
260         try {
261             setPortInfos(portInfoList);
262         } catch(java.beans.PropertyVetoException JavaDoc ex) {
263             // Should not happen.
264
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
265         }
266     }
267
268     private List JavaDoc getDefaultPortInfos() {
269         List JavaDoc result = new ArrayList JavaDoc();
270         try {
271             DeployableObject JavaDoc dobj = getConfig().getDeployableObject();
272
273             // !PW FIXME this does not work for ejb-jar.xml!!!!
274
DDBeanRoot JavaDoc webRootDD = dobj.getDDBeanRoot("WEB-INF/web.xml"); // NOI18N
275
if(webRootDD == null) {
276                 jsr88Logger.warning("ServiceRef.getDefaultServiceRefs() failed to retrieve web DDRoot via xpath. Using fallback method.");
277             }
278
279             if(webRootDD != null) {
280                 DDBean JavaDoc[] serviceRefNameDDBean =
281                     webRootDD.getChildBean("web-app/service-ref/service-ref-name"); //NOI18N
282
// First, find the service-ref that corresponds to this service
283
for(int i = 0; i < serviceRefNameDDBean.length; i++) {
284                     if(serviceRefNameDD.getText().equals(serviceRefNameDDBean[i].getText())) {
285                         DDBean JavaDoc[] portComponentRefDDs =
286                             serviceRefNameDDBean[i].getChildBean("../port-component-ref"); //NOI18N
287
String JavaDoc serviceEndpointInterface = null;
288                         for(int j = 0; j < portComponentRefDDs.length; j++) {
289                             DDBean JavaDoc[] serviceEndpointInterfaceDD =
290                                 portComponentRefDDs[j].getChildBean("service-endpoint-interface"); //NOI18N
291
if(serviceEndpointInterfaceDD != null && serviceEndpointInterfaceDD.length > 0) {
292                                 serviceEndpointInterface = serviceEndpointInterfaceDD[0].getText();
293                                 if(serviceEndpointInterface != null){
294                                     PortInfo portInfo = getConfig().getStorageFactory().createPortInfo();
295                                     portInfo.setServiceEndpointInterface(serviceEndpointInterface);
296                                     result.add(portInfo);
297                                 }
298                             }
299                         }
300                     }
301                 }
302             }
303         } catch(DDBeanCreateException ex) {
304             jsr88Logger.warning(ex.getMessage());
305         } catch(java.io.FileNotFoundException JavaDoc ex) {
306             jsr88Logger.warning(ex.getMessage());
307         } catch(java.lang.NullPointerException JavaDoc ex) {
308             // This can happen if the file is being loaded into a new (and thus partially
309
// constructed tree. Nothing to do but catch it and move on.
310
jsr88Logger.warning(ex.getMessage());
311         }
312
313         return result;
314     }
315
316     /* ------------------------------------------------------------------------
317      * Property getter/setter support
318      */

319
320     /** Getter for property wsdlOverride.
321      * @return Value of property wsdlOverride.
322      */

323     public String JavaDoc getWsdlOverride() {
324         return wsdlOverride;
325     }
326
327     /** Setter for property wsdlOverride.
328      * @param wsdlOverride New value of property wsdlOverride.
329      *
330      * @throws PropertyVetoException
331      */

332     public void setWsdlOverride(String JavaDoc newWsdlOverride) throws java.beans.PropertyVetoException JavaDoc {
333         String JavaDoc oldWsdlOverride = wsdlOverride;
334         getVCS().fireVetoableChange("wsdlOverride", oldWsdlOverride, newWsdlOverride);
335         wsdlOverride = newWsdlOverride;
336         getPCS().firePropertyChange("wsdlOverride", oldWsdlOverride, wsdlOverride);
337     }
338
339     /** Getter for property portInfos.
340      * @return Value of property portInfos.
341      *
342      */

343     public List JavaDoc getPortInfos() {
344         return ports;
345     }
346
347     public PortInfo getPortInfo(int index) {
348         return (PortInfo) ports.get(index);
349     }
350
351     /** Setter for property portInfos.
352      * @param newPorts New value of property portInfos.
353      *
354      * @throws PropertyVetoException
355      *
356      */

357     public void setPortInfos(List JavaDoc newPorts) throws java.beans.PropertyVetoException JavaDoc {
358         List JavaDoc oldPorts = ports;
359         getVCS().fireVetoableChange("portInfos", oldPorts, newPorts); // NOI18N
360
ports = newPorts;
361         getPCS().firePropertyChange("portInfos", oldPorts, ports); // NOI18N
362
}
363
364     public void addPortInfo(PortInfo newPortInfo) throws java.beans.PropertyVetoException JavaDoc {
365         getVCS().fireVetoableChange("portInfo", null, newPortInfo); // NOI18N
366
if(ports == null) {
367             ports = new ArrayList JavaDoc();
368         }
369         ports.add(newPortInfo);
370         getPCS().firePropertyChange("portInfo", null, newPortInfo ); // NOI18N
371
}
372
373     public void removePortInfo(PortInfo oldPortInfo) throws java.beans.PropertyVetoException JavaDoc {
374         getVCS().fireVetoableChange("portInfo", oldPortInfo, null); // NOI18N
375
ports.remove(oldPortInfo);
376         getPCS().firePropertyChange("portInfo", oldPortInfo, null ); // NOI18N
377
}
378
379     /** Note: For now, this method applies a copy of the specified binding to each port
380      * currently configured on this service reference, if any.
381      *
382      * @deprecated
383      */

384     public void setMessageSecurityBinding(MessageSecurityBinding newBinding) throws java.beans.PropertyVetoException JavaDoc, VersionNotSupportedException {
385         if(ports != null && ports.size() > 0) {
386             MessageSecurityBinding oldBinding;
387             oldBinding = ((PortInfo) ports.get(0)).getMessageSecurityBinding();
388             getVCS().fireVetoableChange("messageSecurityBinding", oldBinding, newBinding); // NOI18N
389
Iterator JavaDoc portIterator = ports.iterator();
390             while(portIterator.hasNext()) {
391                 PortInfo portInfo = (PortInfo) portIterator.next();
392                 portInfo.setMessageSecurityBinding((newBinding != null) ? (MessageSecurityBinding) newBinding.clone() : null);
393             }
394             getPCS().firePropertyChange("messageSecurityBinding", oldBinding, newBinding ); // NOI18N
395
}
396     }
397
398     /** Sets the MessageSecurityBinding for the specified port of this ServiceRef. If the port
399      * is not found, a new entry is created for it.
400      */

401     public void setMessageSecurityBinding(String JavaDoc namespaceURI, String JavaDoc localpart, MessageSecurityBinding newBinding)
402     throws java.beans.PropertyVetoException JavaDoc, VersionNotSupportedException {
403         PortInfo thePortInfo = null;
404         
405         /// find the right port...
406
if(ports != null) {
407             Iterator JavaDoc iter = ports.iterator();
408             while(iter.hasNext()) {
409                 PortInfo portInfo = (PortInfo) iter.next();
410                 WsdlPort port = portInfo.getWsdlPort();
411                 if(port != null && namespaceURI.equals(port.getNamespaceURI()) && localpart.equals(port.getLocalpart())) {
412                     thePortInfo = portInfo;
413                     break;
414                 }
415             }
416         } else {
417             // No list of port-info entries yet, make one.
418
ports = new ArrayList JavaDoc();
419         }
420         
421         if(thePortInfo == null) {
422             // if we didn't find the port, make one.
423
thePortInfo = getConfig().getStorageFactory().createPortInfo();
424             WsdlPort wsdlPort = thePortInfo.newWsdlPort();
425             wsdlPort.setNamespaceURI(namespaceURI);
426             wsdlPort.setLocalpart(localpart);
427             thePortInfo.setWsdlPort(wsdlPort);
428             ports.add(thePortInfo);
429         }
430         
431         // finally set the binding.
432
MessageSecurityBinding oldBinding = thePortInfo.getMessageSecurityBinding();
433         getVCS().fireVetoableChange("messageSecurityBinding", oldBinding, newBinding); // NOI18N
434
thePortInfo.setMessageSecurityBinding((newBinding != null) ? (MessageSecurityBinding) newBinding.clone() : null);
435         getPCS().firePropertyChange("messageSecurityBinding", oldBinding, newBinding ); // NOI18N
436
}
437     
438     /** Getter for property callProperties.
439      * @return Value of property callProperties.
440      *
441      */

442     public List JavaDoc getCallProperties() {
443         return callProperties;
444     }
445
446     public CallProperty getCallProperty(int index) {
447         return (CallProperty) ports.get(index);
448     }
449
450     /** Setter for property callProperties.
451      * @param newPorts New value of property callProperties.
452      *
453      * @throws PropertyVetoException
454      *
455      */

456     public void setCallProperties(List JavaDoc newCallProperties) throws java.beans.PropertyVetoException JavaDoc {
457         List JavaDoc oldCallProperties = callProperties;
458         getVCS().fireVetoableChange("callProperties", oldCallProperties, newCallProperties); // NOI18N
459
callProperties = newCallProperties;
460         getPCS().firePropertyChange("callProperties", oldCallProperties, callProperties); // NOI18N
461
}
462
463     public void addCallProperty(CallProperty newCallProperty) throws java.beans.PropertyVetoException JavaDoc {
464         getVCS().fireVetoableChange("callProperty", null, newCallProperty); // NOI18N
465
if(callProperties == null) {
466             callProperties = new ArrayList JavaDoc();
467         }
468         callProperties.add(newCallProperty);
469         getPCS().firePropertyChange("callProperty", null, newCallProperty ); // NOI18N
470
}
471
472     public void removeCallProperty(CallProperty oldCallProperty) throws java.beans.PropertyVetoException JavaDoc {
473         getVCS().fireVetoableChange("callProperty", oldCallProperty, null); // NOI18N
474
callProperties.remove(oldCallProperty);
475         getPCS().firePropertyChange("callProperty", oldCallProperty, null ); // NOI18N
476
}
477
478     public void fireXpathEvent(XpathEvent JavaDoc xpathEvent) {
479         String JavaDoc xpath = xpathEvent.getBean().getXpath();
480         if(xpath.equals("/web-app/service-ref/port-component-ref")){ //NOI18N
481
setDefaultPorts();
482         }
483     }
484
485     /** Api to retrieve the interface definitions for this bean. Aids usability
486      * during configuration, as the editors can display the existing methds
487      * rather than have the user enter them manually.
488      */

489     public java.util.List JavaDoc/*ConfigQuery.MethodData*/ getServiceOperations(DDBean JavaDoc portInfoDD) {
490         /* !PW FIXME Temporary implementation values until plumbing in j2eeserver is worked out.
491          */

492         java.util.List JavaDoc operationList = new ArrayList JavaDoc();
493         operationList.add(new ConfigQuery.MethodData("pi_operation1", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2" } )));
494         operationList.add(new ConfigQuery.MethodData("pi_operation2", java.util.Arrays.asList(new String JavaDoc [] { "arg1" } )));
495         operationList.add(new ConfigQuery.MethodData("pi_operation3", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2", "arg3" } )));
496         return operationList;
497     }
498 }
499
Popular Tags