1 package com.protomatter.syslog; 2 3 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 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 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 117 private static Context getSubContext(Context ctx, String 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 Name resolvedName = x.getResolvedName(); 130 Name remainingName = x.getRemainingName(); 131 132 if (!remainingName.isEmpty()) 133 { 134 String nextName = remainingName.get(0); 136 resolvedName.add(nextName); 137 Context subCtx = context.createSubcontext(resolvedName); 138 return getSubContext(context, name); 140 } 141 else 142 { 143 throw x; 145 } 146 } 147 } 148 149 156 public final void log(SyslogMessage sm) 157 { 158 if (context == null) 159 return; 160 161 String ip = sm.host.getHostAddress(); 162 String loggerClass = sm.loggerClassname; 163 String channel = sm.channel; 164 String message = (sm.msg == null) ? "" : sm.msg.toString(); 165 String detail = null; 166 if (detail == null) 167 { 168 detail = ""; 169 } 170 else 171 { 172 StringBuffer b = new StringBuffer (256); 173 formatter.formatMessageDetail(b, sm); 174 detail = b.toString(); 175 } 176 int level = sm.level; 177 String 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 name = pair.getName(); 187 Object 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 [] { "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 x) 213 { 214 x.printStackTrace(); 216 } 217 } 218 219 222 public void shutdown() 223 { 224 this.context = null; 225 } 226 227 public void flush() 228 { 229 } 231 } 232 | Popular Tags |