FileDocCategorySizeDatePackage
SocketOrChannelAcceptorImpl.javaAPI DocJava SE 5 API17890Fri Aug 26 14:54:34 BST 2005com.sun.corba.se.impl.transport

SocketOrChannelAcceptorImpl

public class SocketOrChannelAcceptorImpl extends EventHandlerBase implements com.sun.corba.se.spi.orbutil.threadpool.Work, com.sun.corba.se.spi.transport.SocketInfo, com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo, com.sun.corba.se.spi.transport.CorbaAcceptor, com.sun.corba.se.spi.transport.SocketOrChannelAcceptor
author
Harold Carr

Fields Summary
protected ServerSocketChannel
serverSocketChannel
protected ServerSocket
serverSocket
protected int
port
protected long
enqueueTime
protected boolean
initialized
protected com.sun.corba.se.impl.logging.ORBUtilSystemException
wrapper
protected com.sun.corba.se.pept.transport.InboundConnectionCache
connectionCache
protected String
type
protected String
name
protected String
hostname
protected int
locatorPort
Constructors Summary
public SocketOrChannelAcceptorImpl(com.sun.corba.se.spi.orb.ORB orb)

    // END Legacy

      
    
	this.orb = orb;
	wrapper = ORBUtilSystemException.get( orb,
	    CORBALogDomains.RPC_TRANSPORT ) ;

	setWork(this);
	initialized = false;

	// BEGIN Legacy support.
	this.hostname = orb.getORBData().getORBServerHost();
	this.name = LegacyServerSocketEndPointInfo.NO_NAME;
	this.locatorPort = -1;
	// END Legacy support.
    
public SocketOrChannelAcceptorImpl(com.sun.corba.se.spi.orb.ORB orb, int port)

	this(orb);
	this.port = port;
    
public SocketOrChannelAcceptorImpl(com.sun.corba.se.spi.orb.ORB orb, int port, String name, String type)

	this(orb, port);
	this.name = name;
	this.type = type;
    
Methods Summary
public voidaccept()

	try {
	    SocketChannel socketChannel = null;
	    Socket socket = null;
	    if (serverSocketChannel == null) {
		socket = serverSocket.accept();
	    } else {
		socketChannel = serverSocketChannel.accept();
		socket = socketChannel.socket();
	    }
	    orb.getORBData().getSocketFactory()
		.setAcceptedSocketOptions(this, serverSocket, socket);
	    if (orb.transportDebugFlag) {
		dprint(".accept: " + 
		       (serverSocketChannel == null 
			? serverSocket.toString()
			: serverSocketChannel.toString()));
	    }

	    CorbaConnection connection = 
		new SocketOrChannelConnectionImpl(orb, this, socket);
	    if (orb.transportDebugFlag) {
		dprint(".accept: new: " + connection);
	    }

	    // NOTE: The connection MUST be put in the cache BEFORE being
	    // registered with the selector.  Otherwise if the bytes
	    // are read on the connection it will attempt a time stamp
	    // but the cache will be null, resulting in NPE.
	    getConnectionCache().put(this, connection);

	    if (connection.shouldRegisterServerReadEvent()) {
		Selector selector = orb.getTransportManager().getSelector(0);
		selector.registerForEvent(connection.getEventHandler());
	    }

	    getConnectionCache().reclaim();

	} catch (IOException e) {
	    if (orb.transportDebugFlag) {
		dprint(".accept:", e);
	    }
	    orb.getTransportManager().getSelector(0).unregisterForEvent(this);
	    // REVISIT - need to close - recreate - then register new one.
	    orb.getTransportManager().getSelector(0).registerForEvent(this);
	    // NOTE: if register cycling we do not want to shut down ORB
	    // since local beans will still work.  Instead one will see
	    // a growing log file to alert admin of problem.
	}
    
public voidaddToIORTemplate(com.sun.corba.se.spi.ior.IORTemplate iorTemplate, com.sun.corba.se.impl.oa.poa.Policies policies, java.lang.String codebase)

	Iterator iterator = iorTemplate.iteratorById(
            org.omg.IOP.TAG_INTERNET_IOP.value);

	String hostname = orb.getORBData().getORBServerHost();

	if (iterator.hasNext()) {
	    // REVISIT - how does this play with legacy ORBD port exchange?
	    IIOPAddress iiopAddress = 
		IIOPFactories.makeIIOPAddress(orb, hostname, port);
	    AlternateIIOPAddressComponent iiopAddressComponent =
		IIOPFactories.makeAlternateIIOPAddressComponent(iiopAddress);

	    while (iterator.hasNext()) {
		TaggedProfileTemplate taggedProfileTemplate =
		    (TaggedProfileTemplate) iterator.next();
		taggedProfileTemplate.add(iiopAddressComponent);
	    }
	} else {
	    GIOPVersion version = orb.getORBData().getGIOPVersion();
	    int templatePort;
	    if (policies.forceZeroPort()) {
		templatePort = 0;
	    } else if (policies.isTransient()) {
		templatePort = port;
	    } else {
		templatePort = orb.getLegacyServerSocketManager()
                   .legacyGetPersistentServerPort(SocketInfo.IIOP_CLEAR_TEXT);
	    }
	    IIOPAddress addr =
		IIOPFactories.makeIIOPAddress(orb, hostname, templatePort);
	    IIOPProfileTemplate iiopProfile = 
		IIOPFactories.makeIIOPProfileTemplate(orb, version, addr);
	    if (version.supportsIORIIOPProfileComponents()) {
		iiopProfile.add(IIOPFactories.makeCodeSetsComponent(orb));
		iiopProfile.add(IIOPFactories.makeMaxStreamFormatVersionComponent());
	        RequestPartitioningPolicy rpPolicy = (RequestPartitioningPolicy)
		    policies.get_effective_policy(
			              ORBConstants.REQUEST_PARTITIONING_POLICY);
		if (rpPolicy != null) {
		    iiopProfile.add(
		         IIOPFactories.makeRequestPartitioningComponent(
			     rpPolicy.getValue()));
		}
		if (codebase != null && codebase != "") {
		    iiopProfile.add(IIOPFactories. makeJavaCodebaseComponent(codebase));
		}
		if (orb.getORBData().isJavaSerializationEnabled()) {
		    iiopProfile.add(
			   IIOPFactories.makeJavaSerializationComponent());
		}
	    }
	    iorTemplate.add(iiopProfile);
	}
    
public voidclose()

	try {
	    if (orb.transportDebugFlag) {
		dprint(".close->:");
	    }
	    Selector selector = orb.getTransportManager().getSelector(0);
	    selector.unregisterForEvent(this);
	    if (serverSocketChannel != null) {
		serverSocketChannel.close();
	    }
	    if (serverSocket != null) {
		serverSocket.close();
	    }
	} catch (IOException e) {
	    if (orb.transportDebugFlag) {
		dprint(".close:", e);
	    }
	} finally {
	    if (orb.transportDebugFlag) {
		dprint(".close<-:");
	    }
	}
    
public com.sun.corba.se.pept.encoding.InputObjectcreateInputObject(com.sun.corba.se.pept.broker.Broker broker, com.sun.corba.se.pept.protocol.MessageMediator messageMediator)

	CorbaMessageMediator corbaMessageMediator = (CorbaMessageMediator)
	    messageMediator;
	return new CDRInputObject((ORB)broker,
				  (CorbaConnection)messageMediator.getConnection(),
				  corbaMessageMediator.getDispatchBuffer(),
				  corbaMessageMediator.getDispatchHeader());
    
public com.sun.corba.se.pept.protocol.MessageMediatorcreateMessageMediator(com.sun.corba.se.pept.broker.Broker broker, com.sun.corba.se.pept.transport.Connection connection)

	// REVISIT - no factoring so cheat to avoid code dup right now.
	// REVISIT **** COUPLING !!!!
	ContactInfo contactInfo = new SocketOrChannelContactInfoImpl();
	return contactInfo.createMessageMediator(broker, connection);
    
public com.sun.corba.se.pept.encoding.OutputObjectcreateOutputObject(com.sun.corba.se.pept.broker.Broker broker, com.sun.corba.se.pept.protocol.MessageMediator messageMediator)

	CorbaMessageMediator corbaMessageMediator = (CorbaMessageMediator)
	    messageMediator;
	return new CDROutputObject((ORB) broker, corbaMessageMediator, 
				   corbaMessageMediator.getReplyHeader(),
				   corbaMessageMediator.getStreamFormatVersion());
    
public voiddoWork()

	try {
	    if (orb.transportDebugFlag) {
		dprint(".doWork->: " + this);
	    }
	    if (selectionKey.isAcceptable()) {
                AccessController.doPrivileged(new PrivilegedAction() {
		    public java.lang.Object run() {
			accept();
			return null;
		    }
		});
	    } else {
		if (orb.transportDebugFlag) {
		    dprint(".doWork: ! selectionKey.isAcceptable: " + this);
		}
	    }
	} catch (SecurityException se) {
	    if (orb.transportDebugFlag) {
		dprint(".doWork: ignoring SecurityException: "
		       + se 
		       + " " + this);
	    }
	    String permissionStr = ORBUtility.getClassSecurityInfo(getClass());
            wrapper.securityExceptionInAccept(se, permissionStr);
	} catch (Exception ex) {
	    if (orb.transportDebugFlag) {
		dprint(".doWork: ignoring Exception: "
		       + ex 
		       + " " + this);
	    }
            wrapper.exceptionInAccept(ex);
	} catch (Throwable t) {
	    if (orb.transportDebugFlag) {
		dprint(".doWork: ignoring Throwable: "
		       + t
		       + " " + this);
	    }
	} finally {

            // IMPORTANT: To avoid bug (4953599), we force the
	    // Thread that does the NIO select to also do the
	    // enable/disable of Ops using SelectionKey.interestOps().
	    // Otherwise, the SelectionKey.interestOps() may block
	    // indefinitely.
	    // NOTE: If "acceptorSocketUseWorkerThreadForEvent" is
	    // set to to false in ParserTable.java, then this method,
	    // doWork(), will get executed by the same thread 
	    // (SelectorThread) that does the NIO select. 
	    // If "acceptorSocketUseWorkerThreadForEvent" is set
	    // to true, a WorkerThread will execute this method,
	    // doWork(). Hence, the registering of the enabling of
	    // the SelectionKey's interestOps is done here instead
	    // of calling SelectionKey.interestOps(<interest op>).

            Selector selector = orb.getTransportManager().getSelector(0);
            selector.registerInterestOps(this);

	    if (orb.transportDebugFlag) {
		dprint(".doWork<-:" + this);
	    }
	}
    
protected voiddprint(java.lang.String msg)

	ORBUtility.dprint(toStringName(), msg);
    
protected voiddprint(java.lang.String msg, java.lang.Throwable t)

	dprint(msg);
	t.printStackTrace(System.out);
    
public com.sun.corba.se.pept.protocol.MessageMediatorfinishCreatingMessageMediator(com.sun.corba.se.pept.broker.Broker broker, com.sun.corba.se.pept.transport.Connection connection, com.sun.corba.se.pept.protocol.MessageMediator messageMediator)

	// REVISIT - no factoring so cheat to avoid code dup right now.
	// REVISIT **** COUPLING !!!!
	ContactInfo contactInfo = new SocketOrChannelContactInfoImpl();
	return contactInfo.finishCreatingMessageMediator(broker,
	                 	          connection, messageMediator);
    
public com.sun.corba.se.pept.transport.AcceptorgetAcceptor()

	return this;
    
public java.nio.channels.SelectableChannelgetChannel()

	return serverSocketChannel;
    
public com.sun.corba.se.pept.transport.ConnectiongetConnection()

	throw new RuntimeException("Should not happen.");
    
public com.sun.corba.se.pept.transport.InboundConnectionCachegetConnectionCache()

	return connectionCache;
    
public java.lang.StringgetConnectionCacheType()

	return this.getClass().toString();
    
public longgetEnqueueTime()

	return enqueueTime;
    
public com.sun.corba.se.pept.transport.EventHandlergetEventHandler()

	return this;
    
public java.lang.StringgetHost()

	return hostname;
    
public java.lang.StringgetHostName()

	return hostname;
    
public intgetInterestOps()

	return SelectionKey.OP_ACCEPT;
    
public intgetLocatorPort()

	return locatorPort;
    
public java.lang.StringgetMonitoringName()

	return "AcceptedConnections";
    
public java.lang.StringgetName()

	// Kluge alert:
	// Work and Legacy both define getName.
	// Try to make this behave best for most cases.
	String result = 
	    name.equals(LegacyServerSocketEndPointInfo.NO_NAME) ?
	    this.toString() : name;
	return result;
    
public java.lang.StringgetObjectAdapterId()

	return null;
    
public java.lang.StringgetObjectAdapterManagerId()

	return null;
    
public intgetPort()

	return port;
    
public java.net.ServerSocketgetServerSocket()

	return serverSocket;
    
public java.lang.StringgetType()

	return type;
    
public booleaninitialize()

	if (initialized) {
	    return false;
	}
	if (orb.transportDebugFlag) {
	    dprint(".initialize: " + this);
	}
	InetSocketAddress inetSocketAddress = null;
	try {
	    if (orb.getORBData().getListenOnAllInterfaces().equals(ORBConstants.LISTEN_ON_ALL_INTERFACES)) {
		inetSocketAddress = new InetSocketAddress(port);
	    } else {
		String host = orb.getORBData().getORBServerHost();
		inetSocketAddress = new InetSocketAddress(host, port);
	    }
	    serverSocket = orb.getORBData().getSocketFactory()
		.createServerSocket(type, inetSocketAddress);
	    internalInitialize();
	} catch (Throwable t) {
	    throw wrapper.createListenerFailed( t, Integer.toString(port) ) ;
	}
	initialized = true;
	return true;
    
public booleaninitialized()

	return initialized;
    
protected voidinternalInitialize()

	// Determine the listening port (for the IOR).
	// This is important when using emphemeral ports (i.e.,
	// when the port value to the constructor is 0).

	port = serverSocket.getLocalPort();

	// Register with transport (also sets up monitoring).

	orb.getCorbaTransportManager().getInboundConnectionCache(this);

	// Finish configuation.

	serverSocketChannel = serverSocket.getChannel();

	if (serverSocketChannel != null) {
	    setUseSelectThreadToWait(
	        orb.getORBData().acceptorSocketUseSelectThreadToWait());
	    serverSocketChannel.configureBlocking(
	        ! orb.getORBData().acceptorSocketUseSelectThreadToWait());
	} else {
	    // Configure to use listener and reader threads.
	    setUseSelectThreadToWait(false);
	}
	setUseWorkerThreadForEvent(
            orb.getORBData().acceptorSocketUseWorkerThreadForEvent());

    
public voidsetConnectionCache(com.sun.corba.se.pept.transport.InboundConnectionCache connectionCache)

	this.connectionCache = connectionCache;
    
public voidsetEnqueueTime(long timeInMillis)

	enqueueTime = timeInMillis;
    
public voidsetLocatorPort(int port)

	locatorPort = port;
    
public booleanshouldRegisterAcceptEvent()

	return true;
    
public java.lang.StringtoString()

	String sock;
	if (serverSocketChannel == null) {
	    if (serverSocket == null) {
		sock = "(not initialized)";
	    } else {
		sock = serverSocket.toString();
	    }
	} else {
	    sock = serverSocketChannel.toString();
	}

	return 
	    toStringName() + 
	    "["
	    + sock + " "
	    + type + " "
	    + shouldUseSelectThreadToWait() + " "
	    + shouldUseWorkerThreadForEvent()
	    + "]" ;
    
protected java.lang.StringtoStringName()

	return "SocketOrChannelAcceptorImpl";