KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > jaxwssupport > JaxWsXmlRefactoringSupport


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 package org.netbeans.modules.j2ee.refactoring.jaxwssupport;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.project.FileOwnerQuery;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.jmi.javamodel.Annotation;
31 import org.netbeans.jmi.javamodel.AttributeValue;
32 import org.netbeans.jmi.javamodel.JavaClass;
33 import org.netbeans.modules.j2ee.refactoring.rename.AbstractRenameRefactoringElement;
34 import org.netbeans.modules.j2ee.refactoring.rename.JaxWsXmlRenameRefactoring;
35 import org.netbeans.modules.javacore.api.JavaModel;
36 import org.netbeans.modules.javacore.internalapi.ExternalChange;
37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
38 import org.netbeans.modules.websvc.api.jaxws.project.config.Endpoint;
39 import org.netbeans.modules.websvc.api.jaxws.project.config.Endpoints;
40 import org.netbeans.modules.websvc.api.jaxws.project.config.EndpointsProvider;
41 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
42 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModelProvider;
43 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
44 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
45 import org.openide.ErrorManager;
46 import org.openide.filesystems.FileObject;
47 import org.openide.util.NbBundle;
48
49 /**
50  * Base class for jax-ws.xml and sun-jaxws.xml refactorings.
51  *
52  * @author Erno Mononen
53  */

54 public abstract class JaxWsXmlRefactoringSupport {
55     
56     /**
57      * Name of an annotation that represents web service.
58      */

59     protected static final String JavaDoc WS_ANNOTATION = "WebService";
60     /**
61      * Name of an annotation element that represents wsdl location.
62      */

63     protected static final String JavaDoc WSDL_LOCATION_ELEMENT = "wsdlLocation";
64     
65     /**
66      * Creates a new instance of JaxWsRefactoringSupport
67      */

68     protected JaxWsXmlRefactoringSupport() {
69     }
70     
71     /**
72      * @return the model representing jax-ws.xml from the project of given
73      * <code>javaClass</code> or null if was not found.
74      */

75     protected JaxWsModel getModel(JavaClass javaClass){
76         JaxWsModelProvider provider = JaxWsModelProvider.getDefault();
77         
78         FileObject fo = JavaModel.getFileObject(javaClass.getResource());
79         Project project = FileOwnerQuery.getOwner(fo);
80         
81         FileObject jaxWsXml = project.getProjectDirectory().getFileObject("nbproject/jax-ws.xml");
82         // no jax-ws.xml file in the project
83
if (jaxWsXml == null){
84             return null;
85         }
86         
87         JaxWsModel model = null;
88         try {
89             model = provider.getJaxWsModel(jaxWsXml);
90         } catch (IOException JavaDoc ex){
91             ErrorManager.getDefault().notify(ex);
92         }
93         
94         return model;
95     }
96     
97     /**
98      * @return true if the given javaClass represents a web service from wsdl.
99      */

100     protected boolean isWebSvcFromWsdl(JavaClass javaClass){
101         for (Object JavaDoc elem : javaClass.getAnnotations()) {
102             Annotation ann = (Annotation) elem;
103             if (ann.getTypeName() == null){
104                 continue;
105             }
106             if (ann.getTypeName().getName().equals(WS_ANNOTATION)){
107                 for (Object JavaDoc elem2 : ann.getAttributeValues()) {
108                     AttributeValue value = (AttributeValue) elem2;
109                     if (value.getName().equals(WSDL_LOCATION_ELEMENT)){
110                         return true;
111                     }
112                 }
113             }
114         }
115         return false;
116     }
117     
118     /**
119      * @return Endpoints whose implementation class is equal to given <code>javaClass</code>,
120      * never null.
121      */

122     protected List JavaDoc<Endpoint> getEndpoints(JavaClass javaClass){
123         Endpoints endpoints = getEndpointsModel(javaClass);
124         
125         if (endpoints == null){
126             return Collections.emptyList();
127         }
128         
129         List JavaDoc<Endpoint> result = new ArrayList JavaDoc<Endpoint>();
130         for (Endpoint each : endpoints.getEndpoints()) {
131             if (javaClass.getName().equals(each.getImplementation())){
132                 result.add(each);
133             }
134         }
135         return result;
136     }
137     
138     /**
139      * @return the model representing <code>sun-jaxws.xml</code> in the
140      * project of given <code>javaClass</code> or null.
141      */

142     protected Endpoints getEndpointsModel(JavaClass javaClass){
143         FileObject sunjaxwsFile = getSunJaxWsXmlFile(javaClass);
144         if (sunjaxwsFile == null){
145             return null;
146         }
147
148         Endpoints endpoints = null;
149         try {
150             endpoints = EndpointsProvider.getDefault().getEndpoints(sunjaxwsFile);
151         } catch (IOException JavaDoc ex) {
152             ErrorManager.getDefault().notify(ex);
153         }
154         
155         return endpoints;
156     }
157     
158     /**
159      * @return the file object representing <code>sun-jaxws.xml</code> in the
160      * project of given <code>javaClass</code> or null.
161      */

162     protected FileObject getSunJaxWsXmlFile(JavaClass javaClass){
163         FileObject javaClassFO = JavaModel.getFileObject(javaClass.getResource());
164         JAXWSSupport jaxwssupport = JAXWSSupport.getJAXWSSupport(javaClassFO);
165         FileObject folder = jaxwssupport.getDeploymentDescriptorFolder();
166         return folder.getFileObject("sun-jaxws.xml");
167     }
168 }
169
Popular Tags