KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > providers > com > Log


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

57 package org.apache.soap.providers.com;
58
59 import java.io.* ;
60 import java.util.* ;
61 import java.text.MessageFormat JavaDoc;
62 import javax.servlet.* ;
63 import javax.servlet.http.* ;
64 import org.apache.soap.util.* ;
65 import java.lang.Math JavaDoc;
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 JavaDoc pname= "org.apache.soap.providers.com";
78   static final String JavaDoc cname= pname + ".COMProvider";
79   protected static final String JavaDoc msgFile= cname;
80   protected java.io.PrintStream JavaDoc ls= null;
81   protected static ResourceBundle rb=null;
82   public static synchronized void init( Servlet servlet) //Specialize for HTTP.
83
{
84     if(log != null) return;
85     String JavaDoc params= null;
86     if(servlet != null) params= servlet.getServletConfig().getInitParameter("comprovider.msglvl");
87    
88     java.io.PrintStream JavaDoc ls= null;
89     String JavaDoc propInit= params == null ? findMessageString("loginit") : params;
90     if(propInit != null)
91     {
92       StringTokenizer tok= new StringTokenizer(propInit);
93       String JavaDoc f1= tok.hasMoreTokens() ? tok.nextToken() : "";
94       if( f1.length() != 0)
95       {
96         
97         if( 0 == f1.compareTo("="))
98         {
99          String JavaDoc f2= null;
100           try{
101             f2= tok.hasMoreTokens() ? tok.nextToken() : "";
102             
103             Class JavaDoc cl= Class.forName(f2);
104             Object JavaDoc 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."); //Just keep going.
113
log= new Log(null, "-file -" ); //Instance allows for subclassing.
114
}
115           }
116           catch ( Exception JavaDoc e)
117           {
118             log= new Log(null, "-file -" );
119             System.err.println("Error in loading log handler=" + f2 + "exception " + e); //Just keep going.
120
}
121           return; //done.
122
}
123       }
124     }
125
126     log= new Log(servlet, params); //Instance allows for subclassing.
127

128   }
129
130   public void init( Servlet servlet, String JavaDoc params){/* Do nothing */};
131   
132   public Log( Servlet servlet, String JavaDoc params)
133   {
134     currentLevel= ERROR;
135       
136     if(null != params)
137     for( StringTokenizer tok= new StringTokenizer(params);tok.hasMoreTokens();)
138     {
139       String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) { currentLevel= ERROR;}
173       }
174     }//endfor
175

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 JavaDoc getMessage(String JavaDoc msgId, Object JavaDoc[] args)
187   {
188      String JavaDoc 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 JavaDoc findMessageString(String JavaDoc msgId)
195   {
196     return findMessageString(msgId, null);
197   }
198   public static String JavaDoc findMessageString(String JavaDoc msgId, Object JavaDoc[] args)
199   {
200      String JavaDoc ret= null;
201
202        try
203        {
204         if( rb == null) rb = ResourceBundle.getBundle (msgFile, Locale.getDefault());
205          String JavaDoc formatString = rb.getString(msgId);
206          if (args == null) ret= formatString;
207          else
208          {
209           MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(formatString);
210           ret= formatter.format(args);
211          }
212        } catch (java.util.MissingResourceException JavaDoc mre) {
213        }
214
215      return ret;
216   }
217   public String JavaDoc logit(final int level, String JavaDoc s) //Could be over ride. called by CPP!!!
218
{
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 JavaDoc logit(final int level, final String JavaDoc msgId, final Object JavaDoc[] args) //Could be over ride.
226
{
227     String JavaDoc s= getMessage(msgId, args);
228     logit(level, s);
229     return s;
230   }
231   public static final String JavaDoc msg(final int level, final String JavaDoc msg )
232   {
233    return log.logit(level, msg);
234   }
235   public static final String JavaDoc msg(final int level, final String JavaDoc msgId, Object JavaDoc o )
236   {
237    if( (level & currentLevel) == 0 ) return "" ;
238    return msg( level, msgId, new Object JavaDoc[] {o});
239   }
240   public static final String JavaDoc msg(final int level, final String JavaDoc msgId, Object JavaDoc o, Object JavaDoc o2 )
241   {
242    if( (level & currentLevel) == 0 ) return "" ;
243    return msg( level, msgId, new Object JavaDoc[] {o,o2});
244   }
245   public static final String JavaDoc msg(final int level, final String JavaDoc msgid, Object JavaDoc o, Object JavaDoc o2, Object JavaDoc o3 )
246   {
247    if( (level & currentLevel) == 0 ) return "" ;
248    return msg( level, msgid, new Object JavaDoc[] {o,o2,o3});
249   }
250   public static final String JavaDoc msg(final int level, final String JavaDoc msgId, Object JavaDoc o, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4 )
251   {
252    if( (level & currentLevel) == 0 ) return "" ;
253    return msg( level, msgId, new Object JavaDoc[] {o,o2,o3,o4});
254   }
255   public static final String JavaDoc msg(final int level, final String JavaDoc msgId, final Object JavaDoc[] 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