View Javadoc

1   package org.apache.velocity.runtime.log;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.util.Vector;
23  import java.util.Iterator;
24  import org.apache.velocity.runtime.RuntimeServices;
25  
26  /**
27   *  Pre-init logger.  I believe that this was suggested by
28   *  Carsten Ziegeler <cziegeler@sundn.de> and
29   *  Jeroen C. van Gelderen.  If this isn't correct, let me
30   *  know as this was a good idea...
31   *
32   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
33   * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
34   * @version $Id: HoldingLogChute.java 463298 2006-10-12 16:10:32Z henning $
35   */
36  class HoldingLogChute implements LogChute
37  {
38      private Vector pendingMessages = new Vector();
39  
40      /**
41       * @see org.apache.velocity.runtime.log.LogChute#init(org.apache.velocity.runtime.RuntimeServices)
42       */
43      public void init(RuntimeServices rs) throws Exception
44      {
45      }
46  
47      /**
48       * Logs messages. All we do is store them until 'later'.
49       *
50       * @param level severity level
51       * @param message complete error message
52       */
53      public void log(int level, String message)
54      {
55          synchronized(this)
56          {
57              Object[] data = new Object[2];
58              // TODO: JDK 1.4+ -> valueOf()
59              data[0] = new Integer(level);
60              data[1] = message;
61              pendingMessages.addElement(data);
62          }
63      }
64  
65      /**
66       * Logs messages and errors. All we do is store them until 'later'.
67       *
68       * @param level severity level
69       * @param message complete error message
70       * @param t the accompanying java.lang.Throwable
71       */
72      public void log(int level, String message, Throwable t)
73      {
74          synchronized(this)
75          {
76              Object[] data = new Object[3];
77              // TODO: JDK 1.4+ -> valueOf()
78              data[0] = new Integer(level);
79              data[1] = message;
80              data[2] = t;
81              pendingMessages.addElement(data);
82          }
83      }
84  
85      /**
86       * @see org.apache.velocity.runtime.log.LogChute#isLevelEnabled(int)
87       */
88      public boolean isLevelEnabled(int level)
89      {
90          return true;
91      }
92  
93      /**
94       * Dumps the log messages this chute is holding into a new chute
95       * @param newChute
96       */
97      public void transferTo(LogChute newChute)
98      {
99          synchronized(this)
100         {
101             if (!pendingMessages.isEmpty())
102             {
103                 // iterate and log each individual message...
104                 for(Iterator i = pendingMessages.iterator(); i.hasNext();)
105                 {
106                     Object[] data = (Object[])i.next();
107                     int level = ((Integer)data[0]).intValue();
108                     String message = (String)data[1];
109                     if (data.length == 2)
110                     {
111                         newChute.log(level, message);
112                     }
113                     else
114                     {
115                         newChute.log(level, message, (Throwable)data[2]);
116                     }
117                 }
118             }
119         }
120     }
121 
122 }