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