KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ServiceModuleImpl


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.project;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.xml.namespace.QName JavaDoc;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
29 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
30 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
31 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
32 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
33 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
34 import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
35 import org.netbeans.modules.schema2beans.BaseBean;
36 import org.netbeans.modules.serviceapi.InterfaceDescription;
37 import org.netbeans.modules.serviceapi.ServiceInterface;
38 import org.netbeans.modules.serviceapi.ServiceLink;
39 import org.netbeans.modules.websvc.spi.webservices.WebServicesSupportImpl;
40 import org.netbeans.modules.serviceapi.ServiceComponent;
41 import org.netbeans.modules.serviceapi.ServiceModule;
42 import org.netbeans.modules.serviceapi.wsdl.WSDL11Description;
43 import org.netbeans.modules.web.project.classpath.ClassPathProviderImpl;
44 import org.netbeans.modules.xml.wsdl.model.PortType;
45 import org.openide.filesystems.FileObject;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.nodes.Node;
49
50 /**
51  *
52  * @author Nam Nguyen
53  */

54 public class ServiceModuleImpl extends ServiceModule {
55     private WebProject project;
56     
57     /** Creates a new instance of ServiceModuleImpl */
58     public ServiceModuleImpl(WebProject webProject) {
59         this.project = webProject;
60     }
61
62     private WebServicesSupportImpl getWebServiceSupport() {
63         return project.getLookup().lookup(WebServicesSupportImpl.class);
64     }
65     
66     /**
67      * @return name of the service module.
68      */

69     public String JavaDoc getName() {
70         return project.getName();
71     }
72
73     /**
74      * Returns service components contained in this module.
75      */

76     public Collection JavaDoc<ServiceComponent> getServiceComponents() {
77         ArrayList JavaDoc<ServiceComponent> ret = new ArrayList JavaDoc<ServiceComponent>();
78         Webservices wss = getWebservices();
79         
80         if (wss != null) {
81             WebserviceDescription[] descriptions = wss.getWebserviceDescription();
82             if (descriptions == null) return ret;
83             for (WebserviceDescription wsd : descriptions) {
84                 PortComponent[] portComponents = wsd.getPortComponent();
85                 if (portComponents == null) continue;
86                 String JavaDoc wsdlPath = wsd.getWsdlFile();
87                 assert wsdlPath != null;
88                 for (PortComponent pc : portComponents) {
89                     ret.add(new ProviderComponent(wsdlPath, pc));
90                 }
91             }
92             
93             for (Servlet s : getWebApp().getServlet()) {
94                 s.getServletClass();
95             }
96         }
97         return ret;
98     }
99
100     public static QName JavaDoc convertSchema2BeansQName(org.netbeans.modules.schema2beans.QName q) {
101         return new QName JavaDoc(q.getNamespaceURI(), q.getLocalPart());
102     }
103
104     private class ProviderComponent extends Component {
105         private PortComponent portComponent;
106         private String JavaDoc wsdlFilePath;
107         
108         private ProviderComponent(String JavaDoc wsdlFilePath, PortComponent pc) {
109             this.wsdlFilePath = wsdlFilePath;
110             portComponent = pc;
111         }
112
113         public String JavaDoc getWsdlFilePath() {
114             return wsdlFilePath;
115         }
116
117         public List JavaDoc<ServiceInterface> getServiceProviders() {
118             QName JavaDoc portQName = convertSchema2BeansQName(portComponent.getWsdlPort());
119             Description def = new Description(getWsdlFilePath(), portQName);
120             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
121         }
122
123         @Override JavaDoc
124         public String JavaDoc getImplClassName() {
125             String JavaDoc link = portComponent.getServiceImplBean().getServletLink();
126             return getWebServiceSupport().getImplementationBean(link);
127         }
128
129     }
130     
131     private abstract class Component extends ServiceComponent {
132         
133         public List JavaDoc<ServiceInterface> getServiceProviders() {
134             return Collections.emptyList();
135         }
136
137         public List JavaDoc<ServiceInterface> getServiceConsumers() {
138             return Collections.emptyList();
139         }
140
141         public Collection JavaDoc<ServiceLink> getServiceLinks() {
142             return Collections.emptyList();
143         }
144
145         public abstract String JavaDoc getImplClassName();
146
147         public Node getNode() {
148             FileObject fo = getSourcesClassPath().findResource(getImplClassName());
149             assert fo != null;
150             try {
151                 DataObject dobj = DataObject.find(fo);
152                 assert dobj != null;
153                 return dobj.getNodeDelegate();
154             } catch(DataObjectNotFoundException ex) {
155                 assert false : "DataObjectNotFoundException: " + ex.getMessage();
156                 return null;
157             }
158         }
159
160         public ServiceInterface createServiceInterface(InterfaceDescription description,
161                                                        boolean provider) {
162             if (provider) return null;
163             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
164         }
165
166         public ServiceInterface createServiceInterface(ServiceInterface other) {
167             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
168         }
169
170         public void removeServiceInterface(ServiceInterface serviceInterface) {
171             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
172         }
173         
174         @Override JavaDoc
175         public int hashCode() {
176             return getImplClassName().hashCode();
177         }
178
179         @Override JavaDoc
180         public boolean equals(Object JavaDoc obj) {
181             if (! (obj instanceof Component)) return false;
182             Component other = (Component) obj;
183             return this.getImplClassName().equals(other.getImplClassName());
184         }
185     }
186
187     private class Interface implements ServiceInterface {
188         Component component;
189         Description description;
190         boolean isProvider = true;
191
192         Interface(Component parent, Description description, boolean isProvider) {
193             this(parent, description);
194             this.isProvider = isProvider;
195         }
196     
197         private Interface(Component parent, Description description) {
198             this.component = parent;
199             this.description = description;
200         }
201
202         public Description getInterfaceDescription() {
203             return description;
204         }
205
206         public ServiceComponent getServiceComponent() {
207             return component;
208         }
209
210         public boolean canConnect(ServiceInterface other) {
211             if (! (other.getInterfaceDescription() instanceof WSDL11Description)) {
212                 return false;
213             }
214             WSDL11Description otherDescription = (WSDL11Description) other.getInterfaceDescription();
215             return other.isProvider() != isProvider() &&
216                    otherDescription.getInterfaceQName().equals(getQName());
217         }
218
219         public boolean isProvider() {
220             return isProvider;
221         }
222
223         public Node getNode() {
224             //TODO how do I get portType node without WSLD UI
225
throw new UnsupportedOperationException JavaDoc("Not supported yet.");
226         }
227
228         public QName JavaDoc getQName() {
229             return getInterfaceDescription().getInterfaceQName();
230         }
231     }
232     
233     private class Description extends WSDL11Description {
234         private String JavaDoc pathToWSDL;
235         private QName JavaDoc portTypeQName;
236
237         Description(String JavaDoc pathToWSDL, QName JavaDoc portTypeQName) {
238             this.pathToWSDL = pathToWSDL;
239             this.portTypeQName = portTypeQName;
240         }
241
242         @Override JavaDoc
243         public QName JavaDoc getInterfaceQName() {
244             return portTypeQName;
245         }
246
247         public String JavaDoc getDisplayName() {
248             return portTypeQName.getLocalPart();
249         }
250
251         public PortType getInterface() {
252             //TODO
253
throw new UnsupportedOperationException JavaDoc("Not supported yet.");
254         }
255     }
256     
257     private Webservices getWebservices() {
258         BaseBean bb = project.getWebModule().getDeploymentDescriptor(J2eeModule.WEBSERVICES_XML);
259         if (bb instanceof Webservices) {
260             return (Webservices) bb;
261         }
262         return null;
263     }
264     
265     private WebApp getWebApp() {
266         BaseBean bb = project.getWebModule().getDeploymentDescriptor(J2eeModule.WEB_XML);
267         if (bb instanceof WebApp) {
268             return (WebApp) bb;
269         }
270         assert false : "Failed to get WebApp";
271         return null;
272     }
273
274     /**
275      * Add service component.
276      */

277     
278     public void addServiceComponent(ServiceComponent component) {
279             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
280     }
281     
282     /**
283      * Remove service component.
284      */

285     public void removeServiceComponent(ServiceComponent component) {
286             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
287     }
288
289     /**
290      * @return the project if applicable (not applicable for service modules from appserver).
291      */

292     public Project getProject() {
293     return project;
294     }
295
296     private ClassPath sourcesClassPath;
297     private ClassPath getSourcesClassPath() {
298         synchronized (this) {
299             if (sourcesClassPath == null) {
300                 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class);
301                 sourcesClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
302                     cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE),
303                 });
304             }
305             return sourcesClassPath;
306         }
307     }
308 }
309
Popular Tags