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 public static final String VELOCITY_PROPERTIES_FILE = "/wsag4j-velocity.properties";
69
70 private VelocityContext context;
71
72 private Template template;
73
74 static
75 {
76 Properties properties = new Properties();
77
78 try
79 {
80 InputStream in = FileTemplate.class.getResourceAsStream( VELOCITY_PROPERTIES_FILE );
81 properties.load( in );
82
83
84
85
86 properties.setProperty( "runtime.log.logsystem.log4j.logger", FileTemplate.class.getName() );
87
88 }
89 catch ( Exception e )
90 {
91 String message = "Failed to load velocity properties file [{0}]. Reason: {1}";
92 LOG.error( LogMessage.getMessage( message, VELOCITY_PROPERTIES_FILE, e.getMessage() ) );
93 LOG.error( "Resource loockup in WSAG4J classpath disabled." );
94 LOG.debug( e );
95 }
96
97 try
98 {
99 Velocity.init( properties );
100 }
101 catch ( Exception e )
102 {
103 LOG.error( "Failed to initialize velocity template engine.", e );
104 LOG.error( "Resource loockup in WSAG4J classpath disabled." );
105 }
106
107 }
108
109
110
111
112
113
114
115
116 public FileTemplate( String filename )
117 {
118 try
119 {
120 this.context = new VelocityContext();
121
122 String currDate = XmlDateTime.Factory.newValue( new Date() ).getStringValue();
123 addParameter( "currentTime", currDate );
124
125
126
127
128 addParameter( "this", "$this" );
129
130 template = Velocity.getTemplate( filename );
131 }
132 catch ( ResourceNotFoundException e )
133 {
134 LOG.error( MessageFormat.format( "error loading template file [{0}]", filename ) );
135 LOG.error( e.getMessage() );
136 }
137 catch ( Exception e )
138 {
139 LOG.error( MessageFormat.format( "error processing template file [{0}]", filename ) );
140 LOG.error( e.getMessage() );
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153 public void addParameter( String key, Object value )
154 {
155 context.put( key, value );
156 }
157
158
159
160
161
162
163
164 public void processTemplate( OutputStream out )
165 {
166 try
167 {
168 BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
169
170 if ( template != null )
171 {
172 template.merge( context, writer );
173 }
174
175 writer.flush();
176 writer.close();
177 }
178 catch ( Exception ex )
179 {
180 LOG.error( "error processing output for template:" );
181 LOG.error( ex.getMessage() );
182 }
183 }
184 }