001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 021 package org.apache.geronimo.naming.deployment; 022 023 import java.util.Collections; 024 import java.util.Map; 025 import java.util.Set; 026 027 import javax.xml.namespace.QName; 028 029 import org.apache.geronimo.common.DeploymentException; 030 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 031 import org.apache.geronimo.gbean.AbstractNameQuery; 032 import org.apache.geronimo.gbean.GBeanInfo; 033 import org.apache.geronimo.gbean.GBeanInfoBuilder; 034 import org.apache.geronimo.j2ee.deployment.Module; 035 import org.apache.geronimo.j2ee.deployment.NamingBuilder; 036 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 037 import org.apache.geronimo.kernel.GBeanNotFoundException; 038 import org.apache.geronimo.kernel.config.Configuration; 039 import org.apache.geronimo.kernel.repository.Artifact; 040 import org.apache.geronimo.kernel.repository.Dependency; 041 import org.apache.geronimo.kernel.repository.Environment; 042 import org.apache.geronimo.kernel.repository.ImportType; 043 import org.apache.geronimo.naming.reference.EntityManagerFactoryReference; 044 import org.apache.geronimo.schema.NamespaceElementConverter; 045 import org.apache.geronimo.schema.SchemaConversionUtils; 046 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefDocument; 047 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefType; 048 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 049 import org.apache.xmlbeans.QNameSet; 050 import org.apache.xmlbeans.XmlObject; 051 052 /** 053 * @version $Rev: 471685 $ $Date: 2006-11-06 02:35:00 -0800 (Mon, 06 Nov 2006) $ 054 */ 055 public class EntityManagerFactoryRefBuilder implements NamingBuilder { 056 private static final QName ENTITY_MANAGER_FACTORY_REF_QNAME = GerEntityManagerFactoryRefDocument.type.getDocumentElementName(); 057 private static final QNameSet ENTITY_MANAGER_FACTORY_REF_QNAME_SET = QNameSet.singleton(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME); 058 059 private final Environment defaultEnvironment = new Environment(); 060 061 public EntityManagerFactoryRefBuilder() { 062 defaultEnvironment.addDependency(new Dependency(new Artifact("org.apache.geronimo.modules", "geronimo-persistence-jpa10", (String)null, "jar"), ImportType.CLASSES)); 063 } 064 065 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) { 066 if (getEntityManagerFactoryRefs(plan).length > 0) { 067 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment); 068 } 069 } 070 071 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException { 072 } 073 074 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException { 075 XmlObject[] EntityManagerFactoryRefsUntyped = getEntityManagerFactoryRefs(plan); 076 for (int i = 0; i < EntityManagerFactoryRefsUntyped.length; i++) { 077 XmlObject EntityManagerFactoryRefUntyped = EntityManagerFactoryRefsUntyped[i]; 078 GerEntityManagerFactoryRefType EntityManagerFactoryRef = (GerEntityManagerFactoryRefType) EntityManagerFactoryRefUntyped.copy().changeType(GerEntityManagerFactoryRefType.type); 079 if (EntityManagerFactoryRef == null) { 080 throw new DeploymentException("Could not read EntityManagerFactoryRef " + EntityManagerFactoryRefUntyped + " as the correct xml type"); 081 } 082 String EntityManagerFactoryRefName = EntityManagerFactoryRef.getEntityManagerFactoryRefName(); 083 084 Set interfaceTypes = Collections.singleton("org.apache.geronimo.persistence.PersistenceUnitGBean"); 085 AbstractNameQuery persistenceUnitNameQuery; 086 if (EntityManagerFactoryRef.isSetPersistenceUnitName()) { 087 String persistenceUnitName = EntityManagerFactoryRef.getPersistenceUnitName(); 088 persistenceUnitNameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", persistenceUnitName), interfaceTypes); 089 } else { 090 GerPatternType gbeanLocator = EntityManagerFactoryRef.getPattern(); 091 092 persistenceUnitNameQuery = ENCConfigBuilder.buildAbstractNameQuery(gbeanLocator, null, null, interfaceTypes); 093 } 094 095 try { 096 localConfiguration.findGBeanData(persistenceUnitNameQuery); 097 } catch (GBeanNotFoundException e) { 098 throw new DeploymentException("Could not resolve reference at deploy time for query " + persistenceUnitNameQuery, e); 099 } 100 101 EntityManagerFactoryReference reference = new EntityManagerFactoryReference(localConfiguration.getId(), persistenceUnitNameQuery); 102 103 ((Map)componentContext.get(JNDI_KEY)).put(ENV + EntityManagerFactoryRefName, reference); 104 105 } 106 } 107 108 public QNameSet getSpecQNameSet() { 109 SchemaConversionUtils.registerNamespaceConversions(Collections.singletonMap(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME.getLocalPart(), new NamespaceElementConverter(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME.getNamespaceURI()))); 110 return QNameSet.EMPTY; 111 } 112 113 public QNameSet getPlanQNameSet() { 114 return EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME_SET; 115 } 116 117 private XmlObject[] getEntityManagerFactoryRefs(XmlObject plan) { 118 return plan == null? NO_REFS: plan.selectChildren(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME_SET); 119 } 120 121 public static final GBeanInfo GBEAN_INFO; 122 123 static { 124 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EntityManagerFactoryRefBuilder.class, NameFactory.MODULE_BUILDER); 125 126 GBEAN_INFO = infoBuilder.getBeanInfo(); 127 } 128 129 public static GBeanInfo getGBeanInfo() { 130 return EntityManagerFactoryRefBuilder.GBEAN_INFO; 131 } 132 }