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 998264 2010-09-17 19:13:02Z apetrelli $
35   * @since 1.5
36   */
37  class HoldingLogChute implements LogChute
38  {
39      private Vector pendingMessages = new Vector();
40      private volatile boolean transferring = false;
41  
42      /**
43       * @see org.apache.velocity.runtime.log.LogChute#init(org.apache.velocity.runtime.RuntimeServices)
44       */
45      public void init(RuntimeServices rs)
46      {
47      }
48  
49      /**
50       * Logs messages. All we do is store them until 'later'.
51       *
52       * @param level severity level
53       * @param message complete error message
54       */
55      public synchronized void log(int level, String message)
56      {
57          if (!transferring)
58          {
59              Object[] data = new Object[2];
60              data[0] = new Integer(level);
61              data[1] = message;
62              pendingMessages.addElement(data);
63          }
64      }
65  
66      /**
67       * Logs messages and errors. All we do is store them until 'later'.
68       *
69       * @param level severity level
70       * @param message complete error message
71       * @param t the accompanying java.lang.Throwable
72       */
73      public synchronized void log(int level, String message, Throwable t)
74      {
75          if (!transferring)
76          {
77              Object[] data = new Object[3];
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 synchronized void transferTo(LogChute newChute)
98      {
99          if (!transferring && !pendingMessages.isEmpty())
100         {
101             // let the other methods know what's up
102             transferring = true;
103 
104             // iterate and log each individual message...
105             for(Iterator i = pendingMessages.iterator(); i.hasNext();)
106             {
107                 Object[] data = (Object[])i.next();
108                 int level = ((Integer)data[0]).intValue();
109                 String message = (String)data[1];
110                 if (data.length == 2)
111                 {
112                     newChute.log(level, message);
113                 }
114                 else
115                 {
116                     newChute.log(level, message, (Throwable)data[2]);
117                 }
118             }
119         }
120     }
121 
122 }