| 1 | package org.apache.velocity.runtime.log; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import java.util.Vector; |
| 20 | import java.util.Enumeration; |
| 21 | |
| 22 | import org.apache.velocity.runtime.RuntimeServices; |
| 23 | |
| 24 | /** |
| 25 | * Pre-init logger. I believe that this was suggested by |
| 26 | * Carsten Ziegeler <cziegeler@sundn.de> and |
| 27 | * Jeroen C. van Gelderen. If this isn't correct, let me |
| 28 | * know as this was a good idea... |
| 29 | * |
| 30 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 31 | * @version $Id: PrimordialLogSystem.java,v 1.4.4.1 2004/03/03 23:22:56 geirm Exp $ |
| 32 | */ |
| 33 | public class PrimordialLogSystem implements LogSystem |
| 34 | { |
| 35 | private Vector pendingMessages = new Vector(); |
| 36 | private RuntimeServices rsvc = null; |
| 37 | |
| 38 | /** |
| 39 | * default CTOR. |
| 40 | */ |
| 41 | public PrimordialLogSystem() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | public void init( RuntimeServices rs ) |
| 46 | throws Exception |
| 47 | { |
| 48 | rsvc = rs; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * logs messages. All we do is store them until |
| 53 | * 'later'. |
| 54 | * |
| 55 | * @param level severity level |
| 56 | * @param message complete error message |
| 57 | */ |
| 58 | public void logVelocityMessage(int level, String message) |
| 59 | { |
| 60 | synchronized( this ) |
| 61 | { |
| 62 | Object[] data = new Object[2]; |
| 63 | data[0] = new Integer(level); |
| 64 | data[1] = message; |
| 65 | pendingMessages.addElement(data); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * dumps the log messages this logger is holding into a new logger |
| 71 | */ |
| 72 | public void dumpLogMessages( LogSystem newLogger ) |
| 73 | { |
| 74 | synchronized( this ) |
| 75 | { |
| 76 | if ( !pendingMessages.isEmpty()) |
| 77 | { |
| 78 | /* |
| 79 | * iterate and log each individual message... |
| 80 | */ |
| 81 | |
| 82 | for( Enumeration e = pendingMessages.elements(); e.hasMoreElements(); ) |
| 83 | { |
| 84 | Object[] data = (Object[]) e.nextElement(); |
| 85 | newLogger.logVelocityMessage(((Integer) data[0]).intValue(), (String) data[1]); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |