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.PortableServer.IdAssignmentPolicyValue;
023    import org.omg.PortableServer.ImplicitActivationPolicyValue;
024    import org.omg.PortableServer.POA;
025    import org.omg.PortableServer.POAPackage.ObjectNotActive;
026    import org.omg.PortableServer.POAPackage.WrongPolicy;
027    import org.omg.PortableServer.RequestProcessingPolicyValue;
028    import org.omg.PortableServer.ServantRetentionPolicyValue;
029    import org.apache.openejb.InterfaceType;
030    import org.apache.geronimo.corba.transaction.ServerTransactionPolicyFactory;
031    
032    /**
033     * @version $Revision: 497125 $ $Date: 2007-01-17 10:51:30 -0800 (Wed, 17 Jan 2007) $
034     */
035    public final class AdapterStateless extends Adapter {
036        private final POA poa;
037        private final byte[] object_id;
038        private final org.omg.CORBA.Object objectReference;
039    
040        public AdapterStateless(TSSLink tssLink, ORB orb, POA parentPOA, Policy securityPolicy) throws CORBAException {
041            super(tssLink, orb, parentPOA, securityPolicy);
042            Any any = orb.create_any();
043            any.insert_Value(tssLink.getRemoteTxPolicyConfig());
044    
045            try {
046                Policy[] policies = new Policy[]{
047                    securityPolicy,
048                    orb.create_policy(ServerTransactionPolicyFactory.POLICY_TYPE, any),
049    //                homePOA.create_lifespan_policy(LifespanPolicyValue.TRANSIENT),
050                    homePOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY),
051                    homePOA.create_servant_retention_policy(ServantRetentionPolicyValue.RETAIN),
052                    homePOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID),
053                    homePOA.create_implicit_activation_policy(ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION),
054                };
055                // make sure we create this with the appropriate ORB-specific policies. 
056                policies = tssLink.addPolicyOverrides(policies); 
057                poa = homePOA.create_POA(tssLink.getContainerId(), homePOA.the_POAManager(), policies);
058    
059                poa.the_POAManager().activate();
060    
061                StandardServant servant = new StandardServant(orb, InterfaceType.EJB_OBJECT, tssLink.getDeployment());
062    
063                poa.activate_object_with_id(object_id = tssLink.getContainerId().getBytes(), servant);
064                objectReference = poa.servant_to_reference(servant);
065            } catch (Exception e) {
066                throw new CORBAException("Unable to activate EJB "+ tssLink.getContainerId() +" as CORBA object", e);
067            }
068        }
069    
070        public void stop() throws CORBAException {
071            try {
072                poa.deactivate_object(object_id);
073                poa.destroy(true, true);
074                super.stop();
075            } catch (ObjectNotActive e) {
076                throw new CORBAException("Unable to activate EJB "+ tssLink.getContainerId() +" as CORBA object", e);
077            } catch (WrongPolicy e) {
078                throw new CORBAException("Unable to activate EJB "+ tssLink.getContainerId() +" as CORBA object", e);
079            }
080        }
081    
082        public org.omg.CORBA.Object genObjectReference(Object primaryKey) {
083            return objectReference;
084        }
085    }