001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020: package org.jacorb.orb.etf;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.net.InetAddress;
025: import java.net.UnknownHostException;
026:
027: import org.apache.avalon.framework.configuration.Configurable;
028: import org.apache.avalon.framework.configuration.Configuration;
029: import org.apache.avalon.framework.configuration.ConfigurationException;
030: import org.apache.avalon.framework.logger.Logger;
031: import org.jacorb.orb.ORB;
032:
033: /**
034: * This an abstract base implementation of the ETF::Connection interface.
035: *
036: * @author Nicolas Noffke
037: * @author Andre Spiegel
038: * @version $Id: ConnectionBase.java,v 1.3 2006/06/26 08:09:30 alphonse.bendt Exp $
039: */
040:
041: public abstract class ConnectionBase extends
042: org.omg.ETF._ConnectionLocalBase implements Configurable {
043: protected boolean connected = false;
044:
045: /**
046: * Optionally initialised to be used in the dumping of messages.
047: * See property <code>jacorb.debug.dump_outgoing_messages</code>.
048: * Default is off.
049: */
050: protected ByteArrayOutputStream b_out = null;
051:
052: /**
053: * Time out after a close connection has been received.
054: * See property <code>jacorb.connection.timeout_after_closeconnection</code>.
055: * Default 20000 milliseconds.
056: */
057: protected int finalTimeout = 20000;
058:
059: /**
060: * The Profile of the target / server side of the connection.
061: */
062: protected ProfileBase profile = null;
063:
064: /** shared with sub classes */
065: protected Logger logger;
066: protected org.jacorb.config.Configuration configuration;
067: protected String connection_info;
068: protected ORB orb;
069:
070: protected ConnectionBase() {
071: super ();
072: }
073:
074: /**
075: * Initialise this instance as a copy of another. Intended for use within subclass
076: * constructors.
077: */
078: protected ConnectionBase(ConnectionBase other) {
079: this .b_out = other.b_out;
080: this .connection_info = other.connection_info;
081: this .finalTimeout = other.finalTimeout;
082: this .profile = other.profile;
083: }
084:
085: public void configure(Configuration config)
086: throws ConfigurationException {
087: configuration = (org.jacorb.config.Configuration) config;
088: orb = configuration.getORB();
089:
090: logger = configuration.getNamedLogger(configuration
091: .getLoggerName(getClass()));
092:
093: if (configuration.getAttribute(
094: "jacorb.debug.dump_outgoing_messages", "off").equals(
095: "on")) {
096: b_out = new ByteArrayOutputStream();
097: }
098:
099: finalTimeout = configuration.getAttributeAsInteger(
100: "jacorb.connection.timeout_after_closeconnection",
101: 20000);
102: }
103:
104: protected abstract void setTimeout(int timeout);
105:
106: protected abstract int getTimeout();
107:
108: public org.omg.ETF.Profile get_server_profile() {
109: return profile;
110: }
111:
112: public synchronized boolean is_connected() {
113: return connected;
114: }
115:
116: /**
117: * This is used to tell the transport that a CloseConnection has
118: * been sent, and that it should set a timeout in case the client
119: * doesn't close its side of the connection right away.
120: *
121: * This should only be called on the thread that listens on the
122: * socket because timeouts are not applied until read() is called
123: * the next time.
124: */
125: public void turnOnFinalTimeout() {
126: setTimeout(finalTimeout);
127: }
128:
129: protected org.omg.CORBA.COMM_FAILURE to_COMM_FAILURE(IOException ex) {
130: if (logger.isDebugEnabled()) {
131: logger.debug("Caught exception", ex);
132: }
133:
134: return new org.omg.CORBA.COMM_FAILURE("IOException: "
135: + ex.toString());
136: }
137:
138: /**
139: * Wait for the given time_out period for incoming data on this
140: * connection. It shall return false if this call times out and
141: * no data is available. It may not throw a TIMEOUT exception.
142: * If data can already be read or arrives before the end of the
143: * time out, this function shall return true, immediately.
144: */
145: public boolean wait_next_data(long time_out) {
146: throw new org.omg.CORBA.NO_IMPLEMENT();
147: }
148:
149: /**
150: * A boolean flag describing whether this connection supports the
151: * Bidirectional GIOP mechanism as described by GIOP-1.2 in CORBA 2.3.1
152: * (OMG Document: formal/99-10-07). It shall return true if it does,
153: * and false if it does not.
154: */
155: public boolean supports_callback() {
156: return true;
157: }
158:
159: /**
160: * A flag directing the ORB to use either the Handle class to perform
161: * data queries with a time_out, or the transport layer (through this
162: * connection). The instance shall return true, if the Handle should
163: * signal time outs for read operations. Then the ORB may not call
164: * wait_next_data. Otherwise, a false shall be returned, and the
165: * function wait_next_data shall be implemented by this class.
166: */
167: public boolean use_handle_time_out() {
168: // We have neither mechanism in JacORB.
169: // I wonder if we should. AS.
170: return false;
171: }
172:
173: protected final String getLocalhost() {
174: String localhost;
175:
176: try {
177: localhost = InetAddress.getLocalHost().getHostAddress();
178: } catch (UnknownHostException uhe) {
179: if (logger.isDebugEnabled()) {
180: logger
181: .debug("Unable to resolve local host - using default 127.0.0.1");
182: }
183:
184: localhost = "127.0.0.1";
185: }
186: return localhost;
187: }
188: }
|