KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > persistence > builder > EntityManagerFactoryRefBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.persistence.builder;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.geronimo.common.DeploymentException;
26 import org.apache.geronimo.gbean.AbstractNameQuery;
27 import org.apache.geronimo.gbean.GBeanInfo;
28 import org.apache.geronimo.gbean.GBeanInfoBuilder;
29 import org.apache.geronimo.j2ee.deployment.Module;
30 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
31 import org.apache.geronimo.kernel.GBeanNotFoundException;
32 import org.apache.geronimo.kernel.config.Configuration;
33 import org.apache.geronimo.kernel.repository.Environment;
34 import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
35 import org.apache.geronimo.naming.reference.EntityManagerFactoryReference;
36 import org.apache.geronimo.schema.NamespaceElementConverter;
37 import org.apache.geronimo.schema.SchemaConversionUtils;
38 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefDocument;
39 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefType;
40 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
41 import org.apache.xmlbeans.QNameSet;
42 import org.apache.xmlbeans.XmlObject;
43
44 /**
45  * @version $Rev: 476980 $ $Date: 2006-11-19 18:55:25 -0500 (Sun, 19 Nov 2006) $
46  */

47 public class EntityManagerFactoryRefBuilder extends AbstractNamingBuilder {
48     private static final QName JavaDoc ENTITY_MANAGER_FACTORY_REF_QNAME = GerEntityManagerFactoryRefDocument.type.getDocumentElementName();
49     private static final QNameSet ENTITY_MANAGER_FACTORY_REF_QNAME_SET = QNameSet.singleton(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME);
50
51
52     public EntityManagerFactoryRefBuilder(Environment defaultEnvironment) {
53         super(defaultEnvironment);
54     }
55
56     protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) throws DeploymentException {
57         return getEntityManagerFactoryRefs(plan).length > 0;
58     }
59
60     public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
61     }
62
63     public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map JavaDoc componentContext) throws DeploymentException {
64         XmlObject[] EntityManagerFactoryRefsUntyped = getEntityManagerFactoryRefs(plan);
65         for (int i = 0; i < EntityManagerFactoryRefsUntyped.length; i++) {
66             GerEntityManagerFactoryRefType EntityManagerFactoryRef = (GerEntityManagerFactoryRefType) EntityManagerFactoryRefsUntyped[i];
67             if (EntityManagerFactoryRef == null) {
68                 throw new DeploymentException("Could not read EntityManagerFactoryRef number " + i + " as the correct xml type");
69             }
70             String JavaDoc EntityManagerFactoryRefName = EntityManagerFactoryRef.getEntityManagerFactoryRefName();
71
72             Set JavaDoc interfaceTypes = Collections.singleton("org.apache.geronimo.persistence.PersistenceUnitGBean");
73             AbstractNameQuery persistenceUnitNameQuery;
74             if (EntityManagerFactoryRef.isSetPersistenceUnitName()) {
75                 String JavaDoc persistenceUnitName = EntityManagerFactoryRef.getPersistenceUnitName();
76                 persistenceUnitNameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", persistenceUnitName), interfaceTypes);
77             } else {
78                 GerPatternType gbeanLocator = EntityManagerFactoryRef.getPattern();
79
80                 persistenceUnitNameQuery = buildAbstractNameQuery(gbeanLocator, null, null, interfaceTypes);
81             }
82
83             try {
84                 localConfiguration.findGBeanData(persistenceUnitNameQuery);
85             } catch (GBeanNotFoundException e) {
86                 throw new DeploymentException("Could not resolve reference at deploy time for query " + persistenceUnitNameQuery, e);
87             }
88
89             EntityManagerFactoryReference reference = new EntityManagerFactoryReference(localConfiguration.getId(), persistenceUnitNameQuery);
90
91             ((Map JavaDoc)componentContext.get(JNDI_KEY)).put(ENV + EntityManagerFactoryRefName, reference);
92
93         }
94     }
95
96     public QNameSet getSpecQNameSet() {
97         SchemaConversionUtils.registerNamespaceConversions(Collections.singletonMap(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME.getLocalPart(), new NamespaceElementConverter(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME.getNamespaceURI())));
98         return QNameSet.EMPTY;
99     }
100
101     public QNameSet getPlanQNameSet() {
102         return EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME_SET;
103     }
104
105     private XmlObject[] getEntityManagerFactoryRefs(XmlObject plan) throws DeploymentException {
106         return plan == null? NO_REFS: convert(plan.selectChildren(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME_SET), NAMING_CONVERTER, GerEntityManagerFactoryRefType.type);
107     }
108
109     public static final GBeanInfo GBEAN_INFO;
110
111     static {
112         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EntityManagerFactoryRefBuilder.class, NameFactory.MODULE_BUILDER);
113         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
114
115         infoBuilder.setConstructor(new String JavaDoc[] {"defaultEnvironment"});
116         GBEAN_INFO = infoBuilder.getBeanInfo();
117     }
118
119     public static GBeanInfo getGBeanInfo() {
120         return EntityManagerFactoryRefBuilder.GBEAN_INFO;
121     }
122 }
123
Popular Tags