1 19 package org.netbeans.modules.j2ee.sun.ide.j2ee; 20 21 import java.net.Socket ; 22 import java.text.MessageFormat ; 23 import java.util.ArrayList ; 24 import java.util.ResourceBundle ; 25 26 import javax.management.Attribute ; 27 import javax.management.ObjectName ; 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 40 public class HttpProxyUpdater { 41 public static final String HTTP_PROXY_HOST = "-Dhttp.proxyHost="; 42 public static final String HTTP_PROXY_PORT = "-Dhttp.proxyPort="; 43 public static final String HTTPS_PROXY_HOST = "-Dhttps.proxyHost="; 44 public static final String HTTPS_PROXY_PORT = "-Dhttps.proxyPort="; 45 public static final String HTTP_PROXY_NO_HOST = "-Dhttp.nonProxyHosts="; 46 47 private String httpProxyPort; 48 private String httpProxyHost; 49 private String httpsProxyPort; 50 private String httpsProxyHost; 51 private String httpProxyNoHost; 52 private static String JVM_OPTIONS = "jvm-options"; 53 private ObjectName jvmOptionsObjectName; 54 55 private ServerInterface si; 56 private String [] options; 57 private boolean serverRunning; 58 59 ResourceBundle bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.j2ee.Bundle"); 66 public HttpProxyUpdater(ServerInterface si, boolean serverRunning) { 67 this.serverRunning = serverRunning; 68 this.si = si; 69 } 70 71 75 private void getOptionsFromServer() throws Exception { 76 jvmOptionsObjectName = new JvmOptions(si.getMBeanServerConnection()).getConfigObjectName(); 77 options = 78 (String []) si.getAttribute(jvmOptionsObjectName, JVM_OPTIONS); 79 80 initializeProps(); 81 } 82 83 private void setOptionsToServer() throws Exception { 84 jvmOptionsObjectName = new JvmOptions(si.getMBeanServerConnection()).getConfigObjectName(); 85 Attribute newOptions = new Attribute (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 104 private boolean removeProperty(String prefix) { 105 ArrayList al = new ArrayList (); 106 for (int i = 0; i < options.length; i++) { 107 String 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 []) al.toArray(new String [al.size()]); 116 } 117 return changed; 118 } 119 120 125 private boolean setProperty(String prefix, String value) { 126 if (value == null) { 127 return removeProperty(prefix); 128 } 129 130 String newValue = prefix + value; 131 132 boolean found = false; 134 for (int i = 0; i < options.length; i++) { 135 String 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 String [] newOptions = new String [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 166 public void addHttpProxySettings() throws Exception { 167 String host = System.getProperty("http.proxyHost", ""); 169 if (host.trim().length() == 0) { 170 host = null; 171 } 172 String port = System.getProperty("http.proxyPort", ""); 173 if (port.trim().length() == 0) { 174 port = null; 175 } 176 177 String nonHosts = System.getProperty("http.nonProxyHosts", ""); if (nonHosts.trim().length() != 0) { 179 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 ex){ 193 throw new Exception (bundle.getString("Err_CannotUpdateProxy")); 194 } 195 checkProxyInfo(host, port); 196 if (updateOptions(host, port, nonHosts)) { 197 try{ 198 setOptionsToServer(); 199 }catch(Exception ex){ 200 String message = MessageFormat.format(bundle.getString("Err_InvalidProxyInfo"), new Object []{host, port}); 201 throw new Exception (message); 202 } 203 } }else{ 205 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 } } 216 } 217 218 222 public void removeHttpProxySettings() throws Exception { 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 { 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 ex){ 249 } 251 } 252 } 253 }catch(Exception ex){ 254 } 256 } 257 258 private boolean updateOptions(String host, String port, String nonHosts) { 259 boolean changed = false; 260 261 if (!host.equals(httpProxyHost)) { 262 setProperty(HTTP_PROXY_HOST, host); 263 changed = true; 264 } 266 if (!port.equals(httpProxyPort)) { 267 setProperty(HTTP_PROXY_PORT, port); 268 changed = true; 269 } 271 if (!host.equals(httpsProxyHost)) { 272 setProperty(HTTPS_PROXY_HOST, host); 273 changed = true; 274 } 276 if (!port.equals(httpsProxyPort)) { 277 setProperty(HTTPS_PROXY_PORT, port); 278 changed = true; 279 } 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 } } 288 289 return changed; 290 } 291 292 private void initializeProps(){ 293 for (int i = 0; i < options.length; i++) { 295 String 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 host, String port) throws Exception { 311 try{ 312 int portNo = Integer.parseInt(port); 313 Socket socket = new Socket (host, portNo); 314 return; 315 }catch(Exception ex){ 316 String message = MessageFormat.format(bundle.getString("Err_InvalidProxyInfo"), new Object []{host, port}); 317 throw new Exception (message); 318 } 319 } 320 } 321 | Popular Tags |