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    package org.apache.geronimo.corba;
018    
019    import javax.ejb.EJBHome;
020    import javax.ejb.EJBMetaData;
021    import javax.ejb.EJBObject;
022    import javax.ejb.Handle;
023    import javax.ejb.HomeHandle;
024    import javax.naming.Context;
025    import javax.naming.InitialContext;
026    import javax.rmi.PortableRemoteObject;
027    
028    import org.omg.CORBA.ORB;
029    import org.apache.openejb.ProxyInfo;
030    import org.apache.openejb.ContainerType;
031    import org.apache.openejb.DeploymentInfo;
032    import org.apache.openejb.spi.ApplicationServer;
033    
034    /**
035     * @version $Revision: 494431 $ $Date: 2007-01-09 07:18:14 -0800 (Tue, 09 Jan 2007) $
036     */
037    public class CorbaApplicationServer implements ApplicationServer {
038        public EJBObject getEJBObject(ProxyInfo proxyInfo) {
039            try {
040                RefGenerator refGenerator = AdapterWrapper.getRefGenerator((String) proxyInfo.getDeploymentInfo().getDeploymentID());
041                org.omg.CORBA.Object object = refGenerator.genObjectReference(proxyInfo.getPrimaryKey());
042                EJBObject ejbObject = (EJBObject) PortableRemoteObject.narrow(object, EJBObject.class);
043                return ejbObject;
044            } catch (Throwable e) {
045                throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL(e.getClass().getName() + " thrown while marshaling the reply: " + e.getMessage(), 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES).initCause(e);
046            }
047        }
048    
049        /**
050         * DMB: My vague understanding is that business interfaces aren't supported by
051         * corba as they do not extend java.rmi.Remote.  But we can try.
052         */
053        public Object getBusinessObject(ProxyInfo proxyInfo) {
054            try {
055                RefGenerator refGenerator = AdapterWrapper.getRefGenerator((String) proxyInfo.getDeploymentInfo().getDeploymentID());
056                org.omg.CORBA.Object object = refGenerator.genObjectReference(proxyInfo.getPrimaryKey());
057                return object;
058            } catch (Throwable e) {
059                throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL(e.getClass().getName() + " thrown while marshaling the reply: " + e.getMessage(), 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES).initCause(e);
060            }
061        }
062    
063        public EJBHome getEJBHome(ProxyInfo proxyInfo) {
064            try {
065                RefGenerator refGenerator = AdapterWrapper.getRefGenerator((String) proxyInfo.getDeploymentInfo().getDeploymentID());
066                org.omg.CORBA.Object home = refGenerator.genHomeReference();
067                EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(home, EJBHome.class);
068                return ejbHome;
069            } catch (Throwable e) {
070                throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL(e.getClass().getName() + " thrown while marshaling the reply: " + e.getMessage(), 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES).initCause(e);
071            }
072        }
073    
074        public javax.ejb.Handle getHandle(ProxyInfo proxyInfo) {
075            Handle handle = new CORBAHandle(getEJBObject(proxyInfo), proxyInfo.getPrimaryKey());
076            return handle;
077        }
078    
079        public javax.ejb.HomeHandle getHomeHandle(ProxyInfo proxyInfo) {
080            org.omg.CORBA.Object ejbHome = (org.omg.CORBA.Object) getEJBHome(proxyInfo);
081            String ior = getOrb().object_to_string(ejbHome);
082            HomeHandle homeHandle = new CORBAHomeHandle(ior);
083            return homeHandle;
084        }
085    
086        public EJBMetaData getEJBMetaData(ProxyInfo proxyInfo) {
087            ContainerType componentType = proxyInfo.getBeanContainer().getContainerType();
088            byte ejbType;
089            switch (componentType) {
090                case STATEFUL:
091                    ejbType = CORBAEJBMetaData.STATEFUL;
092                    break;
093                case STATELESS:
094                    ejbType = CORBAEJBMetaData.STATELESS;
095                    break;
096                case BMP_ENTITY:
097                case CMP_ENTITY:
098                    ejbType = CORBAEJBMetaData.ENTITY;
099                    break;
100                default:
101                    throw new IllegalArgumentException("Unknown component type: " + componentType);
102            }
103    
104            DeploymentInfo deploymentInfo = proxyInfo.getDeploymentInfo();
105            CORBAEJBMetaData ejbMetaData = new CORBAEJBMetaData(getEJBHome(proxyInfo),
106                    ejbType,
107                    deploymentInfo.getHomeInterface(),
108                    deploymentInfo.getRemoteInterface(),
109                    deploymentInfo.getPrimaryKeyClass());
110            return ejbMetaData;
111        }
112    
113        private static ORB getOrb() {
114            try {
115                Context context = new InitialContext();
116                ORB orb = (ORB) context.lookup("java:comp/ORB");
117                return orb;
118            } catch (Throwable e) {
119                throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL("Could not find ORB in jndi at java:comp/ORB", 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES).initCause(e);
120            }
121        }
122    }