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-2006 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:
042: package org.netbeans.modules.identity.profile.ui.support;
043:
044: import com.sun.source.tree.ClassTree;
045: import com.sun.source.tree.Tree;
046: import com.sun.source.util.TreePath;
047: import com.sun.source.util.Trees;
048: import java.io.IOException;
049: import java.util.ArrayList;
050: import java.util.Enumeration;
051: import java.util.List;
052: import java.util.Map;
053: import javax.lang.model.element.AnnotationMirror;
054: import javax.lang.model.element.AnnotationValue;
055: import javax.lang.model.element.ExecutableElement;
056: import javax.lang.model.element.Modifier;
057: import javax.lang.model.element.TypeElement;
058: import javax.lang.model.element.VariableElement;
059: import javax.lang.model.util.ElementFilter;
060: import org.netbeans.api.java.source.CompilationController;
061: import org.netbeans.api.java.source.JavaSource;
062: import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
063: import org.openide.filesystems.FileObject;
064: import org.openide.nodes.Node;
065:
066: /**
067: *
068: * @author PeterLiu
069: */
070: public class J2ee15ProjectHelper extends J2eeProjectHelper {
071:
072: private static final String SERVICE_SUFFIX = "Service"; //NOI18N
073:
074: private static final String WEB_FOLDER = "web"; //NOI18N
075:
076: private static final String JAVA_EXT = "java"; //NOI18N
077:
078: private static final String CONF_FOLDER = "conf"; //NOI18N
079:
080: private static final String TEST_FOLDER = "test"; //NOI18N
081:
082: private String portComponentName;
083: private String serviceDescriptionName;
084: private List<String> serviceRefNames;
085: private List<ServiceRef> serviceRefs;
086:
087: /** Creates a new instance of J2ee15ProjectHelper */
088: protected J2ee15ProjectHelper(Node node, JaxWsModel model) {
089: super (node, model);
090: }
091:
092: // public String getPortComponentName() {
093: // if (portComponentName == null) {
094: // JavaSource source = JavaSource.forFileObject(getJavaSource());
095: //
096: // if (source != null)
097: // portComponentName = getClassName(source);
098: // }
099: //
100: // //System.out.println("J2ee15ProjectHelper.portComponentName = " + portComponentName);
101: // return portComponentName;
102: // }
103: //
104: // public String getServiceDescriptionName() {
105: // if (serviceDescriptionName == null) {
106: // JavaSource source = JavaSource.forFileObject(getJavaSource());
107: //
108: // if (source != null) {
109: // serviceDescriptionName = getServiceName(source);
110: //
111: // if (serviceDescriptionName == null) {
112: // serviceDescriptionName = getPortComponentName() + SERVICE_SUFFIX;
113: // }
114: // }
115: // }
116: //
117: // //System.out.println("J2ee15ProjectHelper.serviceDescriptionName = " + serviceDescriptionName);
118: // return serviceDescriptionName;
119: // }
120:
121: public List<String> getAllServiceRefNames() {
122: if (serviceRefNames == null) {
123: serviceRefNames = new ArrayList<String>();
124: serviceRefs = new ArrayList<ServiceRef>();
125: List<ServiceRef> refs = getServiceRefsFromSources();
126: String wsdlUri = getClient().getWsdlUrl();
127:
128: for (ServiceRef ref : refs) {
129: if (ref.getWsdlLocation().equals(wsdlUri)) {
130: serviceRefNames.add(ref.getName());
131: serviceRefs.add(ref);
132: }
133: }
134: }
135:
136: return serviceRefNames;
137: }
138:
139: private String getClassName(JavaSource source) {
140: final String[] className = new String[1];
141:
142: try {
143: source.runUserActionTask(
144: new AbstractTask<CompilationController>() {
145: public void run(CompilationController controller)
146: throws IOException {
147: controller
148: .toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
149: ClassTree tree = getTopLevelClassTree(controller);
150: className[0] = tree.getSimpleName()
151: .toString();
152: }
153: }, true);
154: } catch (IOException ex) {
155:
156: }
157:
158: return className[0];
159: }
160:
161: private String getServiceName(JavaSource source) {
162: final String[] serviceName = new String[1];
163:
164: try {
165: source.runUserActionTask(
166: new AbstractTask<CompilationController>() {
167: public void run(CompilationController controller)
168: throws IOException {
169: controller
170: .toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
171: TypeElement classElement = getTopLevelClassElement(controller);
172:
173: if (classElement == null) {
174: //System.out.println("Cannot resolve class!");
175: } else {
176: List<? extends AnnotationMirror> annotations = controller
177: .getElements()
178: .getAllAnnotationMirrors(
179: classElement);
180:
181: for (AnnotationMirror annotation : annotations) {
182: if (annotation
183: .toString()
184: .startsWith(
185: "@javax.jws.WebService")) { //NOI18N
186:
187: Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotation
188: .getElementValues();
189:
190: for (ExecutableElement key : values
191: .keySet()) {
192: if (key
193: .getSimpleName()
194: .toString()
195: .equals(
196: "serviceName")) { //NOI18N
197: String name = values
198: .get(key)
199: .toString();
200: serviceName[0] = name
201: .replace("\"",
202: ""); //NOI18N
203:
204: return;
205: }
206:
207: }
208: break;
209: }
210: }
211: }
212: }
213: }, true);
214: } catch (IOException ex) {
215:
216: }
217:
218: return serviceName[0];
219: }
220:
221: private List<ServiceRef> getServiceRefsFromSources() {
222: FileObject[] sourceRoots = getProvider().getSourceRoots();
223: List<ServiceRef> refs = new ArrayList<ServiceRef>();
224:
225: for (FileObject root : sourceRoots) {
226: String name = root.getName();
227:
228: if (name.equals(CONF_FOLDER) || name.equals(WEB_FOLDER)
229: || name.equals(TEST_FOLDER)) {
230: continue;
231: }
232:
233: Enumeration<? extends FileObject> dataFiles = root
234: .getData(true);
235:
236: while (dataFiles.hasMoreElements()) {
237: FileObject fobj = dataFiles.nextElement();
238:
239: if (fobj.getExt().equals(JAVA_EXT)) {
240: //System.out.println("source fobj = " + fobj);
241: JavaSource source = JavaSource.forFileObject(fobj);
242:
243: refs.addAll(getServiceRefsFromSource(source));
244: }
245: }
246: }
247:
248: return refs;
249: }
250:
251: private List<ServiceRef> getServiceRefsFromSource(JavaSource source) {
252: final List<ServiceRef> refs = new ArrayList<ServiceRef>();
253:
254: try {
255: source.runUserActionTask(
256: new AbstractTask<CompilationController>() {
257: public void run(CompilationController controller)
258: throws IOException {
259: controller
260: .toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
261:
262: TypeElement classElement = getTopLevelClassElement(controller);
263: List<VariableElement> fields = ElementFilter
264: .fieldsIn(classElement
265: .getEnclosedElements());
266:
267: for (VariableElement field : fields) {
268: List<? extends AnnotationMirror> annotations = field
269: .getAnnotationMirrors();
270:
271: for (AnnotationMirror annotation : annotations) {
272: if (annotation
273: .toString()
274: .startsWith(
275: "@javax.xml.ws.WebServiceRef")) { //NOI18N
276: Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotation
277: .getElementValues();
278:
279: for (ExecutableElement key : values
280: .keySet()) {
281: if (key
282: .getSimpleName()
283: .toString()
284: .equals(
285: "wsdlLocation")) { //NOI18N
286: String wsdlLocation = values
287: .get(key)
288: .toString()
289: .replace("\"",
290: ""); //NOI18N
291: String refName = classElement
292: .getQualifiedName()
293: .toString()
294: + "/" + //NOI18N
295: field
296: .getSimpleName()
297: .toString();
298: String className = classElement
299: .getSimpleName()
300: .toString();
301:
302: refs
303: .add(new ServiceRef(
304: refName,
305: wsdlLocation,
306: className));
307: }
308: }
309: break;
310: }
311: }
312: }
313: }
314: }, true);
315: } catch (IOException ex) {
316:
317: }
318:
319: return refs;
320: }
321:
322: private ClassTree getTopLevelClassTree(
323: CompilationController controller) {
324: String className = controller.getFileObject().getName();
325:
326: List<? extends Tree> decls = controller.getCompilationUnit()
327: .getTypeDecls();
328:
329: for (Tree decl : decls) {
330: if (decl.getKind() != Tree.Kind.CLASS) {
331: continue;
332: }
333:
334: ClassTree classTree = (ClassTree) decl;
335:
336: if (classTree.getSimpleName().contentEquals(className)
337: && classTree.getModifiers().getFlags().contains(
338: Modifier.PUBLIC))
339: return classTree;
340: }
341:
342: return null;
343: }
344:
345: private TypeElement getTopLevelClassElement(
346: CompilationController controller) {
347: ClassTree classTree = getTopLevelClassTree(controller);
348: Trees trees = controller.getTrees();
349: TreePath path = trees.getPath(controller.getCompilationUnit(),
350: classTree);
351:
352: return (TypeElement) trees.getElement(path);
353: }
354:
355: private static class ServiceRef {
356: private String name;
357: private String wsdlLocation;
358: private String className;
359:
360: public ServiceRef(String name, String wsdlLocation,
361: String className) {
362: this .name = name;
363: this .wsdlLocation = wsdlLocation;
364: this .className = className;
365: }
366:
367: public String getName() {
368: return name;
369: }
370:
371: public String getWsdlLocation() {
372: return wsdlLocation;
373: }
374:
375: public String getClassName() {
376: return className;
377: }
378: }
379: }
|