KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > whereused > WebservicesXmlWhereUsedRefactoring


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.whereused;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.jmi.reflect.RefObject;
30 import org.netbeans.api.project.FileOwnerQuery;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.jmi.javamodel.ClassDefinition;
33 import org.netbeans.jmi.javamodel.Method;
34 import org.netbeans.jmi.javamodel.UnresolvedClass;
35 import org.netbeans.jmi.javamodel.JavaClass;
36 import org.netbeans.jmi.javamodel.Resource;
37 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
38 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
39 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
40 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponentHandler;
41 import org.netbeans.modules.j2ee.refactoring.Utility;
42 import org.netbeans.modules.javacore.api.JavaModel;
43 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
44 import org.netbeans.modules.refactoring.api.Problem;
45 import org.netbeans.modules.refactoring.api.WhereUsedQuery;
46 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
47 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
48 import org.netbeans.modules.schema2beans.QName;
49 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
50 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
51 import org.netbeans.modules.websvc.wsdl.config.api.Configuration;
52 import org.openide.ErrorManager;
53 import org.openide.filesystems.FileObject;
54 import org.openide.filesystems.FileUtil;
55 import org.openide.util.NbBundle;
56
57 /**
58  *
59  * @author Martin Grebac
60  */

61 public final class WebservicesXmlWhereUsedRefactoring {
62     
63     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.whereused"); // NOI18N
64

65     /* stores collection of interface refactorings to be invoked */
66     ArrayList JavaDoc otherRefactors = new ArrayList JavaDoc();
67
68     public WebservicesXmlWhereUsedRefactoring() { }
69     
70     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, RefactoringElementsBag refactoringElements) {
71             
72         Problem problem = null;
73         if (refObject instanceof JavaClass) {
74             
75             JavaClass jClass = (JavaClass)refObject;
76             err.log("jClass: " + jClass);
77             
78             String JavaDoc name = jClass.getName();
79             err.log("name: " + name);
80             Resource res = jClass.getResource();
81             err.log("res: " + res);
82             
83             FileObject fo = JavaModel.getFileObject(res);
84             err.log("fo: " + fo);
85             Collection JavaDoc wsmodules = Utility.getRelevantWSModules(fo);
86             Iterator JavaDoc wsIter = null;
87
88             if (wsmodules != null) {
89                 wsIter = wsmodules.iterator();
90             }
91             if (wsIter != null) {
92                 while (wsIter.hasNext()) {
93                     WebServicesSupport wsm = (WebServicesSupport)wsIter.next();
94                     if (wsm != null) { // the class is in a module with web services support
95

96                         FileObject webservicesXmlFO = wsm.getWebservicesDD();
97                         Webservices webservicesXmlDD = null;
98                         try {
99                             webservicesXmlDD = org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(webservicesXmlFO);
100                         } catch (IOException JavaDoc ioe) {
101                             //ignore
102
}
103
104                         if ((webservicesXmlDD != null) && (webservicesXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)) {
105                             WebserviceDescription[] wservices = webservicesXmlDD.getWebserviceDescription();
106                             err.log("Web Services: " + wservices);
107                             if ((wservices != null) && (wservices.length > 0)) {
108                                 for (int w=0; w < wservices.length; w++) {
109                                     WebserviceDescription webService = wservices[w];
110                                     err.log("Web Service: " + webService);
111
112                                     /* And update the actual web service reference direct in webservices.xml */
113                                     PortComponent[] ports = webService.getPortComponent();
114                                     err.log("Ports: " + ports);
115                                     if ((ports != null) && (ports.length > 0)) for (int p = 0; p < ports.length; p++) {
116
117                                         /* First check for presence in webservices.xml port */
118                                         PortComponent portComponent = ports[p];
119                                         err.log("Port Component: " + portComponent);
120                                         String JavaDoc serviceEndpointInterface = portComponent.getServiceEndpointInterface();
121                                         err.log("serviceEndpointInterface: " + serviceEndpointInterface);
122                                         if ((serviceEndpointInterface != null) &&
123                                                 (serviceEndpointInterface.equals(name))) {
124                                             RefactoringElementImplementation elem =
125                                                     new WebservicesSEIWhereUsedRefactoringElement(webservicesXmlDD,
126                                                             name, webservicesXmlFO);
127                                             refactoringElements.add(refactoring, elem);
128                                         }
129
130                                         // wsdl-port is generated from sei class name as well
131
QName wsdlPort = portComponent.getWsdlPort();
132                                         if (wsdlPort != null) {
133                                             String JavaDoc localPart = wsdlPort.getLocalPart();
134                                             if (jClass.getSimpleName().concat("Port").equals(localPart)) { //NOI18N
135
RefactoringElementImplementation elem =
136                                                         new WebservicesWsdlPortWhereUsedRefactoringElement(
137                                                                 webservicesXmlDD, wsdlPort.getPrefix() + ":" + localPart, webservicesXmlFO); //NOI18N
138
refactoringElements.add(refactoring, elem);
139                                             }
140                                         }
141
142                                         // handlers
143
PortComponentHandler[] handlers = portComponent.getHandler();
144                                         for (int i = 0; i < handlers.length; i++) {
145                                             if (name.equals(handlers[i].getHandlerName())) {
146                                                 RefactoringElementImplementation elem =
147                                                         new WebservicesHandlerNameWhereUsedRefactoringElement(
148                                                                 webservicesXmlDD, name, webservicesXmlFO);
149                                                 refactoringElements.add(refactoring, elem);
150                                             }
151                                             if (name.equals(handlers[i].getHandlerClass())) {
152                                                 RefactoringElementImplementation elem =
153                                                         new WebservicesHandlerClassWhereUsedRefactoringElement(
154                                                                 webservicesXmlDD, name, webservicesXmlFO);
155                                                 refactoringElements.add(refactoring, elem);
156                                             }
157
158                                         }
159
160
161                                         /* try to locate the local wscompile config file:
162                                         * get project for webservices.xml, get name of the webservice and try to find properties of the type
163                                         * $propertyname-config.name - if found, get the property valu and the path and locate the descriptor,
164                                         * then get the dd api descriptor object
165                                         */

166                                         String JavaDoc portName = portComponent.getPortComponentName();
167                                         Project project = FileOwnerQuery.getOwner(webservicesXmlFO);
168                                         Map JavaDoc properties = wsm.getAntProjectHelper().getStandardPropertyEvaluator()
169                                                 .getProperties();
170                                         String JavaDoc propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX;
171                                         String JavaDoc propertyValue = (String JavaDoc) properties.get(propertyName);
172                                         if (propertyValue != null) { // there's such property in the project
173
FileObject wsConfigXmlFO;
174
175                                             File JavaDoc f = new File JavaDoc(propertyValue);
176                                             if (f.isAbsolute()) {
177                                                 wsConfigXmlFO = FileUtil.toFileObject(f);
178                                             } else {
179                                                 wsConfigXmlFO =
180                                                         project.getProjectDirectory().getFileObject(propertyValue);
181                                             }
182                                             if (wsConfigXmlFO != null) {
183                                                 Configuration wsConfigXmlDD;
184                                                 try {
185                                                     wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api
186                                                             .DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
187                                                     org.netbeans.modules.websvc.wsdl.config.api.Service service =
188                                                             wsConfigXmlDD.getService();
189                                                     if (service != null) {
190                                                         org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces =
191                                                                 service.getInterface();
192                                                         if ((interfaces != null) && (interfaces.length > 0)) {
193                                                             for (int ii = 0; ii < interfaces.length; ii++) {
194                                                                 org.netbeans.modules.websvc.wsdl.config.api.Interface iface =
195                                                                         interfaces[ii];
196                                                                 String JavaDoc servant = iface.getServantName();
197                                                                 String JavaDoc iname = iface.getName();
198                                                                 if ((servant != null) && (servant.equals(name))) {
199                                                                     RefactoringElementImplementation elem =
200                                                                             new WebservicesConfigXmlServantWhereUsedRefactoringElement(
201                                                                                     wsConfigXmlDD, name, wsConfigXmlFO);
202                                                                     refactoringElements.add(refactoring, elem);
203                                                                 }
204                                                                 if ((iname != null) && (iname.equals(name))) {
205                                                                     RefactoringElementImplementation elem =
206                                                                             new WebservicesConfigXmlInterfaceWhereUsedRefactoringElement(
207                                                                                     wsConfigXmlDD, name, wsConfigXmlFO);
208                                                                     refactoringElements.add(refactoring, elem);
209                                                                 }
210                                                             }
211                                                         }
212                                                     }
213                                                 } catch (IOException JavaDoc ioe) {
214                                                     // ignore
215
}
216                                             }
217                                         }
218                                     }
219                                 }
220                             }
221                         } else if (webservicesXmlDD != null) {
222                             Problem newProblem = new Problem(false, NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesXmlWhereUsedInvalidProblem"));
223                             problem = Utility.addProblemsToEnd(problem, newProblem);
224                         }
225
226                     } // find usages in websvc.xml & wscompile config xml
227
}
228             }
229         } // javaclass
230

231         if (refObject instanceof Method) {
232             
233             Method method = (Method)refObject;
234             Resource res = method.getResource();
235             FileObject fo = JavaModel.getFileObject(res);
236             Collection JavaDoc wsmodules = Utility.getRelevantWSModules(fo);
237             Iterator JavaDoc wsIter = wsmodules.iterator();
238             String JavaDoc methodName = method.getName();
239             ClassDefinition classDef = method.getDeclaringClass(); // get affected class
240

241             if ((wsmodules != null) && (wsmodules.size() > 0)) { // there are web/ejb modules that can be affected by the change
242
while (wsIter.hasNext()) {
243                     WebServicesSupport ws = (WebServicesSupport)wsIter.next();
244                     if (ws != null) {
245                         FileObject webservicesXmlFO = ws.getWebservicesDD();
246                         Webservices webservicesXmlDD = null;
247                         try {
248                             webservicesXmlDD = org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(webservicesXmlFO);
249                         } catch (IOException JavaDoc ioe) {
250                             //ignore
251
}
252
253                         if ((webservicesXmlDD != null) && (webservicesXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)) {
254                             WebserviceDescription[] wservices = webservicesXmlDD.getWebserviceDescription();
255                             err.log("Web Services: " + wservices);
256                             if ((wservices != null) && (wservices.length > 0)) {
257                                 for (int w=0; w < wservices.length; w++) {
258                                     WebserviceDescription webService = wservices[w];
259                                     err.log("Web Service: " + webService);
260
261                                     PortComponent[] ports = webService.getPortComponent();
262                                     err.log("Ports: " + ports);
263                                     if ((ports != null) && (ports.length > 0)) {
264                                         for (int p=0; p < ports.length; p++) {
265
266                                             /* First check for presence in webservices.xml port */
267                                             PortComponent portComponent = ports[p];
268                                             err.log("Port Component: " + portComponent);
269
270                                             /* try to locate the local wscompile config file:
271                                              * get project for webservices.xml, get name of the webservice and try to find properties of the type
272                                              * $propertyname-config.name - if found, get the property valu and the path and locate the descriptor,
273                                              * then get the dd api descriptor object
274                                              */

275                                             String JavaDoc portName = portComponent.getPortComponentName();
276                                             Project project = FileOwnerQuery.getOwner(webservicesXmlFO);
277                                             Map JavaDoc properties = ws.getAntProjectHelper().getStandardPropertyEvaluator().getProperties();
278                                             String JavaDoc propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX;
279                                             String JavaDoc propertyValue = (String JavaDoc)properties.get(propertyName);
280
281                                             if (propertyValue != null) { // there's such property in the project
282
FileObject wsConfigXmlFO;
283
284                                                 File JavaDoc f = new File JavaDoc(propertyValue);
285                                                 if (f.isAbsolute()) {
286                                                     wsConfigXmlFO = FileUtil.toFileObject(f);
287                                                 } else {
288                                                     wsConfigXmlFO = project.getProjectDirectory().getFileObject(propertyValue);
289                                                 }
290                                                 if (wsConfigXmlFO != null) {
291                                                     Configuration wsConfigXmlDD = null;
292                                                     try {
293                                                         wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
294                                                     } catch (IOException JavaDoc ioe) {
295                                                         //ignore
296
}
297                                                     if (wsConfigXmlDD != null) {
298                                                         org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
299                                                         if (service != null) {
300                                                             org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
301                                                             if ((interfaces != null) && (interfaces.length > 0)) {
302                                                                 for (int ii=0; ii < interfaces.length; ii++) {
303                                                                     org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
304                                                                     String JavaDoc servant = iface.getServantName();
305                                                                     String JavaDoc iname = iface.getName();
306
307                                                                     if (classDef.getName().equals(iname)) {
308                                                                         JavaClass jcl = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(servant);
309                                                                         Method beanMethod = null;
310                                                                         if (!(jcl instanceof UnresolvedClass)) {
311                                                                             beanMethod = jcl.getMethod(methodName, Utility.getTypedParams(method.getParameters()), false); //NOI18N
312
err.log("beanmethod: " + beanMethod);
313                                                                         }
314                                                                         WhereUsedQuery whereBeanMethod = new WhereUsedQuery(beanMethod);
315                                                                         Problem prob = whereBeanMethod.preCheck();
316                                                                         problem = Utility.addProblemsToEnd(problem, prob);
317                                                                         otherRefactors.add(whereBeanMethod);
318
319                                                                     } else if (classDef.getName().equals(servant)) {
320                                                                         JavaClass jcl = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(iname);
321                                                                         Method iMethod = null;
322                                                                         if (!(jcl instanceof UnresolvedClass)) {
323                                                                             iMethod = jcl.getMethod(methodName, Utility.getTypedParams(method.getParameters()), false); //NOI18N
324
err.log("ifacemethod: " + iMethod);
325                                                                         }
326                                                                         WhereUsedQuery whereIfaceMethod = new WhereUsedQuery(iMethod);
327                                                                         Problem prob = whereIfaceMethod.preCheck();
328                                                                         problem = Utility.addProblemsToEnd(problem, prob);
329                                                                         otherRefactors.add(whereIfaceMethod);
330                                                                     }
331                                                                 }
332                                                             }
333                                                         }
334                                                     } else {
335                                                         Problem newProblem = new Problem(false, NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesXmlWhereUsedInvalidProblem"));
336                                                         problem = Utility.addProblemsToEnd(problem, newProblem);
337                                                         return problem;
338                                                     }
339                                                 }
340                                             }
341                                         }
342                                     }
343                                 }
344                             }
345                         }
346                     }
347                 } //while
348
}
349         } // method
350

351         return problem;
352     }
353     
354     public final class WebservicesHandlerClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
355
356         protected Webservices webservicesXmlDD;
357
358         public WebservicesHandlerClassWhereUsedRefactoringElement(Webservices webservicesXmlDD, String JavaDoc name, FileObject parentFile) {
359             this.webservicesXmlDD = webservicesXmlDD;
360             this.name = name;
361             this.parentFile = parentFile;
362         }
363
364         /** Returns text describing the refactoring formatted for display (using HTML tags).
365          * @return Formatted text.
366          */

367         public String JavaDoc getDisplayText() {
368             return NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class,
369                     "TXT_WebservicesXmlHandlerClassWhereUsed", name); //NOI18N
370
}
371     }
372
373     public final class WebservicesHandlerNameWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
374
375         protected Webservices webservicesXmlDD;
376
377         public WebservicesHandlerNameWhereUsedRefactoringElement(Webservices webservicesXmlDD, String JavaDoc name, FileObject parentFile) {
378             this.webservicesXmlDD = webservicesXmlDD;
379             this.name = name;
380             this.parentFile = parentFile;
381         }
382
383         /** Returns text describing the refactoring formatted for display (using HTML tags).
384          * @return Formatted text.
385          */

386         public String JavaDoc getDisplayText() {
387             return NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class,
388                     "TXT_WebservicesXmlHandlerNameWhereUsed", name); //NOI18N
389
}
390     }
391
392     public final class WebservicesWsdlPortWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
393
394         protected Webservices webservicesXmlDD;
395
396         public WebservicesWsdlPortWhereUsedRefactoringElement(Webservices webservicesXmlDD, String JavaDoc name, FileObject parentFile) {
397             this.webservicesXmlDD = webservicesXmlDD;
398             this.name = name;
399             this.parentFile = parentFile;
400         }
401
402         /** Returns text describing the refactoring formatted for display (using HTML tags).
403          * @return Formatted text.
404          */

405         public String JavaDoc getDisplayText() {
406             return NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class,
407                     "TXT_WebservicesXmlWsdlPortWhereUsed", name); //NOI18N
408
}
409     }
410
411     public final class WebservicesSEIWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
412
413         protected Webservices webservicesXmlDD;
414
415         /** Creates a new instance of WebservicesSEIWhereUsedRefactoringElement */
416         public WebservicesSEIWhereUsedRefactoringElement(Webservices webservicesXmlDD, String JavaDoc name, FileObject parentFile) {
417             this.webservicesXmlDD = webservicesXmlDD;
418             this.name = name;
419             this.parentFile = parentFile;
420         }
421
422         /** Returns text describing the refactoring formatted for display (using HTML tags).
423          * @return Formatted text.
424          */

425         public String JavaDoc getDisplayText() {
426             Object JavaDoc[] args = new Object JavaDoc [] {name};
427             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesXmlSEIWhereUsed"), args); //NOI18N
428
}
429     }
430
431     public final class WebservicesConfigXmlServantWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
432
433         protected Configuration wsConfigXmlDD;
434
435         /** Creates a new instance of WebservicesConfigXmlServantWhereUsedRefactoringElement */
436         public WebservicesConfigXmlServantWhereUsedRefactoringElement(Configuration wsConfigXmlDD, String JavaDoc name, FileObject parentFile) {
437             this.wsConfigXmlDD = wsConfigXmlDD;
438             this.name = name;
439             this.parentFile = parentFile;
440         }
441
442         /** Returns text describing the refactoring formatted for display (using HTML tags).
443          * @return Formatted text.
444          */

445         public String JavaDoc getDisplayText() {
446             Object JavaDoc[] args = new Object JavaDoc [] {name};
447             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesConfigXmlServantWhereUsed"), args); //NOI18N
448
}
449     }
450
451     public final class WebservicesConfigXmlInterfaceWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
452
453         protected Configuration wsConfigXmlDD;
454
455         /** Creates a new instance of WebservicesConfigXmlInterfaceWhereUsedRefactoringElement */
456         public WebservicesConfigXmlInterfaceWhereUsedRefactoringElement(Configuration wsConfigXmlDD, String JavaDoc name, FileObject parentFile) {
457             this.wsConfigXmlDD = wsConfigXmlDD;
458             this.name = name;
459             this.parentFile = parentFile;
460         }
461
462         /** Returns text describing the refactoring formatted for display (using HTML tags).
463          * @return Formatted text.
464          */

465         public String JavaDoc getDisplayText() {
466             Object JavaDoc[] args = new Object JavaDoc [] {name};
467             return MessageFormat.format(NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesConfigXmlInterfaceWhereUsed"), args); //NOI18N
468
}
469     }
470     
471 }
472
Popular Tags