1 package org.apache.turbine.services.castor;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.File;
58 import java.io.FileNotFoundException;
59 import java.io.FileReader;
60 import java.io.FileWriter;
61 import java.io.IOException;
62 import java.io.PrintWriter;
63 import java.util.Properties;
64 import org.apache.turbine.services.InitializationException;
65 import org.apache.turbine.services.TurbineBaseService;
66 import org.apache.turbine.util.Log;
67 import org.exolab.castor.jdo.Database;
68 import org.exolab.castor.jdo.DatabaseNotFoundException;
69 import org.exolab.castor.jdo.JDO;
70 import org.exolab.castor.jdo.PersistenceException;
71 import org.exolab.castor.mapping.MappingException;
72 import org.exolab.castor.util.Logger;
73 import org.xml.sax.EntityResolver;
74 import org.xml.sax.InputSource;
75
76 /***
77 * This is a Service that offers access to the object to relational
78 * mapping from <a href="http://castor.exolab.org">Castor</a>.
79 *
80 * Here's an example of how you might use it:<br>
81 *
82 * <code><pre>
83 * TurbineServices ts = TurbineServices.getInstance();
84 * CastorService cs = (CastorService) ts.getService (CastorService.SERVICE_NAME);
85 * Database db = cs.{@link #getDatabase};
86 * db.begin();
87 * ...
88 * db.commit();
89 * db.close();
90 * </pre></code>
91 *
92 * The following properties are needed to configure this service:<br>
93 *
94 * <code><pre>
95 * services.CastorService.classname=org.apache.turbine.services.castor.TurbineCastorService
96 * services.CastorService.properties.logfile=/tmp/castor.log
97 * services.CastorService.properties.logprefix=turbinecastor
98 * services.CastorService.properties.databasefile=/tmp/database.xml
99 * services.CastorService.properties.databasename=turbine
100 * </pre></code>
101 *
102 * <dl>
103 * <dt>classname</dt><dd>the classname of this service</dd>
104 * <dt>logfile</dt><dd>the path to a writable file. Castor uses its own log file</dd>
105 * <dt>logprefix</dt><dd>the prefix used in the logfile to distinguish from
106 * Castor log entry and this Service. This is a recommended property</dd>
107 * <dt>databasefile</dt><dd>the path to a readable file defining the mappings of the
108 * java objects an the underlying tables/columns</dd>
109 * <dt>databasename</dt><dd>references a name of a database definition tag used in
110 * the mapping file</dd>
111 * </dl>
112 *
113 * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
114 * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a>
115 * @version $Id: TurbineCastorService.java,v 1.2 2002/07/11 16:53:28 mpoeschl Exp $
116 * @deprecated
117 */
118 public class TurbineCastorService
119 extends TurbineBaseService
120 implements CastorService
121 {
122 /***
123 * The name of the database to use (references the database tag in
124 * the mapping file, not the real database name).
125 */
126 private String databasename = null;
127
128 /*** The mapping file for the database. */
129 private String databasefile = null;
130
131 /*** Castor logger. */
132 private PrintWriter logger = null;
133
134 /*** Castor JDO object. */
135 private JDO jdo = null;
136
137 /***
138 * Called the first time the Service is used.
139 */
140 public void init()
141 throws InitializationException
142 {
143 try
144 {
145 initCastor();
146 setInit(true);
147 }
148 catch (Exception e)
149 {
150 throw new InitializationException("TurbineCastorService failed to initalize", e);
151 }
152 }
153
154 /***
155 * Checks the properties and hands the mapping file to Castor.
156 *
157 * @exception IOException, if there were problems to get the
158 * logfile.
159 * @exception MappingException, if the mapping file is invalid.
160 * @exception Exception, if no databasefile or databasename is
161 * given in the properties.
162 */
163 private void initCastor ()
164 throws IOException,
165 MappingException,
166 Exception
167 {
168 Properties props = getProperties();
169
170 String logprefix = props.getProperty (LOGPREFIX_PROPERTY);
171 if (logprefix == null)
172 {
173 logprefix = DEFAULT_LOGPREFIX;
174 }
175 String logfile = props.getProperty (LOGFILE_PROPERTY);
176 if (logfile == null)
177 {
178 Log.warn ("CastorService no LogFile property specified");
179 }
180 else
181 {
182 logger = new Logger (new FileWriter (logfile)).setPrefix(logprefix);
183 }
184
185 databasename = props.getProperty (DATABASENAME_PROPERTY);
186 if (databasename == null)
187 {
188 throw new Exception ("TurbineCastor: missing databasename propertiy");
189 }
190
191 databasefile = props.getProperty (DATABASEFILE_PROPERTY);
192 if (databasefile == null)
193 {
194 throw new Exception ("TurbineCastor: missing databasefile propertiy");
195 }
196
197 // FIXME: Upon the release and inclusion of Castor 0.8.9,
198 // remove this block of code and un-comment the block in
199 // getJDO().
200 JDO.loadConfiguration (new InputSource (databasefile),
201 new LocalResolver(databasefile, logger),
202 getClass().getClassLoader());
203 }
204
205 /***
206 * Gets you the PrintWriter object Castor is using for logging.
207 *
208 * @return PrintWriter logger object of Castor.
209 */
210 public PrintWriter getCastorLogger ()
211 {
212 return logger;
213 }
214
215 /***
216 * Gets a JDO object initialized to the database mentioned in the
217 * property databasename.
218 *
219 * @return JDO object initialized to the database mentioned in the
220 * property databasename.
221 */
222 public JDO getJDO ()
223 {
224 if (jdo == null) {
225 jdo = new JDO (databasename);
226 /*
227 // FIXME: Un-comment this block of code upon the release
228 // and inclusion of Castor 0.8.9.
229 jdo.setConfiguration(databasefile);
230 jdo.setEntityResolver(new LocalResolver(databasefile,
231 logger));
232 jdo.setClassLoader(getClass().getClassLoader());
233 */
234 jdo.setLogWriter (logger);
235 }
236 return jdo;
237 }
238
239 /***
240 * Gets a Castor Database object in the context of the defined
241 * environment.
242 *
243 * @return Database object
244 * @exception DatabaseNotFoundException, if attempted to open a
245 * database that does not exist.
246 * @exception PersistenceException, if database access failed.
247 */
248 public Database getDatabase ()
249 throws DatabaseNotFoundException,
250 PersistenceException
251 {
252 return this.getJDO().getDatabase();
253 }
254
255 /***
256 * Internal class to resolve entities in the database mapping
257 * file. Included definition for object mappings must be relative
258 * to the database mapping file.
259 */
260 private class LocalResolver
261 implements EntityResolver
262 {
263 /*** Absolute path to the mapping files. */
264 private String prefix = "unknown";
265
266 /*** A PrintWriter to log activity during resolving. */
267 private PrintWriter logger = null;
268
269 /***
270 * Constructor of this EntityResolver
271 *
272 * @param databasefile A String with the path to the main
273 * mapping file.
274 * @param logger A PrintWriter logger to log activity while
275 * resolving entities.
276 */
277 public LocalResolver (String databasefile,
278 PrintWriter logger)
279 {
280 super ();
281 int i = databasefile.lastIndexOf (File.separator);
282 this.prefix = databasefile.substring (0, i+1);
283 this.logger = logger;
284 }
285
286 /***
287 * Resolves entities in the main database mapping file. Castor
288 * uses this method to get an InputSource object for an
289 * included object mapping definition.
290 *
291 * @param publicId A String with the public id.
292 * @param systemId A String with the system id.
293 * @exception FileNotFoundException, if file was not found.
294 */
295 public InputSource resolveEntity (String publicId,
296 String systemId)
297 throws FileNotFoundException
298 {
299 return new InputSource (new FileReader (prefix + systemId));
300 }
301 }
302 }
This page was automatically generated by Maven