KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > jaxws > J2SEProjectJAXWSClientSupport


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.java.j2seproject.jaxws;
21
22 import java.io.IOException JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.api.project.ProjectManager;
27 import org.netbeans.api.project.libraries.Library;
28 import org.netbeans.api.project.libraries.LibraryManager;
29 import org.netbeans.modules.java.j2seproject.J2SEProject;
30 import org.netbeans.modules.java.j2seproject.SourceRoots;
31 import org.netbeans.modules.java.j2seproject.UpdateHelper;
32 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
33 import org.netbeans.modules.websvc.spi.jaxws.client.ProjectJAXWSClientSupport;
34 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
35 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.openide.DialogDisplayer;
39 import org.openide.ErrorManager;
40 import org.openide.NotifyDescriptor;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.NbBundle;
44
45 /**
46  *
47  * @author mkuchtiak
48  */

49 public class J2SEProjectJAXWSClientSupport extends ProjectJAXWSClientSupport /*implements JAXWSClientSupportImpl*/ {
50     private J2SEProject project;
51     private UpdateHelper updateHelper;
52     private static final String JavaDoc WSDL_FOLDER = "wsdl"; //NOI18N
53
/** proxy host VM property key */
54     private static final String JavaDoc KEY_PROXY_HOST = "http.proxyHost"; // NOI18N
55
/** proxy port VM property key */
56     private static final String JavaDoc KEY_PROXY_PORT = "http.proxyPort"; // NOI18N
57
/** non proxy hosts VM property key */
58     private static final String JavaDoc KEY_NON_PROXY_HOSTS = "http.nonProxyHosts"; // NOI18N
59
/** https proxy host VM property key */
60     private static final String JavaDoc KEY_HTTPS_PROXY_HOST = "https.proxyHost"; // NOI18N
61
/** https proxy port VM property key */
62     private static final String JavaDoc KEY_HTTPS_PROXY_PORT = "https.proxyPort"; // NOI18N
63
/** non proxy hosts VM property key */
64     private static final String JavaDoc KEY_HTTPS_NON_PROXY_HOSTS = "https.nonProxyHosts"; // NOI18N
65

66     private static final String JavaDoc HTTP_PROXY_HOST_OPTION="-Dhttp.proxyHost"; //NOI18N
67
private static final String JavaDoc HTTP_PROXY_PORT_OPTION="-Dhttp.proxyPort"; //NOI18N
68
private static final String JavaDoc HTTP_NON_PROXY_HOSTS_OPTION="-Dhttp.nonProxyHosts"; //NOI18N
69
private static final String JavaDoc HTTPS_PROXY_HOST_OPTION="-Dhttps.proxyHost"; //NOI18N
70
private static final String JavaDoc HTTPS_PROXY_PORT_OPTION="-Dhttps.proxyPort"; //NOI18N
71
private static final String JavaDoc HTTPS_NON_PROXY_HOSTS_OPTION="-Dhttps.nonProxyHosts"; //NOI18N
72

73     /** Creates a new instance of J2SEProjectJAXWSClientSupport */
74     public J2SEProjectJAXWSClientSupport(J2SEProject project, UpdateHelper updateHelper) {
75         super(project);
76         this.project=project;
77         this.updateHelper=updateHelper;
78     }
79
80     public FileObject getWsdlFolder(boolean create) throws IOException JavaDoc {
81         EditableProperties ep = updateHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
82         String JavaDoc metaInfStr = ep.getProperty("meta.inf.dir"); //NOI18N
83
String JavaDoc wsdlFolderStr = metaInfStr + "/" + WSDL_FOLDER; // NOI18N
84
FileObject wsdlFolder = project.getProjectDirectory().getFileObject(wsdlFolderStr);
85         if (wsdlFolder == null && create) {
86             wsdlFolder = FileUtil.createFolder(project.getProjectDirectory(), wsdlFolderStr);
87         }
88         return wsdlFolder;
89     }
90
91     public String JavaDoc addServiceClient(String JavaDoc clientName, String JavaDoc wsdlUrl, String JavaDoc packageName, boolean isJsr109) {
92         
93         // add JAX-WS 2.0 library - now being called from the base class
94
//addJaxWs20Library();
95

96         // create jax-ws.xml if necessary
97
FileObject fo = project.findJaxWsFileObject();
98         if (fo==null) {
99             try {
100                 project.createJaxWsFileObject();
101             } catch (IOException JavaDoc ex) {
102                 ErrorManager.getDefault().notify(ex);
103             }
104         }
105         
106         // call the super.addServiceClient();
107
String JavaDoc serviceIdeName = super.addServiceClient(clientName, wsdlUrl, packageName, false);
108         
109         // add JVM Proxy Options to project's JVM
110
if (serviceIdeName!=null) addJVMProxyOptions(clientName);
111         
112         return serviceIdeName;
113     }
114     
115     protected void addJaxWs20Library() {
116         ClassPath classPath = null;
117         SourceRoots srcRoots = project.getSourceRoots();
118         if (srcRoots!=null && srcRoots.getRoots().length>0) {
119             FileObject srcRoot = srcRoots.getRoots()[0];
120             ClassPath compileClassPath = ClassPath.getClassPath(srcRoot,ClassPath.COMPILE);
121             ClassPath bootClassPath = ClassPath.getClassPath(srcRoot,ClassPath.BOOT);
122             classPath = ClassPathSupport.createProxyClassPath(new ClassPath[]{compileClassPath, bootClassPath});
123         }
124         FileObject webServiceClass=null;
125         if (classPath!=null) {
126             webServiceClass = classPath.findResource("javax/jws/WebService.class"); // NOI18N
127
}
128         if (webServiceClass==null) {
129             // add JAX-WS 2.0 if WsImport is not on classpath
130
ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class);
131             Library jaxwslib = LibraryManager.getDefault().getLibrary("jaxws20"); //NOI18N
132
if ((pce!=null) && (jaxwslib != null)) {
133                 try {
134                     pce.addLibrary(jaxwslib);
135                 } catch (IOException JavaDoc ex) {
136                     ErrorManager.getDefault().log(ex.getMessage());
137                 }
138                 FileObject wscompileFO=null;
139                 if (classPath!=null) {
140                     wscompileFO = classPath.findResource("com/sun/xml/rpc/tools/ant/Wscompile.class"); // NOI18N
141
}
142                 if (wscompileFO!=null) {
143                     NotifyDescriptor desc = new NotifyDescriptor.Message(
144                     NbBundle.getMessage(J2SEProjectJAXWSClientSupport.class,"MSG_RemoveJAX-RPC"), // NOI18N
145
NotifyDescriptor.WARNING_MESSAGE);
146                     DialogDisplayer.getDefault().notify(desc);
147                 }
148             }
149         }
150     }
151
152     private void addJVMProxyOptions(final String JavaDoc clientName) {
153         final String JavaDoc proxyHost = System.getProperty(KEY_PROXY_HOST);
154         if (proxyHost!=null && proxyHost.length()>0) {
155             ProjectManager.mutex().writeAccess ( new Runnable JavaDoc () {
156                 public void run() {
157                     boolean modif=false;
158                     String JavaDoc proxyPort = System.getProperty(KEY_PROXY_PORT);
159                     if (proxyPort==null || proxyPort.length()==0) proxyPort="8080"; //NOI18N
160
String JavaDoc localHosts ="";
161                     localHosts = getDefaultNonProxyHosts();
162                     
163                     EditableProperties ep = updateHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
164                     String JavaDoc jvmOptions = ep.getProperty(J2SEProjectProperties.RUN_JVM_ARGS);
165                     if (jvmOptions==null || jvmOptions.length()==0) {
166                         jvmOptions = HTTP_PROXY_HOST_OPTION+"="+proxyHost+ //NOI18N
167
" "+HTTP_PROXY_PORT_OPTION+"="+proxyPort+ //NOI18N
168
" "+HTTP_NON_PROXY_HOSTS_OPTION+"="+localHosts+ //NOI18N
169
" "+HTTPS_PROXY_HOST_OPTION+"="+proxyHost+ //NOI18N
170
" "+HTTPS_PROXY_PORT_OPTION+"="+proxyPort+ //NOI18N
171
" "+HTTPS_NON_PROXY_HOSTS_OPTION+"="+localHosts; //NOI18N
172
modif=true;
173                     } else {
174                         if (jvmOptions.indexOf(HTTP_PROXY_HOST_OPTION)<0) {
175                             jvmOptions+=" "+HTTP_PROXY_HOST_OPTION+"="+proxyHost; //NOI18N
176
modif=true;
177                         }
178                         if (jvmOptions.indexOf(HTTP_PROXY_PORT_OPTION)<0) {
179                             jvmOptions+=" "+HTTP_PROXY_PORT_OPTION+"="+proxyPort; //NOI18N
180
modif=true;
181                         }
182                         if (jvmOptions.indexOf(HTTP_NON_PROXY_HOSTS_OPTION)<0) {
183                             jvmOptions+=" "+HTTP_NON_PROXY_HOSTS_OPTION+"="+localHosts; //NOI18N
184
modif=true;
185                         }
186                         if (jvmOptions.indexOf(HTTPS_PROXY_HOST_OPTION)<0) {
187                             jvmOptions+=" "+HTTPS_PROXY_HOST_OPTION+"="+proxyHost; //NOI18N
188
modif=true;
189                         }
190                         if (jvmOptions.indexOf(HTTPS_PROXY_PORT_OPTION)<0) {
191                             jvmOptions+=" "+HTTPS_PROXY_PORT_OPTION+"="+proxyPort; //NOI18N
192
modif=true;
193                         }
194                         if (jvmOptions.indexOf(HTTPS_NON_PROXY_HOSTS_OPTION)<0) {
195                             jvmOptions+=" "+HTTPS_NON_PROXY_HOSTS_OPTION+"="+localHosts; //NOI18N
196
modif=true;
197                         }
198                     }
199                     if (modif) {
200                         ep.setProperty(J2SEProjectProperties.RUN_JVM_ARGS,jvmOptions);
201                         updateHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,ep);
202                         try {
203                             ProjectManager.getDefault().saveProject(project);
204                         } catch(IOException JavaDoc ex) {
205                             NotifyDescriptor desc = new NotifyDescriptor.Message(
206                             NbBundle.getMessage(J2SEProjectJAXWSClientSupport.class,"MSG_ErrorSavingOnWSClientAdd", clientName, ex.getLocalizedMessage()), // NOI18N
207
NotifyDescriptor.ERROR_MESSAGE);
208                             DialogDisplayer.getDefault().notify(desc);
209                     }
210                     }
211                 }
212             });
213
214         }
215     }
216     
217      /** Returns the default value for the http.nonProxyHosts system property. <br>
218      * PENDING: should be a user settable property
219      * @return sensible default for non-proxy hosts, including 'localhost'
220      */

221     private String JavaDoc getDefaultNonProxyHosts() {
222         String JavaDoc nonProxy = "localhost|127.0.0.1"; // NOI18N
223
String JavaDoc localhost = ""; // NOI18N
224
try {
225             localhost = InetAddress.getLocalHost().getHostName();
226             if (!localhost.equals("localhost")) { // NOI18N
227
nonProxy = nonProxy + "|" + localhost; // NOI18N
228
} else {
229                 // Avoid this error when hostname == localhost:
230
// Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
231
}
232         } catch (UnknownHostException JavaDoc e) {
233             // OK. Sometimes a hostname is assigned by DNS, but a computer
234
// is later pulled off the network. It may then produce a bogus
235
// name for itself which can't actually be resolved. Normally
236
// "localhost" is aliased to 127.0.0.1 anyway.
237
}
238         try {
239             String JavaDoc localhost2 = InetAddress.getLocalHost().getCanonicalHostName();
240             if (!localhost2.equals("localhost") && !localhost2.equals(localhost)) { // NOI18N
241
nonProxy = nonProxy + "|" + localhost2; // NOI18N
242
} else {
243                 // Avoid this error when hostname == localhost:
244
// Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
245
}
246         } catch (UnknownHostException JavaDoc e) {
247             // OK. Sometimes a hostname is assigned by DNS, but a computer
248
// is later pulled off the network. It may then produce a bogus
249
// name for itself which can't actually be resolved. Normally
250
// "localhost" is aliased to 127.0.0.1 anyway.
251
}
252         return nonProxy;
253     }
254
255     
256 }
257
Popular Tags