KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > vendor > OpenJpaVendorAdapter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jpa.vendor;
18
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.spi.PersistenceProvider;
24
25 import org.apache.openjpa.persistence.OpenJPAEntityManager;
26 import org.apache.openjpa.persistence.PersistenceProviderImpl;
27
28 import org.springframework.orm.jpa.JpaDialect;
29
30 /**
31  * {@link org.springframework.orm.jpa.JpaVendorAdapter} implementation for
32  * Apache OpenJPA. Developed and tested against OpenJPA 0.9.6.
33  *
34  * <p>Exposes OpenJPA's persistence provider and EntityManager extension interface,
35  * and supports AbstractJpaVendorAdapter's common configuration settings.
36  *
37  * @author Costin Leau
38  * @author Juergen Hoeller
39  * @since 2.0
40  * @see org.apache.openjpa.persistence.PersistenceProviderImpl
41  * @see org.apache.openjpa.persistence.OpenJPAEntityManager
42  */

43 public class OpenJpaVendorAdapter extends AbstractJpaVendorAdapter {
44
45     private final PersistenceProvider persistenceProvider = new PersistenceProviderImpl();
46
47     private final OpenJpaDialect jpaDialect = new OpenJpaDialect();
48
49
50     public PersistenceProvider getPersistenceProvider() {
51         return this.persistenceProvider;
52     }
53
54     public Map JavaDoc getJpaPropertyMap() {
55         Properties JavaDoc jpaProperties = new Properties JavaDoc();
56
57         if (getDatabasePlatform() != null) {
58             jpaProperties.setProperty("openjpa.jdbc.DBDictionary", getDatabasePlatform());
59         }
60         else if (getDatabase() != null) {
61             String JavaDoc databaseDictonary = determineDatabaseDictionary(getDatabase());
62             if (databaseDictonary != null) {
63                 jpaProperties.setProperty("openjpa.jdbc.DBDictionary", databaseDictonary);
64             }
65         }
66
67         if (isGenerateDdl()) {
68             jpaProperties.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
69         }
70
71         if (isShowSql()) {
72             // Taken from the OpenJPA 0.9.6 docs ("Standard OpenJPA Log Configuration + All SQL Statements")
73
jpaProperties.setProperty("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE");
74         }
75
76         return jpaProperties;
77     }
78
79     /**
80      * Determine the OpenJPA database dictionary name for the given database.
81      * @param database the specified database
82      * @return the OpenJPA database dictionary name, or <code>null<code> if none found
83      */

84     protected String JavaDoc determineDatabaseDictionary(Database database) {
85         switch (database) {
86             case DB2: return "db2";
87             case HSQL: return "hsql(SimulateLocking=true)";
88             case INFORMIX: return "informix";
89             case MYSQL: return "mysql";
90             case ORACLE: return "oracle";
91             case POSTGRESQL: return "postgres";
92             case SQL_SERVER: return "sqlserver";
93             case SYBASE: return "sybase";
94             default: return null;
95         }
96     }
97
98     public Class JavaDoc<? extends EntityManager> getEntityManagerInterface() {
99         return OpenJPAEntityManager.class;
100     }
101
102     public JpaDialect getJpaDialect() {
103         return this.jpaDialect;
104     }
105
106 }
107
Popular Tags