View Javadoc
1   package org.codehaus.plexus.velocity;
2   
3   /*
4    * Copyright 2001-2016 Codehaus 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.Enumeration;
20  import java.util.Properties;
21  
22  import org.apache.velocity.app.VelocityEngine;
23  import org.apache.velocity.runtime.RuntimeServices;
24  import org.apache.velocity.runtime.RuntimeConstants;
25  import org.apache.velocity.runtime.log.LogChute;
26  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
27  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
28  import org.codehaus.plexus.logging.AbstractLogEnabled;
29  
30  /**
31   * A simple velocity component implementation.
32   * <p/>
33   * A typical configuration will look like this:
34   * <pre>
35   *      <configuration>
36   *        <properties>
37   *          <property>
38   *            <name>resource.loader</name>
39   *            <value>classpath</value>
40   *          </property>
41   *          <property>
42   *            <name>classpath.resource.loader.class</name>
43   *            <value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
44   *          </property>
45   *        </properties>
46   *      </configuration>
47   * </pre>
48   */
49  public class DefaultVelocityComponent
50      extends AbstractLogEnabled
51      implements VelocityComponent, Initializable, LogChute
52  {
53      private VelocityEngine engine;
54  
55      private Properties properties;
56  
57      // ----------------------------------------------------------------------
58      //
59      // ----------------------------------------------------------------------
60  
61      public void initialize()
62          throws InitializationException
63      {
64          engine = new VelocityEngine();
65  
66          // avoid "unable to find resource 'VM_global_library.vm' in any resource loader."
67          engine.setProperty( RuntimeConstants.VM_LIBRARY, "" );
68  
69          engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this );
70  
71          if ( properties != null )
72          {
73              for ( Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); )
74              {
75                  String key = e.nextElement().toString();
76  
77                  String value = properties.getProperty( key );
78  
79                  engine.setProperty( key, value );
80  
81                  getLogger().debug( "Setting property: " + key + " => '" + value + "'." );
82              }
83          }
84  
85          try
86          {
87              engine.init();
88          }
89          catch ( Exception e )
90          {
91              throw new InitializationException( "Cannot start the Velocity engine", e );
92          }
93      }
94  
95      // ----------------------------------------------------------------------
96      //
97      // ----------------------------------------------------------------------
98  
99      public VelocityEngine getEngine()
100     {
101         return engine;
102     }
103     // ----------------------------------------------------------------------
104     //
105     // ----------------------------------------------------------------------
106 
107     private RuntimeServices runtimeServices;
108 
109     public void init( RuntimeServices runtimeServices )
110     {
111         this.runtimeServices = runtimeServices;
112     }
113 
114     public void log(int level, String message)
115     {
116         switch ( level )
117         {
118             case LogChute.WARN_ID:
119                 getLogger().warn( message );
120                 break;
121             case LogChute.INFO_ID:
122                 getLogger().info( message );
123                 break;
124             case LogChute.DEBUG_ID:
125             case LogChute.TRACE_ID:
126                 getLogger().debug( message );
127                 break;
128             case LogChute.ERROR_ID:
129                 getLogger().error( message );
130                 break;
131             default:
132                 getLogger().debug( message );
133                 break;
134         }
135     }
136 
137     public void log(int level, String message, Throwable t)
138     {
139         switch ( level )
140         {
141             case LogChute.WARN_ID:
142                 getLogger().warn( message, t );
143                 break;
144             case LogChute.INFO_ID:
145                 getLogger().info( message, t );
146                 break;
147             case LogChute.DEBUG_ID:
148             case LogChute.TRACE_ID:
149                 getLogger().debug( message, t );
150                 break;
151             case LogChute.ERROR_ID:
152                 getLogger().error( message, t );
153                 break;
154             default:
155                 getLogger().debug( message, t );
156                 break;
157         }
158     }
159 
160     public boolean isLevelEnabled( int level )
161     {
162          switch ( level )
163         {
164             case LogChute.WARN_ID:
165                 return getLogger().isWarnEnabled();
166             case LogChute.INFO_ID:
167                 return getLogger().isInfoEnabled();
168             case LogChute.DEBUG_ID:
169             case LogChute.TRACE_ID:
170                 return getLogger().isDebugEnabled();
171             case LogChute.ERROR_ID:
172                 return getLogger().isErrorEnabled();
173             default:
174                 return getLogger().isDebugEnabled();
175         }
176     }
177 }