1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.geronimo.naming.deployment;
22
23 import java.util.Collections;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.xml.namespace.QName;
28
29 import org.apache.geronimo.common.DeploymentException;
30 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
31 import org.apache.geronimo.gbean.AbstractNameQuery;
32 import org.apache.geronimo.gbean.GBeanInfo;
33 import org.apache.geronimo.gbean.GBeanInfoBuilder;
34 import org.apache.geronimo.j2ee.deployment.Module;
35 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
36 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
37 import org.apache.geronimo.kernel.GBeanNotFoundException;
38 import org.apache.geronimo.kernel.config.Configuration;
39 import org.apache.geronimo.kernel.repository.Artifact;
40 import org.apache.geronimo.kernel.repository.Dependency;
41 import org.apache.geronimo.kernel.repository.Environment;
42 import org.apache.geronimo.kernel.repository.ImportType;
43 import org.apache.geronimo.naming.reference.EntityManagerFactoryReference;
44 import org.apache.geronimo.schema.NamespaceElementConverter;
45 import org.apache.geronimo.schema.SchemaConversionUtils;
46 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefDocument;
47 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefType;
48 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
49 import org.apache.xmlbeans.QNameSet;
50 import org.apache.xmlbeans.XmlObject;
51
52 /**
53 * @version $Rev: 471685 $ $Date: 2006-11-06 02:35:00 -0800 (Mon, 06 Nov 2006) $
54 */
55 public class EntityManagerFactoryRefBuilder implements NamingBuilder {
56 private static final QName ENTITY_MANAGER_FACTORY_REF_QNAME = GerEntityManagerFactoryRefDocument.type.getDocumentElementName();
57 private static final QNameSet ENTITY_MANAGER_FACTORY_REF_QNAME_SET = QNameSet.singleton(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME);
58
59 private final Environment defaultEnvironment = new Environment();
60
61 public EntityManagerFactoryRefBuilder() {
62 defaultEnvironment.addDependency(new Dependency(new Artifact("org.apache.geronimo.modules", "geronimo-persistence-jpa10", (String)null, "jar"), ImportType.CLASSES));
63 }
64
65 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) {
66 if (getEntityManagerFactoryRefs(plan).length > 0) {
67 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
68 }
69 }
70
71 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
72 }
73
74 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException {
75 XmlObject[] EntityManagerFactoryRefsUntyped = getEntityManagerFactoryRefs(plan);
76 for (int i = 0; i < EntityManagerFactoryRefsUntyped.length; i++) {
77 XmlObject EntityManagerFactoryRefUntyped = EntityManagerFactoryRefsUntyped[i];
78 GerEntityManagerFactoryRefType EntityManagerFactoryRef = (GerEntityManagerFactoryRefType) EntityManagerFactoryRefUntyped.copy().changeType(GerEntityManagerFactoryRefType.type);
79 if (EntityManagerFactoryRef == null) {
80 throw new DeploymentException("Could not read EntityManagerFactoryRef " + EntityManagerFactoryRefUntyped + " as the correct xml type");
81 }
82 String EntityManagerFactoryRefName = EntityManagerFactoryRef.getEntityManagerFactoryRefName();
83
84 Set interfaceTypes = Collections.singleton("org.apache.geronimo.persistence.PersistenceUnitGBean");
85 AbstractNameQuery persistenceUnitNameQuery;
86 if (EntityManagerFactoryRef.isSetPersistenceUnitName()) {
87 String persistenceUnitName = EntityManagerFactoryRef.getPersistenceUnitName();
88 persistenceUnitNameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", persistenceUnitName), interfaceTypes);
89 } else {
90 GerPatternType gbeanLocator = EntityManagerFactoryRef.getPattern();
91
92 persistenceUnitNameQuery = ENCConfigBuilder.buildAbstractNameQuery(gbeanLocator, null, null, interfaceTypes);
93 }
94
95 try {
96 localConfiguration.findGBeanData(persistenceUnitNameQuery);
97 } catch (GBeanNotFoundException e) {
98 throw new DeploymentException("Could not resolve reference at deploy time for query " + persistenceUnitNameQuery, e);
99 }
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 }