1 19 20 package org.netbeans.modules.tomcat5.util; 21 22 import java.io.File ; 23 import java.io.InputStreamReader ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.WeakHashMap ; 28 import org.netbeans.modules.tomcat5.TomcatManager; 29 import org.netbeans.modules.tomcat5.TomcatManagerConfig; 30 import org.netbeans.modules.tomcat5.TomcatModule; 31 import org.netbeans.modules.tomcat5.TomcatModuleConfig; 32 import org.openide.NotifyDescriptor; 33 import org.openide.DialogDisplayer; 34 import org.openide.util.NbBundle; 35 import org.openide.ErrorManager; 36 37 43 public class LogManager { 44 private ServerLog serverLog; 45 private LogViewer sharedContextLogViewer; 46 private LogViewer juliLogViewer; 47 private Map tomcatModuleConfigs = Collections.synchronizedMap(new WeakHashMap ()); 48 private Map contextLogViewers = Collections.synchronizedMap(new HashMap ()); 49 private TomcatManager manager; 50 51 private Object serverLogLock = new Object (); 52 private Object sharedContextLogLock = new Object (); 53 private Object juliLogLock = new Object (); 54 private Object contextLogLock = new Object (); 55 56 private Boolean juliJarExist; 57 58 59 public LogManager(TomcatManager tm) { 60 manager = tm; 61 } 62 63 65 68 public void openServerLog() { 69 final Process process = manager.getTomcatProcess(); 70 assert process != null; 71 synchronized(serverLogLock) { 72 if (serverLog != null) { 73 serverLog.takeFocus(); 74 return; 75 } 76 serverLog = new ServerLog( 77 manager.getUri(), 78 manager.getTomcatProperties().getDisplayName(), 79 new InputStreamReader (process.getInputStream()), 80 new InputStreamReader (process.getErrorStream()), 81 true, 82 false); 83 serverLog.start(); 84 } 85 new Thread () { 88 public void run() { 89 try { 90 process.waitFor(); 91 Thread.sleep(2000); } catch (InterruptedException e) { 93 } finally { 94 serverLog.interrupt(); 95 } 96 } 97 }.start(); 98 } 99 100 103 public void closeServerLog() { 104 synchronized(serverLogLock) { 105 if (serverLog != null) { 106 serverLog.interrupt(); 107 serverLog = null; 108 } 109 } 110 } 111 112 118 public boolean hasServerLog() { 119 return manager.getTomcatProcess() != null; 120 } 121 122 124 126 131 public void openSharedContextLog() { 132 TomcatManagerConfig tomcatManagerConfig = manager.getTomcatManagerConfig(); 133 tomcatManagerConfig.refresh(); 134 if (!tomcatManagerConfig.hasLogger()) { 135 return; 136 } 137 LogViewer newSharedContextLog = null; 138 try { 139 TomcatProperties tp = manager.getTomcatProperties(); 140 newSharedContextLog = new LogViewer( 141 tp.getCatalinaDir(), 142 manager.getCatalinaWork(), 143 null, 144 tomcatManagerConfig.loggerClassName(), 145 tomcatManagerConfig.loggerDir(), 146 tomcatManagerConfig.loggerPrefix(), 147 tomcatManagerConfig.loggerSuffix(), 148 tomcatManagerConfig.loggerTimestamp(), 149 false); 150 } catch (UnsupportedLoggerException e) { 151 NotifyDescriptor notDesc = new NotifyDescriptor.Message( 152 NbBundle.getMessage(LogManager.class, "MSG_UnsupportedLogger", 153 e.getLoggerClassName())); 154 DialogDisplayer.getDefault().notify(notDesc); 155 return; 156 } catch (NullPointerException npe) { 157 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe); 158 } 159 160 synchronized(sharedContextLogLock) { 162 if (sharedContextLogViewer != null && sharedContextLogViewer.isOpen() 163 && !sharedContextLogViewer.equals(newSharedContextLog)) { 164 sharedContextLogViewer.removeAllLogViewerStopListener(); 165 sharedContextLogViewer.close(); 166 sharedContextLogViewer = newSharedContextLog; 167 sharedContextLogViewer.addLogViewerStopListener(new LogViewer.LogViewerStopListener() { 168 public void callOnStop() { 169 synchronized(sharedContextLogLock) { 170 sharedContextLogViewer = null; 171 } 172 } 173 }); 174 sharedContextLogViewer.start(); 175 } else if (sharedContextLogViewer == null || !sharedContextLogViewer.isOpen()) { 176 if (sharedContextLogViewer != null) { 177 sharedContextLogViewer.removeAllLogViewerStopListener(); 178 } 179 sharedContextLogViewer = newSharedContextLog; 180 sharedContextLogViewer.addLogViewerStopListener(new LogViewer.LogViewerStopListener() { 181 public void callOnStop() { 182 synchronized(sharedContextLogLock) { 183 sharedContextLogViewer = null; 184 } 185 } 186 }); 187 sharedContextLogViewer.start(); 188 } 189 sharedContextLogViewer.takeFocus(); 190 } 191 } 192 193 194 200 public boolean hasSharedLogger() { 201 TomcatManagerConfig tomcatManagerConfig = manager.getTomcatManagerConfig(); 202 tomcatManagerConfig.refresh(); 203 return tomcatManagerConfig.hasLogger(); 204 } 205 206 208 210 public synchronized boolean hasJuliLog() { 211 if (juliJarExist == null) { 212 if (new File (manager.getTomcatProperties().getCatalinaHome(), "bin/tomcat-juli.jar").exists()) { juliJarExist = Boolean.TRUE; 214 } else { 215 juliJarExist = Boolean.FALSE; 216 } 217 } 218 return juliJarExist.booleanValue(); 219 } 220 221 public void openJuliLog() { 222 synchronized(juliLogLock) { 224 if (juliLogViewer == null || !juliLogViewer.isOpen()) { 225 if (juliLogViewer != null) { 226 juliLogViewer.removeAllLogViewerStopListener(); 227 } 228 try { 229 TomcatProperties tp = manager.getTomcatProperties(); 230 juliLogViewer = new LogViewer(tp.getCatalinaDir(), manager.getCatalinaWork(), 231 null, null, null, "localhost.", null, true, false); juliLogViewer.setDisplayName(NbBundle.getMessage(LogManager.class, "TXT_JuliLogDisplayName", tp.getDisplayName())); 233 } catch (UnsupportedLoggerException e) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 235 return; 236 } catch (NullPointerException npe) { 237 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe); 238 return; 239 } 240 juliLogViewer.addLogViewerStopListener(new LogViewer.LogViewerStopListener() { 241 public void callOnStop() { 242 synchronized(juliLogLock) { 243 juliLogViewer = null; 244 } 245 } 246 }); 247 juliLogViewer.start(); 248 } 249 juliLogViewer.takeFocus(); 250 } 251 } 252 253 255 257 262 public void openContextLog(TomcatModule module) { 263 final String moduleID = module.getModuleID(); 264 Object o = tomcatModuleConfigs.get(module); 265 TomcatModuleConfig moduleConfig = null; 266 LogViewer contextLog = null; 267 if (o == null) { 268 moduleConfig = new TomcatModuleConfig( 269 module.getDocRoot(), 270 module.getPath(), 271 manager.getTomcatManagerConfig().serverXmlPath()); 272 tomcatModuleConfigs.put(module, moduleConfig); 273 } else { 274 moduleConfig = (TomcatModuleConfig)o; 275 moduleConfig.refresh(); 276 } 277 if (!moduleConfig.hasLogger()) return; 278 contextLog = (LogViewer)contextLogViewers.get(moduleID); 279 LogViewer newContextLog = null; 280 try { 281 newContextLog = new LogViewer( 282 manager.getTomcatProperties().getCatalinaDir(), 283 manager.getCatalinaWork(), 284 module.getPath(), 285 moduleConfig.loggerClassName(), 286 moduleConfig.loggerDir(), 287 moduleConfig.loggerPrefix(), 288 moduleConfig.loggerSuffix(), 289 moduleConfig.loggerTimestamp(), 290 false); 291 } catch (UnsupportedLoggerException e) { 292 NotifyDescriptor notDesc = new NotifyDescriptor.Message( 293 NbBundle.getMessage(LogManager.class, "MSG_UnsupportedLogger", 294 e.getLoggerClassName())); 295 DialogDisplayer.getDefault().notify(notDesc); 296 return; 297 } catch (NullPointerException npe) { 298 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe); 299 } 300 301 synchronized(contextLogLock) { 303 if (contextLog != null && contextLog.isOpen() 304 && !contextLog.equals(newContextLog)) { 305 contextLog.removeAllLogViewerStopListener(); 306 contextLog.close(); 307 contextLog = newContextLog; 308 contextLog.addLogViewerStopListener(new LogViewer.LogViewerStopListener() { 309 public void callOnStop() { 310 contextLogViewers.remove(moduleID); 311 } 312 }); 313 contextLogViewers.put(moduleID, contextLog); 314 contextLog.start(); 315 } else if (contextLog == null || !contextLog.isOpen()) { 316 if (contextLog != null) { 317 contextLog.removeAllLogViewerStopListener(); 318 } 319 contextLog = newContextLog; 320 contextLog.addLogViewerStopListener(new LogViewer.LogViewerStopListener() { 321 public void callOnStop() { 322 contextLogViewers.remove(moduleID); 323 } 324 }); 325 contextLogViewers.put(moduleID, contextLog); 326 contextLog.start(); 327 } 328 } 329 contextLog.takeFocus(); 330 } 331 332 339 public boolean hasContextLogger(TomcatModule module) { 340 Object o = tomcatModuleConfigs.get(module); 341 TomcatModuleConfig moduleConfig = null; 342 if (o == null) { 343 moduleConfig = new TomcatModuleConfig( 344 module.getDocRoot(), 345 module.getPath(), 346 manager.getTomcatManagerConfig().serverXmlPath()); 347 tomcatModuleConfigs.put(module, moduleConfig); 348 } else { 349 moduleConfig = (TomcatModuleConfig)o; 350 moduleConfig.refresh(); 351 } 352 return moduleConfig.hasLogger(); 353 } 354 355 } 357 | Popular Tags |