FileDocCategorySizeDatePackage
ASPlainSocketImpl.javaAPI DocGlassfish v2 API16824Tue May 29 01:53:52 BST 2007com.sun.enterprise.server.ss.provider

ASPlainSocketImpl

public class ASPlainSocketImpl extends Object
Wrapper socketimpl above JDK socketimpl implementation. This will be used for any outbound communication.

Fields Summary
private SocketImpl
si
private final String
SOCKET_IMPL_CLASS
private static final Logger
logger
private Method
createMethod
private Method
connectMethod1
private Method
connectMethod2
private Method
connectMethod3
private Method
bindMethod
private Method
listenMethod
private Method
acceptMethod
private Method
getInputStreamMethod
private Method
getOutputStreamMethod
private Method
availableMethod
private Method
closeMethod
private Method
shutdownInputMethod
private Method
shutdownOutputMethod
private Method
sendUrgentDataMethod
private Method
setOptionMethod
private Method
getOptionMethod
private Method
getPortMethod
private Method
supportsUrgentDataMethod
private Method
getInetAddressMethod
private Method
getLocalPortMethod
Constructors Summary
public ASPlainSocketImpl()

      
        try {
            setup();
        } catch (PrivilegedActionException ex) {
            logger.log(Level.FINE, ex.getMessage(), ex);
            throw new RuntimeException(ex);
        }
    
Methods Summary
private java.lang.Object_invoke(java.lang.reflect.Method m, java.lang.Object[] args)

        try {
            return java.security.AccessController.doPrivileged
            (new java.security.PrivilegedExceptionAction() {
                 public java.lang.Object run() throws Exception {
                     return m.invoke(si, args);            
                 }
            });
        } catch (PrivilegedActionException ex) {
            logger.log(Level.FINER, ex.getMessage(), ex);
            Throwable tmpEx = ex;
            if ( ex.getCause() != null) {
                 tmpEx = ex.getCause();
            }

            if (tmpEx instanceof InvocationTargetException) {
                 Throwable e = 
                 ((InvocationTargetException) tmpEx).getTargetException();
                 if (e != null) {
                     tmpEx = e;
                 }
            }
                
            logger.log(Level.FINER, tmpEx.getMessage(), tmpEx);
            if (tmpEx instanceof SocketException) {
                throw (SocketException) tmpEx;
            } else if (tmpEx instanceof IOException) {
                throw (IOException) tmpEx;
            } else {
                IOException ie = new IOException(tmpEx.getMessage());
                throw (IOException) ie.initCause(tmpEx);
            }
        } catch (Exception ex) {
            logger.log(Level.FINER, ex.getMessage(), ex);
            IOException ie = new IOException(ex.getMessage());
            throw (IOException) ie.initCause(ex);
        }    
    
private void_setupMethods(java.lang.Class siClass)
Compare any non-private method. The java.net.socketimpl class has abtsract protected methods. So, its implementation is has to have atleast protected methods.

        Method[] methods = siClass.getDeclaredMethods();
        for (Method m : methods) {
            if (Modifier.isPrivate(m.getModifiers())) {
                continue;
            }            
            m.setAccessible(true);
            if (m.getName().equalsIgnoreCase("CREATE")) {
                if (createMethod == null)
                    createMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("CONNECT")) {
                if (m.getParameterTypes()[0].equals(String.class)) {
                    if (connectMethod1 == null)
                        connectMethod1 = m;
                } else if (m.getParameterTypes()[0].equals(InetAddress.class)){
                    if (connectMethod2 == null)
                        connectMethod2 = m;
                } else {
                    if (connectMethod3 == null)
                        connectMethod3 = m;
                }
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("BIND")) {
                if (bindMethod == null)
                    bindMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("LISTEN")) {
                if (listenMethod == null)
                    listenMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("ACCEPT")) {
                if (acceptMethod == null)
                    acceptMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("GETINPUTSTREAM")) {
                if (getInputStreamMethod == null)
                    getInputStreamMethod = m;
                continue;
            }            
            
            if (m.getName().equalsIgnoreCase("GETOUTPUTSTREAM")) {
                if (getOutputStreamMethod == null)
                    getOutputStreamMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("AVAILABLE")) {
                if (availableMethod == null)
                    availableMethod = m;
                continue;
            }
            
            
            if (m.getName().equalsIgnoreCase("CLOSE")) {
                if (closeMethod == null)
                    closeMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("SHUTDOWNINPUT")) {
                if (shutdownInputMethod == null)
                    shutdownInputMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("SHUTDOWNOUTPUT")) {
                if (shutdownOutputMethod == null)
                    shutdownOutputMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("SENDURGENTDATA")) {
                if (sendUrgentDataMethod == null)
                    sendUrgentDataMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("GETOPTION")) {
                if (getOptionMethod == null)
                    getOptionMethod = m;
                continue;
            }
                        
            if (m.getName().equalsIgnoreCase("SETOPTION")) {
                if (setOptionMethod == null)
                    setOptionMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("GETPORT")) {
                if (getPortMethod == null)
                    getPortMethod = m;
                continue;
            }

            if (m.getName().equalsIgnoreCase("GETINETADDRESS")) {
                if (getInetAddressMethod == null)
                    getInetAddressMethod = m;
                continue;
            }

            
            if (m.getName().equalsIgnoreCase("GETLOCALPORT")) {
                if (getLocalPortMethod == null)
                    getLocalPortMethod = m;
                continue;
            }
            
            if (m.getName().equalsIgnoreCase("SUPPORTSURGENTDATA")) {
                if (supportsUrgentDataMethod == null)
                    supportsUrgentDataMethod = m;
                continue;
            }
        }
    
protected voidaccept(java.net.SocketImpl s)

          
        _invoke(acceptMethod, new Object[] {s});
    
protected intavailable()

         
        return  (Integer) _invoke(availableMethod, null);
    
protected voidbind(java.net.InetAddress host, int port)

            
        _invoke(bindMethod, new Object[] {host, new Integer(port)});
    
protected voidclose()

         
        _invoke(closeMethod, null);
    
protected voidconnect(java.lang.String host, int port)

            
        boolean waitForStartupReqd = ! ASSocketFacadeUtils.getASSocketService().
                                      socketServiceNotified(port);

        _invoke(connectMethod1, new Object[] {host, new Integer(port)});

        if (ASSocketFacadeUtils.getASSocketService().
        isLocalClient(InetAddress.getByName(host)) == false) {
            return;
        }

        if (waitForStartupReqd) {
            ASSocketFacadeUtils.getASSocketService().
            waitOnClientConnection(getPort());
        }


	ASSocketFacadeUtils.getASSocketService().clientSocketConnected(
                                                 getPort(), getLocalPort());
    
protected voidconnect(java.net.InetAddress address, int port)

            
        boolean waitForStartupReqd = ! ASSocketFacadeUtils.getASSocketService().
                                      socketServiceNotified(port);
        _invoke(connectMethod2, new Object[] {address, new Integer(port)});

        if (ASSocketFacadeUtils.getASSocketService().
        isLocalClient(address) == false) {
            return;
        }

        if (waitForStartupReqd) {
            ASSocketFacadeUtils.getASSocketService().
            waitOnClientConnection(getPort());
        }
	ASSocketFacadeUtils.getASSocketService().clientSocketConnected(
                                                 getPort(), getLocalPort());
    
protected voidconnect(java.net.SocketAddress address, int timeout)

            
        InetSocketAddress isa = (InetSocketAddress) address;
        boolean waitForStartupReqd = ! ASSocketFacadeUtils.getASSocketService().
                                      socketServiceNotified(isa.getPort());
        _invoke(connectMethod3, new Object[] {address, new Integer(timeout)});

        if (ASSocketFacadeUtils.getASSocketService().
        isLocalClient(isa.getAddress()) == false) {
            return;
        }

        if (waitForStartupReqd) {
            ASSocketFacadeUtils.getASSocketService().
            waitOnClientConnection(getPort());
        }
	ASSocketFacadeUtils.getASSocketService().clientSocketConnected(
                                                 getPort(), getLocalPort());
    
protected voidcreate(boolean stream)

          
        _invoke(createMethod, new Object[] {new Boolean(stream)});
    
public java.net.InetAddressgetInetAddress()

       
        try {
            return  (InetAddress) _invoke(getInetAddressMethod, null);
        } catch (IOException ex) {
            logger.log(Level.FINER, ex.getMessage(), ex);
            throw new RuntimeException(ex);
        }
    
protected java.io.InputStreamgetInputStream()

         
        return (InputStream) _invoke(getInputStreamMethod, null);
    
public intgetLocalPort()

       
        try {
            return  (Integer) _invoke(getLocalPortMethod, null);        
        } catch (IOException ex) {
            logger.log(Level.FINER, ex.getMessage(), ex);
            throw new RuntimeException(ex);
        }
    
public java.lang.ObjectgetOption(int optID)

          
        try {
            return  _invoke(getOptionMethod, new Object[] {new Integer(optID)});        
        } catch (IOException ie) {
            SocketException se = new SocketException(ie.getMessage());
            throw (SocketException) se.initCause(ie);
        }
    
protected java.io.OutputStreamgetOutputStream()

         
        return (OutputStream) _invoke(getOutputStreamMethod, null);
    
public intgetPort()

       
        try {
            return  (Integer) _invoke(getPortMethod, null);        
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    
protected voidlisten(int backlog)

          
        _invoke(listenMethod, new Object[] {new Integer(backlog)});
    
protected voidsendUrgentData(int data)

          
        _invoke(sendUrgentDataMethod, new Object[] {new Integer(data)});
    
public voidsetOption(int optID, java.lang.Object value)

            
        try {
            _invoke(setOptionMethod, new Object[] {new Integer(optID), value});
        } catch (IOException ie) {
            SocketException se = new SocketException(ie.getMessage());
            throw (SocketException) se.initCause(ie);
        }
    
private voidsetup()
Introspect all the class and create all the method objects. The JDK socket impl implementation has all protected methods and the only way to wrap it is by using setAccessible. The method can be either in the SocksSocketImpl or in any of the parent classes.

        Object impl = java.security.AccessController.doPrivileged
            (new java.security.PrivilegedExceptionAction() {
            public java.lang.Object run() throws Exception {
               Class tmpClass = Class.forName(SOCKET_IMPL_CLASS);            
               Constructor cons = tmpClass.getDeclaredConstructor(new Class[] {});
               cons.setAccessible(true);            
               Object obj = cons.newInstance( (Class[])null);
               while (tmpClass.getName().
               equalsIgnoreCase("JAVA.NET.SOCKETIMPL") == false) {
                   _setupMethods(tmpClass);      
                   tmpClass = tmpClass.getSuperclass();
               }
               return obj;
            }
        });

        this.si = (SocketImpl) impl;
    
protected voidshutdownInput()

         
        _invoke(shutdownInputMethod, null);
    
protected voidshutdownOutput()

         
        _invoke(shutdownOutputMethod, null);
    
public booleansupportsUrgentData()

        
        try {
            return  (Boolean) _invoke(supportsUrgentDataMethod, null);        
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }