?? handler.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 + -