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-2007 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.web.project;
042:
043: import java.io.File;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.List;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import javax.xml.namespace.QName;
051: import org.netbeans.api.java.classpath.ClassPath;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.modules.j2ee.dd.api.web.Servlet;
054: import org.netbeans.modules.j2ee.dd.api.web.WebApp;
055: import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
056: import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
057: import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
058: import org.netbeans.modules.serviceapi.InterfaceDescription;
059: import org.netbeans.modules.serviceapi.ServiceInterface;
060: import org.netbeans.modules.serviceapi.ServiceLink;
061: import org.netbeans.modules.websvc.spi.webservices.WebServicesSupportImpl;
062: import org.netbeans.modules.serviceapi.ServiceComponent;
063: import org.netbeans.modules.serviceapi.ServiceModule;
064: import org.netbeans.modules.serviceapi.wsdl.WSDL11Description;
065: import org.netbeans.modules.web.project.classpath.ClassPathProviderImpl;
066: import org.netbeans.modules.xml.retriever.catalog.Utilities;
067: import org.netbeans.modules.xml.wsdl.model.PortType;
068: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
069: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
070: import org.netbeans.modules.xml.xam.ModelSource;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.FileUtil;
073: import org.openide.loaders.DataObject;
074: import org.openide.loaders.DataObjectNotFoundException;
075: import org.openide.nodes.AbstractNode;
076: import org.openide.nodes.Children;
077: import org.openide.nodes.Node;
078: import org.openide.util.NotImplementedException;
079:
080: /**
081: *
082: * @author Nam Nguyen
083: */
084: public class ServiceModuleImpl extends ServiceModule {
085: private WebProject project;
086:
087: /** Creates a new instance of ServiceModuleImpl */
088: public ServiceModuleImpl(WebProject webProject) {
089: this .project = webProject;
090: }
091:
092: private WebServicesSupportImpl getWebServiceSupport() {
093: return project.getLookup().lookup(WebServicesSupportImpl.class);
094: }
095:
096: /**
097: * @return name of the service module.
098: */
099: public String getName() {
100: return project.getName();
101: }
102:
103: /**
104: * Returns service components contained in this module.
105: */
106: public Collection<ServiceComponent> getServiceComponents() {
107: ArrayList<ServiceComponent> ret = new ArrayList<ServiceComponent>();
108: Webservices wss = getWebservices();
109:
110: if (wss != null) {
111: WebserviceDescription[] descriptions = wss
112: .getWebserviceDescription();
113: if (descriptions == null)
114: return ret;
115: for (WebserviceDescription wsd : descriptions) {
116: PortComponent[] portComponents = wsd.getPortComponent();
117: if (portComponents == null)
118: continue;
119: String wsdlPath = wsd.getWsdlFile();
120: assert wsdlPath != null;
121: for (PortComponent pc : portComponents) {
122: ret.add(new Component(wsdlPath, pc));
123: }
124: }
125:
126: for (Servlet s : getWebApp().getServlet()) {
127: s.getServletClass();
128: }
129: }
130: return ret;
131: }
132:
133: public static QName convertSchema2BeansQName(
134: org.netbeans.modules.schema2beans.QName q) {
135: return new QName(q.getNamespaceURI(), q.getLocalPart());
136: }
137:
138: private class Component extends ServiceComponent {
139: private PortComponent portComponent;
140: private String wsdlFilePath;
141:
142: private Component(String wsdlFilePath) {
143: //absolute path only for now,
144: //PENDING: maybe also support project reference?
145: this .wsdlFilePath = wsdlFilePath;
146: }
147:
148: private Component(String wsdlFilePath, PortComponent pc) {
149: this .wsdlFilePath = wsdlFilePath;
150: portComponent = pc;
151: }
152:
153: public String getWsdlFilePath() {
154: return wsdlFilePath;
155: }
156:
157: public List<ServiceInterface> getServiceProviders() {
158: if (portComponent == null
159: || portComponent.getWsdlPort() == null) {
160: return Collections.emptyList();
161: }
162:
163: QName portQName = convertSchema2BeansQName(portComponent
164: .getWsdlPort());
165: Description def = new Description(getWsdlFilePath(),
166: portQName);
167: Interface i = new Interface(this , def, true);
168: List<ServiceInterface> ret = new ArrayList<ServiceInterface>();
169: ret.add(i);
170: return ret;
171: }
172:
173: public List<ServiceInterface> getServiceConsumers() {
174: //TODO how do I discovery interfaces consume by this component.
175: return Collections.emptyList();
176: }
177:
178: public String getImplClassName() {
179: String link = portComponent.getServiceImplBean()
180: .getServletLink();
181: return getWebServiceSupport().getImplementationBean(link);
182: }
183:
184: public Collection<ServiceLink> getServiceLinks() {
185: // Web project module does not support service links
186: return Collections.emptyList();
187: }
188:
189: public Node getNode() {
190: FileObject fo = getSourcesClassPath().findResource(
191: getImplClassName());
192: assert fo != null;
193: try {
194: DataObject dobj = DataObject.find(fo);
195: assert dobj != null;
196: return dobj.getNodeDelegate();
197: } catch (DataObjectNotFoundException ex) {
198: assert false : "DataObjectNotFoundException: "
199: + ex.getMessage();
200: return null;
201: }
202: }
203:
204: public ServiceInterface createServiceInterface(
205: InterfaceDescription description, boolean provider) {
206: if (provider)
207: return null;
208: throw new UnsupportedOperationException(
209: "Not supported yet.");
210: }
211:
212: public ServiceInterface createServiceInterface(
213: ServiceInterface other) {
214: throw new UnsupportedOperationException(
215: "Not supported yet.");
216: }
217:
218: public void removeServiceInterface(
219: ServiceInterface serviceInterface) {
220: throw new UnsupportedOperationException(
221: "Not supported yet.");
222: }
223:
224: @Override
225: public int hashCode() {
226: return getImplClassName().hashCode();
227: }
228:
229: @Override
230: public boolean equals(Object obj) {
231: if (!(obj instanceof Component))
232: return false;
233: Component other = (Component) obj;
234: return this .getImplClassName().equals(
235: other.getImplClassName());
236: }
237: }
238:
239: private class Interface implements ServiceInterface {
240: Component component;
241: Description description;
242: boolean isProvider = true;
243:
244: Interface(Component parent, Description description,
245: boolean isProvider) {
246: this (parent, description);
247: this .isProvider = isProvider;
248: }
249:
250: private Interface(Component parent, Description description) {
251: this .component = parent;
252: this .description = description;
253: }
254:
255: public Description getInterfaceDescription() {
256: return description;
257: }
258:
259: public ServiceComponent getServiceComponent() {
260: return component;
261: }
262:
263: public boolean canConnect(ServiceInterface other) {
264: if (!(other.getInterfaceDescription() instanceof WSDL11Description)) {
265: return false;
266: }
267: WSDL11Description otherDescription = (WSDL11Description) other
268: .getInterfaceDescription();
269: return other.isProvider() != isProvider()
270: && otherDescription.getInterfaceQName().equals(
271: getQName());
272: }
273:
274: public boolean isProvider() {
275: return isProvider;
276: }
277:
278: public Node getNode() {
279: //TODO probably portType node from WSLD UI or provide more functionality here.
280: return new AbstractNode(Children.LEAF) {
281: public String getName() {
282: return description.getDisplayName();
283: }
284: };
285: }
286:
287: public QName getQName() {
288: return getInterfaceDescription().getInterfaceQName();
289: }
290: }
291:
292: private class Description extends WSDL11Description {
293: private String pathToWSDL;
294: private QName portTypeQName;
295:
296: Description(String pathToWSDL, QName portTypeQName) {
297: this .pathToWSDL = pathToWSDL;
298: this .portTypeQName = portTypeQName;
299: }
300:
301: @Override
302: public QName getInterfaceQName() {
303: return portTypeQName;
304: }
305:
306: public String getDisplayName() {
307: return portTypeQName.getLocalPart();
308: }
309:
310: public PortType getInterface() {
311: File sourceFile = new File(pathToWSDL);
312: FileObject sourceFO = FileUtil.toFileObject(sourceFile);
313: if (sourceFO != null) {
314: try {
315: ModelSource ms = Utilities.createModelSource(
316: sourceFO, true);
317: WSDLModel model = WSDLModelFactory.getDefault()
318: .getModel(ms);
319: if (model.getState().equals(WSDLModel.State.VALID)) {
320: Collection<PortType> portTypes = model
321: .getDefinitions().getPortTypes();
322: if (portTypes.size() > 0) {
323: return portTypes.iterator().next();
324: }
325: }
326: } catch (Exception ex) {
327: Logger.getLogger("global")
328: .log(Level.INFO, null, ex);
329: }
330: }
331: return null;
332: }
333: }
334:
335: private Webservices getWebservices() {
336: throw new NotImplementedException();
337: // TODO MetadataModel:
338: // RootInterface rootInterface = project.getWebModule().getDeploymentDescriptor(J2eeModule.WEBSERVICES_XML);
339: // if (rootInterface instanceof Webservices) {
340: // return (Webservices) rootInterface;
341: // }
342: // return null;
343: }
344:
345: private WebApp getWebApp() {
346: throw new NotImplementedException();
347: // TODO MetadataModel:
348: // RootInterface rootInterface = project.getWebModule().getDeploymentDescriptor(J2eeModule.WEB_XML);
349: // if (rootInterface instanceof WebApp) {
350: // return (WebApp) rootInterface;
351: // }
352: // assert false : "Failed to get WebApp";
353: // return null;
354: }
355:
356: /**
357: * Add service component.
358: */
359:
360: public void addServiceComponent(ServiceComponent component) {
361: throw new UnsupportedOperationException("Not supported yet.");
362: }
363:
364: /**
365: * Remove service component.
366: */
367: public void removeServiceComponent(ServiceComponent component) {
368: throw new UnsupportedOperationException("Not supported yet.");
369: }
370:
371: /**
372: * @return the project if applicable (not applicable for service modules from appserver).
373: */
374: public Project getProject() {
375: return project;
376: }
377:
378: private ClassPath getSourcesClassPath() {
379: ClassPathProviderImpl cpProvider = project
380: .getClassPathProvider();
381: return cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE);
382: }
383: }
|