001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.assembler.classic;
017:
018: import java.io.File;
019: import java.util.HashMap;
020: import javax.persistence.EntityManagerFactory;
021: import javax.persistence.spi.PersistenceProvider;
022: import javax.persistence.spi.PersistenceUnitTransactionType;
023: import javax.sql.DataSource;
024: import javax.naming.Context;
025: import javax.naming.NamingException;
026:
027: import org.apache.openejb.persistence.PersistenceClassLoaderHandler;
028: import org.apache.openejb.persistence.PersistenceUnitInfoImpl;
029: import org.apache.openejb.spi.ContainerSystem;
030: import org.apache.openejb.loader.SystemInstance;
031: import org.apache.openejb.util.Logger;
032: import org.apache.openejb.util.LogCategory;
033: import org.apache.openejb.OpenEJBException;
034:
035: public class PersistenceBuilder {
036:
037: public static final Logger logger = Logger.getInstance(
038: LogCategory.OPENEJB_STARTUP, PersistenceBuilder.class);
039:
040: public static final String PROVIDER_PROP = "javax.persistence.provider";
041:
042: public static final String TRANSACTIONTYPE_PROP = "javax.persistence.transactionType";
043:
044: public static final String JTADATASOURCE_PROP = "javax.persistence.jtaDataSource";
045:
046: public static final String NON_JTADATASOURCE_PROP = "javax.persistence.nonJtaDataSource";
047:
048: private static final String DEFAULT_PERSISTENCE_PROVIDER = "org.apache.openjpa.persistence.PersistenceProviderImpl";
049:
050: /**
051: * External handler which handles adding a runtime ClassTransformer to the classloader.
052: */
053: private final PersistenceClassLoaderHandler persistenceClassLoaderHandler;
054:
055: /**
056: * If set, overrides the persistence provider class name in the persistence.xml.
057: */
058: private String providerEnv;
059:
060: /**
061: * If set, overrides the transaction type in the persistence.xml.
062: */
063: private String transactionTypeEnv;
064:
065: /**
066: * If set, overrides the jta data source class name in the persistence.xml.
067: */
068: private String jtaDataSourceEnv;
069:
070: /**
071: * If set, overrides the non-jta data source class name in the persistence.xml.
072: */
073: private String nonJtaDataSourceEnv;
074:
075: public PersistenceBuilder(
076: PersistenceClassLoaderHandler persistenceClassLoaderHandler) {
077: loadSystemProps();
078: this .persistenceClassLoaderHandler = persistenceClassLoaderHandler;
079: }
080:
081: private void loadSystemProps() {
082: providerEnv = System.getProperty(PROVIDER_PROP);
083: transactionTypeEnv = System.getProperty(TRANSACTIONTYPE_PROP);
084: jtaDataSourceEnv = System.getProperty(JTADATASOURCE_PROP);
085: nonJtaDataSourceEnv = System
086: .getProperty(NON_JTADATASOURCE_PROP);
087: }
088:
089: public EntityManagerFactory createEntityManagerFactory(
090: PersistenceUnitInfo info, ClassLoader classLoader)
091: throws Exception {
092: PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(
093: persistenceClassLoaderHandler);
094:
095: // Persistence Unit Id
096: unitInfo.setId(info.id);
097:
098: // Persistence Unit Name
099: unitInfo.setPersistenceUnitName(info.name);
100:
101: // Persistence Provider Class Name
102: if (providerEnv != null) {
103: unitInfo.setPersistenceProviderClassName(providerEnv);
104: } else {
105: unitInfo.setPersistenceProviderClassName(info.provider);
106: }
107:
108: // ClassLoader
109: unitInfo.setClassLoader(classLoader);
110:
111: // Exclude Unlisted Classes
112: unitInfo.setExcludeUnlistedClasses(info.excludeUnlistedClasses);
113:
114: // JTA Datasource
115: String jtaDataSourceId = info.jtaDataSource;
116: if (jtaDataSourceEnv != null)
117: jtaDataSourceId = jtaDataSourceEnv;
118: if (jtaDataSourceId != null) {
119: if (System.getProperty("duct tape") == null) {
120:
121: try {
122: if (!jtaDataSourceId
123: .startsWith("java:openejb/Resource/"))
124: jtaDataSourceId = "java:openejb/Resource/"
125: + jtaDataSourceId;
126:
127: Context context = SystemInstance.get()
128: .getComponent(ContainerSystem.class)
129: .getJNDIContext();
130: DataSource jtaDataSource = (DataSource) context
131: .lookup(jtaDataSourceId);
132: unitInfo.setJtaDataSource(jtaDataSource);
133: } catch (NamingException e) {
134: throw new OpenEJBException(
135: "Could not lookup <jta-data-source> '"
136: + jtaDataSourceId + "' for unit '"
137: + unitInfo.getPersistenceUnitName()
138: + "'", e);
139: }
140: }
141: }
142:
143: // Managed Class Names
144: unitInfo.setManagedClassNames(info.classes);
145:
146: // Mapping File Names
147: unitInfo.setMappingFileNames(info.mappingFiles);
148:
149: // Handle Properties
150: unitInfo.setProperties(info.properties);
151:
152: // Persistence Unit Transaction Type
153: if (transactionTypeEnv != null) {
154: try {
155: // Override with sys vars
156: PersistenceUnitTransactionType type = Enum.valueOf(
157: PersistenceUnitTransactionType.class,
158: transactionTypeEnv.toUpperCase());
159: unitInfo.setTransactionType(type);
160: } catch (IllegalArgumentException e) {
161: throw (IllegalArgumentException) (new IllegalArgumentException(
162: "Unknown "
163: + TRANSACTIONTYPE_PROP
164: + ", valid options are "
165: + PersistenceUnitTransactionType.JTA
166: + " or "
167: + PersistenceUnitTransactionType.RESOURCE_LOCAL)
168: .initCause(e));
169: }
170: } else {
171: PersistenceUnitTransactionType type = Enum.valueOf(
172: PersistenceUnitTransactionType.class,
173: info.transactionType);
174: unitInfo.setTransactionType(type);
175: }
176:
177: // Non JTA Datasource
178: String nonJtaDataSourceId = info.nonJtaDataSource;
179: if (nonJtaDataSourceEnv != null)
180: nonJtaDataSourceId = nonJtaDataSourceEnv;
181: if (nonJtaDataSourceId != null) {
182: if (System.getProperty("duct tape") == null) {
183: try {
184: if (!nonJtaDataSourceId
185: .startsWith("java:openejb/Resource/"))
186: nonJtaDataSourceId = "java:openejb/Resource/"
187: + nonJtaDataSourceId;
188:
189: Context context = SystemInstance.get()
190: .getComponent(ContainerSystem.class)
191: .getJNDIContext();
192: DataSource nonJtaDataSource = (DataSource) context
193: .lookup(nonJtaDataSourceId);
194: unitInfo.setNonJtaDataSource(nonJtaDataSource);
195: } catch (NamingException e) {
196: throw new OpenEJBException(
197: "Could not lookup <non-jta-data-source> '"
198: + nonJtaDataSourceId
199: + "' for unit '"
200: + unitInfo.getPersistenceUnitName()
201: + "'", e);
202: }
203: }
204: }
205:
206: // Persistence Unit Root Url
207: unitInfo.setRootUrlAndJarUrls(info.persistenceUnitRootUrl,
208: info.jarFiles);
209:
210: // create the persistence provider
211: String persistenceProviderClassName = unitInfo
212: .getPersistenceProviderClassName();
213: if (persistenceProviderClassName == null) {
214: persistenceProviderClassName = DEFAULT_PERSISTENCE_PROVIDER;
215: }
216: unitInfo
217: .setPersistenceProviderClassName(persistenceProviderClassName);
218:
219: Class clazz = classLoader
220: .loadClass(persistenceProviderClassName);
221: PersistenceProvider persistenceProvider = (PersistenceProvider) clazz
222: .newInstance();
223:
224: logger.info("assembler.buildingPersistenceUnit", unitInfo
225: .getPersistenceUnitName(), unitInfo
226: .getPersistenceProviderClassName(), unitInfo
227: .getPersistenceUnitRootUrl(), unitInfo
228: .getTransactionType());
229:
230: // Create entity manager factory
231: EntityManagerFactory emf = persistenceProvider
232: .createContainerEntityManagerFactory(unitInfo,
233: new HashMap());
234: return emf;
235: }
236: }
|