01: package org.apache.velocity.tools.view.servlet;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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 javax.servlet.ServletContext;
23:
24: import org.apache.velocity.runtime.log.LogSystem;
25: import org.apache.velocity.runtime.RuntimeConstants;
26: import org.apache.velocity.runtime.RuntimeServices;
27:
28: /**
29: * Simple wrapper for the servlet log. This has Velocity log
30: * messages to ServletContext.log(String).
31: *
32: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
33: * @version $Revision: 477914 $ $Date: 2006-11-21 13:52:11 -0800 (Tue, 21 Nov 2006) $
34: */
35: public class ServletLogger implements LogSystem {
36: protected ServletContext servletContext = null;
37:
38: public static final String PREFIX = " Velocity ";
39:
40: /**
41: * Construct a simple logger for a servlet environment.
42: * <br>
43: * NOTE: this class expects that the ServletContext has already
44: * been placed in the runtime's application attributes
45: * under its full class name (i.e. "javax.servlet.ServletContext").
46: */
47: public ServletLogger() {
48: }
49:
50: /**
51: * init()
52: *
53: * @throws IllegalStateException if the ServletContext is not available
54: * in the application attributes under the appropriate key.
55: */
56: public void init(RuntimeServices rs) throws Exception {
57: Object obj = rs.getApplicationAttribute(ServletContext.class
58: .getName());
59: if (obj == null) {
60: throw new IllegalStateException(
61: "Could not retrieve ServletContext from application attributes!");
62: }
63: servletContext = (ServletContext) obj;
64: }
65:
66: /**
67: * Send a log message from Velocity.
68: */
69: public void logVelocityMessage(int level, String message) {
70: switch (level) {
71: case LogSystem.WARN_ID:
72: servletContext.log(PREFIX + RuntimeConstants.WARN_PREFIX
73: + message);
74: break;
75: case LogSystem.INFO_ID:
76: servletContext.log(PREFIX + RuntimeConstants.INFO_PREFIX
77: + message);
78: break;
79: case LogSystem.DEBUG_ID:
80: servletContext.log(PREFIX + RuntimeConstants.DEBUG_PREFIX
81: + message);
82: break;
83: case LogSystem.ERROR_ID:
84: servletContext.log(PREFIX + RuntimeConstants.ERROR_PREFIX
85: + message);
86: break;
87: default:
88: servletContext.log(PREFIX + " : " + message);
89: break;
90: }
91: }
92:
93: }
|