KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.*;
55 import java.rmi.*;
56 import java.util.*;
57 import java.text.*;
58
59 import javax.rmi.*;
60 import javax.naming.*;
61
62 import com.protomatter.util.*;
63
64 /**
65  * A logger that sends messages to remote receivers
66  * bound in JNDI.
67  *
68  * Object bound directly under the
69  * "<tt>com.protomatter.syslog.remote</tt>" location
70  * in JNDI will receive the log message if they
71  * implement the {@link RemoteLogReceiver RemoteLogReceiver}
72  * interface.<P>
73  *
74  * When calling methods on the remote objects, they
75  * are first run through <tt>PortableRemoteObject.narrow()</tt>
76  * to ensure everything is OK. This should facilitate
77  * the use of RMI over IIOP and other transports, and the
78  * use of non-Java log receivers via CORBA or another
79  * cross-language transport mechanisms.<P>
80  *
81  * The {@link SyslogServer SyslogServer} class can be used as
82  * a standalone remote message receiver. Please see the JavaDoc
83  * for that class for more information.
84  *
85  * @see com.protomatter.syslog.xml.RemoteLog_Helper XML configuration class
86  */

87 public class RemoteLog
88 extends BasicLogger
89 {
90   private Context context = null;
91   private Context subContext = null;
92   private Context subSubContext = null;
93
94   /**
95    * You will need to call the configure() method if
96    * you use this constructor.
97    */

98   public RemoteLog()
99   {
100     super();
101
102     try
103     {
104       context = new InitialContext();
105       subContext = getSubContext(context, "com.protomatter.syslog");
106       subSubContext = getSubContext(context, "com.protomatter.syslog.remote");
107     }
108     catch (NamingException x)
109     {
110       throw new ChainedRuntimeException(Syslog.getResourceString(MessageConstants.REMOTELOG_JNDI_INIT_EXCEPTION_MESSAGE), x);
111     }
112   }
113
114   /**
115    * getSubContext() -- creates intermediate contexts as needed.
116    */

117   private static Context getSubContext(Context ctx, String JavaDoc name)
118   throws NamingException
119   {
120     try
121     {
122       return (Context)ctx.lookup(name);
123     }
124     catch (NamingException x)
125     {
126       Context context = ctx;
127
128       // create the necessary subcontexts
129
Name resolvedName = x.getResolvedName();
130       Name remainingName = x.getRemainingName();
131
132       if (!remainingName.isEmpty())
133       {
134         // create subcontext
135
String JavaDoc nextName = remainingName.get(0);
136         resolvedName.add(nextName);
137         Context subCtx = context.createSubcontext(resolvedName);
138         // try lookup again
139
return getSubContext(context, name);
140       }
141       else
142       {
143         // can't help here...
144
throw x;
145       }
146     }
147   }
148
149   /**
150    * Log the given message to all bound listeners. If a
151    * <tt>RemoteException</tt> is thrown while calling the
152    * logging callback method on the receiver, then
153    * the receiver is unbound from JNDI to prevent further
154    * problems with that receiver.
155    */

156   public final void log(SyslogMessage sm)
157   {
158     if (context == null)
159       return;
160
161     String JavaDoc ip = sm.host.getHostAddress();
162     String JavaDoc loggerClass = sm.loggerClassname;
163     String JavaDoc channel = sm.channel;
164     String JavaDoc message = (sm.msg == null) ? "" : sm.msg.toString();
165     String JavaDoc detail = null;
166     if (detail == null)
167     {
168       detail = "";
169     }
170     else
171     {
172       StringBuffer JavaDoc b = new StringBuffer JavaDoc(256);
173       formatter.formatMessageDetail(b, sm);
174       detail = b.toString();
175     }
176     int level = sm.level;
177     String JavaDoc threadName = (sm.thread != null) ? sm.thread.getName() : "";
178     long time = sm.time;
179
180     try
181     {
182       NamingEnumeration e = subContext.list("remote");
183       while (e.hasMore())
184       {
185         NameClassPair pair = (NameClassPair)e.next();
186         String JavaDoc name = pair.getName();
187         Object JavaDoc thing = subSubContext.lookup(name);
188         if (thing instanceof RemoteLogReceiver)
189         {
190           try
191           {
192             RemoteLogReceiver receiver = (RemoteLogReceiver)thing;
193             receiver = (RemoteLogReceiver)PortableRemoteObject.narrow(receiver, RemoteLogReceiver.class);
194             receiver.log(ip, loggerClass, channel, message, detail, level, threadName, time);
195           }
196           catch (RemoteException rx)
197           {
198             System.err.println(MessageFormat.format(
199               Syslog.getResourceString(MessageConstants.REMOTELOG_CANNOT_WRITE_REMOTE_MESSAGE),
200               new Object JavaDoc[] { "RemoteException", name }));
201             rx.printStackTrace();
202             System.err.println(Syslog.getResourceString(MessageConstants.REMOTELOG_REMOVE_RECEIVER_MESSAGE));
203             try
204             {
205               subSubContext.unbind(name);
206             }
207             catch (NamingException nx) { ; }
208           }
209         }
210       }
211     }
212     catch (Exception JavaDoc x)
213     {
214       // TODO: don't know what to do!
215
x.printStackTrace();
216     }
217   }
218
219   /**
220    * Prepare for shutdown.
221    */

222   public void shutdown()
223   {
224     this.context = null;
225   }
226
227   public void flush()
228   {
229     // do nothing.
230
}
231 }
232
Popular Tags