001: /*
002: * <copyright>
003: * Copyright 1997-2004 BBNT Solutions, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency (DARPA).
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the Cougaar Open Source License as published by
008: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
009: *
010: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
011: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
012: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
013: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
014: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
015: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
016: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
017: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
018: * PERFORMANCE OF THE COUGAAR SOFTWARE.
019: * </copyright>
020: */
021:
022: package org.cougaar.mts.corba;
023:
024: import org.cougaar.mts.corba.idlj.*;
025:
026: import org.cougaar.core.mts.MessageAddress;
027: import org.cougaar.core.mts.MessageAttributes;
028: import org.cougaar.core.mts.SerializationUtils;
029: import org.cougaar.mts.base.CommFailureException;
030: import org.cougaar.mts.base.DestinationLink;
031: import org.cougaar.mts.base.DontRetryException;
032: import org.cougaar.mts.base.LinkProtocol; // javadoc
033: import org.cougaar.mts.base.MisdeliveredMessageException;
034: import org.cougaar.mts.base.NameLookupException;
035: import org.cougaar.mts.base.RPCLinkProtocol;
036: import org.cougaar.mts.base.UnregisteredNameException;
037: import org.cougaar.mts.std.AttributedMessage;
038:
039: import org.omg.CORBA.ORB;
040: import org.omg.PortableServer.POA;
041: import org.omg.PortableServer.POAHelper;
042:
043: import java.net.URI;
044:
045: /**
046: * This {@link LinkProtocol} uses CORBA for communication.
047: */
048: public class CorbaLinkProtocol extends RPCLinkProtocol {
049: public static final String PROTOCOL_TYPE = "-CORBA";
050:
051: private MT myProxy;
052: private ORB orb;
053: private POA poa;
054:
055: public CorbaLinkProtocol() {
056: super ();
057: String[] args = null;
058: orb = ORB.init(args, null);
059: try {
060: org.omg.CORBA.Object raw = orb
061: .resolve_initial_references("RootPOA");
062: poa = POAHelper.narrow(raw);
063: poa.the_POAManager().activate();
064: } catch (Exception error) {
065: loggingService.error(null, error);
066: }
067:
068: }
069:
070: protected String getProtocolType() {
071: return PROTOCOL_TYPE;
072: }
073:
074: protected Boolean usesEncryptedSocket() {
075: return Boolean.FALSE;
076: }
077:
078: // If this is called, we've already found the remote reference.
079: // The cost is currently hardwired at an arbitrary value of 1001
080: // (a little more than RMI).
081: protected int computeCost(AttributedMessage message) {
082: return 1001;
083: }
084:
085: protected DestinationLink createDestinationLink(
086: MessageAddress address) {
087: return new CorbaLink(address);
088: }
089:
090: protected void findOrMakeNodeServant() {
091: if (myProxy != null)
092: return;
093: MessageAddress myAddress = getNameSupport()
094: .getNodeMessageAddress();
095: MTImpl impl = new MTImpl(myAddress, getDeliverer());
096: try {
097: poa.activate_object(impl);
098: } catch (Exception ex) {
099: loggingService.error(null, ex);
100: }
101: myProxy = impl._this ();
102: setNodeURI(URI.create(orb.object_to_string(myProxy)));
103: }
104:
105: protected void remakeNodeServant() {
106: if (myProxy != null) {
107: try {
108: byte[] oid = poa.reference_to_id(myProxy);
109: poa.deactivate_object(oid);
110: } catch (Exception ex) {
111: loggingService.error(null, ex);
112: }
113: }
114: myProxy = null;
115: findOrMakeNodeServant();
116: }
117:
118: /**
119: * The DestinationLink class for this transport. Forwarding a
120: * message with this link means looking up the MT proxy for a
121: * remote MTImpl, and calling rerouteMessage on it. */
122: class CorbaLink extends Link {
123:
124: CorbaLink(MessageAddress destination) {
125: super (destination);
126: }
127:
128: protected Object decodeRemoteRef(URI ref) throws Exception {
129: String ior = ref.toString();
130: org.omg.CORBA.Object raw = orb.string_to_object(ior);
131: MT mt = MTHelper.narrow(raw);
132: return mt;
133: }
134:
135: public Class getProtocolClass() {
136: return CorbaLinkProtocol.class;
137: }
138:
139: protected MessageAttributes forwardByProtocol(
140: Object remote_ref, AttributedMessage message)
141: throws NameLookupException, UnregisteredNameException,
142: CommFailureException, MisdeliveredMessageException {
143: byte[] bytes = null;
144: try {
145: bytes = SerializationUtils.toByteArray(message);
146: } catch (DontRetryException mex) {
147: throw new CommFailureException(mex);
148: } catch (java.io.IOException iox) {
149: // What would this mean?
150: }
151:
152: byte[] res = null;
153: try {
154: res = ((MT) remote_ref).rerouteMessage(bytes);
155: } catch (CorbaMisdeliveredMessage mis) {
156: // force recache of remote
157: decache();
158: throw new MisdeliveredMessageException(message);
159: } catch (CorbaDontRetryException mex) {
160: byte[] ex_bytes = mex.cause;
161: try {
162: DontRetryException mse = (DontRetryException) SerializationUtils
163: .fromByteArray(ex_bytes);
164: throw new CommFailureException(mse);
165: } catch (Exception ex) {
166: // ???
167: }
168: } catch (Exception corba_ex) {
169: // Some other CORBA failure. Decache and retry.
170: decache();
171: throw new CommFailureException(corba_ex);
172: }
173:
174: MessageAttributes attrs = null;
175: try {
176: attrs = (MessageAttributes) SerializationUtils
177: .fromByteArray(res);
178: } catch (DontRetryException mex) {
179: throw new CommFailureException(mex);
180: } catch (java.io.IOException iox) {
181: // What would this mean?
182: } catch (ClassNotFoundException cnf) {
183: }
184:
185: return attrs;
186: }
187:
188: }
189:
190: protected void releaseNodeServant() {
191: }
192:
193: }
|