1 57 package org.apache.soap.providers.com; 58 59 import java.io.* ; 60 import java.util.* ; 61 import java.text.MessageFormat ; 62 import javax.servlet.* ; 63 import javax.servlet.http.* ; 64 import org.apache.soap.util.* ; 65 import java.lang.Math ; 66 67 public class Log 68 { 69 private ServletContext sc= null; 70 static Log log= null; 71 public static final short INFORMATION = 0x1000; 72 public static final short SUCCESS= 0x100; 73 public static final short WARNING = 0x10; 74 public static final short ERROR = 1; 75 public static final short NOINIT = -1; 76 static short currentLevel=NOINIT; 77 static final String pname= "org.apache.soap.providers.com"; 78 static final String cname= pname + ".COMProvider"; 79 protected static final String msgFile= cname; 80 protected java.io.PrintStream ls= null; 81 protected static ResourceBundle rb=null; 82 public static synchronized void init( Servlet servlet) { 84 if(log != null) return; 85 String params= null; 86 if(servlet != null) params= servlet.getServletConfig().getInitParameter("comprovider.msglvl"); 87 88 java.io.PrintStream ls= null; 89 String propInit= params == null ? findMessageString("loginit") : params; 90 if(propInit != null) 91 { 92 StringTokenizer tok= new StringTokenizer(propInit); 93 String f1= tok.hasMoreTokens() ? tok.nextToken() : ""; 94 if( f1.length() != 0) 95 { 96 97 if( 0 == f1.compareTo("=")) 98 { 99 String f2= null; 100 try{ 101 f2= tok.hasMoreTokens() ? tok.nextToken() : ""; 102 103 Class cl= Class.forName(f2); 104 Object o= cl.newInstance(); 105 if( o instanceof Log) 106 { 107 ((Log)o).init( servlet, params); 108 } 109 else 110 { 111 ls= System.err; 112 System.err.println("Log handler:" + f2 + " not an instance of Log."); log= new Log(null, "-file -" ); } 115 } 116 catch ( Exception e) 117 { 118 log= new Log(null, "-file -" ); 119 System.err.println("Error in loading log handler=" + f2 + "exception " + e); } 121 return; } 123 } 124 } 125 126 log= new Log(servlet, params); 128 } 129 130 public void init( Servlet servlet, String params){}; 131 132 public Log( Servlet servlet, String params) 133 { 134 currentLevel= ERROR; 135 136 if(null != params) 137 for( StringTokenizer tok= new StringTokenizer(params);tok.hasMoreTokens();) 138 { 139 String f1= tok.nextToken(); 140 if( 0 == f1.compareTo("-noservletlog")) 141 { 142 servlet= null; 143 } 144 else if( 0 == f1.compareTo("-file")) 145 { 146 if(tok.hasMoreTokens()) 147 { 148 String f2= tok.nextToken(); 149 if( 0 == f2.compareTo("+") ) ls= System.out; 150 else if( 0 == f2.compareTo("-")) ls= System.err; 151 else 152 { 153 try 154 { 155 ls= new PrintStream(new BufferedOutputStream( new FileOutputStream( f2, true)), true); 156 }catch( java.io.FileNotFoundException e) 157 { 158 ls= System.err; 159 } 160 } 161 } 162 } 163 else 164 { 165 166 try{ 167 if(0==f1.compareToIgnoreCase("INFORMATION")) currentLevel= INFORMATION | SUCCESS | WARNING | ERROR; 168 else if(0==f1.compareToIgnoreCase("SUCCESS")) currentLevel= SUCCESS | WARNING | ERROR; 169 else if(0==f1.compareToIgnoreCase("WARNING")) currentLevel= WARNING | ERROR; 170 else if(0==f1.compareToIgnoreCase("ERROR")); 171 else currentLevel= Short.parseShort(params); 172 }catch(Exception e) { currentLevel= ERROR;} 173 } 174 } 176 if(null != servlet) sc= servlet.getServletConfig().getServletContext(); 177 178 179 180 } 181 182 public static void init() 183 { 184 init( null); 185 } 186 public static String getMessage(String msgId, Object [] args) 187 { 188 String ret= findMessageString(msgId, args); 189 if(ret == null) 190 ret= "Missing msg id \""+ msgId +"\" in \"" + msgFile +"\"."; 191 192 return ret; 193 } 194 public static String findMessageString(String msgId) 195 { 196 return findMessageString(msgId, null); 197 } 198 public static String findMessageString(String msgId, Object [] args) 199 { 200 String ret= null; 201 202 try 203 { 204 if( rb == null) rb = ResourceBundle.getBundle (msgFile, Locale.getDefault()); 205 String formatString = rb.getString(msgId); 206 if (args == null) ret= formatString; 207 else 208 { 209 MessageFormat formatter = new MessageFormat (formatString); 210 ret= formatter.format(args); 211 } 212 } catch (java.util.MissingResourceException mre) { 213 } 214 215 return ret; 216 } 217 public String logit(final int level, String s) { 219 if( s == null) s= "null"; 220 if(ls != null) ls.println(s); 221 if( null != sc) sc.log(s); 222 return s; 223 } 224 225 public String logit(final int level, final String msgId, final Object [] args) { 227 String s= getMessage(msgId, args); 228 logit(level, s); 229 return s; 230 } 231 public static final String msg(final int level, final String msg ) 232 { 233 return log.logit(level, msg); 234 } 235 public static final String msg(final int level, final String msgId, Object o ) 236 { 237 if( (level & currentLevel) == 0 ) return "" ; 238 return msg( level, msgId, new Object [] {o}); 239 } 240 public static final String msg(final int level, final String msgId, Object o, Object o2 ) 241 { 242 if( (level & currentLevel) == 0 ) return "" ; 243 return msg( level, msgId, new Object [] {o,o2}); 244 } 245 public static final String msg(final int level, final String msgid, Object o, Object o2, Object o3 ) 246 { 247 if( (level & currentLevel) == 0 ) return "" ; 248 return msg( level, msgid, new Object [] {o,o2,o3}); 249 } 250 public static final String msg(final int level, final String msgId, Object o, Object o2, Object o3, Object o4 ) 251 { 252 if( (level & currentLevel) == 0 ) return "" ; 253 return msg( level, msgId, new Object [] {o,o2,o3,o4}); 254 } 255 public static final String msg(final int level, final String msgId, final Object [] args) 256 { 257 if( (level & currentLevel) == 0 ) return "" ; 258 return log.logit( level, msgId, args); 259 } 260 public static final short getLevel() 261 { 262 return currentLevel; 263 } 264 public static final boolean willLog(short level) 265 { 266 return (level & currentLevel) == 0; 267 } 268 269 270 } 271 | Popular Tags |