KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > util > Util


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.wsitconf.util;
21
22 import java.awt.Component JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import javax.swing.JLabel JavaDoc;
25 import java.awt.Container JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.security.KeyStoreException JavaDoc;
32 import java.security.NoSuchAlgorithmException JavaDoc;
33 import java.security.cert.CertificateException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import java.util.Vector JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.HashSet JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.Set JavaDoc;
46 import javax.xml.namespace.QName JavaDoc;
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.refactoring.api.Problem;
62
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"); // NOI18N
84

85     /*
86      * Changes the text of a JLabel in component from oldLabel to newLabel
87      */

88     public static void changeLabelInComponent(JComponent JavaDoc component, String JavaDoc oldLabel, String JavaDoc newLabel) {
89         JLabel JavaDoc label = findLabel(component, oldLabel);
90         if(label != null) {
91             label.setText(newLabel);
92         }
93     }
94     
95     /*
96      * Hides a JLabel and the component that it is designated to labelFor, if any
97      */

98     public static void hideLabelAndLabelFor(JComponent JavaDoc component, String JavaDoc lab) {
99         JLabel JavaDoc label = findLabel(component, lab);
100         if(label != null) {
101             label.setVisible(false);
102             Component JavaDoc c = label.getLabelFor();
103             if(c != null) {
104                 c.setVisible(false);
105             }
106         }
107     }
108     
109     /*
110      * Recursively gets all components in the components array and puts it in allComponents
111      */

112     public static void getAllComponents( Component JavaDoc[] components, Collection JavaDoc allComponents ) {
113         for( int i = 0; i < components.length; i++ ) {
114             if( components[i] != null ) {
115                 allComponents.add( components[i] );
116                 if( ( ( Container JavaDoc )components[i] ).getComponentCount() != 0 ) {
117                     getAllComponents( ( ( Container JavaDoc )components[i] ).getComponents(), allComponents );
118                 }
119             }
120         }
121     }
122     
123     /*
124      * Recursively finds a JLabel that has labelText in comp
125      */

126     public static JLabel JavaDoc findLabel(JComponent JavaDoc comp, String JavaDoc labelText) {
127         Vector JavaDoc allComponents = new Vector JavaDoc();
128         getAllComponents(comp.getComponents(), allComponents);
129         Iterator JavaDoc iterator = allComponents.iterator();
130         while(iterator.hasNext()) {
131             Component JavaDoc c = (Component JavaDoc)iterator.next();
132             if(c instanceof JLabel JavaDoc) {
133                 JLabel JavaDoc label = (JLabel JavaDoc)c;
134                 if(label.getText().equals(labelText)) {
135                     return label;
136                 }
137             }
138         }
139         return null;
140     }
141     
142     /**
143      * Returns Java source groups for all source packages in given project.<br>
144      * Doesn't include test packages.
145      *
146      * @param project Project to search
147      * @return Array of SourceGroup. It is empty if any probelm occurs.
148      */

149     public static SourceGroup[] getJavaSourceGroups(Project project) {
150         SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(
151                                     JavaProjectConstants.SOURCES_TYPE_JAVA);
152         Set JavaDoc testGroups = getTestSourceGroups(sourceGroups);
153         List JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc/*<SourceGroup>*/ getTestSourceGroups(SourceGroup[] sourceGroups) {
163         Map JavaDoc foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
164         Set JavaDoc testGroups = new HashSet JavaDoc();
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 JavaDoc createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) {
172         Map JavaDoc result;
173         if (sourceGroups.length == 0) {
174             result = Collections.EMPTY_MAP;
175         } else {
176             result = new HashMap JavaDoc(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 JavaDoc/*<FileObject>*/ getFileObjects(URL JavaDoc[] urls) {
186         List JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc(
195                        "No FileObject found for the following URL: " + urls[i])); //NOI18N
196
}
197             }
198         }
199         return result;
200     }
201     
202     private static List JavaDoc/*<SourceGroup>*/ getTestTargets(SourceGroup sourceGroup, Map JavaDoc foldersToSourceGroupsMap) {
203         final URL JavaDoc[] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder());
204         if (rootURLs.length == 0) {
205             return new ArrayList JavaDoc();
206         }
207         List JavaDoc result = new ArrayList JavaDoc();
208         List JavaDoc 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     /* Used to validate number inputs
220      */

221     public static boolean isPositiveNumber(String JavaDoc s, boolean zeroAllowed, boolean allowEmptyValue) {
222         Integer JavaDoc 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 JavaDoc nfe) {}
235         return false;
236     }
237
238 // public static void addProblemsToEnd(Problem[] where, Problem what) {
239
// where[0] = addProblemsToEnd(where[0], what);
240
// }
241
//
242
// public static Problem addProblemsToEnd(Problem where, Problem what) {
243
// if (where == null) {
244
// return what;
245
// }
246
// if (what != null) {
247
// Problem tail = where;
248
//
249
// while (tail.getNext() != null) {
250
// tail = tail.getNext();
251
// }
252
// tail.setNext(what);
253
// }
254
// return where;
255
// }
256

257     /** Finds all WS projects affected by the change of FileObject 'fo' */
258     public static Collection JavaDoc/*WebServicesSupport*/ getRelevantWSModules(FileObject fo) {
259         
260         Project affectedProject = FileOwnerQuery.getOwner(fo);
261         Collection JavaDoc wsmodules = new ArrayList JavaDoc();
262         Collection JavaDoc projects = new ArrayList JavaDoc();
263         
264         if (affectedProject != null) {
265             // first check if the project which directly contains fo is relevant
266
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 JavaDoc 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 JavaDoc obj = project.getLookup().lookup(SubprojectProvider.class);
298                     if ((obj != null) && (obj instanceof SubprojectProvider)) {
299                         Set JavaDoc 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)) { // include each project only once
304
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 JavaDoc)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 JavaDoc<String JavaDoc> getAliases(String JavaDoc storePath, char[] password, String JavaDoc type) throws IOException JavaDoc {
324         if ((storePath == null) || (type == null)) return null;
325         FileInputStream JavaDoc iStream;
326         try {
327             File JavaDoc f = new File JavaDoc(storePath);
328             if ((f == null) || (!f.exists())) {
329                 throw new IOException JavaDoc();
330             }
331             iStream = new FileInputStream JavaDoc(new File JavaDoc(storePath));
332             java.security.KeyStore JavaDoc keyStore;
333             keyStore = java.security.KeyStore.getInstance(type);
334             keyStore.load(iStream, password);
335             return keyStore.aliases();
336         } catch (FileNotFoundException JavaDoc ex) {
337             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
338         } catch (KeyStoreException JavaDoc ex) {
339             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
340         } catch (NoSuchAlgorithmException JavaDoc ex) {
341             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
342         } catch (CertificateException JavaDoc ex) {
343             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
344         }
345         return null;
346     }
347
348     public static final boolean isWsitSupported(Project p) {
349
350         // check if the wsimport class is already present - this means we don't need to add the library
351
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"); // NOI18N
354

355         if (wsimportFO != null) {
356             return true;
357         }
358         
359         J2eeModuleProvider mp = (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class);
360         if (mp != null) {
361             String JavaDoc sID = mp.getServerInstanceID();
362             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID);
363             return j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIT); //NOI18N
364
} 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 JavaDoc id = mp.getServerID();
374                 String JavaDoc instid = mp.getServerInstanceID();
375                 if ((instid != null) && (instid.toLowerCase().contains("tomcat"))) { //NOI18N
376
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 JavaDoc operName, Collection JavaDoc<BindingOperation> operations) {
405         Iterator JavaDoc<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 JavaDoc<BindingOperation> refreshOperations(Binding binding, FileObject jc) {
416         
417         Collection JavaDoc<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 JavaDoc portTypeQName = binding.getType().getQName();
432         PortType portType = null;
433         
434         Collection JavaDoc<PortType> portTypes = d.getPortTypes();
435         Iterator JavaDoc<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         // create operations and add them to the binding element
446
List JavaDoc<String JavaDoc> bindingOperationNames = JavaWsdlMapper.getOperationNames(jc);
447         for (String JavaDoc 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                 // add input/output messages
454
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"); //NOI18N
460
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                 //add faults
480
List JavaDoc<String JavaDoc> operationFaults = JavaWsdlMapper.getOperationFaults(jc, name);
481                 for (String JavaDoc 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