/*
* Copyright 2012 Adaptrex, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.persistence.jpa;
import com.adaptrex.core.config.AdaptrexConfig;
import com.adaptrex.core.persistence.AdaptrexPersistence;
import com.adaptrex.core.persistence.AdaptrexPersistenceManager;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.xml.parsers.SAXParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author
* adaptrex
*/
public class JpaPersistenceManager implements AdaptrexPersistenceManager {
private static Logger log = LoggerFactory.getLogger(JpaPersistenceManager.class);
private Map<String, AdaptrexPersistence> adaptrexPersistenceMap;
private AdaptrexPersistence defaultPersistence;
public JpaPersistenceManager(AdaptrexConfig config) throws IOException {
adaptrexPersistenceMap = new HashMap<String, AdaptrexPersistence>();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
List<InputStream> inputStreams = loadResources("META-INF/persistence.xml", cl);
if (inputStreams.isEmpty()) {
log.warn("Couldn't find persistence.xml");
return;
}
final String defaultFactoryName = config.get(AdaptrexConfig.PERSISTENCE_DEFAULT);
final boolean singleFactory = inputStreams.size() == 1;
try {
for (InputStream stream : inputStreams) {
SAXParserFactory.newInstance().newSAXParser().parse(stream, new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (!qName.equalsIgnoreCase("persistence-unit")) {
return;
}
String persistenceUnitName = attributes.getValue("name");
log.debug("Creating EntityManagerFactory: " + persistenceUnitName);
EntityManagerFactory factory =
Persistence.createEntityManagerFactory(persistenceUnitName);
AdaptrexPersistence persistence = new JpaPersistence(factory);
adaptrexPersistenceMap.put(persistenceUnitName, persistence);
if (singleFactory || persistenceUnitName.equals(defaultFactoryName)) {
defaultPersistence = persistence;
}
}
});
}
if (defaultPersistence == null) {
log.warn("*** Multiple persistence.xml descriptors found but no default has been defined in adaptrex.webapp.config.");
}
} catch (Exception e) {
log.warn("Error", e);
}
}
@Override
public AdaptrexPersistence getPersistence(String factoryName) {
return factoryName != null ? adaptrexPersistenceMap.get(factoryName) : defaultPersistence;
}
@Override
public AdaptrexPersistence getPersistence() {
return defaultPersistence;
}
@Override
public void shutdown() {
for (Map.Entry<String, AdaptrexPersistence> persistenceEntry : adaptrexPersistenceMap.entrySet()) {
log.info("Shutting Down EntityManagerFactory: " + persistenceEntry.getKey());
((EntityManagerFactory) persistenceEntry.getValue().getFactory()).close();
}
}
/*
* Ripped from: http://goo.gl/CYwbl
*/
public static List<InputStream> loadResources(
final String name, final ClassLoader classLoader) throws IOException {
final List<InputStream> list = new ArrayList<InputStream>();
final Enumeration<URL> systemResources = classLoader.getResources(name);
while (systemResources.hasMoreElements()) {
list.add(systemResources.nextElement().openStream());
}
return list;
}
}