/*
$Header: /cvsroot/xorm/xorm/src/org/xorm/XORM.java,v 1.28 2004/05/30 06:19:36 wbiggs Exp $
This file is part of XORM.
XORM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
XORM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XORM; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xorm;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import javax.jdo.JDOFatalUserException;
import javax.jdo.JDOHelper;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.spi.PersistenceCapable;
import javax.sql.DataSource;
import org.xorm.datastore.ConnectionInfo;
import org.xorm.datastore.sql.SQLConnectionInfo;
/**
* XORM helper class to instantiate objects and access XORM-specific features.
*/
public class XORM implements I15d {
/**
* The System property that the no-argument
* newPersistenceManagerFactory() method reads.
*/
public static final String XORM_PROPERTIES = "xorm.properties";
/**
* Construct a factory using the setting of the system property
* "xorm.properties" (the constant defined here as XORM_PROPERTIES)
* to reference a properties file. The current thread's
* ClassLoader is used.
*/
public static PersistenceManagerFactory newPersistenceManagerFactory() {
return newPersistenceManagerFactory
(Thread.currentThread().getContextClassLoader());
}
/**
* Construct a factory using the Java system property
* "xorm.properties" to find a properties file. It can refer to
* either a filename or a classpath resource.
*/
public static PersistenceManagerFactory newPersistenceManagerFactory(ClassLoader loader) {
// Uses the defaults
InputStream stream = null;
String xormProperties = System.getProperty(XORM_PROPERTIES);
if (xormProperties == null) {
throw new JDOFatalUserException(I18N.msg("E_no_sys_prop", XORM_PROPERTIES));
}
try {
stream = new FileInputStream(xormProperties);
} catch (FileNotFoundException e) {
stream = XORM.class.getResourceAsStream(xormProperties);
if(stream == null){
stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xormProperties);
}
if (stream == null) {
throw new JDOFatalUserException(I18N.msg("E_no_prop_file", xormProperties));
}
}
return newPersistenceManagerFactory(stream, loader);
}
/**
* Creates a PersistenceManagerFactory using the standard JDO
* properties.
*/
public static PersistenceManagerFactory newPersistenceManagerFactory(String propertiesFilename) {
return newPersistenceManagerFactory(propertiesFilename, Thread.currentThread().getContextClassLoader());
}
/**
* Creates a PersistenceManagerFactory using the standard JDO
* properties and the specified ClassLoader.
*/
public static PersistenceManagerFactory newPersistenceManagerFactory(String propertiesFilename, ClassLoader loader) {
FileInputStream stream = null;
try {
stream = new FileInputStream(propertiesFilename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (stream == null) {
throw new JDOFatalUserException(I18N.msg("E_no_prop_file", propertiesFilename));
}
return newPersistenceManagerFactory(stream,loader);
}
/** Creates a PersistenceManagerFactory using the standard JDO properties. */
public static PersistenceManagerFactory newPersistenceManagerFactory(InputStream propertiesInputStream ) {
return newPersistenceManagerFactory(propertiesInputStream,Thread.currentThread().getContextClassLoader());
}
/**
* Creates a PersistenceManagerFactory using the standard JDO
* properties and the specified ClassLoader.
*/
public static PersistenceManagerFactory newPersistenceManagerFactory(InputStream propertiesInputStream, ClassLoader loader) {
Properties properties = new Properties();
try {
properties.load(propertiesInputStream);
} catch (IOException e) {
e.printStackTrace();
}
return JDOHelper.getPersistenceManagerFactory(properties,loader);
}
/**
* Creates a PersistenceManagerFactory using a passed in Properties object.
*/
public static PersistenceManagerFactory
newPersistenceManagerFactory(Properties properties) {
return newPersistenceManagerFactory(properties,
Thread.currentThread().getContextClassLoader());
}
/**
* Creates a PersistenceManagerFactory using a passed in
* Properties object and the specified ClassLoader.
*/
public static PersistenceManagerFactory
newPersistenceManagerFactory(Properties properties,
ClassLoader loader) {
return JDOHelper.getPersistenceManagerFactory(properties, loader);
}
/**
* Creates a new instance managed by the given PersistenceManager and implementing
* the interface of the given interface class.
*/
public static Object newInstance(PersistenceManager mgr, Class mappedClass) {
if (!(mgr instanceof InterfaceManager)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pm"));
}
return create((InterfaceManagerFactory) mgr.getPersistenceManagerFactory(), mappedClass);
}
/**
* Creates a new instance managed by the given PersistenceManager and implementing
* the interface of the given interface class.
*/
public static Object newInstance(PersistenceManagerFactory factory, Class mappedClass) {
if (!(factory instanceof InterfaceManagerFactory)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
}
return create((InterfaceManagerFactory) factory, mappedClass);
}
/**
* Creates an instance of a datastore identity that can be used in
* PersistenceManager.getObjectById().
*/
public static ObjectId newObjectId(Class mappedClass, int id) {
return new ObjectId(mappedClass, new Integer(id));
}
/**
* Creates an instance of a datastore identity that can be used in
* PersistenceManager.getObjectById().
*/
public static ObjectId newObjectId(Class mappedClass, long id) {
return new ObjectId(mappedClass, new Long(id));
}
/**
* Creates an instance of a datastore identity that can be used in
* PersistenceManager.getObjectById().
*/
public static ObjectId newObjectId(Class mappedClass, Object id) {
return new ObjectId(mappedClass, id);
}
/**
* Takes an object ID as returned by PersistenceManager.getObjectId()
* or JDOHelper.getObjectId() and returns the portion that refers
* to the datastore primary key. This is the inverse of the
* newObjectId() operation.
*/
public static Object extractPrimaryKey(Object objectId) {
return ((ObjectId) objectId).id;
}
/**
* Takes an object ID as returned by PersistenceManager.getObjectId()
* or JDOHelper.getObjectId() and returns the portion that refers
* to the datastore primary key. This is the inverse of the
* newObjectId() operation.
*/
public static int extractPrimaryKeyAsInt(Object objectId) {
return ((Number) extractPrimaryKey(objectId)).intValue();
}
/**
* Takes an object ID as returned by PersistenceManager.getObjectId()
* or JDOHelper.getObjectId() and returns the portion that refers
* to the datastore primary key. This is the inverse of the
* newObjectId() operation.
*/
public static long extractPrimaryKeyAsLong(Object objectId) {
return ((Number) extractPrimaryKey(objectId)).longValue();
}
/**
* Takes an object ID as returned by PersistenceManager.getObjectId()
* or JDOHelper.getObjectId() and returns the portion that refers
* to the class of the object. This is the inverse of the
* newObjectId() operation.
*/
public static Class extractClass(Object objectId) {
return ((ObjectId) objectId).mappedClass;
}
/**
* Returns the ModelMapping associated with the given manager's factory.
* Throws JDOFatalUserException if it is invoked on a non-XORM manager.
*/
public static ModelMapping getModelMapping(PersistenceManager mgr) {
if (!(mgr instanceof InterfaceManager)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pm"));
}
return ((InterfaceManager) mgr).getInterfaceManagerFactory().getModelMapping();
}
/**
* Returns the ModelMapping associated with the given factory.
* Throws JDOFatalUserException if it is invoked on a non-XORM factory.
*/
public static ModelMapping getModelMapping(PersistenceManagerFactory factory) {
if (!(factory instanceof InterfaceManagerFactory)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
}
return ((InterfaceManagerFactory) factory).getModelMapping();
}
/**
* Returns the FetchGroupManager associated with the given persistence
* manager.
*/
public static FetchGroupManager getFetchGroupManager(PersistenceManager mgr) {
if (!(mgr instanceof InterfaceManager)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pm"));
}
return ((InterfaceManager) mgr).getInterfaceManagerFactory().getFetchGroupManager();
}
/**
* Creates a new proxy object that implements the desired interface.
* New instances are not automatically persistent and must be made so
* by calling mgr.makePersistent(Object).
*
* @exception JDOUserException if no mapping is found for the class
*/
static Object create(InterfaceManagerFactory factory, Class clazz) {
if (PersistenceCapable.class.isAssignableFrom(clazz)) {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
throw new JDOUserException(I18N.msg("E_instantiation", clazz.getName()));
} catch (IllegalAccessException e) {
throw new JDOUserException(I18N.msg("E_illegal_access", clazz.getName()));
}
} else {
ClassMapping classMapping = factory.getModelMapping().getClassMapping(clazz);
InterfaceInvocationHandler handler = new InterfaceInvocationHandler(factory, classMapping, null);
return handler.newProxy();
}
}
/**
* Clears all data from the factory-wide cache.
*/
public static void clearCache(PersistenceManagerFactory factory) {
if (!(factory instanceof InterfaceManagerFactory)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
}
((InterfaceManagerFactory) factory).clearCache();
}
/**
* Returns the data source in use by the PersistenceManagerFactory.
* This allows you to acquire native JDBC connections to use
* alongside JDO.
*/
public static DataSource getDataSource(PersistenceManagerFactory factory) {
if (!(factory instanceof InterfaceManagerFactory)) {
throw new JDOFatalUserException(I18N.msg("E_non_xorm_pmf"));
}
ConnectionInfo connectionInfo = ((InterfaceManagerFactory) factory)
.getConnectionInfo();
if (connectionInfo instanceof SQLConnectionInfo) {
return ((SQLConnectionInfo) connectionInfo).getDataSource();
}
return null;
}
public static String getVersion() {
return "Beta 6";
}
public static void main(String[] argv) {
System.out.println("XORM version " + getVersion() + " (C) 2002-2004 The XORM Project");
System.out.println("XORM comes with ABSOLUTELY NO WARRANTY; for details see the file COPYING.");
System.out.println("This is free software, and you are welcome to redistribute it");
System.out.println("under certain conditions as specified in COPYING.");
}
}