KickJava   Java API By Example, From Geeks To Geeks.

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


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

51 public class PersistenceContextRefBuilder extends AbstractNamingBuilder {
52     private static final QName JavaDoc PERSISTENCE_CONTEXT_REF_QNAME = GerPersistenceContextRefDocument.type.getDocumentElementName();
53     private static final QNameSet PERSISTENCE_CONTEXT_REF_QNAME_SET = QNameSet.singleton(PERSISTENCE_CONTEXT_REF_QNAME);
54
55     public PersistenceContextRefBuilder(Environment defaultEnvironment) {
56         super(defaultEnvironment);
57     }
58
59     protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) throws DeploymentException {
60         return getPersistenceContextRefs(plan).length > 0;
61     }
62
63     public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map JavaDoc componentContext) throws DeploymentException {
64         XmlObject[] persistenceContextRefsUntyped = getPersistenceContextRefs(plan);
65         for (int i = 0; i < persistenceContextRefsUntyped.length; i++) {
66             GerPersistenceContextRefType persistenceContextRef = (GerPersistenceContextRefType) persistenceContextRefsUntyped[i];
67             if (persistenceContextRef == null) {
68                 throw new DeploymentException("Could not read persistenceContextRef number " + i + " as the correct xml type");
69             }
70             String JavaDoc persistenceContextRefName = persistenceContextRef.getPersistenceContextRefName();
71             boolean transactionScoped = !persistenceContextRef.getPersistenceContextType().equals(GerPersistenceContextTypeType.EXTENDED);
72             GerPropertyType[] propertyTypes = persistenceContextRef.getPropertyArray();
73             Map JavaDoc properties = new HashMap JavaDoc();
74             for (int j = 0; j < propertyTypes.length; j++) {
75                 GerPropertyType propertyType = propertyTypes[j];
76                 String JavaDoc key = propertyType.getKey();
77                 String JavaDoc value = propertyType.getValue();
78                 properties.put(key, value);
79             }
80
81
82             Set JavaDoc interfaceTypes = Collections.singleton("org.apache.geronimo.persistence.PersistenceUnitGBean");
83             AbstractNameQuery persistenceUnitNameQuery;
84             if (persistenceContextRef.isSetPersistenceUnitName()) {
85                 String JavaDoc persistenceUnitName = persistenceContextRef.getPersistenceUnitName();
86                 persistenceUnitNameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", persistenceUnitName), interfaceTypes);
87             } else {
88                 GerPatternType gbeanLocator = persistenceContextRef.getPattern();
89
90                 persistenceUnitNameQuery = buildAbstractNameQuery(gbeanLocator, null, null, interfaceTypes);
91             }
92
93             try {
94                 localConfiguration.findGBeanData(persistenceUnitNameQuery);
95             } catch (GBeanNotFoundException e) {
96                 throw new DeploymentException("Could not resolve reference at deploy time for query " + persistenceUnitNameQuery, e);
97             }
98
99             PersistenceContextReference reference = new PersistenceContextReference(localConfiguration.getId(), persistenceUnitNameQuery, transactionScoped, properties);
100
101             getJndiContextMap(componentContext).put(ENV + persistenceContextRefName, reference);
102
103         }
104     }
105
106     public QNameSet getSpecQNameSet() {
107         SchemaConversionUtils.registerNamespaceConversions(Collections.singletonMap(PERSISTENCE_CONTEXT_REF_QNAME.getLocalPart(), new NamespaceElementConverter(PERSISTENCE_CONTEXT_REF_QNAME.getNamespaceURI())));
108         return QNameSet.EMPTY;
109     }
110
111     public QNameSet getPlanQNameSet() {
112         return PERSISTENCE_CONTEXT_REF_QNAME_SET;
113     }
114
115     private XmlObject[] getPersistenceContextRefs(XmlObject plan) throws DeploymentException {
116         return plan == null? NO_REFS: convert(plan.selectChildren(PersistenceContextRefBuilder.PERSISTENCE_CONTEXT_REF_QNAME_SET), NAMING_CONVERTER, GerPersistenceContextRefType.type);
117     }
118
119     public static final GBeanInfo GBEAN_INFO;
120
121     static {
122         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceContextRefBuilder.class, NameFactory.MODULE_BUILDER);
123         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
124
125         infoBuilder.setConstructor(new String JavaDoc[] {"defaultEnvironment"});
126         GBEAN_INFO = infoBuilder.getBeanInfo();
127     }
128
129     public static GBeanInfo getGBeanInfo() {
130         return GBEAN_INFO;
131     }
132 }
133
Popular Tags