| /******************************************************************************* |
| * 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.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import javax.persistence.EntityManagerFactory; |
| import javax.xml.bind.JAXBContext; |
| import javax.xml.bind.JAXBException; |
| |
| import org.eclipse.persistence.config.SessionCustomizer; |
| import org.eclipse.persistence.jaxb.JAXBContextFactory; |
| import org.eclipse.persistence.sessions.Session; |
| |
| import example.mysports.MySportsConfig; |
| import example.mysports.model.Divisions; |
| |
| /** |
| * Create an EclipseLink {@link JAXBContext} which is built from the |
| * {@value #MAPPING_FILE} combined with the virtual attribute extended mappings |
| * returned from the call to the Admin server. |
| * |
| * @author dclarke |
| * @since EclipseLink 2.4 |
| */ |
| public class JaxbContextFactory implements SessionCustomizer { |
| |
| /** |
| * Property name for caching the {@link JAXBContext} for the league within |
| * its {@link EntityManagerFactory} |
| */ |
| private static final String JAXB_CONTEXT = org.eclipse.persistence.jaxb.JAXBContext.class.getName(); |
| |
| /** |
| * EclipseLink MOXy mapping file to use when creating the context |
| */ |
| private static final String MAPPING_FILE = "META-INF/eclipselink-oxm.xml"; |
| |
| @Override |
| public void customize(Session session) throws Exception { |
| // getJAXBContext(session.getProperties()); |
| } |
| |
| /** |
| * Create an EclipseLink {@link JAXBContext} which is built from the |
| * {@value #MAPPING_FILE} combined with the virtual attribute extended |
| * mappings returned from the call to the Admin server. |
| * |
| * @param properties |
| */ |
| @SuppressWarnings({ "rawtypes", "unchecked" }) |
| public static JAXBContext getJAXBContext(Map properties) { |
| JAXBContext context = (JAXBContext) properties.get(JAXB_CONTEXT); |
| MySportsConfig config = MySportsConfig.get(properties); |
| String leagueId = (String) properties.get(MySportsConfig.LEAGUE_CONTEXT); |
| |
| if (context == null && leagueId != null) { |
| Map<String, Object> props = new HashMap<String, Object>(); |
| |
| List<String> xmlBindings = new ArrayList<String>(); |
| xmlBindings.add(MAPPING_FILE); |
| |
| String url = config.getAdminConnector().getOxmURL(leagueId); |
| if (url != null) { |
| xmlBindings.add(url); |
| } |
| props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, xmlBindings); |
| |
| try { |
| context = JAXBContextFactory.createContext(new Class[] { Divisions.class }, props); |
| } catch (JAXBException e) { |
| throw new RuntimeException("JAXB Failure to create context. League: " + leagueId, e); |
| } |
| |
| // Cache the JAXB context in the shared session's properties |
| properties.put(JAXB_CONTEXT, context); |
| } |
| return context; |
| } |
| |
| } |