Permalink
Browse files

Merge pull request #23 from basicsensei/master

tweaks with: cljminecraft.jar, *use-context-classloader* and getResource()

Don't add cljminecraft.jar to classpath because parent already has it
Make sure default *use-context-classloader* being false is explicit
getResource will warn if there is resource shadowing happening in the future.
  • Loading branch information...
CmdrDats committed Dec 7, 2012
2 parents 39c29fa + 27e7b74 commit f9b16db21d8840cd4561166ef293585383ed6f5a
Showing with 61 additions and 4 deletions.
  1. +58 −2 javasrc/cljminecraft/BasePlugin.java
  2. +3 −2 javasrc/cljminecraft/ClojurePlugin.java
@@ -17,6 +17,7 @@
public abstract class BasePlugin extends JavaPlugin{
+ protected final static String selfPluginName=ClojurePlugin.class.getPackage().getName();//"cljminecraft";
public final static Charset UTF8 = Charset.forName("UTF-8");
@@ -100,6 +101,11 @@ public Object run() {
clojure.lang.RT.F
, true);
//the above is equivalent to clojure code: (set! *warn-on-reflection* true)
+
+ clojure.lang.Var.intern(clojure.lang.RT.CLOJURE_NS,
+ clojure.lang.Symbol.intern("*use-context-classloader*")
+ ,clojure.lang.RT.F
+ , true);
}finally{
Thread.currentThread().setContextClassLoader(previous);
}
@@ -128,7 +134,10 @@ public final void onLoad() {
info( "loaded jar: " + jarURL );
assert clojure.lang.Compiler.LOADER.isBound();
- ( (clojure.lang.DynamicClassLoader)clojure.lang.Compiler.LOADER.deref() ).addURL( jarURL );
+ if (!getName().equals(selfPluginName)) {
+ //we don't have to add cljminecraft.jar to classpath here
+ ( (clojure.lang.DynamicClassLoader)clojure.lang.Compiler.LOADER.deref() ).addURL( jarURL );
+ }
}
public BasePlugin() {
@@ -140,10 +149,11 @@ public BasePlugin() {
//TODO: maybe add a test to make sure this didn't change in the future
}
- public static void showClassPath(String prefix, ClassLoader cl){
+ public static String showClassPath(String prefix, ClassLoader cl){
_info("=="+prefix+"== For classloader "+cl+" ----------");
_info(getClassPath(cl));
_info("=="+prefix+"== ----END---"+cl+" ----------");
+ return "";
}
@@ -177,6 +187,52 @@ public final static String getClassPath(ClassLoader cl) {
return cp;
}
+ protected String showResources(ClassLoader cl, String file1) {
+ try {
+ Enumeration<URL> urls = cl.getResources( file1 );
+ System.out.println("all `"+file1+"` Resources: { ");
+ while (urls.hasMoreElements()) {
+ System.out.println(urls.nextElement());
+ }
+ System.out.println(" }");
+ } catch ( IOException e ) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ protected int count(Enumeration e) {
+ assert null != e:"you passed null, bug somewhere";
+ int count=0;
+ while(e.hasMoreElements()) {
+ count++;
+ e.nextElement();
+ }
+ return count;
+ }
+
+ @Override
+ public InputStream getResource( String fileName ) {
+ //XXX: when a file within the .jar of current plugin is needed then from within clojure or java either call this method or call
+ //clojure.lang.RT.getResource(this.getClassLoader(), fileName); where this == plugin instance
+ //or else it will get the file from cljminecraft not from memorystone, ie. file name like config.yml which exists in both in same location
+ assert isNotMoreThanOneResource(fileName):"more than 1 file with the same name was detected in classpath,"
+ +showClassPath("",getClassLoader())
+ +showResources( getClassLoader(), fileName )
+ +" PLEASE SEE ABOVE ";
+
+ return super.getResource( fileName );
+ }
+
+ protected boolean isNotMoreThanOneResource(String fileName) {
+ try {
+ return count(getClassLoader().getResources( fileName )) <= 1;
+ } catch ( IOException e ) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ }
+
public final void severe(String msg) {
info(ChatColor.RED+"[SEVERE] "+ChatColor.RESET+msg);//because colored won't show [SEVERE] only [INFO] level msgs
}
@@ -1,5 +1,7 @@
package cljminecraft;
+import java.io.*;
+
/**
* an instance of this class is created for every plugin (including the main cljminecraft one) that depends on cljminecraft, because
* it will have to have in its plugin.yml the following:<br>
@@ -8,7 +10,7 @@
*/
public class ClojurePlugin extends BasePlugin {
- private final static String selfPluginName=ClojurePlugin.class.getPackage().getName();//"cljminecraft";
+
private final static String selfCoreScript=selfPluginName+".core";
private final static String selfEnableFunction="on-enable";
private final static String selfDisableFunction="on-disable";
@@ -57,7 +59,6 @@ public boolean start() {
return success;
}
-
@Override
public void stop() {//called only when onEnable didn't fail (if we did the logic right)
String pluginName = getDescription().getName();

0 comments on commit f9b16db

Please sign in to comment.