KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > SyslogT3Startup


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.util.*;
55 import java.text.*;
56
57 import javax.naming.*;
58 import weblogic.common.*;
59 import com.protomatter.syslog.xml.*;
60
61 /**
62  * Configure syslog to start when WebLogic does.
63  * For use with <a HREF="http://www.beasys.com">BEA WebLogic Server</a>.
64  * This class implements the <tt>weblogic.common.T3StartupDef</tt>
65  * interface and is able to initialize Syslog when WebLogic starts.
66  * This class also implements the <TT>T3ShutdownDef</TT> interface and
67  * so can be used to shutdown syslog when WebLogic shuts down.
68  *
69  * @see Syslog
70  * @see Syslogger
71  */

72 public class SyslogT3Startup
73 implements T3StartupDef, T3ShutdownDef
74 {
75   private T3ServicesDef services = null;
76   private static boolean configured = false;
77
78   private static String JavaDoc LOG_PREFIX = "Protomatter Syslog: ";
79
80   /**
81    * Default constructor -- called by WebLogic.
82    */

83   public SyslogT3Startup()
84   {
85     super();
86   }
87
88   /**
89    * Part of the <tt>weblogic.common.T3StartupDef</tt> interface.
90    *
91    * @see weblogic.common.T3StartupDef
92    */

93   public void setServices(T3ServicesDef services)
94   {
95     this.services = services;
96   }
97
98   /**
99    * Start Syslog services.
100    * <P>
101    * Syslog is configured from an XML file specified by the
102    * <tt>Syslog.config.xml</tt> system property.
103    * Multiple calls to this method are ignored.
104    *
105    * @see com.protomatter.syslog.xml.SyslogXML#configure(File)
106    */

107   public String JavaDoc startup(String JavaDoc name, Hashtable ht)
108   throws Exception JavaDoc
109   {
110     // setup syslog
111
LogServicesDef log = services.log();
112     try
113     {
114       log.info(LOG_PREFIX + "SyslogT3Startup " + Syslog.getResourceString(MessageConstants.T3_INIT_MESSAGE));
115
116       // make sure someone else didn't already run us.
117
if (configured)
118         return Syslog.getResourceString(MessageConstants.T3_ALREADY_INIT_MESSAGE);
119
120       // get the path to the config file from the
121
// "Syslog.config.xml" system property.
122
String JavaDoc xmlConfigFile = System.getProperty("Syslog.config.xml");
123       if (xmlConfigFile == null)
124       {
125         log.error(LOG_PREFIX + MessageFormat.format(
126           Syslog.getResourceString(MessageConstants.T3_MUST_SPECIFY_1_MESSAGE),
127           new Object JavaDoc[] { "Syslog.config.xml" }));
128         log.error(LOG_PREFIX + Syslog.getResourceString(MessageConstants.T3_MUST_SPECIFY_2_MESSAGE));
129         log.error(LOG_PREFIX + " -DSyslog.config.xml=" +
130           Syslog.getResourceString(MessageConstants.T3_MUST_SPECIFY_BLAH_MESSAGE));
131         return Syslog.getResourceString(MessageConstants.T3_FAILURE_MESSAGE);
132       }
133       log.info(LOG_PREFIX + MessageFormat.format(
134         Syslog.getResourceString(MessageConstants.CONFIGURING_SYSLOG_FROM_MESSAGE),
135         new Object JavaDoc[] { xmlConfigFile }));
136       SyslogXML.configure(new File(xmlConfigFile));
137       this.configured = true;
138
139       Iterator loggers = Syslog.getLoggers();
140       while (loggers.hasNext())
141       {
142         Syslogger logger = (Syslogger)loggers.next();
143         if (logger.getName() != null)
144         {
145           log.info(LOG_PREFIX + MessageFormat.format(
146             Syslog.getResourceString(MessageConstants.T3_LOGGER_ISA_MESSAGE),
147             new Object JavaDoc[] { logger.getName(), logger.getClass().getName() }));
148         }
149         else
150         {
151           log.info(LOG_PREFIX + MessageFormat.format(
152             Syslog.getResourceString(MessageConstants.T3_LOGGER_NONAME_ISA_MESSAGE),
153             new Object JavaDoc[] { logger.getClass().getName() }));
154         }
155       }
156     }
157     catch (Exception JavaDoc x)
158     {
159       this.configured = false;
160       log.error(LOG_PREFIX + Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x);
161       return Syslog.getResourceString(MessageConstants.T3_FAILURE_MESSAGE);
162     }
163
164     return Syslog.getResourceString(MessageConstants.T3_SUCCESS_MESSAGE);
165   }
166
167   /**
168    * Shutdown Syslog services. This method simply calls
169    * <TT>Syslog.shutdown()</TT>.
170    */

171   public String JavaDoc shutdown(String JavaDoc name, Hashtable ht)
172   {
173     Syslog.shutdown();
174     return Syslog.getResourceString(MessageConstants.T3_SUCCESS_MESSAGE);
175   }
176
177   /**
178    * A shortcut to starting syslog services. This is
179    * generally used by classes that want to ensure that
180    * Syslog has been started before they start. You can
181    * basically do this:<P>
182    *
183    * <blockquote><pre>
184    * (new SyslogT3Startup()).startup(services);
185    * </pre></blockquote>
186    *
187    * From inside the <tt>startup(...)</tt> method in
188    * your startup class to make sure that syslog
189    * gets started.<P>
190    */

191   public boolean startup(T3ServicesDef services)
192   {
193     try
194     {
195       setServices(services);
196       startup(null, null);
197       return true;
198     }
199     catch (Exception JavaDoc x)
200     {
201       return false;
202     }
203   }
204 }
205
Popular Tags