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 org.omg.CORBA.Any;
020    import org.omg.CORBA.ORB;
021    import org.omg.CORBA.Policy;
022    import org.omg.CosNaming.NameComponent;
023    import org.omg.CosNaming.NamingContext;
024    import org.omg.CosNaming.NamingContextExt;
025    import org.omg.CosNaming.NamingContextExtHelper;
026    import org.omg.CosNaming.NamingContextHelper;
027    import org.omg.CosNaming.NamingContextPackage.NotEmpty;
028    import org.omg.CosNaming.NamingContextPackage.NotFound;
029    import org.omg.PortableServer.IdAssignmentPolicyValue;
030    import org.omg.PortableServer.ImplicitActivationPolicyValue;
031    import org.omg.PortableServer.POA;
032    import org.omg.PortableServer.RequestProcessingPolicyValue;
033    import org.omg.PortableServer.ServantRetentionPolicyValue;
034    import org.apache.geronimo.corba.transaction.ServerTransactionPolicyFactory;
035    import org.apache.openejb.InterfaceType;
036    import org.apache.geronimo.openejb.EjbDeployment;
037    
038    /**
039     * @version $Revision: 497125 $ $Date: 2007-01-17 10:51:30 -0800 (Wed, 17 Jan 2007) $
040     */
041    public abstract class Adapter implements RefGenerator {
042        protected final EjbDeployment deployment;
043        protected final POA homePOA;
044        protected final ORB orb;
045        protected final NamingContextExt initialContext;
046        protected final byte[] home_id;
047        protected final org.omg.CORBA.Object homeReference;
048        protected final TSSLink tssLink; 
049    
050        protected Adapter(TSSLink tssLink, ORB orb, POA parentPOA, Policy securityPolicy) throws CORBAException {
051            this.tssLink = tssLink; 
052            this.deployment = tssLink.getDeployment();
053            this.home_id = tssLink.getContainerId().getBytes();
054            this.orb = orb;
055    
056            Any any = orb.create_any();
057            any.insert_Value(tssLink.getHomeTxPolicyConfig());
058    
059            try {
060                Policy[] policies = new Policy[]{
061                    securityPolicy,
062                    orb.create_policy(ServerTransactionPolicyFactory.POLICY_TYPE, any),
063    //                parentPOA.create_lifespan_policy(LifespanPolicyValue.TRANSIENT),
064                    parentPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY),
065                    parentPOA.create_servant_retention_policy(ServantRetentionPolicyValue.RETAIN),
066                    parentPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID),
067                    parentPOA.create_implicit_activation_policy(ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION),
068                };
069                // make sure we create this with the appropriate ORB-specific policies. 
070                policies = tssLink.addPolicyOverrides(policies);
071                homePOA = parentPOA.create_POA(tssLink.getContainerId(), parentPOA.the_POAManager(), policies);
072    
073                homePOA.the_POAManager().activate();
074    
075                StandardServant servant = new StandardServant(orb, InterfaceType.EJB_HOME, deployment);
076    
077                homePOA.activate_object_with_id(home_id, servant);
078                homeReference = homePOA.servant_to_reference(servant);
079    
080                org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService");
081                initialContext = NamingContextExtHelper.narrow(obj);
082                String[] names = tssLink.getJndiNames();
083                for (int i = 0; i < names.length; i++) {
084                    NameComponent[] nameComponent = initialContext.to_name(names[i]);
085                    NamingContext currentContext = initialContext;
086                    NameComponent[] nc = new NameComponent[1];
087                    int lastComponent = nameComponent.length - 1;
088                    for (int j = 0; j < lastComponent; ++j) {
089                        nc[0] = nameComponent[j];
090                        try {
091                            currentContext = NamingContextHelper.narrow(currentContext.resolve(nc));
092                        } catch (NotFound nf) {
093                            currentContext = currentContext.bind_new_context(nc);
094                        }
095                    }
096                    nc[0] = nameComponent[lastComponent];
097                    currentContext.rebind(nc, homeReference);
098                }
099            } catch (Exception e) {
100                throw new CORBAException("Unable to activate EJB as CORBA object.", e);
101            }
102    
103        }
104    
105        public EjbDeployment getDeployment() {
106            return deployment;
107        }
108    
109        public NamingContextExt getInitialContext() {
110            return initialContext;
111        }
112    
113        public org.omg.CORBA.Object getHomeReference() {
114            return homeReference;
115        }
116    
117        public ORB getOrb() {
118            return orb;
119        }
120    
121        public void stop() throws CORBAException {
122            try {
123                String[] names = tssLink.getJndiNames();
124                for (int i = 0; i < names.length; i++) {
125                    NameComponent[] nameComponent = initialContext.to_name(names[i]);
126                    initialContext.unbind(nameComponent);
127    
128                    for (int j = nameComponent.length - 1; 0 < j; --j) {
129                        NameComponent[] nc = new NameComponent[j];
130                        System.arraycopy(nameComponent, 0, nc, 0, j);
131                        NamingContext currentContext = NamingContextHelper.narrow(initialContext.resolve(nc));
132                        try {
133                            currentContext.destroy();
134                        } catch (NotEmpty ne) {
135                            break;
136                        }
137                    }
138                }
139    
140                homePOA.deactivate_object(home_id);
141                homePOA.destroy(true, true);
142            } catch (Exception e) {
143                throw new CORBAException("Error deactivating EJB as CORBA object", e);
144            }
145        }
146    
147        public org.omg.CORBA.Object genHomeReference() throws CORBAException {
148            return this.getHomeReference();
149        }
150    }