KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > client > wizard > JaxWsClientCreator


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.core.client.wizard;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.java.queries.UnitTestForSourceQuery;
34 import org.netbeans.api.progress.ProgressHandle;
35 import org.netbeans.api.progress.ProgressHandleFactory;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectUtils;
38 import org.netbeans.api.project.SourceGroup;
39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
41 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
42 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
43 import org.netbeans.modules.websvc.core.ClientWizardProperties;
44 import org.openide.DialogDisplayer;
45 import org.openide.ErrorManager;
46 import org.openide.NotifyDescriptor;
47 import org.openide.WizardDescriptor;
48 import org.openide.filesystems.FileObject;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.filesystems.URLMapper;
51 import org.openide.util.NbBundle;
52 import org.openide.util.RequestProcessor;
53 import org.netbeans.modules.j2ee.common.Util;
54 import org.openide.util.Task;
55 import org.netbeans.modules.websvc.core.ClientCreator;
56
57 /**
58  *
59  * @author Radko, Milan Kuchtiak
60  */

61 public class JaxWsClientCreator implements ClientCreator {
62     private Project project;
63     private WizardDescriptor wiz;
64     
65     private static final boolean DEBUG = false;
66     private static final int JSE_PROJECT_TYPE = 0;
67     private static final int WEB_PROJECT_TYPE = 1;
68     private static final int EJB_PROJECT_TYPE = 2;
69     private static final int CAR_PROJECT_TYPE = 3;
70     
71     /**
72      * Creates a new instance of WebServiceClientCreator
73      */

74     public JaxWsClientCreator(Project project, WizardDescriptor wiz) {
75         this.project = project;
76         this.wiz = wiz;
77     }
78         
79     public void createClient() throws IOException JavaDoc {
80         
81         final boolean isJsr109Supported = isJsr109Supported();
82         final boolean isJsr109OldSupported = isJsr109OldSupported();
83         final boolean isJWSDPSupported = isJWSDPSupported();
84         
85         // Use Progress API to display generator messages.
86
final ProgressHandle handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(JaxWsClientCreator.class, "MSG_WizCreateClient")); //NOI18N
87

88         task = new Task(new Runnable JavaDoc() {
89             public void run() {
90                 try {
91 // String jaxVersion = (String) wiz.getProperty(ClientWizardProperties.JAX_VERSION);
92
// if (jaxVersion.equals(WizardProperties.JAX_WS)) {
93
handle.start();
94                         generate15Client((isJsr109Supported || isJWSDPSupported), handle);
95 // } else {
96
// handle.start(100);
97
// generate14Client(handle);
98
// }
99
} catch (IOException JavaDoc exc) {
100                     //finish progress bar
101
handle.finish();
102                     
103                     ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc);
104                 }
105             }
106         });
107         RequestProcessor.getDefault().post(task);
108     }
109     
110     private void generate15Client(boolean isJsr109Platform, ProgressHandle handle) throws IOException JavaDoc {
111         
112         // !PW Get client support from project (from first page of wizard)
113
JAXWSClientSupport jaxWsClientSupport=null;
114         if(project != null) {
115             jaxWsClientSupport = JAXWSClientSupport.getJaxWsClientSupport(project.getProjectDirectory());
116         }
117         if(jaxWsClientSupport == null) {
118             // notify no client support
119
// String mes = MessageFormat.format (
120
// NbBundle.getMessage (WebServiceClientWizardIterator.class, "ERR_WebServiceClientSupportNotFound"),
121
// new Object [] {"Servlet Listener"}); //NOI18N
122
String JavaDoc mes = NbBundle.getMessage(WebServiceClientWizardIterator.class, "ERR_NoWebServiceClientSupport"); // NOI18N
123
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
124             DialogDisplayer.getDefault().notify(desc);
125         }
126         
127         String JavaDoc wsdlUrl = (String JavaDoc)wiz.getProperty(ClientWizardProperties.WSDL_DOWNLOAD_URL);
128         String JavaDoc filePath = (String JavaDoc)wiz.getProperty(ClientWizardProperties.WSDL_FILE_PATH);
129         //if (wsdlUrl==null) wsdlUrl = "file:"+(filePath.startsWith("/")?filePath:"/"+filePath); //NOI18N
130
if(wsdlUrl == null){
131             wsdlUrl = FileUtil.toFileObject(FileUtil.normalizeFile(new File JavaDoc(filePath))).getURL().toExternalForm();
132         }
133         String JavaDoc packageName = (String JavaDoc)wiz.getProperty(ClientWizardProperties.WSDL_PACKAGE_NAME);
134         if (packageName!=null && packageName.length()==0) packageName=null;
135         jaxWsClientSupport.addServiceClient(getWsdlName(wsdlUrl),wsdlUrl,packageName, isJsr109Platform);
136         
137         handle.finish();
138     }
139     
140     private String JavaDoc getWsdlName(String JavaDoc wsdlUrl) {
141         int ind = wsdlUrl.lastIndexOf("/"); //NOI18N
142
String JavaDoc wsdlName = ind>=0?wsdlUrl.substring(ind+1):wsdlUrl;
143         if (wsdlName.toUpperCase().endsWith("?WSDL")) wsdlName = wsdlName.substring(0,wsdlName.length()-5); //NOI18N
144
ind = wsdlName.lastIndexOf(".wsdl"); //NOI18N
145
if (ind>0) wsdlName = wsdlName.substring(0,ind);
146         // replace special characters with '_'
147
return convertAllSpecialChars(wsdlName);
148     }
149     
150     private String JavaDoc convertAllSpecialChars(String JavaDoc resultStr){
151         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(resultStr);
152         for(int i = 0; i < sb.length(); i++){
153             char c = sb.charAt(i);
154             if( Character.isLetterOrDigit(c) ||
155                     (c == '/') ||
156                     (c == '.') ||
157                     (c == '_') ||
158                     (c == ' ') ||
159                     (c == '-')){
160                 continue;
161             }else{
162                 sb.setCharAt(i, '_');
163             }
164         }
165         return sb.toString();
166     }
167     
168     /**
169      * Returns Java source groups for all source packages in given project.<br>
170      * Doesn't include test packages.
171      *
172      * @param project Project to search
173      * @return Array of SourceGroup. It is empty if any probelm occurs.
174      */

175     static SourceGroup[] getJavaSourceGroups(Project project) {
176         SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(
177                 JavaProjectConstants.SOURCES_TYPE_JAVA);
178         Set JavaDoc<SourceGroup> testGroups = getTestSourceGroups(project, sourceGroups);
179         List JavaDoc<SourceGroup> result = new ArrayList JavaDoc<SourceGroup>();
180         for (int i = 0; i < sourceGroups.length; i++) {
181             if (!testGroups.contains(sourceGroups[i])) {
182                 result.add(sourceGroups[i]);
183             }
184         }
185         return result.<SourceGroup>toArray(new SourceGroup[result.size()]);
186     }
187     
188     private static Set JavaDoc<SourceGroup> getTestSourceGroups(Project project, SourceGroup[] sourceGroups) {
189         Map JavaDoc<FileObject, SourceGroup> foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
190         Set JavaDoc<SourceGroup> testGroups = new HashSet JavaDoc<SourceGroup>();
191         for (int i = 0; i < sourceGroups.length; i++) {
192             testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap));
193         }
194         return testGroups;
195     }
196     
197     private static List JavaDoc<SourceGroup> getTestTargets(SourceGroup sourceGroup, Map JavaDoc<FileObject, SourceGroup> foldersToSourceGroupsMap) {
198         final URL JavaDoc[] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder());
199         if (rootURLs.length == 0) {
200             return new ArrayList JavaDoc<SourceGroup>();
201         }
202         List JavaDoc<SourceGroup> result = new ArrayList JavaDoc<SourceGroup>();
203         List JavaDoc<FileObject> sourceRoots = getFileObjects(rootURLs);
204         for (int i = 0; i < sourceRoots.size(); i++) {
205             FileObject sourceRoot = sourceRoots.get(i);
206             SourceGroup srcGroup = foldersToSourceGroupsMap.get(sourceRoot);
207             if (srcGroup != null) {
208                 result.add(srcGroup);
209             }
210         }
211         return result;
212     }
213     
214     private static Map JavaDoc<FileObject, SourceGroup> createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) {
215         Map JavaDoc<FileObject, SourceGroup> result;
216         if (sourceGroups.length == 0) {
217             result = Collections.<FileObject, SourceGroup>emptyMap();
218         } else {
219             result = new HashMap JavaDoc<FileObject, SourceGroup>(2 * sourceGroups.length, .5f);
220             for (int i = 0; i < sourceGroups.length; i++) {
221                 SourceGroup sourceGroup = sourceGroups[i];
222                 result.put(sourceGroup.getRootFolder(), sourceGroup);
223             }
224         }
225         return result;
226     }
227     
228     private static List JavaDoc<FileObject> getFileObjects(URL JavaDoc[] urls) {
229         List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
230         for (int i = 0; i < urls.length; i++) {
231             FileObject sourceRoot = URLMapper.findFileObject(urls[i]);
232             if (sourceRoot != null) {
233                 result.add(sourceRoot);
234             } else {
235                 int severity = ErrorManager.INFORMATIONAL;
236                 if (ErrorManager.getDefault().isNotifiable(severity)) {
237                     ErrorManager.getDefault().notify(severity, new IllegalStateException JavaDoc(
238                             "No FileObject found for the following URL: " + urls[i])); //NOI18N
239
}
240             }
241         }
242         return result;
243     }
244     
245     private J2eePlatform getJ2eePlatform(){
246         J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
247         if(provider != null){
248             String JavaDoc serverInstanceID = provider.getServerInstanceID();
249             if(serverInstanceID != null && serverInstanceID.length() > 0) {
250                 return Deployment.getDefault().getJ2eePlatform(serverInstanceID);
251             }
252         }
253         return null;
254     }
255     
256     private boolean isJWSDPSupported(){
257         J2eePlatform j2eePlatform = getJ2eePlatform();
258         if(j2eePlatform != null){
259             return j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP);
260         }
261         return false;
262     }
263     
264     private boolean isJsr109Supported(){
265         J2eePlatform j2eePlatform = getJ2eePlatform();
266         if(j2eePlatform != null){
267             return j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109);
268         }
269         return false;
270     }
271     
272     private boolean isJsr109OldSupported(){
273         J2eePlatform j2eePlatform = getJ2eePlatform();
274         if(j2eePlatform != null){
275             return j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE);
276         }
277         return false;
278     }
279
280     /**
281       *
282       * <b>DON'T USE</b>, for tests only
283       */

284     Task task;
285 }
286
Popular Tags