1 19 20 package org.netbeans.modules.websvc.wsitconf.util; 21 22 import java.awt.Component ; 23 import java.util.Arrays ; 24 import javax.swing.JLabel ; 25 import java.awt.Container ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.net.URL ; 31 import java.security.KeyStoreException ; 32 import java.security.NoSuchAlgorithmException ; 33 import java.security.cert.CertificateException ; 34 import java.util.ArrayList ; 35 import javax.swing.JComponent ; 36 import java.util.Vector ; 37 import java.util.Iterator ; 38 import java.util.Collection ; 39 import java.util.Collections ; 40 import java.util.Enumeration ; 41 import java.util.HashMap ; 42 import java.util.HashSet ; 43 import java.util.List ; 44 import java.util.Map ; 45 import java.util.Set ; 46 import javax.xml.namespace.QName ; 47 import org.netbeans.api.java.classpath.ClassPath; 48 import org.netbeans.api.java.project.JavaProjectConstants; 49 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 50 import org.netbeans.api.project.FileOwnerQuery; 51 import org.netbeans.api.project.Project; 52 import org.netbeans.api.project.ProjectUtils; 53 import org.netbeans.api.project.SourceGroup; 54 import org.netbeans.api.project.ui.OpenProjects; 55 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 56 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 57 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 58 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 59 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider; 60 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 61 import org.netbeans.modules.web.api.webmodule.WebModule; 63 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 64 import org.netbeans.modules.websvc.jaxwsruntimemodel.JavaWsdlMapper; 65 import org.netbeans.modules.xml.wsdl.model.Binding; 66 import org.netbeans.modules.xml.wsdl.model.BindingOperation; 67 import org.netbeans.modules.xml.wsdl.model.Definitions; 68 import org.netbeans.modules.xml.wsdl.model.PortType; 69 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory; 70 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 71 import org.netbeans.spi.project.SubprojectProvider; 72 import org.openide.ErrorManager; 73 import org.openide.filesystems.FileObject; 74 import org.openide.filesystems.URLMapper; 75 76 public class Util { 77 78 public enum ProjectType { 79 WEB, EJB, CLIENT, UNKNOWN 80 }; 81 82 private static final ErrorManager err = 83 ErrorManager.getDefault().getInstance("org.netbeans.modules.websvc.wsitconf"); 85 88 public static void changeLabelInComponent(JComponent component, String oldLabel, String newLabel) { 89 JLabel label = findLabel(component, oldLabel); 90 if(label != null) { 91 label.setText(newLabel); 92 } 93 } 94 95 98 public static void hideLabelAndLabelFor(JComponent component, String lab) { 99 JLabel label = findLabel(component, lab); 100 if(label != null) { 101 label.setVisible(false); 102 Component c = label.getLabelFor(); 103 if(c != null) { 104 c.setVisible(false); 105 } 106 } 107 } 108 109 112 public static void getAllComponents( Component [] components, Collection allComponents ) { 113 for( int i = 0; i < components.length; i++ ) { 114 if( components[i] != null ) { 115 allComponents.add( components[i] ); 116 if( ( ( Container )components[i] ).getComponentCount() != 0 ) { 117 getAllComponents( ( ( Container )components[i] ).getComponents(), allComponents ); 118 } 119 } 120 } 121 } 122 123 126 public static JLabel findLabel(JComponent comp, String labelText) { 127 Vector allComponents = new Vector (); 128 getAllComponents(comp.getComponents(), allComponents); 129 Iterator iterator = allComponents.iterator(); 130 while(iterator.hasNext()) { 131 Component c = (Component )iterator.next(); 132 if(c instanceof JLabel ) { 133 JLabel label = (JLabel )c; 134 if(label.getText().equals(labelText)) { 135 return label; 136 } 137 } 138 } 139 return null; 140 } 141 142 149 public static SourceGroup[] getJavaSourceGroups(Project project) { 150 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 151 JavaProjectConstants.SOURCES_TYPE_JAVA); 152 Set testGroups = getTestSourceGroups(sourceGroups); 153 List result = new ArrayList (); 154 for (int i = 0; i < sourceGroups.length; i++) { 155 if (!testGroups.contains(sourceGroups[i])) { 156 result.add(sourceGroups[i]); 157 } 158 } 159 return (SourceGroup[]) result.toArray(new SourceGroup[result.size()]); 160 } 161 162 private static Set getTestSourceGroups(SourceGroup[] sourceGroups) { 163 Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups); 164 Set testGroups = new HashSet (); 165 for (int i = 0; i < sourceGroups.length; i++) { 166 testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap)); 167 } 168 return testGroups; 169 } 170 171 private static Map createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) { 172 Map result; 173 if (sourceGroups.length == 0) { 174 result = Collections.EMPTY_MAP; 175 } else { 176 result = new HashMap (2 * sourceGroups.length, .5f); 177 for (int i = 0; i < sourceGroups.length; i++) { 178 SourceGroup sourceGroup = sourceGroups[i]; 179 result.put(sourceGroup.getRootFolder(), sourceGroup); 180 } 181 } 182 return result; 183 } 184 185 private static List getFileObjects(URL [] urls) { 186 List result = new ArrayList (); 187 for (int i = 0; i < urls.length; i++) { 188 FileObject sourceRoot = URLMapper.findFileObject(urls[i]); 189 if (sourceRoot != null) { 190 result.add(sourceRoot); 191 } else { 192 int severity = ErrorManager.INFORMATIONAL; 193 if (ErrorManager.getDefault().isNotifiable(severity)) { 194 ErrorManager.getDefault().notify(severity, new IllegalStateException ( 195 "No FileObject found for the following URL: " + urls[i])); } 197 } 198 } 199 return result; 200 } 201 202 private static List getTestTargets(SourceGroup sourceGroup, Map foldersToSourceGroupsMap) { 203 final URL [] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); 204 if (rootURLs.length == 0) { 205 return new ArrayList (); 206 } 207 List result = new ArrayList (); 208 List sourceRoots = getFileObjects(rootURLs); 209 for (int i = 0; i < sourceRoots.size(); i++) { 210 FileObject sourceRoot = (FileObject) sourceRoots.get(i); 211 SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap.get(sourceRoot); 212 if (srcGroup != null) { 213 result.add(srcGroup); 214 } 215 } 216 return result; 217 } 218 219 221 public static boolean isPositiveNumber(String s, boolean zeroAllowed, boolean allowEmptyValue) { 222 Integer i = null; 223 if ((s == null) || ("".equals(s))) { 224 return allowEmptyValue ? true : false; 225 } 226 try { 227 i = Integer.parseInt(s); 228 if (i != null) { 229 if (zeroAllowed) { 230 return i.intValue() >= 0; 231 } 232 return i.intValue() > 0; 233 } 234 } catch (NumberFormatException nfe) {} 235 return false; 236 } 237 238 257 258 public static Collection getRelevantWSModules(FileObject fo) { 259 260 Project affectedProject = FileOwnerQuery.getOwner(fo); 261 Collection wsmodules = new ArrayList (); 262 Collection projects = new ArrayList (); 263 264 if (affectedProject != null) { 265 JAXWSSupport wsmod = JAXWSSupport.getJAXWSSupport(affectedProject.getProjectDirectory()); 267 if (wsmod != null) { 268 projects.add(affectedProject); 269 } 270 271 272 projects.add(affectedProject);for (Project project : OpenProjects.getDefault().getOpenProjects()){ 273 274 Object isJ2eeApp = project.getLookup().lookup(J2eeAppProvider.class); 275 if (isJ2eeApp != null) { 276 J2eeAppProvider j2eeApp = (J2eeAppProvider)isJ2eeApp; 277 J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders(); 278 279 if (j2eeModules != null) { 280 J2eeModuleProvider affectedPrjProvider = 281 (J2eeModuleProvider)affectedProject.getLookup().lookup(J2eeModuleProvider.class); 282 if (affectedPrjProvider != null) { 283 if (Arrays.asList(j2eeModules).contains(affectedPrjProvider)) { 284 for (int i=0; i<j2eeModules.length; i++) { 285 FileObject[] sourceRoots = j2eeModules[i].getSourceRoots(); 286 if (sourceRoots != null && sourceRoots.length > 0){ 287 FileObject srcRoot = sourceRoots[0]; 288 Project p = FileOwnerQuery.getOwner(srcRoot); 289 if ((p != null) && (!projects.contains(p))) { 290 projects.add(p); 291 } 292 } 293 } 294 } 295 } 296 } 297 Object obj = project.getLookup().lookup(SubprojectProvider.class); 298 if ((obj != null) && (obj instanceof SubprojectProvider)) { 299 Set subprojects = ((SubprojectProvider)obj).getSubprojects(); 300 if (subprojects.contains(affectedProject)) { 301 JAXWSSupport ws = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory()); 302 if (ws != null) { 303 if (!projects.contains(project)) { projects.add(project); 305 } 306 } 307 } 308 } 309 } 310 } 311 } 312 313 for (int j=0; j < projects.size(); j++) { 314 Project prj = (Project)((ArrayList )projects).get(j); 315 JAXWSSupport websvc = JAXWSSupport.getJAXWSSupport(prj.getProjectDirectory()); 316 wsmodules.add(websvc); 317 } 318 319 err.log("Affected ws modules: " + wsmodules); 320 return wsmodules; 321 } 322 323 public static Enumeration <String > getAliases(String storePath, char[] password, String type) throws IOException { 324 if ((storePath == null) || (type == null)) return null; 325 FileInputStream iStream; 326 try { 327 File f = new File (storePath); 328 if ((f == null) || (!f.exists())) { 329 throw new IOException (); 330 } 331 iStream = new FileInputStream (new File (storePath)); 332 java.security.KeyStore keyStore; 333 keyStore = java.security.KeyStore.getInstance(type); 334 keyStore.load(iStream, password); 335 return keyStore.aliases(); 336 } catch (FileNotFoundException ex) { 337 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 338 } catch (KeyStoreException ex) { 339 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 340 } catch (NoSuchAlgorithmException ex) { 341 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 342 } catch (CertificateException ex) { 343 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 344 } 345 return null; 346 } 347 348 public static final boolean isWsitSupported(Project p) { 349 350 SourceGroup[] sgs = ProjectUtils.getSources(p).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 352 ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE); 353 FileObject wsimportFO = classPath.findResource("com/sun/xml/ws/policy/Policy.class"); 355 if (wsimportFO != null) { 356 return true; 357 } 358 359 J2eeModuleProvider mp = (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class); 360 if (mp != null) { 361 String sID = mp.getServerInstanceID(); 362 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID); 363 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIT); } else { 365 return true; 366 } 367 } 368 369 public static final boolean isTomcat(Project project) { 370 if (project != null) { 371 J2eeModuleProvider mp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 372 if (mp != null) { 373 String id = mp.getServerID(); 374 String instid = mp.getServerInstanceID(); 375 if ((instid != null) && (instid.toLowerCase().contains("tomcat"))) { return true; 377 } 378 } 379 } 380 return false; 381 } 382 383 public static final boolean isWebProject(Project project) { 384 if (getProjectType(project) == ProjectType.WEB) { 385 return true; 386 } 387 return false; 388 } 389 390 public static final ProjectType getProjectType(Project project) { 391 ProjectType pt = ProjectType.UNKNOWN; 392 if (project != null) { 393 WebModule wm = WebModule.getWebModule(project.getProjectDirectory()); 394 EjbJar em = EjbJar.getEjbJar(project.getProjectDirectory()); 395 if (wm != null) { 396 pt = ProjectType.WEB; 397 } else if (em != null) { 398 pt = ProjectType.EJB; 399 } 400 } 401 return pt; 402 } 403 404 private static boolean isOperationInList(String operName, Collection <BindingOperation> operations) { 405 Iterator <BindingOperation> i = operations.iterator(); 406 while (i.hasNext()) { 407 BindingOperation bo = i.next(); 408 if ((bo != null) && (operName.equals(bo.getName()))) { 409 return true; 410 } 411 } 412 return false; 413 } 414 415 public static Collection <BindingOperation> refreshOperations(Binding binding, FileObject jc) { 416 417 Collection <BindingOperation> operations = binding.getBindingOperations(); 418 if ((binding == null) || (jc == null)) { 419 return operations; 420 } 421 422 WSDLModel model = binding.getModel(); 423 boolean isTransaction = model.isIntransaction(); 424 if (!isTransaction) { 425 model.startTransaction(); 426 } 427 428 WSDLComponentFactory wcf = model.getFactory(); 429 Definitions d = (Definitions) binding.getParent(); 430 431 QName portTypeQName = binding.getType().getQName(); 432 PortType portType = null; 433 434 Collection <PortType> portTypes = d.getPortTypes(); 435 Iterator <PortType> i = portTypes.iterator(); 436 while (i.hasNext()) { 437 PortType pt = i.next(); 438 if (pt != null) { 439 if (portTypeQName.getLocalPart().equals(pt.getName())) { 440 portType = pt; 441 break; 442 } 443 } 444 } 445 List <String > bindingOperationNames = JavaWsdlMapper.getOperationNames(jc); 447 for (String name : bindingOperationNames) { 448 if (!isOperationInList(name, operations)) { 449 org.netbeans.modules.xml.wsdl.model.BindingOperation bindingOperation = wcf.createBindingOperation(); 450 bindingOperation.setName(name); 451 binding.addBindingOperation(bindingOperation); 452 453 org.netbeans.modules.xml.wsdl.model.Message inputMsg = wcf.createMessage(); 455 inputMsg.setName(name); 456 d.addMessage(inputMsg); 457 458 org.netbeans.modules.xml.wsdl.model.Message outMsg = wcf.createMessage(); 459 outMsg.setName(name + "Response"); d.addMessage(outMsg); 461 462 org.netbeans.modules.xml.wsdl.model.RequestResponseOperation oper = wcf.createRequestResponseOperation(); 463 oper.setName(name); 464 portType.addOperation(oper); 465 466 org.netbeans.modules.xml.wsdl.model.Input input = wcf.createInput(); 467 oper.setInput(input); 468 input.setMessage(input.createReferenceTo(inputMsg, org.netbeans.modules.xml.wsdl.model.Message.class)); 469 470 org.netbeans.modules.xml.wsdl.model.Output out = wcf.createOutput(); 471 oper.setOutput(out); 472 out.setMessage(out.createReferenceTo(outMsg, org.netbeans.modules.xml.wsdl.model.Message.class)); 473 474 org.netbeans.modules.xml.wsdl.model.BindingOutput bindingOutput = wcf.createBindingOutput(); 475 bindingOperation.setBindingOutput(bindingOutput); 476 org.netbeans.modules.xml.wsdl.model.BindingInput bindingInput = wcf.createBindingInput(); 477 bindingOperation.setBindingInput(bindingInput); 478 479 List <String > operationFaults = JavaWsdlMapper.getOperationFaults(jc, name); 481 for (String fault : operationFaults) { 482 org.netbeans.modules.xml.wsdl.model.BindingFault bindingFault = wcf.createBindingFault(); 483 bindingOperation.addBindingFault(bindingFault); 484 } 485 } 486 } 487 488 if (!isTransaction) { 489 model.endTransaction(); 490 } 491 492 return binding.getBindingOperations(); 493 } 494 495 } 496 | Popular Tags |