| /******************************************************************************* |
| * Copyright (c) 2010-2012 Oracle. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| * which accompanies this distribution. |
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| * and the Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * dclarke - EclipseLink 2.4 - MySports Demo Bug 344608 |
| ******************************************************************************/ |
| package example.mysports.persistence; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import javax.persistence.EntityManagerFactory; |
| import javax.persistence.spi.PersistenceUnitInfo; |
| |
| import org.eclipse.persistence.config.PersistenceUnitProperties; |
| import org.eclipse.persistence.internal.jpa.deployment.JPAInitializer; |
| import org.eclipse.persistence.jpa.PersistenceProvider; |
| |
| import example.mysports.MySportsConfig; |
| |
| /** |
| * Custom {@link javax.persistence.spi.PersistenceProvider} to support |
| * EclipseLink JPA usage with an {@link EntityManagerFactory} per tenant. This |
| * implementation is specific to the MySports example |
| * |
| * @author dclarke |
| * @since EclipseLink 2.4 |
| */ |
| @SuppressWarnings({ "rawtypes", "unchecked" }) |
| public class MySportsProvider extends PersistenceProvider { |
| |
| public static final String SEPARATOR = "-"; |
| |
| @Override |
| public EntityManagerFactory createEntityManagerFactory(String name, Map properties) { |
| String puName = name; |
| Map props = properties == null ? new HashMap() : properties; |
| |
| if (name.contains(SEPARATOR)) { |
| int sepIndex = name.indexOf(SEPARATOR); |
| puName = name.substring(0, sepIndex); |
| props.put(PersistenceUnitProperties.SESSION_NAME, name); |
| props.put(PersistenceUnitProperties.MULTITENANT_SHARED_EMF, Boolean.FALSE.toString()); |
| props.put(MySportsConfig.LEAGUE_CONTEXT, name.substring(sepIndex + 1)); |
| } |
| |
| return super.createEntityManagerFactory(puName, props); |
| } |
| |
| @Override |
| public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { |
| // TODO Auto-generated method stub |
| return super.createContainerEntityManagerFactory(info, properties); |
| } |
| |
| @Override |
| public JPAInitializer getInitializer(String emName, Map m) { |
| ClassLoader classLoader = getClassLoader(emName, m); |
| return MySportsProviderInitializer.getJavaSECMPInitializer(classLoader); |
| } |
| } |