1/*2 * Copyright 2001-2008 The Apache Software Foundation.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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 *16 */1718package org.apache.juddi.config;
1920import java.util.Properties;
2122import javax.persistence.EntityManagerFactory;
23import javax.persistence.Persistence;
24import javax.persistence.EntityManager;
2526import org.apache.commons.configuration.ConfigurationException;
27import org.apache.commons.logging.Log;
28import org.apache.commons.logging.LogFactory;
2930publicclassPersistenceManager {
31privatestatic Log log = LogFactory.getLog(PersistenceManager.class);
3233publicstaticfinal String PERSISTENCE_UNIT_NAME = "juddiDatabase";
3435privatestatic EntityManagerFactory emf;
3637publicstatic EntityManager getEntityManager() {
38try {
39// Now, the factory will be initialized in the config. This will force the config to initialize, thereby initializing emf.40if (emf == null)
41 AppConfig.getInstance();
42 }
43catch (ConfigurationException e) {
44 log.error("Error initializing config in PersistenceManager", e);
45thrownew ExceptionInInitializerError(e);
46 }
4748return emf.createEntityManager();
49 }
5051publicstaticvoid closeEntityManager() {
52if (emf.isOpen())
53 emf.close();
54 }
5556protectedstaticvoid initializeEntityManagerFactory(String persistenceUnitName, Properties properties) {
57try {
58if (emf == null)
59if (properties==null || properties.size()==0) {
60 emf = Persistence.createEntityManagerFactory(persistenceUnitName);
61 } else {
62 emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
63 }
64 }
65catch (Throwable t) {
66 log.error("entityManagerFactory creation failed", t);
67thrownew ExceptionInInitializerError(t);
68 }
6970 }
7172 }