001: package org.jacorb.transport;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.avalon.framework.configuration.Configurable;
027: import org.apache.avalon.framework.configuration.Configuration;
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.avalon.framework.logger.Logger;
030: import org.jacorb.orb.giop.TransportListener;
031: import org.omg.CORBA.LocalObject;
032:
033: /**
034: *
035: * A default implementation of the
036: *
037: * @see org.jacorb.transport.Current interface. Provides access to information
038: * about a Transport - the Traits.
039: *
040: * @author Iliyan Jeliazkov
041: *
042: */
043: public class DefaultCurrentImpl extends LocalObject implements Current,
044: TransportListener, Configurable {
045:
046: private int statistics_provider_index_ = -1;
047:
048: /**
049: * ctor
050: */
051: public DefaultCurrentImpl() {
052:
053: }
054:
055: public void configure(Configuration configuration)
056: throws ConfigurationException {
057:
058: org.jacorb.config.Configuration cfg = (org.jacorb.config.Configuration) configuration;
059:
060: logger_ = cfg.getNamedLogger("jacorb.transport.current");
061:
062: // Plug-in a statistics provider, we know how to use
063: List statsProviderClassNames = cfg
064: .getAttributeList("jacorb.connection.statistics_providers");
065: statsProviderClassNames.add(DefaultStatisticsProvider.class
066: .getName());
067:
068: StringBuffer buff = new StringBuffer();
069: for (Iterator iter = statsProviderClassNames.iterator(); iter
070: .hasNext();) {
071: buff.append(iter.next());
072: if (iter.hasNext())
073: buff.append(',');
074: }
075: cfg.setAttribute("jacorb.connection.statistics_providers", buff
076: .toString());
077:
078: statistics_provider_index_ = statsProviderClassNames.size() - 1;
079: }
080:
081: //
082: // TCPConnectionListener interface
083: //
084: public void transportSelected(Event event) {
085:
086: tss_transport_event_.set(event);
087:
088: if (logger_.isInfoEnabled()) {
089: logger_.info("Transport selected " + event);
090: }
091: }
092:
093: /**
094: * Retrieves the last event for the current thread.
095: *
096: * @return Event
097: * @throws BadContext
098: */
099: protected Event getLatestTransportCurentEvent() throws NoContext {
100:
101: Event e = (Event) tss_transport_event_.get();
102: if (e == null) {
103: if (logger_.isErrorEnabled()) {
104: logger_
105: .error("No events were available. Is traits() called outside of an upcall or interceptor?");
106: }
107: throw new NoContext();
108: }
109: return e;
110: }
111:
112: /**
113: * A thread-specific storage for the Transport the thread has chosen. Null,
114: * if no transport has been chosen yet.
115: */
116: private static final ThreadLocal tss_transport_event_ = new ThreadLocal();
117:
118: /**
119: * A logger.
120: */
121: private Logger logger_;
122:
123: public int id() throws NoContext {
124: Event e = getLatestTransportCurentEvent();
125: return e.hashCode();
126: }
127:
128: public long bytes_sent() throws NoContext {
129: Event e = getLatestTransportCurentEvent();
130: DefaultStatisticsProvider p = (DefaultStatisticsProvider) e
131: .getStatisticsProvider(statistics_provider_index_);
132: return p.bytes_sent_;
133: }
134:
135: public long bytes_received() throws NoContext {
136: Event e = getLatestTransportCurentEvent();
137: DefaultStatisticsProvider p = (DefaultStatisticsProvider) e
138: .getStatisticsProvider(statistics_provider_index_);
139: return p.bytes_received_;
140: }
141:
142: public long messages_sent() throws NoContext {
143: Event e = getLatestTransportCurentEvent();
144: DefaultStatisticsProvider p = (DefaultStatisticsProvider) e
145: .getStatisticsProvider(statistics_provider_index_);
146: return p.messages_sent_;
147: }
148:
149: public long messages_received() throws NoContext {
150: Event e = getLatestTransportCurentEvent();
151: DefaultStatisticsProvider p = (DefaultStatisticsProvider) e
152: .getStatisticsProvider(statistics_provider_index_);
153: return p.messages_received_;
154: }
155:
156: public long open_since() throws NoContext {
157: Event e = getLatestTransportCurentEvent();
158: DefaultStatisticsProvider p = (DefaultStatisticsProvider) e
159: .getStatisticsProvider(statistics_provider_index_);
160: return p.created_;
161: }
162:
163: }
|