View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.naming.deployment;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.apache.geronimo.common.DeploymentException;
29  import org.apache.geronimo.gbean.AbstractNameQuery;
30  import org.apache.geronimo.gbean.GBeanInfo;
31  import org.apache.geronimo.gbean.GBeanInfoBuilder;
32  import org.apache.geronimo.kernel.GBeanNotFoundException;
33  import org.apache.geronimo.kernel.config.Configuration;
34  import org.apache.geronimo.kernel.repository.Artifact;
35  import org.apache.geronimo.kernel.repository.Dependency;
36  import org.apache.geronimo.kernel.repository.Environment;
37  import org.apache.geronimo.kernel.repository.ImportType;
38  import org.apache.geronimo.naming.reference.PersistenceContextReference;
39  import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
40  import org.apache.geronimo.xbeans.geronimo.naming.GerPersistenceContextRefDocument;
41  import org.apache.geronimo.xbeans.geronimo.naming.GerPersistenceContextRefType;
42  import org.apache.geronimo.xbeans.geronimo.naming.GerPersistenceContextTypeType;
43  import org.apache.geronimo.xbeans.geronimo.naming.GerPropertyType;
44  import org.apache.geronimo.schema.SchemaConversionUtils;
45  import org.apache.geronimo.schema.NamespaceElementConverter;
46  import org.apache.geronimo.deployment.service.EnvironmentBuilder;
47  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
48  import org.apache.geronimo.j2ee.deployment.Module;
49  import org.apache.geronimo.j2ee.deployment.NamingBuilder;
50  import org.apache.xmlbeans.QNameSet;
51  import org.apache.xmlbeans.XmlObject;
52  
53  /**
54   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
55   */
56  public class PersistenceContextRefBuilder implements NamingBuilder {
57      private static final QName PERSISTENCE_CONTEXT_REF_QNAME = GerPersistenceContextRefDocument.type.getDocumentElementName();
58      private static final QNameSet PERSISTENCE_CONTEXT_REF_QNAME_SET = QNameSet.singleton(PERSISTENCE_CONTEXT_REF_QNAME);
59  
60      private final Environment defaultEnvironment = new Environment();
61  
62      public PersistenceContextRefBuilder() {
63          defaultEnvironment.addDependency(new Dependency(new Artifact("org.apache.geronimo.modules", "geronimo-persistence-jpa10", (String)null, "jar"), ImportType.CLASSES));
64      }
65  
66      public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) {
67          if (getPersistenceContextRefs(plan).length > 0) {
68              EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
69          }
70      }
71  
72      public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
73      }
74  
75      public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException {
76          XmlObject[] persistenceContextRefsUntyped = getPersistenceContextRefs(plan);
77          for (int i = 0; i < persistenceContextRefsUntyped.length; i++) {
78              XmlObject persistenceContextRefUntyped = persistenceContextRefsUntyped[i];
79              GerPersistenceContextRefType persistenceContextRef = (GerPersistenceContextRefType) persistenceContextRefUntyped.copy().changeType(GerPersistenceContextRefType.type);
80              if (persistenceContextRef == null) {
81                  throw new DeploymentException("Could not read persistenceContextRef " + persistenceContextRefUntyped + " as the correct xml type");
82              }
83              String persistenceContextRefName = persistenceContextRef.getPersistenceContextRefName();
84              boolean transactionScoped = !persistenceContextRef.getPersistenceContextType().equals(GerPersistenceContextTypeType.EXTENDED);
85              GerPropertyType[] propertyTypes = persistenceContextRef.getPropertyArray();
86              Map properties = new HashMap();
87              for (int j = 0; j < propertyTypes.length; j++) {
88                  GerPropertyType propertyType = propertyTypes[j];
89                  String key = propertyType.getKey();
90                  String value = propertyType.getValue();
91                  properties.put(key, value);
92              }
93  
94  
95              Set interfaceTypes = Collections.singleton("org.apache.geronimo.persistence.PersistenceUnitGBean");
96              AbstractNameQuery persistenceUnitNameQuery;
97              if (persistenceContextRef.isSetPersistenceUnitName()) {
98                  String persistenceUnitName = persistenceContextRef.getPersistenceUnitName();
99                  persistenceUnitNameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", persistenceUnitName), interfaceTypes);
100             } else {
101                 GerPatternType gbeanLocator = persistenceContextRef.getPattern();
102 
103                 persistenceUnitNameQuery = ENCConfigBuilder.buildAbstractNameQuery(gbeanLocator, null, null, interfaceTypes);
104             }
105 
106             try {
107                 localConfiguration.findGBeanData(persistenceUnitNameQuery);
108             } catch (GBeanNotFoundException e) {
109                 throw new DeploymentException("Could not resolve reference at deploy time for query " + persistenceUnitNameQuery, e);
110             }
111 
112             PersistenceContextReference reference = new PersistenceContextReference(localConfiguration.getId(), persistenceUnitNameQuery, transactionScoped, properties);
113 
114             ((Map)componentContext.get(JNDI_KEY)).put(ENV + persistenceContextRefName, reference);
115 
116         }
117     }
118 
119     public QNameSet getSpecQNameSet() {
120         SchemaConversionUtils.registerNamespaceConversions(Collections.singletonMap(PERSISTENCE_CONTEXT_REF_QNAME.getLocalPart(), new NamespaceElementConverter(PERSISTENCE_CONTEXT_REF_QNAME.getNamespaceURI())));
121         return QNameSet.EMPTY;
122     }
123 
124     public QNameSet getPlanQNameSet() {
125         return PERSISTENCE_CONTEXT_REF_QNAME_SET;
126     }
127 
128     private XmlObject[] getPersistenceContextRefs(XmlObject plan) {
129         return plan == null? NO_REFS: plan.selectChildren(PersistenceContextRefBuilder.PERSISTENCE_CONTEXT_REF_QNAME_SET);
130     }
131 
132     public static final GBeanInfo GBEAN_INFO;
133 
134     static {
135         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceContextRefBuilder.class, NameFactory.MODULE_BUILDER);
136 
137         GBEAN_INFO = infoBuilder.getBeanInfo();
138     }
139 
140     public static GBeanInfo getGBeanInfo() {
141         return GBEAN_INFO;
142     }
143 }