[Design the new POJO MicroContainer] - Re: Module.getModuleForClassLoader()
by adrian@jboss.org
"kabir.khan(a)jboss.com" wrote : Similar to the getModuleForClass() from JBCL-78
| discussed here http://www.jboss.com/index.html?module=bb&op=viewtopic&t=143639&postdays=...
|
| I would like to add an additional method to Module:
|
| | public Module getModuleForClassLoader(ClassLoader cl) throws ClassNotFoundException
| | {
| | SecurityManager sm = System.getSecurityManager();
| | if (sm != null)
| | sm.checkPermission(new RuntimePermission("getClassLoader"));
| |
| | // Determine the module (if any) for the classloader
| | if (cl != null)
| | return modulesByClassLoader.get(cl);
| | // Unknown
| | return null;
| | }
| |
| I need this to initialise my classpool domains with the bootstrap classloaders, since they are not getting picked up by the AOPClassLoaderDeployer in AS.
Ok with me. Your "workaround" looks horrible. :-)
The use of the MC to implement the classloading dependencies is an implemention detail,
it could change without notice.
anonymous wrote :
| I have created an org.jboss.bootstrap.spi.Bootstrap implementation to pick up the classloaders created by the bootstrap deployments.
|
| Next I need a method to get the Module for each classloader so I can push that into the AOP registry. I am not sure if Module is the right place for it, since I have no initiating Module at this stage.
|
I don't mind you adding static query methods to the Module class as long as
they have permissions checks.
In fact, I think the first part of your post would be better implemented by listing
all existing modules at aop initialization?
That way you don't depend upon the specific bootstrap implementation.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4203609#4203609
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4203609
17 years, 2 months
[Javassist development Forum] - CannotCompileExceptio-[source error]: no such class
by leooon
Hi, I am getting 'CannotCompileExceptio-[source error]: no such class: MyAlert' error while making a reference to an static Inner class name 'MyAlert'.
*********below is my code***********
public void addCodeToStartup(String strClass, String classpath){
System.out.println("\tupdating '"+strClass.trim()+"' located at '"+classpath+"' ...");
String cp2 = "";
try{
cp2 = classpath.substring(0, classpath.lastIndexOf('\\'));
System.out.println("\tcp2 '"+cp2);
//String pkg = "";
//pkg = strClass.substring(0, strClass.lastIndexOf('.'));
}catch(StringIndexOutOfBoundsException siobe){
System.out.println("StringIndexOutOfBoundsException: "+siobe.getMessage());
}
//System.out.println("\tpkg '"+pkg+"'");
try {
ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(".");
cp.insertClassPath(cp2+'\\');
cp.insertClassPath(classpath+'\\');
cp.importPackage("javax.microedition.lcdui");
cp.importPackage("javax.microedition.midlet");
cp.importPackage("javax.microedition.rms");
////////////////////////////////////////////////////////////////////////
CtClass cc = cp.get(strClass.trim());
CtClass anInterface = cp.get("javax.microedition.lcdui.CommandListener");
CtClass aSuperClass = cp.get("javax.microedition.lcdui.Alert");
CtClass aUiClass = cc.makeNestedClass("MyAlert", true);
System.out.println("\t1");
aUiClass.setSuperclass(aSuperClass);
aUiClass.addInterface(anInterface);
System.out.println("\t2");
CtField f = CtField.make("Command cmdOk;", aUiClass);
aUiClass.addField(f);
System.out.println("\t3");
f = CtField.make("static MIDlet midlet1;", aUiClass);
aUiClass.addField(f);
System.out.println("\t4");
CtConstructor c = CtNewConstructor.make(
"MyAlert(MIDlet midlet, String info) {"
+"\n super(\"Warning\", info, null, AlertType.WARNING);"
+"\n midlet1 = midlet;"
+"\n cmdOk = new Command(\"OK\", Command.EXIT, 1);"
+"\n addCommand(cmdOk);"
+"\n setCommandListener(this);"
+"\n }",
aUiClass);
aUiClass.addConstructor(c);
System.out.println("\t5");
CtMethod m = CtMethod.make(
"public void commandAction( Command cmd, Displayable displayable){"
+"\n\nif (cmd == cmdOk){"
+"\n midlet1.notifyDestroyed();"
+"\n}"
+"\n}",
aUiClass);
aUiClass.addMethod(m);
System.out.println("\t6");
m = CtMethod.make(
"static MyAlert getMyAlert(MIDlet newMIDlet, String s){"
+"\nreturn new MyAlert(newMIDlet, s);"
+"\n}",
aUiClass
);
aUiClass.addMethod(m);
System.out.println("\t7");
aUiClass.writeFile();
System.out.println("\tMyAlert class created!");
String dest = classpath +'\\'+ aUiClass.getName();
dest = dest.replace('.', '\\');
dest += ".class";
String newClass = aUiClass.getName();
newClass = newClass.replace('.', '\\')+".class";
System.out.println("\tnewClass: "+newClass);
System.out.println("\tdest: "+dest);
File file = null, destFile = null;
file = new File(newClass);
//destFile = new File(dest);
//file.renameTo(destFile);
//if (file.delete())
//System.out.println("\tfile succefull renames and copy is deleted!");
////////////////////////////////////////////////////////////////////////
// MIDlet TO BE MODIFIED //
////////////////////////////////////////////////////////////////////////
m = cc.getDeclaredMethod("startApp");
System.out.println("\t1");
//ADD YOUR startUp code here
m.insertBefore(
"{"
+"\n\n boolean flag=true;"
+"\n byte[] kb=null;"
+"\n\n try {"
+"\n RecordStore settings = RecordStore.openRecordStore(\"Demo\", true);"
+"\n if (settings.getNumRecords() <= 0) {"
+"\n //startTime = System.currentTimeMillis();"
+"\n kb=Long.toString(System.currentTimeMillis()).getBytes();"
+"\n settings.addRecord(kb,0,kb.length);// time in millis"
+"\n byte[] counter={0};"
+"\n settings.addRecord(counter,0,1); // counter"
+"\n flag= true;"
+"\n }"
+"\n\n kb=settings.getRecord(2);"
+"\n kb[0]=(byte)(kb[0]+1);"
+"\n System.out.println(\"value of counter \" + kb[0]);"
+"\n settings.setRecord(2,kb,0,1);"
+"\n\n if(settings.getRecord(2)[0]>2) {"
+"\n flag=false;"
+"\n }"
+"\n\n settings.closeRecordStore();"
+"\n }catch(Exception e) {"
+"\n e.printStackTrace();"
+"\n flag=false;"
+"\n }"
+"\n if(!flag) "
+"\n Display.getDisplay(this).setCurrent(MyAlert.getMyAlert(this, \"Your MIDlet expired, download again\"));"
+"\n else"
+"\n Display.getDisplay(this).setCurrent(MyAlert.getMyAlert(this, \"Still Valid!\"));"
+"\n "
+"}"
);
System.out.println("\tmethod created");
cc.writeFile();
System.out.println("\tYessssss");
}catch (NotFoundException nfe) {
System.out.println("\tNotFoundException: "+nfe.getMessage());
}catch (CannotCompileException cce) {
System.out.println("\tCannotCompileException: "+cce.getMessage());
System.out.println(cce.getReason());
System.out.println(cce.getCause());
}catch (IOException ioe) {
System.out.println("\tIOException: "+ioe.getMessage());
}
}
**********************************
Any sample example how to inject a static InnerClass is appreciated.
Best Regards
Mukesh
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4203602#4203602
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4203602
17 years, 2 months
[Design the new POJO MicroContainer] - Re: Module.getModuleForClassLoader()
by kabir.khan@jboss.com
I've worked around it for now in my AOPBootstrap
| public void start(Server server) throws Exception
| {
| log.debug("Registering bootstrap classloaders with AOP");
| if(server instanceof MCServer)
| {
| MCServer mcserver = MCServer.class.cast(server);
| Map<String, KernelDeployment> serverDeployments = mcserver.getDeployments();
| KernelController controller = mcserver.getKernel().getController();
|
| for(KernelDeployment kd : serverDeployments.values())
| {
| String loaderName = getClassLoaderName(kd);
| if (loaderName == null)
| {
| continue;
| }
| registerClassLoader(controller, loaderName);
| }
| }
| else
| {
| log.warn("Server is not an instance of MCServer. Ignoring bootstrap loaders");
| }
| }
|
| private String getClassLoaderName(KernelDeployment kd)
| {
| ClassLoaderMetaData clmd = kd.getClassLoader();
| if (clmd == null)
| {
| log.debug("No ClassLoaderMetaData for " + kd);
| return null;
| }
| ValueMetaData vmd = clmd.getClassLoader();
| if (vmd == null)
| {
| log.debug("No ValueMetaData for " + clmd + " from " + kd);
| return null;
| }
| Object ldr = vmd.getUnderlyingValue();
| if (ldr == null)
| {
| log.debug("No loader in " + vmd + " for " + clmd + " for " + kd);
| return null;
| }
| if (ldr instanceof String == false)
| {
| log.warn("Loader name " + ldr + " for " + kd + " is not a String");
| }
| return (String)ldr;
| }
|
| private void registerClassLoader(KernelController controller, String name)
| {
| ControllerContext loaderCtx = controller.getContext(name, ControllerState.INSTALLED);
| if (loaderCtx == null)
| {
| throw new IllegalStateException("No classloader found with name " + name);
| }
| ClassLoader loader = (ClassLoader)loaderCtx.getTarget();
| if (loader == null)
| {
| throw new IllegalStateException("Classloader was null " + name);
| }
|
| registerLoaderWithAOP(loader, findModule(controller, loaderCtx));
| }
|
| private Module findModule(KernelController controller, ControllerContext loaderCtx)
| {
| //Figure out the Module from the dependencies
| DependencyInfo info = loaderCtx.getDependencyInfo();
| Set<DependencyItem> loaderDependencies = info.getIDependOn(null);
| Module module = null;
| for (DependencyItem dep : loaderDependencies)
| {
| Object name = dep.getIDependOn();
| ControllerContext candidateCtx = controller.getContext(name, ControllerState.INSTALLED);
| if (candidateCtx.getTarget() instanceof Module)
| {
| if (module != null)
| {
| throw new IllegalStateException("Found more than one Module in the dependencies of " +
| loaderCtx.getName() + ":" + module.getContextName() + " and " + name);
| }
| module = (Module)candidateCtx.getTarget();
| }
| }
|
| if (module == null)
| {
| throw new IllegalStateException("No Module found for loader " + loaderCtx.getName());
| }
|
| return module;
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4203576#4203576
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4203576
17 years, 2 months