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

📄 handler.java

📁 精通RMI 这是RMI的入门基础 特别对刚开始学RMI的同胞们很有帮助
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package smartproxy.dynamic;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

import smartproxy.proxies.DynamicRemoteStub;

/**
 *    This is a special URL stream handler that generates the RMI object stub class.
 *    It loads a general one and replaces its name with something that the RMI runtime can accept.
 *
 *   @author Rickard 謆erg (rickard@dreambean.com)
 */
public class Handler
   extends URLStreamHandler
{
   // Static --------------------------------------------------------
   static ThreadLocal currentClass = new ThreadLocal();
   
   public static Class getCurrentClass()
   {
      return (Class)currentClass.get();
   }
   
   // Constructors --------------------------------------------------
   
   // Public implementation -----------------------------------------
   protected URLConnection openConnection(URL u)
      throws IOException
   {
//      System.out.println("URL:"+u);
      if (u.getFile().endsWith("_Stub.class"))
      {
         String stubName = u.getFile();
         stubName = stubName.substring(1, stubName.length()-6);
         String name = stubName.substring(0,stubName.length()-5);
         name = name.replace('/','.');
         
         // Get impl
         try
         {
            Class cl = Class.forName(name);
            currentClass.set(cl.getInterfaces()[0]); // Assume that it only implements one remote interface
         } catch (ClassNotFoundException e)
         {
            System.out.println("Exception:"+e);
            throw new IOException("Could not locate class:"+name);
         }
         
         try
         {
         
         // Read DynamicRemoteStub class bytes
         URL jar = DynamicRemoteStub.class.getProtectionDomain().getCodeSource().getLocation();
         URL stubFile = new URL("jar:"+jar+"!/"+DynamicRemoteStub.class.getName().replace('.','/')+".class");
         URLConnection uc = stubFile.openConnection();
         InputStream in = uc.getInputStream();
         byte[] arr = new byte[in.available()];
         in.read(arr);
         in.close();
         
         int idx = new String(arr).indexOf(DynamicRemoteStub.class.getName().replace('.','/'));
         
         int src = idx+DynamicRemoteStub.class.getName().length();
         int dest = idx+stubName.length();
         int length = arr.length - idx - DynamicRemoteStub.class.getName().length();
         byte[] newArr = new byte[arr.length-DynamicRemoteStub.class.getName().length()+stubName.length()];

         // Copy prefix
         System.arraycopy(arr, 0, newArr, 0 , idx - 1);
         
         // Copy suffix
         System.arraycopy(arr, src, newArr, dest , length );
         
         // Change name
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         DataOutputStream dout = new DataOutputStream(bout);
         dout.writeUTF(stubName.replace('.','/'));
         dout.close();
         dest = idx-2;
         length = bout.toByteArray().length;
         System.arraycopy(bout.toByteArray(), 0, newArr, dest, length);
         
         final byte[] byteArr = newArr;
         
         return new URLConnection(null)
         {
            public void connect()
            {
               
            }
            
            public InputStream getInputStream()
            {
               return new ByteArrayInputStream(byteArr);
            }
         };
      
         } catch (Throwable e)
         {
            e.printStackTrace();
            throw new IOException(e.getMessage());
         }
      } else
      {
         throw new IOException("Not a stub class");
      }
   }
}

⌨️ 快捷键说明

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