You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by SunSeaAndPalms <Su...@gmail.com> on 2010/05/29 00:22:06 UTC

How to specify classloader for web application deployed on Geronimo.

Technologies that are used in my application requires classloader with
'addTransformer(ClassFileTransformer)' method and claims that Geronimo's 
default org.apache.geronimo.kernel.config.MultiParentClassLoader , doesn't
have it. So I need to setup my special class loader for my application
(war). I use Geronimo with Tomcat. I was told that specifying classloader is
a very simple process on Tomcat. Following example shows how to do it (The
file is named "context.xml"):
 
<Context path="/myWebApp" docBase="/my/webApp/location">
    <Loader
       
loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>

The alternative for Geronimo is using "geronimo-tomcat.xml" in deployment
plan (I read about it from 
http://www.ibm.com/developerworks/java/library/os-ag-tomcat/ this doc ). The
problem is that there isn't a classloader property specified.

So my question is: How can I change default classloader with a special one
for my web application deployed on Geronimo?


-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/How-to-specify-classloader-for-web-application-deployed-on-Geronimo-tp853251p853251.html
Sent from the Users mailing list archive at Nabble.com.

Re: How to specify classloader for web application deployed on Geronimo.

Posted by SunSeaAndPalms <Su...@gmail.com>.
I am new to Geronimo and JavaEE in common. I need to add <gbean>
declaration for PersistenceUnitGBean in my deployment plan, am I right?
How do I set PRIORITY_CLASSLOADER? Is it an attribute of the mentioned
above <gbean> declaration? But there isn't such field in
PersistenceUnitGBean class.
 
David, thanks you for help!
 
On Птн, 2010-05-28 at 16:03 -0700, David Jencks [via Apache Geronimo]
wrote:
> 
> On May 28, 2010, at 3:22 PM, SunSeaAndPalms wrote: 
> 
> > 
> > Technologies that are used in my application requires classloader
> with 
> > 'addTransformer(ClassFileTransformer)' method and claims that
> Geronimo's 
> > default org.apache.geronimo.kernel.config.MultiParentClassLoader ,
> doesn't 
> > have it. So I need to setup my special class loader for my
> application 
> > (war). I use Geronimo with Tomcat. I was told that specifying
> classloader is 
> > a very simple process on Tomcat. Following example shows how to do
> it (The 
> > file is named "context.xml"): 
> > 
> > <Context path="/myWebApp" docBase="/my/webApp/location"> 
> >    <Loader 
> > 
> >
> loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> 
> > </Context> 
> > 
> > The alternative for Geronimo is using "geronimo-tomcat.xml" in
> deployment 
> > plan (I read about it from 
> > http://www.ibm.com/developerworks/java/library/os-ag-tomcat/ this
> doc ). The 
> > problem is that there isn't a classloader property specified. 
> > 
> > So my question is: How can I change default classloader with a
> special one 
> > for my web application deployed on Geronimo?
> 
> You can't change what classloader geronimo uses for your web app.
>  However, you can register a ClassTransformer quite easily.  We do
> this for jpa.  Here's the code we use, from 2.2
>  plugins/openjpa/geronimo-persistence-jpa10/src/main/java/org/apache/geronimo/persistence/PersistenceUnitGBean.java: 
> 
>         public void addTransformer(ClassTransformer classTransformer)
> { 
>             TransformerWrapper transformer = new
> TransformerWrapper(classTransformer, classLoader); 
>             transformers.add(transformer); 
>             TransformerAgent.addTransformer(transformer); 
>         } 
> 
> 
> public class TransformerWrapper implements ClassFileTransformer { 
> 
>     private final ClassTransformer classTransformer; 
>     private final ClassLoader classLoader; 
> 
>     public TransformerWrapper(ClassTransformer classTransformer,
> ClassLoader classLoader) { 
>         this.classTransformer = classTransformer; 
>         this.classLoader = classLoader; 
>     } 
> 
>     public byte[] transform(ClassLoader loader, String className,
> Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
> byte[] classfileBuffer) throws IllegalClassFormatException { 
>         if (loader != classLoader) { 
>             return null; 
>         } 
>         try { 
>             return classTransformer.transform(loader, className,
> classBeingRedefined,  protectionDomain, classfileBuffer); 
>         } catch (IllegalClassFormatException e) { 
>             throw e; 
>         } catch (RuntimeException e) { 
>             return null; 
>         } 
>     } 
> } 
> 
> 
> If you don't care whether all classes, not just the ones in your app,
> are fed to your ClassTransformer, you can leave out the
> TransformerWrapper. 
> 
> This code should go in a gbean with priority PRIORITY_CLASSLOADER to
> make sure it is installed before other components are started and
> perhaps load your classes untransformed.  See the PersistenceUnitGBean
> for more details. 
> 
> Hope this helps 
> david jencks 
> 
> 
> > 
> > 
> > -- 
> > View this message in context:
> http://apache-geronimo.328035.n3.nabble.com/How-to-specify-classloader-for-web-application-deployed-on-Geronimo-tp853251p853251.html
> > Sent from the Users mailing list archive at Nabble.com. 
> 
> 
> 
> 
> ______________________________________________________________________
> View message @
> http://apache-geronimo.328035.n3.nabble.com/How-to-specify-classloader-for-web-application-deployed-on-Geronimo-tp853251p853323.html 
> To unsubscribe from How to specify classloader for web application
> deployed on Geronimo., click here.
> 



-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/How-to-specify-classloader-for-web-application-deployed-on-Geronimo-tp853251p854098.html
Sent from the Users mailing list archive at Nabble.com.

Re: How to specify classloader for web application deployed on Geronimo.

Posted by David Jencks <da...@yahoo.com>.
On May 28, 2010, at 3:22 PM, SunSeaAndPalms wrote:

> 
> Technologies that are used in my application requires classloader with
> 'addTransformer(ClassFileTransformer)' method and claims that Geronimo's 
> default org.apache.geronimo.kernel.config.MultiParentClassLoader , doesn't
> have it. So I need to setup my special class loader for my application
> (war). I use Geronimo with Tomcat. I was told that specifying classloader is
> a very simple process on Tomcat. Following example shows how to do it (The
> file is named "context.xml"):
> 
> <Context path="/myWebApp" docBase="/my/webApp/location">
>    <Loader
> 
> loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
> </Context>
> 
> The alternative for Geronimo is using "geronimo-tomcat.xml" in deployment
> plan (I read about it from 
> http://www.ibm.com/developerworks/java/library/os-ag-tomcat/ this doc ). The
> problem is that there isn't a classloader property specified.
> 
> So my question is: How can I change default classloader with a special one
> for my web application deployed on Geronimo?

You can't change what classloader geronimo uses for your web app.  However, you can register a ClassTransformer quite easily.  We do this for jpa.  Here's the code we use, from 2.2  plugins/openjpa/geronimo-persistence-jpa10/src/main/java/org/apache/geronimo/persistence/PersistenceUnitGBean.java:

        public void addTransformer(ClassTransformer classTransformer) {
            TransformerWrapper transformer = new TransformerWrapper(classTransformer, classLoader);
            transformers.add(transformer);
            TransformerAgent.addTransformer(transformer);
        }


public class TransformerWrapper implements ClassFileTransformer {

    private final ClassTransformer classTransformer;
    private final ClassLoader classLoader;

    public TransformerWrapper(ClassTransformer classTransformer, ClassLoader classLoader) {
        this.classTransformer = classTransformer;
        this.classLoader = classLoader;
    }

    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        if (loader != classLoader) {
            return null;
        }
        try {
            return classTransformer.transform(loader, className, classBeingRedefined,  protectionDomain, classfileBuffer);
        } catch (IllegalClassFormatException e) {
            throw e;
        } catch (RuntimeException e) {
            return null;
        }
    }
}


If you don't care whether all classes, not just the ones in your app, are fed to your ClassTransformer, you can leave out the TransformerWrapper.

This code should go in a gbean with priority PRIORITY_CLASSLOADER to make sure it is installed before other components are started and perhaps load your classes untransformed.  See the PersistenceUnitGBean for more details.

Hope this helps
david jencks


> 
> 
> -- 
> View this message in context: http://apache-geronimo.328035.n3.nabble.com/How-to-specify-classloader-for-web-application-deployed-on-Geronimo-tp853251p853251.html
> Sent from the Users mailing list archive at Nabble.com.