1 19 20 package org.netbeans.modules.tomcat5.ide; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.net.URL ; 30 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 31 import org.netbeans.modules.j2ee.dd.api.web.Filter; 32 import org.netbeans.modules.j2ee.dd.api.web.FilterMapping; 33 import org.netbeans.modules.j2ee.dd.api.common.InitParam; 34 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 35 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 36 import org.netbeans.modules.tomcat5.TomcatManager; 37 import org.openide.modules.ModuleInfo; 38 import org.openide.util.Lookup; 39 import org.openide.util.LookupListener; 40 import org.openide.util.LookupEvent; 41 42 import org.openide.ErrorManager; 43 import org.openide.modules.InstalledFileLocator; 44 import org.xml.sax.SAXException ; 45 46 import org.netbeans.modules.schema2beans.Common; 47 import org.netbeans.modules.schema2beans.BaseBean; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileSystem; 50 import org.openide.filesystems.Repository; 51 import org.openide.filesystems.URLMapper; 52 53 57 public class MonitorSupport { 58 59 public static final String MONITOR_ENABLED_PROPERTY_NAME = "monitor_enabled"; private static final String MONITOR_MODULE_NAME="org.netbeans.modules.web.monitor"; private static ModuleInfo httpMonitorInfo; 63 private static ModuleSpy monitorSpy; 64 private static Lookup.Result res; 65 private static MonitorInfoListener monitorInfoListener; 66 private static MonitorLookupListener monitorLookupListener; 67 68 private static final String MONITOR_FILTER_NAME = "HTTPMonitorFilter"; private static final String MONITOR_FILTER_CLASS = "org.netbeans.modules.web.monitor.server.MonitorFilter"; private static final String MONITOR_FILTER_PATTERN = "/*"; private static final String MONITOR_INTERNALPORT_PARAM_NAME = "netbeans.monitor.ide"; 74 public static void setMonitorFlag(String managerURL, boolean enable) { 75 InstanceProperties ip = InstanceProperties.getInstanceProperties(managerURL); 76 ip.setProperty(MONITOR_ENABLED_PROPERTY_NAME, Boolean.toString(enable)); 77 } 78 79 public static boolean getMonitorFlag(String managerURL) { 80 InstanceProperties ip = InstanceProperties.getInstanceProperties(managerURL); 81 String prop = ip.getProperty(MONITOR_ENABLED_PROPERTY_NAME); 82 return (prop == null) ? true : Boolean.valueOf(prop).booleanValue(); 83 } 84 85 public static void setMonitorFlag(TomcatManager tm, boolean enable) { 86 setMonitorFlag(tm.getUri(), enable); 87 } 88 89 public static boolean getMonitorFlag(TomcatManager tm) { 90 return getMonitorFlag(tm.getUri()); 91 } 92 93 public static void synchronizeMonitorWithFlag(TomcatManager tm, boolean alsoSetPort, boolean alsoCopyJars) throws IOException , SAXException { 94 String url = tm.getUri(); 95 boolean monitorFlag = getMonitorFlag(url); 96 boolean monitorModuleAvailable = isMonitorEnabled(); 97 boolean shouldInstall = monitorModuleAvailable && monitorFlag; 98 99 File webXML = getDefaultWebXML(tm); 101 if (webXML == null) { 102 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception (url)); 103 return; 104 } 105 WebApp webApp = DDProvider.getDefault().getDDRoot(webXML); 106 if (webApp == null) { 107 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception (url)); 108 return; 109 } 110 boolean needsSave = false; 111 boolean result; 112 if (shouldInstall) { 113 if (alsoCopyJars) { 114 addMonitorJars(tm); 115 } 116 result = changeFilterMonitor(webApp, true); 117 needsSave = needsSave || result; 118 if (alsoSetPort) { 119 result = specifyFilterPortParameter(webApp); 120 needsSave = needsSave || result; 121 } 122 } 123 else { 124 result = changeFilterMonitor(webApp, false); 125 needsSave = needsSave || result; 126 } 127 if (needsSave) { 128 OutputStream os = new FileOutputStream (webXML); 129 try { 130 webApp.write(os); 131 } 132 finally { 133 os.close(); 134 } 135 } 136 } 137 138 private static File getDefaultWebXML(TomcatManager tm) { 139 File cb = tm.getTomcatProperties().getCatalinaDir(); 140 File webXML = new File (cb, "conf" + File.separator + "web.xml"); 141 if (webXML.exists()) 142 return webXML; 143 return null; 144 } 145 146 private static void addMonitorJars(TomcatManager tm) throws IOException { 147 File instDir = tm.getTomcatProperties().getCatalinaHome(); 149 if (instDir==null) return; 150 String libFolder = tm.libFolder(); 151 copyFromIDEInstToDir("modules/ext/org-netbeans-modules-web-httpmonitor.jar" , instDir, libFolder + "/org-netbeans-modules-web-httpmonitor.jar"); copyFromIDEInstToDir("modules/org-netbeans-modules-schema2beans.jar" , instDir, libFolder + "/org-netbeans-modules-schema2beans.jar"); 154 } 156 157 private static boolean changeFilterMonitor(WebApp webApp,boolean full) { 158 boolean filterWasChanged=false; 159 if (full) { boolean isFilter=false; 162 Filter[] filters = webApp.getFilter(); 163 for(int i=0;i<filters.length;i++) { 164 if (filters[i].getFilterName().equals(MONITOR_FILTER_NAME)){ 165 isFilter=true; 166 break; 167 } 168 } 169 if (!isFilter) { 170 try { 171 Filter filter = (Filter)webApp.createBean("Filter"); filter.setFilterName(MONITOR_FILTER_NAME); 173 filter.setFilterClass(MONITOR_FILTER_CLASS); 174 178 webApp.addFilter(filter); 179 filterWasChanged=true; 180 }catch (ClassNotFoundException ex) {} 181 } 182 183 boolean isMapping=false; 184 FilterMapping[] maps = webApp.getFilterMapping(); 185 for(int i=0;i<maps.length;i++) { 186 if (maps[i].getFilterName().equals(MONITOR_FILTER_NAME)){ 187 isMapping=true; 188 break; 189 } 190 } 191 if (!isMapping) { 192 try { 193 FilterMapping filterMapping = (FilterMapping)webApp.createBean("FilterMapping"); 195 String [] dispatcher = new String [] {"REQUEST","FORWARD","INCLUDE","ERROR"}; try { 198 filterMapping.setDispatcher(dispatcher); 199 } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) { 200 ((BaseBean)filterMapping).createProperty("dispatcher", "Dispatcher", Common.TYPE_0_N | Common.TYPE_STRING | Common.TYPE_KEY, 203 java.lang.String .class); 204 ((BaseBean)filterMapping).setValue("Dispatcher",dispatcher); } 206 207 filterMapping.setFilterName(MONITOR_FILTER_NAME); 208 filterMapping.setUrlPattern(MONITOR_FILTER_PATTERN); 209 webApp.addFilterMapping(filterMapping); 210 filterWasChanged=true; 211 } catch (ClassNotFoundException ex) {} 212 } 213 } else { FilterMapping[] maps = webApp.getFilterMapping(); 216 for(int i=0;i<maps.length;i++) { 217 218 if (maps[i].getFilterName().equals(MONITOR_FILTER_NAME)){ 219 webApp.removeFilterMapping(maps[i]); 220 filterWasChanged=true; 221 break; 222 } 223 } 224 Filter[] filters = webApp.getFilter(); 225 for(int i=0;i<filters.length;i++) { 226 if (filters[i].getFilterName().equals(MONITOR_FILTER_NAME)){ 227 webApp.removeFilter(filters[i]); 228 filterWasChanged=true; 229 break; 230 } 231 } 232 } 233 return filterWasChanged; 234 } 235 236 243 private static File findInstallationFile(String instRelPath) { 244 return InstalledFileLocator.getDefault().locate(instRelPath, null, false); 245 } 246 247 private static void copyFromIDEInstToDir(String sourceRelPath, File copyTo, String targetRelPath) throws IOException { 248 File targetFile = findFileUnderBase(copyTo, targetRelPath); 249 File sourceFile = findInstallationFile(sourceRelPath); 250 if (sourceFile != null && sourceFile.exists()) { 251 if (!targetFile.exists() 252 || sourceFile.length() != targetFile.length()) { 253 copy(sourceFile,targetFile); 254 } 255 } 256 } 257 258 private static void copy(File file1, File file2) throws IOException { 259 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file1)); 260 BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (file2)); 261 int b; 262 while((b=bis.read())!=-1)bos.write(b); 263 bis.close(); 264 bos.close(); 265 } 266 267 private static File findFileUnderBase(File base, String fileRelPath) { 268 if (fileRelPath.startsWith("/")) { fileRelPath = fileRelPath.substring(1); 270 } 271 fileRelPath = fileRelPath.replace('/', File.separatorChar); 272 return new File (base, fileRelPath); 273 } 274 275 281 private static boolean specifyFilterPortParameter(WebApp webApp) { 282 Filter[] filters = webApp.getFilter(); 283 Filter myFilter = null; 284 for(int i=0; i<filters.length; i++) { 285 if (MONITOR_FILTER_NAME.equals(filters[i].getFilterName())) { 286 myFilter = filters[i]; 287 break; 288 } 289 } 290 if (myFilter == null) 292 return false; 293 294 InitParam[] params = myFilter.getInitParam(); 296 InitParam myParam = null; 297 for(int i=0; i<params.length; i++) { 298 if (MONITOR_INTERNALPORT_PARAM_NAME.equals(params[i].getParamName())) { 299 myParam = params[i]; 300 break; 301 } 302 } 303 304 String correctParamValue = getLocalHost() + ":" + getInternalServerPort(); 307 if (myParam == null) { 309 try { 311 InitParam init = (InitParam)myFilter.createBean("InitParam"); init.setParamName(MONITOR_INTERNALPORT_PARAM_NAME); 313 init.setParamValue(correctParamValue); 314 myFilter.addInitParam(init); 315 } catch (ClassNotFoundException ex){} 316 return true; 317 } 318 else { 319 if (correctParamValue.equals(myParam.getParamValue())) { 321 return false; 323 } 324 else { 325 myParam.setParamValue(correctParamValue); 327 return true; 328 } 329 } 330 331 } 333 public static String getLocalHost() { 334 return "127.0.0.1"; 344 } 345 346 private static URL getSampleHTTPServerURL() { 347 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 348 FileObject fo = fs.findResource("HTTPServer_DUMMY"); 349 if (fo == null) { 350 return null; 351 } 352 URL u = URLMapper.findURL(fo, URLMapper.NETWORK); 353 return u; 354 } 355 356 private static String getInternalServerPort() { 357 URL u = getSampleHTTPServerURL(); 359 if (u != null) { 360 return "" + u.getPort(); } 362 else { 363 return "8082"; } 365 } 366 367 417 418 private static void startModuleSpy (final ModuleSpy spy) { 419 res = Lookup.getDefault().lookup(new Lookup.Template(ModuleInfo.class)); 421 java.util.Iterator it = res.allInstances ().iterator (); 422 final String moduleId = spy.getModuleId(); 423 boolean found = false; 424 while (it.hasNext ()) { 425 org.openide.modules.ModuleInfo mi = (ModuleInfo)it.next (); 426 if (mi.getCodeName ().startsWith(moduleId)) { 427 httpMonitorInfo=mi; 428 spy.setEnabled(mi.isEnabled()); 429 monitorInfoListener = new MonitorInfoListener(spy); 430 httpMonitorInfo.addPropertyChangeListener(monitorInfoListener); 431 found=true; 432 break; 433 } 434 } 435 monitorLookupListener = new MonitorLookupListener(spy,httpMonitorInfo); 437 res.addLookupListener(monitorLookupListener); 438 } 439 440 private static class ModuleSpy { 441 private boolean enabled; 442 private String moduleId; 443 444 public ModuleSpy (String moduleId) { 445 this.moduleId=moduleId; 446 } 447 public void setModuleId(String moduleId){ 448 this.moduleId=moduleId; 449 } 450 public void setEnabled(boolean enabled){ 451 this.enabled=enabled; 452 } 453 public boolean isEnabled(){ 454 return enabled; 455 } 456 public String getModuleId(){ 457 return moduleId; 458 } 459 } 460 461 static synchronized boolean isMonitorEnabled(){ 462 if (monitorSpy==null) { 463 monitorSpy = new ModuleSpy(MONITOR_MODULE_NAME); 464 startModuleSpy(monitorSpy); 465 } 466 return monitorSpy.isEnabled(); 467 } 468 469 void removeListeners() { 471 if (httpMonitorInfo!=null) { 472 httpMonitorInfo.removePropertyChangeListener(monitorInfoListener); 473 } 474 if (res!=null) { 475 res.removeLookupListener(monitorLookupListener); 476 } 477 } 478 479 private static class MonitorInfoListener implements java.beans.PropertyChangeListener { 480 ModuleSpy spy; 481 MonitorInfoListener(ModuleSpy spy) { 482 this.spy=spy; 483 } 484 485 public void propertyChange(java.beans.PropertyChangeEvent evt) { 486 if (evt.getPropertyName().equals("enabled")){ spy.setEnabled(((Boolean )evt.getNewValue()).booleanValue()); 488 } 489 } 490 } 491 492 private static class MonitorLookupListener implements LookupListener { 493 494 ModuleSpy spy; 495 ModuleInfo httpMonitorInfo; 496 MonitorLookupListener(ModuleSpy spy, ModuleInfo httpMonitorInfo) { 497 this.spy=spy; 498 this.httpMonitorInfo=httpMonitorInfo; 499 } 500 501 public void resultChanged(LookupEvent lookupEvent) { 502 java.util.Iterator it = res.allInstances ().iterator (); 503 boolean moduleFound=false; 504 while (it.hasNext ()) { 505 ModuleInfo mi = (ModuleInfo)it.next (); 506 if (mi.getCodeName ().startsWith(spy.getModuleId())) { 507 spy.setEnabled(mi.isEnabled()); 508 if (httpMonitorInfo==null) { 509 httpMonitorInfo=mi; 510 monitorInfoListener = new MonitorInfoListener(spy); 511 httpMonitorInfo.addPropertyChangeListener(monitorInfoListener); 512 } 513 moduleFound=true; 514 break; 515 } 516 } 517 if (!moduleFound) { 518 if (httpMonitorInfo!=null) { 519 httpMonitorInfo.removePropertyChangeListener(monitorInfoListener); 520 httpMonitorInfo=null; 521 spy.setEnabled(false); 522 } 523 } 524 } 525 526 } 527 } 528 | Popular Tags |