KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxwsmodel > project > metadata > JAXWSNNListener


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.websvc.jaxwsmodel.project.metadata;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Timer JavaDoc;
27 import java.util.TimerTask JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.java.project.JavaProjectConstants;
30 import org.netbeans.api.project.FileOwnerQuery;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectUtils;
33 import org.netbeans.api.project.SourceGroup;
34 //retouche:
35
//import org.netbeans.jmi.javamodel.Annotation;
36
//import org.netbeans.jmi.javamodel.AnnotationType;
37
//import org.netbeans.jmi.javamodel.Element;
38
//import org.netbeans.jmi.javamodel.JavaClass;
39
//import org.netbeans.modules.j2ee.metadata.NNListener;
40
//import org.netbeans.modules.j2ee.metadata.Utils;
41
//import org.netbeans.modules.javacore.api.JavaModel;
42
import org.netbeans.modules.websvc.api.jaxws.project.WSUtils;
43 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
44 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
45 import org.netbeans.modules.websvc.api.jaxws.project.config.ServiceAlreadyExistsExeption;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48
49 /**
50  *
51  * @author Marek Fukala
52  */

53 //retouche:
54
public class JAXWSNNListener {//implements NNListener {
55

56     private static final ArrayList JavaDoc<String JavaDoc> supportedAnnotations;
57     static {
58         supportedAnnotations = new ArrayList JavaDoc<String JavaDoc>();
59         supportedAnnotations.add("javax.jws.WebService");//NOI18N
60
}
61     
62     private static final String JavaDoc WS_NAME = "name";//NOI18N
63
private static final String JavaDoc WSDL_LOCATION_NN_ATTRIBUTE = "wsdlLocation"; //NOI18N
64

65     private JaxWsModel jaxWSmodel;
66     private ClassPath cp;
67     
68     //jax-ws.xml update task timer
69
private Timer JavaDoc timer = new Timer JavaDoc();
70     private TimerTask JavaDoc ttask = createTimerTask();
71     
72     /** Creates a new instance of JAXWSNNListener */
73     public JAXWSNNListener(JaxWsModel jaxWSmodel, ClassPath cp) {
74         this.jaxWSmodel = jaxWSmodel;
75         this.cp = cp;
76     }
77     
78     public ClassPath getClassPath() {
79         return cp;
80     }
81     
82     public Collection JavaDoc<String JavaDoc> getSupportedAnnotations() {
83         return supportedAnnotations;
84     }
85     
86     public Collection JavaDoc<String JavaDoc> getPrimaryAnnotations() {
87         return Collections.<String JavaDoc>emptyList();
88     }
89     
90 //retouche:
91
// public void addClassAnnotation(JavaClass javaClass, Annotation annotation, AnnotationType annotationType) {
92
// //do not update the jaxwsmodel if the annotation contains wsdlLocation attribute
93
// //it is necessary for special handling of WS generated from WSDL file
94
// if(Utils.getNNAttributes(annotation).get(WSDL_LOCATION_NN_ATTRIBUTE) == null) {
95
// String wsName = getWSName(javaClass, annotation);
96
// String className = javaClass.getName();
97
// FileObject fo = JavaModel.getFileObject(javaClass.getResource());
98
// if(isInSourceGroup(fo) && !javaClass.isInterface()) {
99
// try {
100
// Service service = jaxWSmodel.findServiceByImplementationClass(className);
101
// if (service == null) {
102
// jaxWSmodel.addService(wsName, className);
103
// requestModelUpdate();
104
// }
105
// }catch(ServiceAlreadyExistsExeption e) {
106
// //the service name already exists - we need to check whether the impl. class is also the same
107
// Service service = jaxWSmodel.findServiceByImplementationClass(className);
108
// if(service == null || !wsName.equals(service.getName())) {
109
// //there is no service for the impl. class with the wsName name => generate new wsName
110
// String newName = WSUtils.findProperServiceName(wsName, jaxWSmodel);
111
// try {
112
// jaxWSmodel.addService(newName, className);
113
// } catch (ServiceAlreadyExistsExeption ex1) {
114
// //this shouldn't happen
115
// }
116
// }
117
// }
118
// }
119
// }
120
// }
121
//
122
// public void removeClassAnnotation(JavaClass javaClass, org.netbeans.jmi.javamodel.Annotation annotation, AnnotationType annotationType) {
123
// //do not update the jaxwsmodel if the annotation contains wsdlLocation attribute
124
// //it is necessary for special handling of WS generated from WSDL file
125
// FileObject fo = JavaModel.getFileObject(javaClass.getResource());
126
// if(fo == null || isInSourceGroup(fo)) {
127
// Service serviceToRemove = jaxWSmodel.findServiceByImplementationClass(javaClass.getName());
128
// if (serviceToRemove != null && serviceToRemove.getWsdlUrl() == null) {
129
// jaxWSmodel.removeServiceByClassName(javaClass.getName());
130
// requestModelUpdate();
131
// }
132
// }
133
// }
134

135     public void classRemoved(String JavaDoc fqn) {
136         //do not update the jaxwsmodel if the annotation contains wsdlLocation attribute
137
//it is necessary for special handling of WS generated from WSDL file
138
// TODO: check if service was in source group, but how (file doesn't exist anymore)?
139
Service serviceToRemove = jaxWSmodel.findServiceByImplementationClass(fqn);
140         if (serviceToRemove != null && serviceToRemove.getWsdlUrl() == null) {
141             jaxWSmodel.removeServiceByClassName(fqn);
142             requestModelUpdate();
143         }
144     }
145     
146 //retouche:
147
// public void addClassAttribute(JavaClass javaClass, Annotation annotation, AnnotationType t, String attributeName, String attributeValue) {
148
// }
149
//
150
// public void removeClassAttribute(JavaClass javaClass, Annotation annotation, AnnotationType t, String attributeName) {
151
// }
152
//
153
// public void addMemberAttribute(JavaClass javaClass, Element member, Annotation annotation, AnnotationType t, String attributeName, String attributeValue) {
154
// }
155
//
156
// public void removeMemberAttribute(JavaClass javaClass, Element memberName, Annotation annotation, AnnotationType t, String attributeName) {
157
// }
158
//
159
// public void addMemberAnnotation(boolean field, JavaClass javaClass, Element member, Annotation annotation, AnnotationType t) {
160
// }
161
//
162
// public void removeMemberAnnotation(boolean field, JavaClass javaClass, String memberName, String memberType, Annotation annotation, AnnotationType t) {
163
// }
164

165     private boolean isInSourceGroup(FileObject fo) {
166         Project project = FileOwnerQuery.getOwner(jaxWSmodel.getJaxWsFile());
167         SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
168         for(int i = 0; i < sourceGroups.length; i++) {
169             if(FileUtil.isParentOf(sourceGroups[i].getRootFolder(), fo)) return true;
170         }
171         return false;
172     }
173     
174     private void requestModelUpdate() {
175         synchronized (ttask) {
176             ttask.cancel();
177             //create a new task a schedule it
178
ttask = createTimerTask();
179             timer.schedule(ttask, 100);
180         }
181     }
182     
183     private TimerTask JavaDoc createTimerTask() {
184         return new TimerTask JavaDoc() {
185             public void run() {
186                 synchronized (ttask) {
187                     try {
188                         //try to ensure
189
jaxWSmodel.write();
190                     }catch(IOException JavaDoc ioe) {
191                         //probably cannot get exclusive access to the file -> try it later
192
requestModelUpdate();
193                     }
194                 }
195             }
196         };
197     }
198     
199 //retouche:
200
// private String getWSName(JavaClass javaClass, Annotation annotation) {
201
// String wsname = (String)Utils.getNNAttributes(annotation).get(WS_NAME);
202
// if(wsname == null) {
203
// //use default name
204
// wsname = javaClass.getSimpleName();
205
// }
206
// return wsname;
207
// }
208

209 }
210
Popular Tags