001: package org.jacorb.orb.giop;
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.net.Socket;
024:
025: import org.apache.avalon.framework.logger.Logger;
026: import org.jacorb.config.Configuration;
027: import org.jacorb.orb.iiop.ServerIIOPConnection;
028: import org.jacorb.util.threadpool.Consumer;
029:
030: /**
031: * @author Nicolas Noffke
032: * @version $Id: MessageReceptor.java,v 1.13 2006/07/03 12:59:03 alphonse.bendt Exp $
033: */
034:
035: public class MessageReceptor implements Consumer {
036: private final Logger logger;
037: private final boolean enhanceThreadName;
038:
039: public MessageReceptor(Configuration configuration) {
040: logger = configuration.getNamedLogger("jacorb.orb.giop");
041: enhanceThreadName = configuration.getAttributeAsBoolean(
042: "jacorb.enhanced_thread_name", false);
043: }
044:
045: public void doWork(Object job) {
046: try {
047: final GIOPConnection connection = (GIOPConnection) job;
048: final String oldName;
049:
050: if (enhanceThreadName) {
051: oldName = Thread.currentThread().getName();
052:
053: org.omg.ETF.Connection transport = connection
054: .getTransport();
055:
056: String enhancedName = getEnhancedName(transport,
057: oldName);
058:
059: Thread.currentThread().setName(enhancedName);
060: } else {
061: oldName = null;
062: }
063:
064: try {
065: (connection).receiveMessages();
066: } finally {
067: if (enhanceThreadName) {
068: Thread.currentThread().setName(oldName);
069: }
070: }
071: } catch (Exception e) {
072: logger.error("unexpected exception during doWork", e);
073: }
074: }
075:
076: private String getEnhancedName(org.omg.ETF.Connection transport,
077: final String oldName) {
078: if (transport instanceof ServerIIOPConnection) {
079: Socket socket = ((ServerIIOPConnection) transport)
080: .getSocket();
081: int localPort = socket.getLocalPort();
082: int port = socket.getPort();
083:
084: StringBuffer buffer = new StringBuffer();
085: buffer.append(oldName);
086: buffer.append('[');
087: buffer.append(localPort);
088: buffer.append(socket.getInetAddress().toString());
089: buffer.append(':');
090: buffer.append(port);
091: buffer.append("] [");
092: buffer.append(System.currentTimeMillis());
093: buffer.append(']');
094:
095: return buffer.toString();
096: }
097:
098: return oldName;
099: }
100: }
|