Questions & Answers

dheim
I'm new here

Zugriff auf Third Party Klassen im Beanshell Skript

Hallo zusammen,

ich versuche Testweise in einem Beanshell-Skript auf eine Klasse (CoreContextFactory) zuzugreifen die in einem jar liegt, welche als lib in einer Library-Komponente auf dem Server installiert ist.

// LDAP-Connection

Hashtable env = new Hashtable(6);

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.core.jndi.CoreContextFactory");

env.put(Context.PROVIDER_URL, "ldap://localhost:10389");

env.put(Context.SECURITY_AUTHENTICATION, "simple");

env.put(Context.SECURITY_PRINCIPAL, "cn=system administrator");

env.put(Context.SECURITY_CREDENTIALS, "admin");

DirContext ldap = new InitialDirContext(env);

dabei kommt es in der letzten Zeile zu einer ClassNotFoundException weil er die Factory-Klasse nicht findet. Habe ich eine falsche Vorstellung davon wie auf eine Library vom BeanShell-Skript aus zugegriffen werden kann?

Bin Dankbar fรผr jeden Tipp,

Viele GrรผรŸe

Dominik

Fehler bei der Skriptausfรผhrung in Zeile 50.

FSVersion=5.0.210.55494#3070;JDK=1.7.0_15 64bit Oracle Corporation;OS=Windows 7 6.1 amd64;Date=21.03.2013 14:02:22

de.espirit.firstspirit.access.script.ExecutionException: Typed variable declaration : Object constructor : at Line: 50 : in file: inline evaluation of: ``__execute() { import javax.naming.NamingEnumeration;  import javax.naming.Contex . . . '' : new InitialDirContext ( env )

Called from method: __execute

Target exception: javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.directory.server.core.jndi.CoreContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.directory.server.core.jndi.CoreContextFactory]

at line 50

    at de.espirit.firstspirit.server.script.BeanshellScriptEngine$BeanshellExecutable.execute(BeanshellScriptEngine.java:108)

    at de.espirit.firstspirit.client.gui.applications.ApplicationTabRegistry$IdentifiableExecutable.execute(ApplicationTabRegistry.java:129)

    at de.espirit.firstspirit.common.ScriptUtil.execute(ScriptUtil.java:97)

    at de.espirit.firstspirit.client.action.ScriptMenuAction$ScriptAction$1.run(ScriptMenuAction.java:218)

    at de.espirit.firstspirit.client.gui.util.GuiUtil$RunnableWrapper.run(GuiUtil.java:1928)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

    at java.lang.Thread.run(Unknown Source)

Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.directory.server.core.jndi.CoreContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.directory.server.core.jndi.CoreContextFactory]

    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

    at javax.naming.InitialContext.init(Unknown Source)

    at javax.naming.InitialContext.<init>(Unknown Source)

    at javax.naming.directory.InitialDirContext.<init>(Unknown Source)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

    at java.lang.reflect.Constructor.newInstance(Unknown Source)

    at bsh.Reflect.constructObject(Reflect.java:574)

    at bsh.BSHAllocationExpression.constructObject(BSHAllocationExpression.java:125)

    at bsh.BSHAllocationExpression.objectAllocation(BSHAllocationExpression.java:114)

    at bsh.BSHAllocationExpression.eval(BSHAllocationExpression.java:62)

    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)

    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)

    at bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:86)

    at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:84)

    at bsh.BSHBlock.evalBlock(BSHBlock.java:130)

    at bsh.BSHBlock.eval(BSHBlock.java:80)

    at bsh.BshMethod.invokeImpl(BshMethod.java:362)

    at bsh.BshMethod.invoke(BshMethod.java:258)

    at bsh.BshMethod.invoke(BshMethod.java:161)

    at bsh.PreparsedScript.invoke(PreparsedScript.java:65)

    at de.espirit.firstspirit.server.script.BeanshellScriptEngine$BeanshellExecutable.execute(BeanshellScriptEngine.java:99)

    ... 7 more

Caused by: java.lang.ClassNotFoundException: org.apache.directory.server.core.jndi.CoreContextFactory

    at java.net.URLClassLoader$1.run(Unknown Source)

    at java.net.URLClassLoader$1.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(Unknown Source)

    at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at java.lang.Class.forName0(Native Method)

    at java.lang.Class.forName(Unknown Source)

    at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)

    ... 31 more

0 Kudos
10 Replies
Peter_Jodeleit
Crownpeak employee

Kannst du die module.xml des Moduls bitte posten.

Und noch ein Tipp: Wenn du code Tags benutzt, werden deine Posts besser lesbar.

Peter
0 Kudos

Natรผrlich. Smiley Happy  Und ich werde gleich die Code-Tag Sache versuchen.

<!DOCTYPE module SYSTEM "../lib/module.dtd">

<module>

    <name>LibraryModule</name>

    <version>1.0</version>

    <description>LibraryModule</description>

    <vendor>21TORR</vendor>

    <components>   

              <library>

                       <name>Executable for FirstSpirit</name>

                       <version>1.1</version>

                       <description>Executable for FirstSpirit</description>

                      <resources>

                       <resource>lib/apacheds-service-2.0.0-M11.jar</resource>

                 </resources>

              </library> 

    </components>

</module>

0 Kudos
arnbae
I'm new here

Hi,

nur ein Strohhalm, weil wir ein รคhnliches Problem hatten, welches nun gelรถst ist. Unser Script lief unter der Nutzung der Klasse aus der Library genau einmal durch, warf eine "PermissionException", und ab da (bis zum Neustart des Java-Clients) nur noch eine "Class not found"-Exception.

Lรถsung: In der Bibliotheks-Modul-Konfiguration auf dem Server (Admin-Konsole) den Haken bei "Alle Rechte" gesetzt, und jetzt geht alles.

Laienhafte Erklรคrungsthese: Die Klasse beinhaltet einen HTTP-Client, der Netzwerkverbindungen aufmacht. FS erlaubt das nicht, deshalb wird nach dem ersten Aufruf das ganze Klassenfile gesperrt.

Vielleicht hilft das ja. Kann aber auch sein, dass meine Erklรคrung kompletter Quatsch ist, und an Deinem Problem vorbei geht. Habe nur gesehen, dass Du auch eine LDAP-Verbindung aufmachst.

GrรผรŸe,

Arndt

0 Kudos

Hallo Arndt,

danke fรผr den Strohhalm, leider hat das dran klammern nicht funktioniert.

Hab dem Modul wie beschrieben "alle Rechte" gegeben, รคndert leider nichts an der Tatsache.

GrรผรŸe

Dominik

0 Kudos

Natรผrlich. Smiley Happy  Und ich werde gleich die Code-Tag Sache versuchen.

Hat super geklappt Smiley Wink

[EDIT]

Die "ClassNotFoundException" kommt aus einer core-Java-Klasse. Diese kann nur Klassen finden, die im classpath liegen.

Da ich mich mit den Interna von JNDI nicht so auskenne, kann ich nur vermuten, das man dem Problem durch setzen des contextClassLoaders umgehen kann (wenn man das jar nicht in den Klassenpfad legen will/kann).

Peter
0 Kudos

so, der nรคchste Versuch war auch nicht ganz von Erfolg geprรคgt

das mit dem scope="server" hat leider auch nicht funktioniert.

GrรผรŸe,

Dominik

0 Kudos

Sorry, da vorher die Exception nicht so gut lesbar war, war ich erst "auf dem falschen Dampfer".

Das Lรถschen meines Beitrages hatte wohl nicht die gewรผnschte Wirkung, bzw. du hast ihn doch noch gesehen/gelesen...

Peter
0 Kudos

Fรผr JNDI muss das JAR entweder im Klassenpfad liegen oder รผber den "contextClassLoader" erreichbar sein.

Versuch bitte mal

oldClassLoader = Thread.currentThread.getContextClassLoader();

try {

     Thread.currentThread.setContextClassLoader(context.connection.getClassLoader());

     ... // your code

} finally {

     Thread.currentThread.setContextClassLoader(oldClassLoader);

}

Peter
0 Kudos

Klasse! Hat geklappt! Vielen Dank fรผr den Support Peter.

Class.forName ging nicht aber das laden direkt รผber den Class-Loader ging und was noch viel wichtiger ist der indirekte Aufruf der LDAP-Parameter ebenso.

Viele GrรผรŸe,

Dominik

oldClassLoader = Thread.currentThread().getContextClassLoader();

try {

     Thread.currentThread().setContextClassLoader(context.connection.getClassLoader());

        //working

        Class lClass = Thread.currentThread().getContextClassLoader().loadClass("org.apache.directory.server.core.jndi.CoreContextFactory");

     Object lInstance = lClass.newInstance();

       

     // not working    

     // Class lClass = Class.forName("org.apache.directory.server.core.jndi.CoreContextFactory");

       

        //also working!!!! thank you!!

       Hashtable env = new Hashtable(6);

    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.core.jndi.CoreContextFactory");

    env.put(Context.PROVIDER_URL, "ldap://localhost:10389");

    env.put(Context.SECURITY_AUTHENTICATION, "simple");

    env.put(Context.SECURITY_PRINCIPAL, "cn=system administrator");

    env.put(Context.SECURITY_CREDENTIALS, "admin");

    DirContext ldap = new InitialDirContext(env);

       

      

} finally {

     Thread.currentThread().setContextClassLoader(oldClassLoader);

}

0 Kudos

Type a product name