1 /*
2 * Copyright (c) 2007, Fraunhofer-Gesellschaft
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * (1) Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the disclaimer at the end.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * (2) Neither the name of Fraunhofer nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * DISCLAIMER
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35 package org.ogf.graap.wsag.server.actions.impl;
36
37 import java.io.BufferedWriter;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40 import java.io.OutputStreamWriter;
41 import java.text.MessageFormat;
42 import java.util.Date;
43 import java.util.Properties;
44
45 import org.apache.log4j.Logger;
46 import org.apache.velocity.Template;
47 import org.apache.velocity.VelocityContext;
48 import org.apache.velocity.app.Velocity;
49 import org.apache.velocity.exception.ResourceNotFoundException;
50 import org.apache.xmlbeans.XmlDateTime;
51 import org.ogf.graap.wsag.api.logging.LogMessage;
52
53 /**
54 * Default implementation to load files from the classpath an process these files as Velocity templates.
55 * Additional parameters can be specified, which can be used in the Velocity macros defined in the file.
56 *
57 * @author owaeld
58 *
59 */
60 public class FileTemplate
61 {
62
63 private static final Logger LOG = Logger.getLogger( FileTemplate.class );
64
65 /**
66 * Velocity properties filename. Applications may provide a custom properties file in order to overwrite
67 * default configuration. If this file does not exist in the classpath the default velocity configuration
68 * is used.
69 */
70 public static final String VELOCITY_PROPERTIES_FILE = "/wsag4j-velocity.properties";
71
72 /**
73 * Default velocity properties filename.
74 */
75 public static final String VELOCITY_PROPERTIES_FILE_DEFAULT = "/wsag4j-velocity.properties.default";
76
77 private VelocityContext context;
78
79 private Template template;
80
81 static
82 {
83 Properties properties = new Properties();
84
85 try
86 {
87 //
88 // try to load user provided velocity properties if available in the classpath, otherwise use
89 // default properties
90 //
91 InputStream in = FileTemplate.class.getResourceAsStream( VELOCITY_PROPERTIES_FILE );
92 if ( in == null )
93 {
94 in = FileTemplate.class.getResourceAsStream( VELOCITY_PROPERTIES_FILE_DEFAULT );
95 }
96
97 properties.load( in );
98
99 //
100 // set Velocity log4j logger name
101 //
102 properties.setProperty( "runtime.log.logsystem.log4j.logger", FileTemplate.class.getName() );
103
104 }
105 catch ( Exception e )
106 {
107 String message = "Failed to load velocity properties file [{0}]. Reason: {1}";
108 LOG.error( LogMessage.getMessage( message, VELOCITY_PROPERTIES_FILE, e.getMessage() ) );
109 LOG.error( "Resource loockup in WSAG4J classpath disabled." );
110 LOG.debug( e );
111 }
112
113 try
114 {
115 Velocity.init( properties );
116 }
117 catch ( Exception e )
118 {
119 LOG.error( "Failed to initialize velocity template engine.", e );
120 LOG.error( "Resource loockup in WSAG4J classpath disabled." );
121 }
122
123 }
124
125 /**
126 * Creates a new file template for the given file name. The file with the specified name is resolved from
127 * the classpath.
128 *
129 * @param filename
130 * the file name
131 */
132 public FileTemplate( String filename )
133 {
134 try
135 {
136 this.context = new VelocityContext();
137
138 String currDate = XmlDateTime.Factory.newValue( new Date() ).getStringValue();
139 addParameter( "currentTime", currDate );
140
141 //
142 // avoid escaping all the XPath expression
143 //
144 addParameter( "this", "$this" );
145
146 template = Velocity.getTemplate( filename );
147 }
148 catch ( ResourceNotFoundException e )
149 {
150 LOG.error( MessageFormat.format( "error loading template file [{0}]", filename ) );
151 LOG.error( e.getMessage() );
152 }
153 catch ( Exception e )
154 {
155 LOG.error( MessageFormat.format( "error processing template file [{0}]", filename ) );
156 LOG.error( e.getMessage() );
157 }
158 }
159
160 /**
161 * Adds a new parameter to the Velocity context. Parameters can be accessed in the template by its key.
162 *
163 * @param key
164 * the parameter name
165 *
166 * @param value
167 * the parameter value
168 */
169 public void addParameter( String key, Object value )
170 {
171 context.put( key, value );
172 }
173
174 /**
175 * Processes the file template as Velocity template.
176 *
177 * @param out
178 * the {@link OutputStream} where the result is written to
179 */
180 public void processTemplate( OutputStream out )
181 {
182 try
183 {
184 BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
185
186 if ( template != null )
187 {
188 template.merge( context, writer );
189 }
190
191 writer.flush();
192 writer.close();
193 }
194 catch ( Exception ex )
195 {
196 LOG.error( "error processing output for template:" );
197 LOG.error( ex.getMessage() );
198 }
199 }
200 }