1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
55
56
57
58
59
60 public class FileTemplate
61 {
62
63 private static final Logger LOG = Logger.getLogger( FileTemplate.class );
64
65
66
67
68
69
70 public static final String VELOCITY_PROPERTIES_FILE = "/wsag4j-velocity.properties";
71
72
73
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
89
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
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
127
128
129
130
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
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
162
163
164
165
166
167
168
169 public void addParameter( String key, Object value )
170 {
171 context.put( key, value );
172 }
173
174
175
176
177
178
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 }