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