Source Code Cross Referenced for PersistenceBuilder.java in  » J2EE » openejb3 » org » apache » openejb » assembler » classic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.assembler.classic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.