You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Travis Bear <tb...@myrio.com> on 2005/02/01 21:48:41 UTC

Re: Classpath/Classloader issue with java program invoked via Velocitycontext int Tomcat

Hmm.  I'm using an extension of the VelocityLayoutServlet, so what I was
calling the context was actually a java class I configured in as a tool,
via toolbox.xml.  This class does not actually implement the Context (or
any other) interface, although I could certainly do that if needed.

Here's the toolbox.xml:

<?xml version="1.0"?>
<toolbox>
    <tool>
        <key>setup</key>
        <class>com.myrio.qa.qas.SetupUI</class>
    </tool>

    <tool>
        <key>params</key>
        <scope>request</scope>

<class>org.apache.velocity.tools.view.tools.ParameterParser</class>
    </tool>
</toolbox>



Here's the Velocity template code that invokes the runSetup() method:

#if ($request.method=="POST")
    $setup.runSetup()
#end


Thanks,



-Travis



On Tue, 2005-02-01 at 11:37 -0800, Will Glass-Husain wrote:
> Travis,
> 
> When you say "context" do you mean an object that defines the Context 
> interface, or do you mean a web application?
> 
> WILL
> 
> ----- Original Message ----- 
> From: "Travis Bear" <tb...@myrio.com>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Tuesday, February 01, 2005 11:23 AM
> Subject: Classpath/Classloader issue with java program invoked via 
> Velocitycontext int Tomcat
> 
> 
> > I'm really puzzled by an issue I'm seeing when I invoke an external
> > object's main method from inside the Velocity context.  All my code is
> > running as a webapp inside Tomcat.  The external object is a Jython
> > script compiled into a java class via jythonc.  This class is put into a
> > jar file and placed in $CATALINA_HOME/webapps/$WEBAPP_NAME/WEB-INF/lib.
> >
> > When I invoke the Jython script from inside Tomcat, it breaks because it
> > can't locate any of the packages I try to import from.  The puzzling
> > part is that the context itself is able to import from the same package
> > that breaks the jython script, and it's running in the same VM.
> >
> > Here is the Jython script Setup.py:
> >
> >
> >
> > print "Entering Setup"
> > from org.jtmb.sql import DataEngine
> > from java.io import File
> > propsFile = File("conf/DataEngine.properties")
> > de = DataEngine.getStaticInstance(propsFile)
> > print de.getString("select version as name from branch")
> >
> >
> >
> > Here is the code from the Velocity context where I statically invoke the
> > jython script:
> >
> > com.myrio.setup.Setup.main(new String[]{});
> >
> > Going on the theory that the context must have had a good classloader
> > because it was able to instantiate the DataEngine, I attempted to load
> > my class by explicitly using the same classloader:
> >
> >    ClassLoader loader = this.getClass().getClassLoader();
> >    Object setup = Class.forName("com.myrio.setup.Setup", true,
> > loader).newInstance();
> >    logger.fatal ("Attempting to load setup with custom classloader");
> >    ((com.myrio.setup.Setup) setup).main(new String[]{});
> >
> >
> > In both cases, I get the same result.  Here's the output in
> > catalina.out.
> >
> > Entering Setup
> > Traceback (innermost last):
> >  File "/home/travis/p4/3.5/qa/qas/code/jython/Setup.py", line 0, in
> > main
> > ImportError: No module named jtmb
> >
> >
> >
> > Thanks in advance -- this is my first effort at using classloaders, so I
> > could easily be missing something.  I'm pretty sure I could get it to
> > work by manually setting the system classpath via system properties.
> > But that seems like a pretty broken approach.  Any help is appreciated!
> >
> >
> > -Travis
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 
> 
> _________________________________________________
> Scanned on 01 Feb 2005 20:24:56
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Classpath/Classloader issue with java program invoked viaVelocitycontext int Tomcat

Posted by Will Glass-Husain <wg...@forio.com>.
Hi Travis,

What happens when you put $setup into the template all by itself.  Does it 
show the results of calling toString?  This would demonstrate that the 
SetupUI class is properly loaded.

By the way, this is a very unusual way of using Velocity.  (Not just the 
jython).  Typically, Velocity is used to display data, not to change state. 
Generally you do any set up type actions before the Velocity page is 
displayed.  A good rule is that method calls can be used to display 
information but not to change the state of the running app.  For example, 
you could subclass VelocityLayoutServlet and call setup functionality in the 
init() method.

I'd suggest you do this - try to call the jython script from the servlet 
itself and see if it works.

WILL


----- Original Message ----- 
From: "Travis Bear" <tb...@myrio.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Tuesday, February 01, 2005 12:48 PM
Subject: Re: Classpath/Classloader issue with java program invoked 
viaVelocitycontext int Tomcat


> Hmm.  I'm using an extension of the VelocityLayoutServlet, so what I was
> calling the context was actually a java class I configured in as a tool,
> via toolbox.xml.  This class does not actually implement the Context (or
> any other) interface, although I could certainly do that if needed.
>
> Here's the toolbox.xml:
>
> <?xml version="1.0"?>
> <toolbox>
>    <tool>
>        <key>setup</key>
>        <class>com.myrio.qa.qas.SetupUI</class>
>    </tool>
>
>    <tool>
>        <key>params</key>
>        <scope>request</scope>
>
> <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
>    </tool>
> </toolbox>
>
>
>
> Here's the Velocity template code that invokes the runSetup() method:
>
> #if ($request.method=="POST")
>    $setup.runSetup()
> #end
>
>
> Thanks,
>
>
>
> -Travis
>
>
>
> On Tue, 2005-02-01 at 11:37 -0800, Will Glass-Husain wrote:
>> Travis,
>>
>> When you say "context" do you mean an object that defines the Context
>> interface, or do you mean a web application?
>>
>> WILL
>>
>> ----- Original Message ----- 
>> From: "Travis Bear" <tb...@myrio.com>
>> To: "Velocity Users List" <ve...@jakarta.apache.org>
>> Sent: Tuesday, February 01, 2005 11:23 AM
>> Subject: Classpath/Classloader issue with java program invoked via
>> Velocitycontext int Tomcat
>>
>>
>> > I'm really puzzled by an issue I'm seeing when I invoke an external
>> > object's main method from inside the Velocity context.  All my code is
>> > running as a webapp inside Tomcat.  The external object is a Jython
>> > script compiled into a java class via jythonc.  This class is put into 
>> > a
>> > jar file and placed in $CATALINA_HOME/webapps/$WEBAPP_NAME/WEB-INF/lib.
>> >
>> > When I invoke the Jython script from inside Tomcat, it breaks because 
>> > it
>> > can't locate any of the packages I try to import from.  The puzzling
>> > part is that the context itself is able to import from the same package
>> > that breaks the jython script, and it's running in the same VM.
>> >
>> > Here is the Jython script Setup.py:
>> >
>> >
>> >
>> > print "Entering Setup"
>> > from org.jtmb.sql import DataEngine
>> > from java.io import File
>> > propsFile = File("conf/DataEngine.properties")
>> > de = DataEngine.getStaticInstance(propsFile)
>> > print de.getString("select version as name from branch")
>> >
>> >
>> >
>> > Here is the code from the Velocity context where I statically invoke 
>> > the
>> > jython script:
>> >
>> > com.myrio.setup.Setup.main(new String[]{});
>> >
>> > Going on the theory that the context must have had a good classloader
>> > because it was able to instantiate the DataEngine, I attempted to load
>> > my class by explicitly using the same classloader:
>> >
>> >    ClassLoader loader = this.getClass().getClassLoader();
>> >    Object setup = Class.forName("com.myrio.setup.Setup", true,
>> > loader).newInstance();
>> >    logger.fatal ("Attempting to load setup with custom classloader");
>> >    ((com.myrio.setup.Setup) setup).main(new String[]{});
>> >
>> >
>> > In both cases, I get the same result.  Here's the output in
>> > catalina.out.
>> >
>> > Entering Setup
>> > Traceback (innermost last):
>> >  File "/home/travis/p4/3.5/qa/qas/code/jython/Setup.py", line 0, in
>> > main
>> > ImportError: No module named jtmb
>> >
>> >
>> >
>> > Thanks in advance -- this is my first effort at using classloaders, so 
>> > I
>> > could easily be missing something.  I'm pretty sure I could get it to
>> > work by manually setting the system classpath via system properties.
>> > But that seems like a pretty broken approach.  Any help is appreciated!
>> >
>> >
>> > -Travis
>> >
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>
>>
>> _________________________________________________
>> Scanned on 01 Feb 2005 20:24:56
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org