001: /**
002: * Copyright (C) 2001-2005 France Telecom R&D
003: */package org.objectweb.speedo.ejb;
004:
005: import org.objectweb.speedo.AbstractSpeedo;
006: import org.objectweb.speedo.api.SpeedoProperties;
007: import org.objectweb.speedo.lib.Personality;
008: import org.objectweb.speedo.pm.ejb.api.EJBPOManagerFactoryItf;
009:
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.util.HashMap;
013: import java.util.Map;
014: import java.util.Properties;
015:
016: import javax.persistence.EntityManager;
017: import javax.persistence.EntityManagerFactory;
018: import javax.persistence.PersistenceContextType;
019: import javax.persistence.PersistenceException;
020: import javax.persistence.spi.PersistenceProvider;
021: import javax.persistence.spi.PersistenceUnitInfo;
022:
023: public class EJBSpeedo extends AbstractSpeedo implements
024: PersistenceProvider, EJBPOManagerFactoryItf {
025:
026: public EJBSpeedo() throws Throwable {
027: super ();
028: }
029:
030: /**
031: * It creates and initializes a real POManagerFactory with
032: * Julia (Fractal implementation).
033: */
034: public EJBSpeedo(Map props) throws Throwable {
035: super (props);
036: }
037:
038: protected void throwUserException(String msg) {
039: throw new PersistenceException(msg);
040: }
041:
042: private Map getSpeedoPropertiesFromFile(String fileName) {
043: ClassLoader loader = Thread.currentThread()
044: .getContextClassLoader();
045: InputStream is = loader.getResourceAsStream(fileName);
046: if (is == null) {
047: return null;
048: }
049: Properties props = new Properties();
050: try {
051: props.load(is);
052: } catch (IOException e) {
053: throw new PersistenceException(
054: "Error during the file reading: " + fileName);
055: }
056: return new HashMap(props);
057: }
058:
059: protected boolean isOptimisticTransaction(Map props) {
060: // TODO Auto-generated method stub
061: return false;
062: }
063:
064: // IMPLEMENTS THE INTERFACE PersistenceProvider //
065: //----------------------------------------------//
066:
067: public EntityManagerFactory createEntityManagerFactory(String name,
068: Map props) {
069: if (props == null || props.size() == 0) {
070: //load file from the the emf name
071: props = getSpeedoPropertiesFromFile("speedo-" + name
072: + ".properties");
073: if (props == null || props.size() == 0) {
074: return null;
075: }
076: }
077: try {
078: return new EJBSpeedo(props);
079: } catch (Throwable e) {
080: throw new PersistenceException(e.getMessage());
081: }
082: }
083:
084: public EntityManagerFactory createContainerEntityManagerFactory(
085: PersistenceUnitInfo pi) {
086: if (!SpeedoProperties.VENDOR_NAME.equals(pi
087: .getPersistenceProviderClassName())) {
088: return null;
089: }
090: //TODO: support direct reference to Datasource
091: //Object ds1 = pi.getJtaDataSource();
092: //TODO: support non JTA datasource for optimistic concurrency
093: //Object ds2 = pi.getNonJtaDataSource();
094: // TODO implements createEntityManagerFactory(PersistenceInfo)
095: return createEntityManagerFactory(pi.getPersistenceUnitName(),
096: new HashMap(pi.getProperties()));
097: }
098:
099: // IMPLEMENTS THE INTERFACE EJBPOManagerFactoryItf //
100: //-------------------------------------------------//
101:
102: public Personality getPersonality() {
103: return Personality.EJB;
104: }
105:
106: public void close() {
107: ((EJBPOManagerFactoryItf) delegate).close();
108: }
109:
110: public EntityManager createEntityManager() {
111: return ((EJBPOManagerFactoryItf) delegate)
112: .createEntityManager();
113: }
114:
115: public EntityManager createEntityManager(PersistenceContextType pct) {
116: return ((EJBPOManagerFactoryItf) delegate)
117: .createEntityManager(pct);
118: }
119:
120: public EntityManager getEntityManager() {
121: return ((EJBPOManagerFactoryItf) delegate).getEntityManager();
122: }
123:
124: public boolean isOpen() {
125: return ((EJBPOManagerFactoryItf) delegate).isOpen();
126: }
127:
128: }
|