001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/XORM.java,v 1.28 2004/05/30 06:19:36 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.util.Properties;
027: import javax.jdo.JDOFatalUserException;
028: import javax.jdo.JDOHelper;
029: import javax.jdo.JDOUserException;
030: import javax.jdo.PersistenceManager;
031: import javax.jdo.PersistenceManagerFactory;
032: import javax.jdo.spi.PersistenceCapable;
033: import javax.sql.DataSource;
034:
035: import org.xorm.datastore.ConnectionInfo;
036: import org.xorm.datastore.sql.SQLConnectionInfo;
037:
038: /**
039: * XORM helper class to instantiate objects and access XORM-specific features.
040: */
041: public class XORM implements I15d {
042: /**
043: * The System property that the no-argument
044: * newPersistenceManagerFactory() method reads.
045: */
046: public static final String XORM_PROPERTIES = "xorm.properties";
047:
048: /**
049: * Construct a factory using the setting of the system property
050: * "xorm.properties" (the constant defined here as XORM_PROPERTIES)
051: * to reference a properties file. The current thread's
052: * ClassLoader is used.
053: */
054: public static PersistenceManagerFactory newPersistenceManagerFactory() {
055: return newPersistenceManagerFactory(Thread.currentThread()
056: .getContextClassLoader());
057: }
058:
059: /**
060: * Construct a factory using the Java system property
061: * "xorm.properties" to find a properties file. It can refer to
062: * either a filename or a classpath resource.
063: */
064: public static PersistenceManagerFactory newPersistenceManagerFactory(
065: ClassLoader loader) {
066: // Uses the defaults
067: InputStream stream = null;
068: String xormProperties = System.getProperty(XORM_PROPERTIES);
069: if (xormProperties == null) {
070: throw new JDOFatalUserException(I18N.msg("E_no_sys_prop",
071: XORM_PROPERTIES));
072: }
073: try {
074: stream = new FileInputStream(xormProperties);
075: } catch (FileNotFoundException e) {
076: stream = XORM.class.getResourceAsStream(xormProperties);
077: if (stream == null) {
078: stream = Thread.currentThread().getContextClassLoader()
079: .getResourceAsStream(xormProperties);
080: }
081: if (stream == null) {
082: throw new JDOFatalUserException(I18N.msg(
083: "E_no_prop_file", xormProperties));
084: }
085: }
086: return newPersistenceManagerFactory(stream, loader);
087: }
088:
089: /**
090: * Creates a PersistenceManagerFactory using the standard JDO
091: * properties.
092: */
093: public static PersistenceManagerFactory newPersistenceManagerFactory(
094: String propertiesFilename) {
095: return newPersistenceManagerFactory(propertiesFilename, Thread
096: .currentThread().getContextClassLoader());
097: }
098:
099: /**
100: * Creates a PersistenceManagerFactory using the standard JDO
101: * properties and the specified ClassLoader.
102: */
103: public static PersistenceManagerFactory newPersistenceManagerFactory(
104: String propertiesFilename, ClassLoader loader) {
105: FileInputStream stream = null;
106: try {
107: stream = new FileInputStream(propertiesFilename);
108: } catch (FileNotFoundException e) {
109: e.printStackTrace();
110: }
111: if (stream == null) {
112: throw new JDOFatalUserException(I18N.msg("E_no_prop_file",
113: propertiesFilename));
114: }
115: return newPersistenceManagerFactory(stream, loader);
116: }
117:
118: /** Creates a PersistenceManagerFactory using the standard JDO properties. */
119: public static PersistenceManagerFactory newPersistenceManagerFactory(
120: InputStream propertiesInputStream) {
121: return newPersistenceManagerFactory(propertiesInputStream,
122: Thread.currentThread().getContextClassLoader());
123: }
124:
125: /**
126: * Creates a PersistenceManagerFactory using the standard JDO
127: * properties and the specified ClassLoader.
128: */
129: public static PersistenceManagerFactory newPersistenceManagerFactory(
130: InputStream propertiesInputStream, ClassLoader loader) {
131: Properties properties = new Properties();
132: try {
133: properties.load(propertiesInputStream);
134: } catch (IOException e) {
135: e.printStackTrace();
136: }
137: return JDOHelper.getPersistenceManagerFactory(properties,
138: loader);
139: }
140:
141: /**
142: * Creates a PersistenceManagerFactory using a passed in Properties object.
143: */
144: public static PersistenceManagerFactory newPersistenceManagerFactory(
145: Properties properties) {
146: return newPersistenceManagerFactory(properties, Thread
147: .currentThread().getContextClassLoader());
148: }
149:
150: /**
151: * Creates a PersistenceManagerFactory using a passed in
152: * Properties object and the specified ClassLoader.
153: */
154: public static PersistenceManagerFactory newPersistenceManagerFactory(
155: Properties properties, ClassLoader loader) {
156: return JDOHelper.getPersistenceManagerFactory(properties,
157: loader);
158: }
159:
160: /**
161: * Creates a new instance managed by the given PersistenceManager and implementing
162: * the interface of the given interface class.
163: */
164: public static Object newInstance(PersistenceManager mgr,
165: Class mappedClass) {
166: if (!(mgr instanceof InterfaceManager)) {
167: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pm"));
168: }
169: return create((InterfaceManagerFactory) mgr
170: .getPersistenceManagerFactory(), mappedClass);
171: }
172:
173: /**
174: * Creates a new instance managed by the given PersistenceManager and implementing
175: * the interface of the given interface class.
176: */
177: public static Object newInstance(PersistenceManagerFactory factory,
178: Class mappedClass) {
179: if (!(factory instanceof InterfaceManagerFactory)) {
180: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
181: }
182: return create((InterfaceManagerFactory) factory, mappedClass);
183: }
184:
185: /**
186: * Creates an instance of a datastore identity that can be used in
187: * PersistenceManager.getObjectById().
188: */
189: public static ObjectId newObjectId(Class mappedClass, int id) {
190: return new ObjectId(mappedClass, new Integer(id));
191: }
192:
193: /**
194: * Creates an instance of a datastore identity that can be used in
195: * PersistenceManager.getObjectById().
196: */
197: public static ObjectId newObjectId(Class mappedClass, long id) {
198: return new ObjectId(mappedClass, new Long(id));
199: }
200:
201: /**
202: * Creates an instance of a datastore identity that can be used in
203: * PersistenceManager.getObjectById().
204: */
205: public static ObjectId newObjectId(Class mappedClass, Object id) {
206: return new ObjectId(mappedClass, id);
207: }
208:
209: /**
210: * Takes an object ID as returned by PersistenceManager.getObjectId()
211: * or JDOHelper.getObjectId() and returns the portion that refers
212: * to the datastore primary key. This is the inverse of the
213: * newObjectId() operation.
214: */
215: public static Object extractPrimaryKey(Object objectId) {
216: return ((ObjectId) objectId).id;
217: }
218:
219: /**
220: * Takes an object ID as returned by PersistenceManager.getObjectId()
221: * or JDOHelper.getObjectId() and returns the portion that refers
222: * to the datastore primary key. This is the inverse of the
223: * newObjectId() operation.
224: */
225: public static int extractPrimaryKeyAsInt(Object objectId) {
226: return ((Number) extractPrimaryKey(objectId)).intValue();
227: }
228:
229: /**
230: * Takes an object ID as returned by PersistenceManager.getObjectId()
231: * or JDOHelper.getObjectId() and returns the portion that refers
232: * to the datastore primary key. This is the inverse of the
233: * newObjectId() operation.
234: */
235: public static long extractPrimaryKeyAsLong(Object objectId) {
236: return ((Number) extractPrimaryKey(objectId)).longValue();
237: }
238:
239: /**
240: * Takes an object ID as returned by PersistenceManager.getObjectId()
241: * or JDOHelper.getObjectId() and returns the portion that refers
242: * to the class of the object. This is the inverse of the
243: * newObjectId() operation.
244: */
245: public static Class extractClass(Object objectId) {
246: return ((ObjectId) objectId).mappedClass;
247: }
248:
249: /**
250: * Returns the ModelMapping associated with the given manager's factory.
251: * Throws JDOFatalUserException if it is invoked on a non-XORM manager.
252: */
253: public static ModelMapping getModelMapping(PersistenceManager mgr) {
254: if (!(mgr instanceof InterfaceManager)) {
255: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pm"));
256: }
257: return ((InterfaceManager) mgr).getInterfaceManagerFactory()
258: .getModelMapping();
259: }
260:
261: /**
262: * Returns the ModelMapping associated with the given factory.
263: * Throws JDOFatalUserException if it is invoked on a non-XORM factory.
264: */
265: public static ModelMapping getModelMapping(
266: PersistenceManagerFactory factory) {
267: if (!(factory instanceof InterfaceManagerFactory)) {
268: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
269: }
270: return ((InterfaceManagerFactory) factory).getModelMapping();
271: }
272:
273: /**
274: * Returns the FetchGroupManager associated with the given persistence
275: * manager.
276: */
277: public static FetchGroupManager getFetchGroupManager(
278: PersistenceManager mgr) {
279: if (!(mgr instanceof InterfaceManager)) {
280: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pm"));
281: }
282: return ((InterfaceManager) mgr).getInterfaceManagerFactory()
283: .getFetchGroupManager();
284: }
285:
286: /**
287: * Creates a new proxy object that implements the desired interface.
288: * New instances are not automatically persistent and must be made so
289: * by calling mgr.makePersistent(Object).
290: *
291: * @exception JDOUserException if no mapping is found for the class
292: */
293: static Object create(InterfaceManagerFactory factory, Class clazz) {
294: if (PersistenceCapable.class.isAssignableFrom(clazz)) {
295: try {
296: return clazz.newInstance();
297: } catch (InstantiationException e) {
298: throw new JDOUserException(I18N.msg("E_instantiation",
299: clazz.getName()));
300: } catch (IllegalAccessException e) {
301: throw new JDOUserException(I18N.msg("E_illegal_access",
302: clazz.getName()));
303: }
304: } else {
305: ClassMapping classMapping = factory.getModelMapping()
306: .getClassMapping(clazz);
307: InterfaceInvocationHandler handler = new InterfaceInvocationHandler(
308: factory, classMapping, null);
309: return handler.newProxy();
310: }
311: }
312:
313: /**
314: * Clears all data from the factory-wide cache.
315: */
316: public static void clearCache(PersistenceManagerFactory factory) {
317: if (!(factory instanceof InterfaceManagerFactory)) {
318: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
319: }
320: ((InterfaceManagerFactory) factory).clearCache();
321: }
322:
323: /**
324: * Returns the data source in use by the PersistenceManagerFactory.
325: * This allows you to acquire native JDBC connections to use
326: * alongside JDO.
327: */
328: public static DataSource getDataSource(
329: PersistenceManagerFactory factory) {
330: if (!(factory instanceof InterfaceManagerFactory)) {
331: throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
332: }
333:
334: ConnectionInfo connectionInfo = ((InterfaceManagerFactory) factory)
335: .getConnectionInfo();
336: if (connectionInfo instanceof SQLConnectionInfo) {
337: return ((SQLConnectionInfo) connectionInfo).getDataSource();
338: }
339: return null;
340: }
341:
342: public static String getVersion() {
343: return "Beta 6";
344: }
345:
346: public static void main(String[] argv) {
347: System.out.println("XORM version " + getVersion()
348: + " (C) 2002-2004 The XORM Project");
349: System.out
350: .println("XORM comes with ABSOLUTELY NO WARRANTY; for details see the file COPYING.");
351: System.out
352: .println("This is free software, and you are welcome to redistribute it");
353: System.out
354: .println("under certain conditions as specified in COPYING.");
355: }
356: }
|