KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > HttpProxyUpdater


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 package org.netbeans.modules.j2ee.sun.ide.j2ee;
20
21 import java.net.Socket JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 import javax.management.Attribute JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28
29 import org.netbeans.modules.j2ee.sun.api.ServerInterface;
30 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
31 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
32 import org.netbeans.modules.j2ee.sun.ide.j2ee.mbmapping.JvmOptions;
33
34 /**
35  * @author edwingo
36  *
37  * Manipulate the app server http proxy options.
38  * Requires app server to be running before this class can be used.
39  */

40 public class HttpProxyUpdater {
41     public static final String JavaDoc HTTP_PROXY_HOST = "-Dhttp.proxyHost=";
42     public static final String JavaDoc HTTP_PROXY_PORT = "-Dhttp.proxyPort=";
43     public static final String JavaDoc HTTPS_PROXY_HOST = "-Dhttps.proxyHost=";
44     public static final String JavaDoc HTTPS_PROXY_PORT = "-Dhttps.proxyPort=";
45     public static final String JavaDoc HTTP_PROXY_NO_HOST = "-Dhttp.nonProxyHosts=";
46
47     private String JavaDoc httpProxyPort;
48     private String JavaDoc httpProxyHost;
49     private String JavaDoc httpsProxyPort;
50     private String JavaDoc httpsProxyHost;
51     private String JavaDoc httpProxyNoHost;
52     private static String JavaDoc JVM_OPTIONS = "jvm-options";
53     private ObjectName JavaDoc jvmOptionsObjectName;
54
55     private ServerInterface si;
56     private String JavaDoc[] options;
57     private boolean serverRunning;
58     
59     ResourceBundle JavaDoc bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.j2ee.Bundle"); // NOI18N
60
/**
61      * Requires app server to be running before this class can be used.
62      *
63      * @param si
64      * @throws Exception if http proxy options cannot be accessed.
65      */

66     public HttpProxyUpdater(ServerInterface si, boolean serverRunning) {
67         this.serverRunning = serverRunning;
68         this.si = si;
69     }
70
71     /*
72      * Get list of proxy settings form JVM Options if any
73      *
74      */

75     private void getOptionsFromServer() throws Exception JavaDoc {
76         jvmOptionsObjectName = new JvmOptions(si.getMBeanServerConnection()).getConfigObjectName();
77         options =
78             (String JavaDoc[]) si.getAttribute(jvmOptionsObjectName, JVM_OPTIONS);
79
80         initializeProps();
81     }
82
83     private void setOptionsToServer() throws Exception JavaDoc {
84         jvmOptionsObjectName = new JvmOptions(si.getMBeanServerConnection()).getConfigObjectName();
85         Attribute JavaDoc newOptions = new Attribute JavaDoc(JVM_OPTIONS, options);
86         si.setAttribute(jvmOptionsObjectName, newOptions);
87     }
88     
89     private void getOptionsFromXml() {
90         DomainEditor dEditor = new DomainEditor(si.getDeploymentManager());
91         options = dEditor.getHttpProxyOptions();
92         initializeProps();
93     }
94     
95     private void setOptionsToXml() {
96         DomainEditor dEditor = new DomainEditor(si.getDeploymentManager());
97         dEditor.setHttpProxyOptions(options);
98     }
99
100     /**
101      * @param prefix
102      * @return true iff anything changed
103      */

104     private boolean removeProperty(String JavaDoc prefix) {
105         ArrayList JavaDoc al = new ArrayList JavaDoc();
106         for (int i = 0; i < options.length; i++) {
107             String JavaDoc option = options[i].trim();
108             if (!option.startsWith(prefix)) {
109                 al.add(options[i]);
110             }
111         }
112
113         boolean changed = options.length != al.size();
114         if (changed) {
115             options = (String JavaDoc[]) al.toArray(new String JavaDoc[al.size()]);
116         }
117         return changed;
118     }
119
120     /**
121      * @param prefix
122      * @param value
123      * @return true iff anything changed
124      */

125     private boolean setProperty(String JavaDoc prefix, String JavaDoc value) {
126         if (value == null) {
127             return removeProperty(prefix);
128         }
129
130         String JavaDoc newValue = prefix + value;
131         
132         // Replace any existing properties
133
boolean found = false;
134         for (int i = 0; i < options.length; i++) {
135             String JavaDoc option = options[i].trim();
136             if (option.startsWith(prefix)) {
137                 options[i] = newValue;
138                 found = true;
139                 break;
140             }
141         }
142         if (found) {
143             return true;
144         }
145
146         // Not found so add a new property to the list
147
String JavaDoc[] newOptions = new String JavaDoc[options.length + 1];
148         System.arraycopy(options, 0, newOptions, 0, options.length);
149         newOptions[options.length] = newValue;
150         options = newOptions;
151         return true;
152     }
153
154     private boolean removeProxy() {
155         boolean changed = removeProperty(HTTP_PROXY_HOST);
156         changed |= removeProperty(HTTP_PROXY_PORT);
157         changed |= removeProperty(HTTPS_PROXY_HOST);
158         changed |= removeProperty(HTTPS_PROXY_PORT);
159         changed |= removeProperty(HTTP_PROXY_NO_HOST);
160         return changed;
161     }
162
163     /**
164      * @return true iff HTTP proxy settings were changed on app server
165      */

166     public void addHttpProxySettings() throws Exception JavaDoc {
167         // Get the current IDE proxy settings
168
String JavaDoc host = System.getProperty("http.proxyHost", "");
169         if (host.trim().length() == 0) {
170             host = null;
171         }
172         String JavaDoc port = System.getProperty("http.proxyPort", "");
173         if (port.trim().length() == 0) {
174             port = null;
175         }
176         
177         String JavaDoc nonHosts = System.getProperty("http.nonProxyHosts", ""); //NOI18N
178
if (nonHosts.trim().length() != 0) {
179             // the property might contain spaces like -Dhttp.nonProxyHosts= localhost localhost.czech.sun.com
180
nonHosts = nonHosts;
181         } else {
182             nonHosts = null;
183         }
184
185         if(this.serverRunning){
186             try{
187                 getOptionsFromServer();
188                 if (host == null || port == null || nonHosts == null) {
189                     removeOptionsFromServer();
190                     return;
191                 }
192             }catch(Exception JavaDoc ex){
193                 throw new Exception JavaDoc(bundle.getString("Err_CannotUpdateProxy"));
194             }
195             checkProxyInfo(host, port);
196             if (updateOptions(host, port, nonHosts)) {
197                 try{
198                     setOptionsToServer();
199                 }catch(Exception JavaDoc ex){
200                     String JavaDoc message = MessageFormat.format(bundle.getString("Err_InvalidProxyInfo"), new Object JavaDoc[]{host, port});
201                     throw new Exception JavaDoc(message);
202                 }
203             } // end of if (changed)
204
}else{
205             //Manipulate domain.xml since server is stopped
206
getOptionsFromXml();
207             if (host == null || port == null || nonHosts == null) {
208                 removeOptionsFromXml();
209                 return;
210             }
211             checkProxyInfo(host, port);
212             if (updateOptions(host, port, nonHosts)) {
213                 setOptionsToXml();
214             } // end of if (changed)
215
}
216     }
217     
218      /*
219      * Remove Proxy setting from the appserver if present
220      *
221      */

222     public void removeHttpProxySettings() throws Exception JavaDoc {
223         if(this.serverRunning){
224             getOptionsFromServer();
225             removeOptionsFromServer();
226         }else{
227             getOptionsFromXml();
228             removeOptionsFromXml();
229         }
230     }
231     
232     private void removeOptionsFromXml() {
233         if (httpProxyHost != null || httpProxyPort != null || httpProxyNoHost != null ||
234                 httpsProxyHost != null || httpsProxyPort != null) {
235             if (removeProxy()) {
236                 setOptionsToXml();
237             }
238         }
239     }
240     
241     private void removeOptionsFromServer() throws Exception JavaDoc {
242         try{
243             if (httpProxyHost != null || httpProxyPort != null || httpProxyNoHost != null ||
244                     httpsProxyHost != null || httpsProxyPort != null) {
245                 if (removeProxy()) {
246                     try{
247                         setOptionsToServer();
248                     }catch(Exception JavaDoc ex){
249                         //Should not come here
250
}
251                 }
252             }
253         }catch(Exception JavaDoc ex){
254             //Should not come here
255
}
256     }
257
258     private boolean updateOptions(String JavaDoc host, String JavaDoc port, String JavaDoc nonHosts) {
259         boolean changed = false;
260         
261         if (!host.equals(httpProxyHost)) {
262             setProperty(HTTP_PROXY_HOST, host);
263             changed = true;
264         } // end of if (!host.equals(httpProxyHost))
265

266         if (!port.equals(httpProxyPort)) {
267             setProperty(HTTP_PROXY_PORT, port);
268             changed = true;
269         } // end of if (!port.equals(httpProxyPort))
270

271         if (!host.equals(httpsProxyHost)) {
272             setProperty(HTTPS_PROXY_HOST, host);
273             changed = true;
274         } // end of if (!host.equals(httpsProxyHost))
275

276         if (!port.equals(httpsProxyPort)) {
277             setProperty(HTTPS_PROXY_PORT, port);
278             changed = true;
279         } // end of if (!port.equals(httpsProxyPort))
280

281         SunDeploymentManagerInterface sdm = (SunDeploymentManagerInterface)si.getDeploymentManager();
282         if(ServerLocationManager.isGlassFish(sdm.getPlatformRoot())){
283             if (!nonHosts.equals(httpProxyNoHost)) {
284                 setProperty(HTTP_PROXY_NO_HOST, nonHosts);
285                 changed = true;
286             } // end of if (!nonHosts.equals(httpProxyNoHost))
287
}
288         
289         return changed;
290     }
291     
292     private void initializeProps(){
293         // Extract the HTTP proxy properties
294
for (int i = 0; i < options.length; i++) {
295             String JavaDoc option = options[i].trim();
296             if (option.startsWith(HTTP_PROXY_HOST)) {
297                 httpProxyHost = option.substring(HTTP_PROXY_HOST.length());
298             } else if (option.startsWith(HTTP_PROXY_PORT)) {
299                 httpProxyPort = option.substring(HTTP_PROXY_PORT.length());
300             } else if (option.startsWith(HTTPS_PROXY_HOST)) {
301                 httpsProxyHost = option.substring(HTTPS_PROXY_HOST.length());
302             } else if (option.startsWith(HTTPS_PROXY_PORT)) {
303                 httpsProxyPort = option.substring(HTTPS_PROXY_PORT.length());
304             } else if (option.startsWith(HTTP_PROXY_NO_HOST)) {
305                 httpProxyNoHost = option.substring(HTTP_PROXY_NO_HOST.length());
306             }
307         }
308     }
309     
310     private void checkProxyInfo(String JavaDoc host, String JavaDoc port) throws Exception JavaDoc {
311         try{
312             int portNo = Integer.parseInt(port);
313             Socket JavaDoc socket = new Socket JavaDoc(host, portNo);
314             return;
315         }catch(Exception JavaDoc ex){
316             String JavaDoc message = MessageFormat.format(bundle.getString("Err_InvalidProxyInfo"), new Object JavaDoc[]{host, port});
317             throw new Exception JavaDoc(message);
318         }
319     }
320 }
321
Popular Tags