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 java.io.ByteArrayInputStream; 020 import java.io.ByteArrayOutputStream; 021 import java.io.IOException; 022 import java.io.ObjectInputStream; 023 import java.io.ObjectOutputStream; 024 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 import org.omg.CORBA.Any; 028 import org.omg.CORBA.LocalObject; 029 import org.omg.CORBA.OBJECT_NOT_EXIST; 030 import org.omg.CORBA.ORB; 031 import org.omg.CORBA.Policy; 032 import org.omg.PortableServer.IdAssignmentPolicyValue; 033 import org.omg.PortableServer.ImplicitActivationPolicyValue; 034 import org.omg.PortableServer.LifespanPolicyValue; 035 import org.omg.PortableServer.POA; 036 import org.omg.PortableServer.RequestProcessingPolicyValue; 037 import org.omg.PortableServer.Servant; 038 import org.omg.PortableServer.ServantLocator; 039 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder; 040 import org.omg.PortableServer.ServantRetentionPolicyValue; 041 import org.apache.openejb.InterfaceType; 042 import org.apache.geronimo.corba.transaction.ServerTransactionPolicyFactory; 043 import org.apache.geronimo.openejb.EjbDeployment; 044 045 /** 046 * @version $Revision: 497125 $ $Date: 2007-01-17 10:51:30 -0800 (Wed, 17 Jan 2007) $ 047 */ 048 public class AdapterStateful extends Adapter { 049 private final Log log = LogFactory.getLog(AdapterStateful.class); 050 051 private final POA poa; 052 private final String referenceInterface; 053 private String deploymentId; 054 055 public AdapterStateful(TSSLink tssLink, ORB orb, POA parentPOA, Policy securityPolicy) throws CORBAException { 056 super(tssLink, orb, parentPOA, securityPolicy); 057 058 deploymentId = tssLink.getContainerId(); 059 060 Any any = orb.create_any(); 061 any.insert_Value(tssLink.getRemoteTxPolicyConfig()); 062 063 try { 064 Policy[] policies = new Policy[]{ 065 securityPolicy, 066 orb.create_policy(ServerTransactionPolicyFactory.POLICY_TYPE, any), 067 homePOA.create_lifespan_policy(LifespanPolicyValue.TRANSIENT), 068 homePOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER), 069 homePOA.create_servant_retention_policy(ServantRetentionPolicyValue.NON_RETAIN), 070 homePOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID), 071 homePOA.create_implicit_activation_policy(ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION), 072 }; 073 // make sure we create this with the appropriate ORB-specific policies. 074 policies = tssLink.addPolicyOverrides(policies); 075 poa = homePOA.create_POA(tssLink.getContainerId(), homePOA.the_POAManager(), policies); 076 poa.set_servant_manager(new ObjectActivator()); 077 078 poa.the_POAManager().activate(); 079 080 StandardServant servant = new StandardServant(orb, InterfaceType.EJB_OBJECT, tssLink.getDeployment()); 081 referenceInterface = servant._all_interfaces(null, null)[0]; 082 } catch (Exception e) { 083 throw new CORBAException("Unable to activate EJB "+ deploymentId +" as CORBA object", e); 084 } 085 } 086 087 public POA getPOA() { 088 return poa; 089 } 090 091 public void stop() throws CORBAException { 092 poa.destroy(true, true); 093 super.stop(); 094 } 095 096 public org.omg.CORBA.Object genObjectReference(Object primaryKey) throws CORBAException { 097 098 byte[] bytes; 099 try { 100 ByteArrayOutputStream b = new ByteArrayOutputStream(); 101 ObjectOutputStream os = new ObjectOutputStream(b); 102 103 os.writeObject(primaryKey); 104 bytes = b.toByteArray(); 105 106 os.close(); 107 } catch (IOException e) { 108 log.error("Could not serialize deployment info for " + deploymentId, e); 109 throw new CORBAException("Could not serialize deployment info for " + deploymentId, e); 110 } 111 return poa.create_reference_with_id(bytes, referenceInterface); 112 } 113 114 protected class ObjectActivator extends LocalObject implements ServantLocator { 115 116 public Servant preinvoke(byte[] oid, POA poa, String operation, CookieHolder cookie) { 117 // the byte array can be cached in a weak hash map for performance 118 Object pk = null; 119 120 try { 121 ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(oid)); 122 pk = is.readObject(); 123 is.close(); 124 125 EjbDeployment deployment = getDeployment(); 126 return new StandardServant(getOrb(), InterfaceType.EJB_OBJECT, deployment, pk); 127 } catch (IOException e) { 128 // if we can't deserialize, then this object can't exist in this process 129 throw (OBJECT_NOT_EXIST)new OBJECT_NOT_EXIST(0, org.omg.CORBA.CompletionStatus.COMPLETED_NO).initCause(e); 130 } catch (Exception e) { 131 log.error("Exception during dispatch to method " + operation + " in bean " + pk, e); 132 return null; 133 } 134 } 135 136 public void postinvoke(byte[] oid, POA poa, String operation, Object cookie, Servant servant) { 137 } 138 } 139 }