1 19 20 package org.netbeans.modules.j2ee.refactoring.whereused; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.Map ; 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 61 public final class WebservicesXmlWhereUsedRefactoring { 62 63 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.whereused"); 65 66 ArrayList otherRefactors = new ArrayList (); 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 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 wsmodules = Utility.getRelevantWSModules(fo); 86 Iterator 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) { 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 ioe) { 101 } 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 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 118 PortComponent portComponent = ports[p]; 119 err.log("Port Component: " + portComponent); 120 String 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 QName wsdlPort = portComponent.getWsdlPort(); 132 if (wsdlPort != null) { 133 String localPart = wsdlPort.getLocalPart(); 134 if (jClass.getSimpleName().concat("Port").equals(localPart)) { RefactoringElementImplementation elem = 136 new WebservicesWsdlPortWhereUsedRefactoringElement( 137 webservicesXmlDD, wsdlPort.getPrefix() + ":" + localPart, webservicesXmlFO); refactoringElements.add(refactoring, elem); 139 } 140 } 141 142 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 166 String portName = portComponent.getPortComponentName(); 167 Project project = FileOwnerQuery.getOwner(webservicesXmlFO); 168 Map properties = wsm.getAntProjectHelper().getStandardPropertyEvaluator() 169 .getProperties(); 170 String propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX; 171 String propertyValue = (String ) properties.get(propertyName); 172 if (propertyValue != null) { FileObject wsConfigXmlFO; 174 175 File f = new File (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 servant = iface.getServantName(); 197 String 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 ioe) { 214 } 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 } } 228 } 229 } 231 if (refObject instanceof Method) { 232 233 Method method = (Method)refObject; 234 Resource res = method.getResource(); 235 FileObject fo = JavaModel.getFileObject(res); 236 Collection wsmodules = Utility.getRelevantWSModules(fo); 237 Iterator wsIter = wsmodules.iterator(); 238 String methodName = method.getName(); 239 ClassDefinition classDef = method.getDeclaringClass(); 241 if ((wsmodules != null) && (wsmodules.size() > 0)) { 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 ioe) { 250 } 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 267 PortComponent portComponent = ports[p]; 268 err.log("Port Component: " + portComponent); 269 270 275 String portName = portComponent.getPortComponentName(); 276 Project project = FileOwnerQuery.getOwner(webservicesXmlFO); 277 Map properties = ws.getAntProjectHelper().getStandardPropertyEvaluator().getProperties(); 278 String propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX; 279 String propertyValue = (String )properties.get(propertyName); 280 281 if (propertyValue != null) { FileObject wsConfigXmlFO; 283 284 File f = new File (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 ioe) { 295 } 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 servant = iface.getServantName(); 305 String 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); 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); 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 } } 349 } 351 return problem; 352 } 353 354 public final class WebservicesHandlerClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 355 356 protected Webservices webservicesXmlDD; 357 358 public WebservicesHandlerClassWhereUsedRefactoringElement(Webservices webservicesXmlDD, String name, FileObject parentFile) { 359 this.webservicesXmlDD = webservicesXmlDD; 360 this.name = name; 361 this.parentFile = parentFile; 362 } 363 364 367 public String getDisplayText() { 368 return NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, 369 "TXT_WebservicesXmlHandlerClassWhereUsed", name); } 371 } 372 373 public final class WebservicesHandlerNameWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 374 375 protected Webservices webservicesXmlDD; 376 377 public WebservicesHandlerNameWhereUsedRefactoringElement(Webservices webservicesXmlDD, String name, FileObject parentFile) { 378 this.webservicesXmlDD = webservicesXmlDD; 379 this.name = name; 380 this.parentFile = parentFile; 381 } 382 383 386 public String getDisplayText() { 387 return NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, 388 "TXT_WebservicesXmlHandlerNameWhereUsed", name); } 390 } 391 392 public final class WebservicesWsdlPortWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 393 394 protected Webservices webservicesXmlDD; 395 396 public WebservicesWsdlPortWhereUsedRefactoringElement(Webservices webservicesXmlDD, String name, FileObject parentFile) { 397 this.webservicesXmlDD = webservicesXmlDD; 398 this.name = name; 399 this.parentFile = parentFile; 400 } 401 402 405 public String getDisplayText() { 406 return NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, 407 "TXT_WebservicesXmlWsdlPortWhereUsed", name); } 409 } 410 411 public final class WebservicesSEIWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 412 413 protected Webservices webservicesXmlDD; 414 415 416 public WebservicesSEIWhereUsedRefactoringElement(Webservices webservicesXmlDD, String name, FileObject parentFile) { 417 this.webservicesXmlDD = webservicesXmlDD; 418 this.name = name; 419 this.parentFile = parentFile; 420 } 421 422 425 public String getDisplayText() { 426 Object [] args = new Object [] {name}; 427 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesXmlSEIWhereUsed"), args); } 429 } 430 431 public final class WebservicesConfigXmlServantWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 432 433 protected Configuration wsConfigXmlDD; 434 435 436 public WebservicesConfigXmlServantWhereUsedRefactoringElement(Configuration wsConfigXmlDD, String name, FileObject parentFile) { 437 this.wsConfigXmlDD = wsConfigXmlDD; 438 this.name = name; 439 this.parentFile = parentFile; 440 } 441 442 445 public String getDisplayText() { 446 Object [] args = new Object [] {name}; 447 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesConfigXmlServantWhereUsed"), args); } 449 } 450 451 public final class WebservicesConfigXmlInterfaceWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 452 453 protected Configuration wsConfigXmlDD; 454 455 456 public WebservicesConfigXmlInterfaceWhereUsedRefactoringElement(Configuration wsConfigXmlDD, String name, FileObject parentFile) { 457 this.wsConfigXmlDD = wsConfigXmlDD; 458 this.name = name; 459 this.parentFile = parentFile; 460 } 461 462 465 public String getDisplayText() { 466 Object [] args = new Object [] {name}; 467 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlWhereUsedRefactoring.class, "TXT_WebservicesConfigXmlInterfaceWhereUsed"), args); } 469 } 470 471 } 472 | Popular Tags |