KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > util > LogManager


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.tomcat5.util;
21
22 import java.io.File JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
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 /**
38  * <code>LogManager</code> manages all context and shared context logs for one
39  * Tomcat server instace (one <code>TomcatManager</code>).
40  *
41  * @author Stepan Herold
42  */

43 public class LogManager {
44     private ServerLog serverLog;
45     private LogViewer sharedContextLogViewer;
46     private LogViewer juliLogViewer;
47     private Map JavaDoc/*<TomcatModule, TomcatModuleConfig>*/ tomcatModuleConfigs = Collections.synchronizedMap(new WeakHashMap JavaDoc());
48     private Map JavaDoc/*<String, LogViewer>*/ contextLogViewers = Collections.synchronizedMap(new HashMap JavaDoc());
49     private TomcatManager manager;
50     
51     private Object JavaDoc serverLogLock = new Object JavaDoc();
52     private Object JavaDoc sharedContextLogLock = new Object JavaDoc();
53     private Object JavaDoc juliLogLock = new Object JavaDoc();
54     private Object JavaDoc contextLogLock = new Object JavaDoc();
55     
56     private Boolean JavaDoc juliJarExist;
57     
58     /** Creates a new instance of LogManager */
59     public LogManager(TomcatManager tm) {
60         manager = tm;
61     }
62     
63     // ------- server log (output) ---------------------------------------------
64

65     /**
66      * Open the server log (output).
67      */

68     public void openServerLog() {
69         final Process JavaDoc 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 JavaDoc(process.getInputStream()),
80                 new InputStreamReader JavaDoc(process.getErrorStream()),
81                 true,
82                 false);
83             serverLog.start();
84         }
85         //PENDING: currently we copy only Tomcat std & err output. We should
86
// also support copying to Tomcat std input.
87
new Thread JavaDoc() {
88             public void run() {
89                 try {
90                     process.waitFor();
91                     Thread.sleep(2000); // time for server log
92
} catch (InterruptedException JavaDoc e) {
93                 } finally {
94                     serverLog.interrupt();
95                 }
96             }
97         }.start();
98     }
99     
100     /**
101      * Stop the server log thread, if started.
102      */

103     public void closeServerLog() {
104         synchronized(serverLogLock) {
105             if (serverLog != null) {
106                 serverLog.interrupt();
107                 serverLog = null;
108             }
109         }
110     }
111
112     /**
113      * Can be the server log (output) displayed?
114      *
115      * @return <code>true</code> if the server log can be displayed, <code>false</code>
116      * otherwise.
117      */

118     public boolean hasServerLog() {
119         return manager.getTomcatProcess() != null;
120     }
121     
122     // ------- end of server log (output) --------------------------------------
123

124     // ------- shared context log ----------------------------------------------
125

126     /**
127      * Opens shared context log. Shared context log can be defined in the host or
128      * engine element. Definition in the host element overrides definition in the
129      * engine element.
130      */

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 JavaDoc npe) {
157             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe);
158         }
159         
160         // ensure only one thread will be opened
161
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     /**
195      * Is shared context log defined for this server?
196      *
197      * @return <code>true</code> shared context log is defined, <code>false</code>
198      * otherwise.
199      */

200     public boolean hasSharedLogger() {
201         TomcatManagerConfig tomcatManagerConfig = manager.getTomcatManagerConfig();
202         tomcatManagerConfig.refresh();
203         return tomcatManagerConfig.hasLogger();
204     }
205     
206     // ------- end of shared context log ---------------------------------------
207

208     // ------- juli log --------------------------------------------------------
209

210     public synchronized boolean hasJuliLog() {
211         if (juliJarExist == null) {
212             if (new File JavaDoc(manager.getTomcatProperties().getCatalinaHome(), "bin/tomcat-juli.jar").exists()) { // NOI18N
213
juliJarExist = Boolean.TRUE;
214             } else {
215                 juliJarExist = Boolean.FALSE;
216             }
217         }
218         return juliJarExist.booleanValue();
219     }
220     
221     public void openJuliLog() {
222         // ensure only one thread will be opened
223
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); // NOI18N
232
juliLogViewer.setDisplayName(NbBundle.getMessage(LogManager.class, "TXT_JuliLogDisplayName", tp.getDisplayName()));
233                 } catch (UnsupportedLoggerException e) { // should never occur
234
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
235                     return;
236                 } catch (NullPointerException JavaDoc 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     // ------- end of juli log -------------------------------------------------
254

255     // ------- context log -----------------------------------------------------
256

257     /**
258      * Open a context log for the specified module.
259      *
260      * @param module its context log should be opened.
261      */

262     public void openContextLog(TomcatModule module) {
263         final String JavaDoc moduleID = module.getModuleID();
264         Object JavaDoc 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 JavaDoc npe) {
298             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe);
299         }
300         
301         // ensure only one thread will be opened
302
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     /**
333      * Is context log defined for the specified module.
334      *
335      * @param module which should be examined.
336      * @return <code>true</code> if specified module has a context log defined,
337      * <code>false</code> otherwise.
338      */

339     public boolean hasContextLogger(TomcatModule module) {
340         Object JavaDoc 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     // ------- end of context log ----------------------------------------------
356
}
357
Popular Tags