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.POA;
035 import org.omg.PortableServer.RequestProcessingPolicyValue;
036 import org.omg.PortableServer.Servant;
037 import org.omg.PortableServer.ServantLocator;
038 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder;
039 import org.omg.PortableServer.ServantRetentionPolicyValue;
040 import org.apache.geronimo.corba.transaction.ServerTransactionPolicyFactory;
041 import org.apache.openejb.InterfaceType;
042 import org.apache.geronimo.openejb.EjbDeployment;
043
044
045 /**
046 * @version $Revision: 497125 $ $Date: 2007-01-17 10:51:30 -0800 (Wed, 17 Jan 2007) $
047 */
048 public final class AdapterEntity extends Adapter {
049 private final Log log = LogFactory.getLog(AdapterEntity.class);
050
051 private final POA poa;
052 private final String referenceInterface;
053 private String deploymentId;
054
055 public AdapterEntity(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.PERSISTENT),
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
074 // make sure we create this with the appropriate ORB-specific policies.
075 policies = tssLink.addPolicyOverrides(policies);
076
077 poa = homePOA.create_POA(tssLink.getContainerId(), homePOA.the_POAManager(), policies);
078 poa.set_servant_manager(new ObjectActivator());
079
080 poa.the_POAManager().activate();
081
082 StandardServant servant = new StandardServant(orb, InterfaceType.EJB_OBJECT, tssLink.getDeployment());
083 referenceInterface = servant._all_interfaces(null, null)[0];
084 } catch (Exception e) {
085 throw new CORBAException("Error activating EJB as CORBA object", e);
086 }
087 }
088
089 public POA getPOA() {
090 return poa;
091 }
092
093 public void stop() throws CORBAException {
094 poa.destroy(true, true);
095 super.stop();
096 }
097
098 public org.omg.CORBA.Object genObjectReference(Object primaryKey) throws CORBAException {
099
100 byte[] bytes;
101 try {
102 ByteArrayOutputStream b = new ByteArrayOutputStream();
103 ObjectOutputStream os = new ObjectOutputStream(b);
104
105 os.writeObject(primaryKey);
106 bytes = b.toByteArray();
107
108 os.close();
109 } catch (IOException e) {
110 log.error("Could not serialize deployment info for " + deploymentId, e);
111 throw new CORBAException("Could not serialize deployment info for " + deploymentId, e);
112 }
113 return poa.create_reference_with_id(bytes, referenceInterface);
114 }
115
116 protected class ObjectActivator extends LocalObject implements ServantLocator {
117
118 public Servant preinvoke(byte[] oid, POA poa, String operation, CookieHolder cookie) {
119 // the byte array can be cached in a weak hash map for performance
120 Object pk = null;
121
122 try {
123 ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(oid));
124 pk = is.readObject();
125 is.close();
126
127 EjbDeployment deployment = getDeployment();
128 return new StandardServant(getOrb(), InterfaceType.EJB_OBJECT, deployment, pk);
129 } catch (IOException e) {
130 // if we can't deserialize, then this object can't exist in this process
131 throw (OBJECT_NOT_EXIST)new OBJECT_NOT_EXIST(0, org.omg.CORBA.CompletionStatus.COMPLETED_NO).initCause(e);
132 } catch (Exception e) {
133 log.error("Exception during dispatch to method " + operation + " in bean " + pk, e);
134 return null;
135 }
136 }
137
138 public void postinvoke(byte[] oid, POA poa, String operation, Object cookie, Servant servant) {
139 }
140 }
141 }