[srmi commit] r59 - in SRMI: . nbproject src/SRMI/RemotingServices src/SRMI/RemotingServices/ClassGeneration sr...

0 views
Skip to first unread message

codesite...@google.com

unread,
Dec 25, 2007, 7:31:40 AM12/25/07
to srmi...@googlegroups.com
Author: mohammad.nabil.h
Date: Tue Dec 25 04:31:09 2007
New Revision: 59

Added:
SRMI/src/SRMI/Tests/SRMIClassLoader.java
SRMI/src/SRMI/Tests/Test.java
Modified:
SRMI/manifest.mf
SRMI/nbproject/project.properties
SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerationUtils.java
SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerator.java
SRMI/src/SRMI/RemotingServices/ClassGeneration/Client/RmiStubAdapterGenerator.java
SRMI/src/SRMI/RemotingServices/ClassGeneration/PassByRemoteStubGenerator.java
SRMI/src/SRMI/RemotingServices/ClassGeneration/Server/RmiServiceWrapperGenerator.java
SRMI/src/SRMI/RemotingServices/ClientUtils.java
SRMI/src/SRMI/RemotingServices/CommonUtils.java
SRMI/src/SRMI/RemotingServices/ServerUtils.java
SRMI/src/SRMI/Tests/MyClient.java
SRMI/src/SRMI/Tests/MyServer.java

Log:
* Added a handy Test class to provide a live sample for users.
* Removed the silly unchecked warning for not using Class<?>. Just used
Class<?> instead of Class. Those people who wrote those checks... so
easy to fool !
* Added jarred projects support.
** Added a way to generate the folder structure.
** Added SRMIClassLoader to solve the jar issue (issue #2).
** ClassGenerator uses a URLClassLoader if the system class loader
can't load the classes.
* Clarified some exceptions, and used some assertions.
* Allowed an interface to be compiled alone ( no on-progress -style
bulk compiling). The interface don't need it cuz it wouldn't have
circular dependencies. Plus the interface must be loaded cuz it is used
by the other classes.

Modified: SRMI/manifest.mf
==============================================================================
--- SRMI/manifest.mf (original)
+++ SRMI/manifest.mf Tue Dec 25 04:31:09 2007
@@ -1,5 +1,4 @@
-Manifest-Version: 1.0
-X-COMMENT: Main-Class will be added automatically by build
-Premain-Class: SRMI.Tests.Agent
-Can-Redefine-Classes: true
-
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+Premain-Class: SRMI.Tests.Agent
+Can-Redefine-Classes: true

Modified: SRMI/nbproject/project.properties
==============================================================================
--- SRMI/nbproject/project.properties (original)
+++ SRMI/nbproject/project.properties Tue Dec 25 04:31:09 2007
@@ -29,7 +29,7 @@
${libs.BCEL.classpath}:\
${libs.JavAssist.classpath}
# Space-separated list of extra javac options
-javac.compilerargs=-Xlint:unchecked
+javac.compilerargs=-Xlint:unchecked -Xlint:deprecation
javac.deprecation=false
javac.source=1.5
javac.target=1.6
@@ -38,7 +38,7 @@
${build.classes.dir}:\
${libs.junit.classpath}
javadoc.additionalparam=
-javadoc.author=false
+javadoc.author=true
javadoc.encoding=
javadoc.noindex=false
javadoc.nonavbar=false
@@ -46,7 +46,7 @@
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
-javadoc.version=false
+javadoc.version=true
javadoc.windowtitle=
# Property libs.BCEL.classpath is set here just to make sharing of
project simpler.
# The library definition has always preference over this property.
@@ -54,7 +54,7 @@
# Property libs.JavAssist.classpath is set here just to make sharing
of project simpler.
# The library definition has always preference over this property.
libs.JavAssist.classpath=../../java/javassist-3.6/javassist.jar
-main.class=SRMI.Tests.DefaultCtorNeutralizer
+main.class=SRMI.Tests.Test
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
no.dependencies=true

Modified: SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerationUtils.java
==============================================================================
---
SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerationUtils.java (original)
+++
SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerationUtils.java
Tue Dec 25 04:31:09 2007
@@ -252,9 +252,9 @@

}

- private static boolean IsSerializable(Class type)
+ private static boolean IsSerializable(Class<?> type)
{
- Class ser = Serializable.class;
+ Class<Serializable> ser = Serializable.class;
boolean retval =
type.isPrimitive() // includes void
|| ser.isAssignableFrom(type)

Modified: SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerator.java
==============================================================================
--- SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerator.java (original)
+++ SRMI/src/SRMI/RemotingServices/ClassGeneration/ClassGenerator.java
Tue Dec 25 04:31:09 2007
@@ -20,6 +20,8 @@

package SRMI.RemotingServices.ClassGeneration;

+import SRMI.SRMIException;
+import SRMI.Tests.SRMIClassLoader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
@@ -27,6 +29,9 @@
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.HashSet;
@@ -80,12 +85,13 @@
* Prepare the generated class (Emits the file only. Doesn't load the
* generated class).
*/
- public void Prepare()
+ public void Prepare() throws SRMIException
{
synchronized (getWrappedClass())
{
PrepareEmit();
-
+ PrepareFolderHeirarchy();
+
if(classPath == null
|| classesFolder == null
|| destinationFolder == null
@@ -128,19 +134,37 @@
* generators. Calls Perpare() if not already generated, or if
needs regeneration.
* @return
*/
- public Class GetGenerated()
+ public Class<?> GetGenerated() throws SRMIException
{
PrepareEmit();
+ PrepareFolderHeirarchy();

String fullPathOfDependencyClassFile = destinationFolder +

package_.replace(".",java.lang.System.getProperty("file.separator")) +
java.lang.System.getProperty("file.separator") +
wrappedUnQualifiedClassName +
".class";
-
+
+ ClassLoader loader = null;
try
+ {
+ if(SRMIClassLoader.getInstance() != null)
+ loader = SRMIClassLoader.getInstance();
+ else
+ loader = new URLClassLoader(new URL[]{new File(destinationFolder).toURL()});
+ }
+ catch(MalformedURLException ex)
{
- Class ret = Class.forName(package_ + "." + generatedClassName);
+ l.error("Can't create URLClassLoader to load the generated classes");
+ throw new SRMIException("Can't create URLClassLoader to
load the generated classes",ex);
+ }
+
+ try
+ {
+ Class ret = loader.loadClass(package_ + "." + generatedClassName);
+ //Class.forName(package_ + "." + generatedClassName);
+
+ assert ret != null : "the class laoder " + loader + " have
loaded a null class !";

l.info(generatedClassName + " is already in cache,
checking if up-to-date...");

@@ -199,7 +223,7 @@
regenerate = true;
} catch (IOException ex2)
{
- l.error("",ex2);
+ l.error("Exception creating file " + srcFile.toString(),ex2);
}
}

@@ -226,7 +250,7 @@
// - and that class is in this JVM ( since same static var
onProgress )
// - and since no threads in SRMI ( currently ) then
// - this barrier is absolutely useless
- if(onProgress.size() > 0)
+ if(onProgress.size() > 0 && !classKeyword.equals("interface"))
{
try
{
@@ -300,7 +324,8 @@
{
try
{
- Class ret = Class.forName(package_ + "." + generatedClassName);
+ Class ret = loader.loadClass(package_ + "." + generatedClassName);
+ //Class.forName(package_ + "." + generatedClassName);
l.trace("Success, loaded compiled class file");
return ret;
}
@@ -315,7 +340,9 @@
l.info("Failed; javac returned " + errCode);
}
}
- assert false; // should never reach here
+ assert false : "should never reach here"; // should never
reach here
+ System.out.println("SHOULD NEVER REACH HERE ClassGenerator");
+ System.exit(1);
return null;
}

@@ -349,19 +376,19 @@
private Class wrappedClass;
protected String classKeyword = "class";

- protected abstract void PrepareEmit();
- protected abstract void EmitSupers();
- protected abstract void EmitConstructor();
- protected abstract void EmitFunction(Method m);
- protected abstract void EmitMembersVariables();
- protected abstract String getGeneratedClassNameSuffix();
+ protected abstract void PrepareEmit() throws SRMIException;
+ protected abstract void EmitSupers() throws SRMIException;
+ protected abstract void EmitConstructor() throws SRMIException;
+ protected abstract void EmitFunction(Method m) throws SRMIException;
+ protected abstract void EmitMembersVariables() throws SRMIException;
+ protected abstract String getGeneratedClassNameSuffix() throws SRMIException;

protected void EmitPackage()
{
file.println(String.format("package %s;\n",package_ ));
}

- protected void EmitClass()
+ protected void EmitClass() throws SRMIException
{
file.print(String.format("public %s %s ",classKeyword,this.generatedClassName));
EmitSupers();
@@ -374,7 +401,7 @@
file.println("}\n");
}

- protected void EmitFunctions()
+ protected void EmitFunctions() throws SRMIException
{
for(Method m : getWrappedClass().getDeclaredMethods())
EmitFunction(m);
@@ -408,7 +435,7 @@
}
}

- protected void EmitArg(Class param, boolean noEmitType, int number)
+ protected void EmitArg(Class<?> param, boolean noEmitType, int number)
{
if(!noEmitType)
{
@@ -461,24 +488,54 @@
generatedClassName
= wrappedUnQualifiedClassName + getGeneratedClassNameSuffix();
classesFolder
- =
java.lang.System.getProperty("srmi.basedir")/*new
File(".").getCanonicalPath()*/ + java.lang.System.getProperty("file.separator");
+ = System.getProperty("srmi.basedir")/*new
File(".").getCanonicalPath()*/ + System.getProperty("file.separator");
destinationFolder
= classesFolder;
fullPathToSourceFileName
- = destinationFolder +
package_.replace(".",java.lang.System.getProperty("file.separator")) +
java.lang.System.getProperty("file.separator") + generatedClassName + ".java";
+ = destinationFolder +
package_.replace(".",System.getProperty("file.separator")) +
System.getProperty("file.separator") + generatedClassName + ".java";
fullPathOfGeneratedClassFile = destinationFolder +
-
package_.replace(".",java.lang.System.getProperty("file.separator")) +
- java.lang.System.getProperty("file.separator") +
+
package_.replace(".",System.getProperty("file.separator")) +
+ System.getProperty("file.separator") +
generatedClassName +
".class";
classPath
- = classesFolder +
java.lang.System.getProperty("path.separator") + java.lang.System.getProperty("srmi.jar.path");
+ = classesFolder +
+ // System.getProperty("path.separator") +
System.getProperty("srmi.jar.path") + // i probly don't need to add it
cuz it is already in the classpath
+ System.getProperty("path.separator") +
+ System.getProperty("java.class.path");
}
catch (Exception ex)
{
l.error("",ex);
}
}
+
+ // prepare the package folder
+ private void PrepareFolderHeirarchy() throws SRMIException
+ {
+ String absdir = destinationFolder + package_.replace(".",System.getProperty("file.separator"));
+ if(new File(absdir).exists())
+ return;
+ String accumulativeDir = "";
+ for(String dir : absdir.split(System.getProperty("file.separator")))
+ {
+ if(dir.length() < 1)
+ continue;
+
+ accumulativeDir += System.getProperty("file.separator") + dir;
+ File file = new File(accumulativeDir);
+
+ if(file.exists())
+ continue;
+
+ if(!file.mkdir())
+ {
+ l.fatal("Can't create " + accumulativeDir);
+ throw new SRMIException("Can't create destination
folder (" + accumulativeDir + ") of the generated class");
+ }
+ }
+ l.info("Created dir: " + accumulativeDir);
+ }

private void OpenFile()
{
@@ -493,11 +550,20 @@
}
else if(!outputFile.exists())
{
- outputFile.createNewFile();
+ try
+ {
+ outputFile.createNewFile();
+ }
+ catch(IOException ex)
+ {
+ l.error("Exception creating file " + outputFile.toString(),ex);
+ return;
+ }
}// else, it exists, and it's size is zero, the wanted

GetLock(outputFile);

+ l.trace("Creating PrintWriter to " + fullPathToSourceFileName);
file = new PrintWriter(new File(fullPathToSourceFileName));
}
catch (Exception ex)
@@ -537,7 +603,7 @@
l.info("Already in progress : " + generatedClassName +
(onProgress.contains(generatedClassName)?
" in the current JVM":
- " still uncompiled source (by another JVM maybe)"));
+ " still uncompiled source (by another JVM
maybe), or already compiled and existing"));

return yes;
}

Modified: SRMI/src/SRMI/RemotingServices/ClassGeneration/Client/RmiStubAdapterGenerator.java
==============================================================================
---
SRMI/src/SRMI/RemotingServices/ClassGeneration/Client/RmiStubAdapterGenerator.java (original)
+++
SRMI/src/SRMI/RemotingServices/ClassGeneration/Client/RmiStubAdapterGenerator.java
Tue Dec 25 04:31:09 2007
@@ -24,6 +24,7 @@
import SRMI.RemotingServices.ClassGeneration.PassByRemoteStubGenerator;
import SRMI.RemotingServices.ClassGeneration.RemoteInterfaceGenerator;
import SRMI.RemotingServices.PassableByRemote;
+import SRMI.SRMIException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -38,18 +39,19 @@

public class RmiStubAdapterGenerator extends ClassGenerator
{
- protected Class remoteInterface;
- protected Class remoteExceptionClass;
+ protected Class<?> remoteInterface;
+ protected Class<?> remoteExceptionClass;

/**
* @param wrapped The class to generate a stub adapter to.
*/
- public RmiStubAdapterGenerator(Class wrapped)
+ public RmiStubAdapterGenerator(Class<?> wrapped) throws SRMIException
{
- Class remoteInterface = new RemoteInterfaceGenerator(wrapped).GetGenerated();
+ Class<?> remoteInterface = new RemoteInterfaceGenerator(wrapped).GetGenerated();

this.setWrappedClass(wrapped);
this.remoteInterface = remoteInterface;
+ assert remoteInterface != null : "null interface class";
try
{
remoteExceptionClass = Class.forName("java.rmi.RemoteException");
@@ -99,7 +101,7 @@
file.println(String.format("\tprotected %s rmistub;\n\tpublic
java.util.UUID uuid;\n\tpublic String host;\n\n",remoteInterface.getName()));
}

- protected void EmitFunction(Method m)
+ protected void EmitFunction(Method m) throws SRMIException
{
int mod = m.getModifiers();
if(!Modifier.isPrivate(mod)
@@ -139,11 +141,11 @@
}
}

- protected void PreEmitArgs(Method m)
+ protected void PreEmitArgs(Method m) throws SRMIException
{
// get's the appropriate pass-by-remote objects for the args
int number = 0;
- for(Class param : m.getParameterTypes())
+ for(Class<?> param : m.getParameterTypes())
{
if(param.getAnnotation(PassableByRemote.class) != null)
{
@@ -153,7 +155,7 @@
}
}

- private void PreparePassByRemoteParam(Class param, int number)
+ private void PreparePassByRemoteParam(Class<?> param, int number)
throws SRMIException
{
//TODO get '_pass_by_remote' string from the generator itself

@@ -165,7 +167,7 @@
param.getName(), "p" + number, "p" + number,
param.getName(), "p" + number));
}

- protected void EmitArg(Class param, boolean noEmitType, int number)
+ protected void EmitArg(Class<?> param, boolean noEmitType, int number)
{
if(!noEmitType)
{
@@ -178,7 +180,7 @@
file.print("p" + number);
}

- protected void EmitWrappedCall(Method m)
+ protected void EmitWrappedCall(Method m) throws SRMIException
{
PreEmitArgs(m);


Modified: SRMI/src/SRMI/RemotingServices/ClassGeneration/PassByRemoteStubGenerator.java
==============================================================================
---
SRMI/src/SRMI/RemotingServices/ClassGeneration/PassByRemoteStubGenerator.java (original)
+++
SRMI/src/SRMI/RemotingServices/ClassGeneration/PassByRemoteStubGenerator.java
Tue Dec 25 04:31:09 2007
@@ -21,6 +21,7 @@
package SRMI.RemotingServices.ClassGeneration;

import SRMI.RemotingServices.ClassGeneration.Client.RmiStubAdapterGenerator;
+import SRMI.SRMIException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

@@ -34,7 +35,7 @@
/**
* @param wrapped The class to generated a pass by remote stub for.
*/
- public PassByRemoteStubGenerator(Class wrapped)
+ public PassByRemoteStubGenerator(Class wrapped) throws SRMIException
{
// if someone needs a pass-by-remote, they also gonna need a
remote interface :)
new RemoteInterfaceGenerator(wrapped).Prepare();
@@ -58,7 +59,7 @@
file.print("extends " + super.wrappedFullyQualifiedClassName
+ " implements java.io.Serializable");
}

- protected void EmitConstructor()
+ protected void EmitConstructor() throws SRMIException
{
// default constructor ( server side ?? )
file.print(String.format(

Modified: SRMI/src/SRMI/RemotingServices/ClassGeneration/Server/RmiServiceWrapperGenerator.java
==============================================================================
---
SRMI/src/SRMI/RemotingServices/ClassGeneration/Server/RmiServiceWrapperGenerator.java (original)
+++
SRMI/src/SRMI/RemotingServices/ClassGeneration/Server/RmiServiceWrapperGenerator.java
Tue Dec 25 04:31:09 2007
@@ -24,6 +24,7 @@
import SRMI.RemotingServices.ClassGeneration.PassByRemoteStubGenerator;
import SRMI.RemotingServices.ClassGeneration.RemoteInterfaceGenerator;
import SRMI.RemotingServices.PassableByRemote;
+import SRMI.SRMIException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -40,12 +41,13 @@
/**
* @param wrapped The class to generate rmi service wrapper for.
*/
- public RmiServiceWrapperGenerator(Class wrapped)
+ public RmiServiceWrapperGenerator(Class wrapped) throws SRMIException
{
Class remoteInterface = new RemoteInterfaceGenerator(wrapped).GetGenerated();

this.setWrappedClass(wrapped);
this.remoteInterface = remoteInterface;
+ assert remoteInterface != null : "null interface class";
try
{
remoteExceptionClass = Class.forName("java.rmi.RemoteException");
@@ -72,7 +74,8 @@
"\tpublic %s(%s wrapped) throws
java.rmi.RemoteException\n\t{\n\t\tthis.wrapped = wrapped;\n\t}\n\n",generatedClassName,wrappedFullyQualifiedClassName));
}

- protected void EmitFunctions()
+ @Override
+ protected void EmitFunctions() throws SRMIException
{
for(Method m : remoteInterface.getMethods())
EmitFunction(m);
@@ -88,7 +91,7 @@
return "_serverside_wrapper";
}

- protected void EmitFunction(Method m)
+ protected void EmitFunction(Method m) throws SRMIException
{
/*
public int Quack(int i) throws java.rmi.RemoteException
@@ -127,7 +130,7 @@
}
}

- protected void EmitWrappedCall(Method m)
+ protected void EmitWrappedCall(Method m) throws SRMIException
{
file.print(
String.format(
@@ -161,7 +164,7 @@

// just assure thre pass-by-remote classes are generated
//////////////////////////////////////////////
- for(Class param : m.getParameterTypes())
+ for(Class<?> param : m.getParameterTypes())
if(param.getAnnotation(PassableByRemote.class) != null)
// assure it is generated
new PassByRemoteStubGenerator(param).Prepare();

Modified: SRMI/src/SRMI/RemotingServices/ClientUtils.java
==============================================================================
--- SRMI/src/SRMI/RemotingServices/ClientUtils.java (original)
+++ SRMI/src/SRMI/RemotingServices/ClientUtils.java Tue Dec 25 04:31:09 2007
@@ -88,7 +88,7 @@

int i = 0;
HashMap<UUID,Object> availableObjects = new HashMap<UUID,Object>();
- Class clientside_adapter = new
RmiStubAdapterGenerator(requestedClass).GetGenerated(); // must be
generated before lookup so the lookup routine finds the generated interface
+ Class<?> clientside_adapter = new
RmiStubAdapterGenerator(requestedClass).GetGenerated(); // must be
generated before lookup so the lookup routine finds the generated interface

String existingServices[] = null;
try

Modified: SRMI/src/SRMI/RemotingServices/CommonUtils.java
==============================================================================
--- SRMI/src/SRMI/RemotingServices/CommonUtils.java (original)
+++ SRMI/src/SRMI/RemotingServices/CommonUtils.java Tue Dec 25 04:31:09 2007
@@ -50,7 +50,7 @@
*/
public class CommonUtils
{
- protected final static int registryPortNumber = 1111;
+ public final static int registryPortNumber = 1111;
protected final static String CACHED_IP_FILE = "cached-external-ip";

private static final Logger l = Logger.getLogger(CommonUtils.class);

Modified: SRMI/src/SRMI/RemotingServices/ServerUtils.java
==============================================================================
--- SRMI/src/SRMI/RemotingServices/ServerUtils.java (original)
+++ SRMI/src/SRMI/RemotingServices/ServerUtils.java Tue Dec 25 04:31:09 2007
@@ -130,7 +130,7 @@
*/
public static Object DeclareObject(Object wrappedObject,UUID uuid)
throws SRMIException
{
- //InstallShutDownHook();
+ InstallShutDownHook();
assert shutdownHookAdded;

if(java.lang.System.getProperty("srmi.basedir") == null)
@@ -230,7 +230,8 @@
return entry.getKey();
}
}
- l.info("Object reference for the specific instance of " +
O.getClass().getName() + " not found in declared object");
+ l.info("Object reference for the specific instance of " +
O.getClass().getName() + "@" + O.hashCode() +
+ " not found in declared objects");
return null;
}
}

Modified: SRMI/src/SRMI/Tests/MyClient.java
==============================================================================
--- SRMI/src/SRMI/Tests/MyClient.java (original)
+++ SRMI/src/SRMI/Tests/MyClient.java Tue Dec 25 04:31:09 2007
@@ -46,7 +46,7 @@
server.CallMe( this );
}

- protected void CallingYou()
+ public void CallingYou()
{
System.out.println("i've been called");
}

Modified: SRMI/src/SRMI/Tests/MyServer.java
==============================================================================
--- SRMI/src/SRMI/Tests/MyServer.java (original)
+++ SRMI/src/SRMI/Tests/MyServer.java Tue Dec 25 04:31:09 2007
@@ -29,7 +29,7 @@
System.out.println("Evil <init> called");
}

- protected String cat(String F, String U)
+ public String cat(String F, String U)
{
System.out.println("request to cat " + F + " + " + U);
return F + U;
@@ -37,7 +37,7 @@

private MyClient clnt;

- protected void CallMe(MyClient clnt)
+ public void CallMe(MyClient clnt)
{
System.out.println("calling client");
clnt.CallingYou();
@@ -53,12 +53,12 @@
SRMI.RemotingServices.ServerUtils.DeclareObject(new MyServer());
}

- void CallMe(MyIndirectClient myIndirectClient)
+ public void CallMe(MyIndirectClient myIndirectClient)
{
myIndirectClient.CallMe(clnt);
}

- MyClient GetMyClient()
+ public MyClient GetMyClient()
{
return clnt;
}

Added: SRMI/src/SRMI/Tests/SRMIClassLoader.java
==============================================================================
--- (empty file)
+++ SRMI/src/SRMI/Tests/SRMIClassLoader.java Tue Dec 25 04:31:09 2007
@@ -0,0 +1,117 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package SRMI.Tests;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.log4j.Logger;
+
+/**
+ *
+ * @author meemo
+ */
+public class SRMIClassLoader extends ClassLoader {
+
+ private static Logger l = null;
+
+ public SRMIClassLoader(ClassLoader parent)
+ {
+ super(parent);
+ instance = this;
+ }
+
+ private static SRMIClassLoader instance = null;
+
+ public static SRMIClassLoader getInstance()
+ {
+ return instance;
+ }
+
+ private String path = null;
+
+ public void SetPath(String path)
+ {
+ if(l == null)
+ l = Logger.getLogger(SRMIClassLoader.class);
+ this.path = path;
+ }
+
+ @Override
+ public Class<?> loadClass(String name) throws
ClassNotFoundException {
+ try
+ {
+ if(l != null) l.trace("SRMIClassLoader: trying to load " +
name + " ... ");
+
+ Class<?> cls = null;
+ try
+ {
+ cls = getParent().loadClass(name);
+ }
+ catch(ClassNotFoundException ex)
+ {
+ cls = findClass(name);
+ }
+ assert cls != null;
+ if(l != null) l.trace("success");
+ return cls;
+ }
+ catch(ClassNotFoundException ex)
+ {
+ if (path == null)
+ {
+ throw new ClassNotFoundException("SRMIClassLoader path
not set", ex);
+ }
+
+ String sep = System.getProperty("file.separator");
+ String file = path + sep + name.replace(".", sep) + ".class";
+ InputStream stream = null;
+
+ try
+ {
+ if(l != null) l.trace("trying " + file + " ... ");
+
+ stream = new File(file).toURL().openStream();
+
+ byte[] bytes = new byte[stream.available()];
+ stream.read(bytes);
+
+ Class<?> cls = null;
+
+ try
+ {
+ cls = defineClass(name, bytes, 0, bytes.length);
+ }
+ catch(LinkageError le)
+ {
+ // duplicate definition ...
+ return super.loadClass(name);
+ }
+
+ if(l != null) l.trace("success");
+
+ return cls;
+ }
+ catch (Exception ex1)
+ {
+ if(l != null) l.trace("failed");
+ throw new ClassNotFoundException("Can't open stream "
+ file, ex1);
+ }
+ finally
+ {
+ try
+ {
+ if(stream != null)
+ stream.close();
+ }
+ catch (IOException ex1)
+ {
+ throw new ClassNotFoundException("Can't close
stream " + file, ex1);
+ }
+ }
+ }
+ }
+}

Added: SRMI/src/SRMI/Tests/Test.java
==============================================================================
--- (empty file)
+++ SRMI/src/SRMI/Tests/Test.java Tue Dec 25 04:31:09 2007
@@ -0,0 +1,66 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package SRMI.Tests;
+
+import java.io.File;
+
+/**
+ *
+ * @author meemo
+ */
+public class Test {
+ public static void main(String[] args) throws Exception {
+ Process p = null;
+ try
+ {
+ if(args.length < 1)
+ {
+ System.out.println("Usage: java -ea
-Djava.system.class.loader=SRMI.Tests.SRMIClassLoader -jar SRMI.jar
[server|client|indrclient]");
+ return;
+ }
+
+ String basedir = null;
+
+ if(System.getProperty("srmi.basedir") == null)
+ {
+ basedir = new File(".").getAbsolutePath();
+ basedir = basedir.substring(0,basedir.length()-2);
+ System.out.println("Setting srmi.basedir to " + basedir);
+ System.setProperty("srmi.basedir", basedir);
+ }
+
+ if(SRMIClassLoader.getInstance() != null)
+ {
+ System.out.println("SRMIClassLoader is intact");
+ SRMIClassLoader.getInstance().SetPath(basedir);
+ }
+ else
+ System.out.println("SRMIClassLoader is NOT intact");
+
+ if(args[0].equalsIgnoreCase("server"))
+ {
+ MyServer.main(new String[]{});
+ }
+ else if(args[0].equalsIgnoreCase("client"))
+ {
+ MyClient.main(new String[]{});
+ }
+ else if(args[0].equalsIgnoreCase("indrclient"))
+ {
+ MyIndirectClient.main(new String[]{});
+ }
+ }
+ catch(Throwable ex)
+ {
+ System.out.println("Exception caught !");
+ do{
+ ex.printStackTrace();
+ ex = ex.getCause();
+ System.out.println("Caused by:");
+ } while(ex != null);
+ }
+ }
+}

codesite...@google.com

unread,
Dec 25, 2007, 7:31:40 AM12/25/07
to srmi...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages