001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.corba.deployment;
019
020 import java.net.URI;
021 import java.net.URISyntaxException;
022 import java.util.HashMap;
023 import java.util.Map;
024 import javax.naming.Reference;
025 import javax.xml.namespace.QName;
026
027 import org.apache.geronimo.common.DeploymentException;
028 import org.apache.geronimo.gbean.AbstractNameQuery;
029 import org.apache.geronimo.gbean.GBeanInfo;
030 import org.apache.geronimo.gbean.GBeanInfoBuilder;
031 import org.apache.geronimo.j2ee.deployment.Module;
032 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
033 import org.apache.geronimo.kernel.GBeanNotFoundException;
034 import org.apache.geronimo.kernel.config.Configuration;
035 import org.apache.geronimo.kernel.repository.Environment;
036 import org.apache.geronimo.openejb.deployment.EjbRefBuilder;
037 import org.apache.geronimo.schema.NamespaceElementConverter;
038 import org.apache.geronimo.xbeans.geronimo.naming.GerEjbRefDocument;
039 import org.apache.geronimo.xbeans.geronimo.naming.GerEjbRefType;
040 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
041 import org.apache.geronimo.xbeans.javaee.EjbRefType;
042 import org.apache.geronimo.corba.proxy.CORBAProxyReference;
043 import org.apache.xmlbeans.QNameSet;
044 import org.apache.xmlbeans.XmlObject;
045
046 /**
047 * Installs ejb refs that use corba transport into jndi context.
048 * Such ejb refs are determined by the nscorbaloc element in the openejb ejb plan.
049 *
050 * @version $Revision: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
051 */
052 public class OpenEjbCorbaRefBuilder extends EjbRefBuilder {
053
054 private static final QName GER_EJB_REF_QNAME = GerEjbRefDocument.type.getDocumentElementName();
055 private static final QNameSet GER_EJB_REF_QNAME_SET = QNameSet.singleton(GER_EJB_REF_QNAME);
056 private static final NamespaceElementConverter OPENEJB_CONVERTER = new NamespaceElementConverter(GER_EJB_REF_QNAME.getNamespaceURI());
057
058 private static final QName GER_NS_CORBA_LOC_QNAME = new QName(GER_EJB_REF_QNAME.getNamespaceURI(), "ns-corbaloc");
059 private static final QNameSet GER_NS_CORBA_LOC_QNAME_SET = QNameSet.singleton(GER_NS_CORBA_LOC_QNAME);
060
061 private final QNameSet ejbRefQNameSet;
062
063 public OpenEjbCorbaRefBuilder(Environment defaultEnvironment, String[] eeNamespaces) throws URISyntaxException {
064 super(defaultEnvironment, new String[0], null, -1);
065 ejbRefQNameSet = buildQNameSet(eeNamespaces, "ejb-ref");
066 }
067
068 @Override
069 public QNameSet getSpecQNameSet() {
070 return ejbRefQNameSet;
071 }
072
073 @Override
074 public QNameSet getPlanQNameSet() {
075 return GER_NS_CORBA_LOC_QNAME_SET;
076 }
077
078
079 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
080 // return hasCssRefs(plan);
081 return true;
082 }
083
084 // static boolean hasCssRefs(XmlObject plan) throws DeploymentException {
085 // XmlObject[] refs = plan == null ? NO_REFS : convert(plan.selectChildren(GER_EJB_REF_QNAME_SET), OPENEJB_CONVERTER, GerEjbRefType.type);
086 // for (int i = 0; i < refs.length; i++) {
087 // GerEjbRefType ref = (GerEjbRefType) refs[i];
088 // if (ref.isSetNsCorbaloc()) {
089 // return true;
090 // }
091 // }
092 // return false;
093 // }
094
095 public void buildNaming(XmlObject specDD, XmlObject plan, Module module, Map componentContext) throws DeploymentException {
096 XmlObject[] ejbRefsUntyped = convert(specDD.selectChildren(ejbRefQNameSet), JEE_CONVERTER, EjbRefType.type);
097 XmlObject[] gerEjbRefsUntyped = plan == null ? NO_REFS : convert(plan.selectChildren(GER_EJB_REF_QNAME_SET), OPENEJB_CONVERTER, GerEjbRefType.type);
098 Map ejbRefMap = mapEjbRefs(gerEjbRefsUntyped);
099 ClassLoader cl = module.getEarContext().getClassLoader();
100
101 for (XmlObject anEjbRefsUntyped : ejbRefsUntyped) {
102 EjbRefType ejbRef = (EjbRefType) anEjbRefsUntyped;
103
104 String ejbRefName = getStringValue(ejbRef.getEjbRefName());
105 addInjections(ejbRefName, ejbRef.getInjectionTargetArray(), componentContext);
106 GerEjbRefType remoteRef = (GerEjbRefType) ejbRefMap.get(ejbRefName);
107
108 Reference ejbReference = addEJBRef(module, ejbRef, remoteRef, cl);
109 if (ejbReference != null) {
110 getJndiContextMap(componentContext).put(ENV + ejbRefName, ejbReference);
111 }
112 }
113 }
114
115 private Reference addEJBRef(Module module, EjbRefType ejbRef, GerEjbRefType remoteRef, ClassLoader cl) throws DeploymentException {
116 Reference ejbReference = null;
117 if (remoteRef != null && remoteRef.isSetNsCorbaloc()) {
118 String refName = getStringValue(ejbRef.getEjbRefName());
119 String home = getStringValue(ejbRef.getHome());
120 String remote = getStringValue(ejbRef.getRemote());
121
122 verifyInterfaces(refName, module.getModuleURI(), cl, remote, home);
123
124 try {
125 // create the cssBean query
126 AbstractNameQuery cssBean;
127 if (remoteRef.isSetCssLink()) {
128 String cssLink = remoteRef.getCssLink().trim();
129 cssBean = buildAbstractNameQuery(null, null, cssLink, NameFactory.CORBA_CSS, NameFactory.EJB_MODULE);
130 } else {
131 GerPatternType css = remoteRef.getCss();
132 cssBean = buildAbstractNameQuery(css, NameFactory.CORBA_CSS, NameFactory.EJB_MODULE, null);
133 }
134
135 // verify the cssBean query is valid
136 try {
137 module.getEarContext().findGBean(cssBean);
138 } catch (GBeanNotFoundException e) {
139 throw new DeploymentException("Could not find css bean matching " + cssBean + " from configuration " + module.getConfigId(), e);
140 }
141
142 // create ref
143 ejbReference = new CORBAProxyReference(module.getConfigId(), cssBean, new URI(remoteRef.getNsCorbaloc().trim()), remoteRef.getName().trim(), home);
144 } catch (URISyntaxException e) {
145 throw new DeploymentException("Could not construct CORBA NameServer URI: " + remoteRef.getNsCorbaloc(), e);
146 }
147 }
148 return ejbReference;
149 }
150
151 private void verifyInterfaces(String refName, URI moduleURI, ClassLoader cl, String remote, String home) throws DeploymentException {
152 try {
153 assureInterface(remote, "javax.ejb.EJBObject", "Remote", cl);
154 } catch (DeploymentException e) {
155 throw new DeploymentException("Error processing 'remote' element for EJB Reference '" + refName + "' for module '" + moduleURI + "': " + e.getMessage(), e);
156 }
157 try {
158 assureInterface(home, "javax.ejb.EJBHome", "Home", cl);
159 } catch (DeploymentException e) {
160 throw new DeploymentException("Error processing 'home' element for EJB Reference '" + refName + "' for module '" + moduleURI + "': " + e.getMessage(), e);
161 }
162 }
163
164 private static Map mapEjbRefs(XmlObject[] refs) {
165 Map refMap = new HashMap();
166 if (refs != null) {
167 for (int i = 0; i < refs.length; i++) {
168 GerEjbRefType ref = (GerEjbRefType) refs[i];
169 refMap.put(ref.getRefName().trim(), ref);
170 }
171 }
172 return refMap;
173 }
174
175
176 public static final GBeanInfo GBEAN_INFO;
177
178 static {
179 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(OpenEjbCorbaRefBuilder.class, NameFactory.MODULE_BUILDER); //TODO decide what type this should be
180 infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
181 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
182
183 infoBuilder.setConstructor(new String[]{"defaultEnvironment", "eeNamespaces"});
184
185 GBEAN_INFO = infoBuilder.getBeanInfo();
186 }
187
188 public static GBeanInfo getGBeanInfo() {
189 return GBEAN_INFO;
190 }
191
192 }