⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reconnectproxy.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package smartproxy.proxies;

import java.lang.reflect.Proxy;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.rmi.Naming;

/**
 *    This is a smart proxy wrapper that reconnects on failures
 *     
 *    Note that it is not tightly bound to the remote object interface,
 *    and can therefore be reused with any remote object(!)
 *
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version $Revision:$
 */
public class ReconnectProxy
   implements InvocationHandler, java.io.Serializable, LinkedProxy
{
   Object next;
   String name;

   // Constructors --------------------------------------------------
   public ReconnectProxy(Object next, String name)
   {
      this.next = next;
      this.name = name;
   }
   
   // Public --------------------------------------------------------
   public Object invoke(Object proxy,
                        Method method,
                        Object[] args)
      throws Throwable
   {
      validate();
      try
      {
         return method.invoke(next, args);
      } catch (InvocationTargetException e)
      {
         try
         {
            throw e.getTargetException();
         } catch (RemoteException ex)
         {
            next = null;
            throw ex;
         }
      }
   }
   
   public Object getNext()
   {
      return next;
   }
   
   public void setNext(Object next)
   {
      this.next = next;
   }
   
   protected void validate()
      throws RemoteException
   {
      if (next == null)
      {
         // Look it up again
         try
         {
            next = Naming.lookup(name);
         } catch (Exception e)
         {
            throw new ServerException("Could not reconnect to remote object", e);
         }
         
         // Peel off proxies until we reach the reconnect proxy
         while (Proxy.isProxyClass(next.getClass()) && 
                Proxy.getInvocationHandler(next) instanceof LinkedProxy &&
                !(Proxy.getInvocationHandler(next) instanceof ReconnectProxy))
         {
            next = ((LinkedProxy)Proxy.getInvocationHandler(next)).getNext();
         }
         // next is now the reconnect proxy - get the next one
         next = ((LinkedProxy)Proxy.getInvocationHandler(next)).getNext();
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -