You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Josh Canfield <jo...@gmail.com> on 2011/07/29 19:19:27 UTC

Tapestry Plastic

I'd like to integrate some advice used in a tapestry web project into
a non-tapestry web project. I can add a dependency on plastic, but not
a tapestry-ioc.

I'm at the experimental stage at this point and I've hit a snag. I
figure I can wade around for a couple hours, or Howard can whip up in
example off the top of his head.

Here's some pieces of code.

        plasticManager = PlasticManager
                .withContextClassLoader()
                .packages(Collections.singleton(basePackage))
                .create();


public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {

        return plasticManager.createClass(monitoredClass, new
PlasticClassTransformer() {

            public void transform(PlasticClass plasticClass) {

// At this point plasticclass has methodNames, but no methods so the
following fails to find my annotation.
// Do I need to manually override the methods?

                List<PlasticMethod> methods =
plasticClass.getMethodsWithAnnotation(Monitor.class);
                for (PlasticMethod method : methods) {
                    final Stopwatch stopwatch =
createStopwatch(method.getAnnotation(Monitor.class), method);
                    logger.debug("Adding advice for {}", stopwatch.getName());
                    method.addAdvice(new MonitorAdvice(stopwatch));
                }
            }

        }).newInstance();
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 29 Jul 2011 22:19:03 -0300, Josh Canfield <jo...@gmail.com>  
wrote:

> Sorry, I wasn't very clear. I want to "advise" some services in an
> application that isn't being built using tapestry. First usage will be
> for adding performance monitoring, but other usages are envisioned.
> The server is using ATG's Nucleus for service bean creation and
> dependency injection.

Couldn't you use Tapestry-IoC? This would avoid the need to use Plastic  
directly.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by Howard Lewis Ship <hl...@gmail.com>.
What you need is a hook that says:

   instantiate an instance of class <class name>

and you replace

   Class.forName("xxx").newInstance()

with

  mgr.getClassInstantiator("xxx").newInstance()

Or, you can define a proxy that takes an original object in its
InstanceContext. It can delegate each method to the instance context
value and advise each method with the advice. See IoC code for
examples.

Again, part of Plastic is that only Plastic can instantiate the
transformed class, because the transform class picks up dependencies
from outside the class itself that have to come in through the
constructor.

I have been thinking of making the StaticContext a static field of the
class rather than the constructor, and providing an option to
instantiate the class via a default constructor instead of via
Plastic.  I'm not sure that would solve everyone's issues.

On Fri, Jul 29, 2011 at 6:19 PM, Josh Canfield <jo...@gmail.com> wrote:
>> I'm not sure what you are trying to do.
>
> Sorry, I wasn't very clear. I want to "advise" some services in an
> application that isn't being built using tapestry. First usage will be
> for adding performance monitoring, but other usages are envisioned.
> The server is using ATG's Nucleus for service bean creation and
> dependency injection.
>
> I'm trying to work out a minimally intrusive way to build in some AOP
> without resorting to a build time rewriting. I have a trivial case
> working now, but it required moving my service interface out of the
> plastic managed package.
>
> Since I don't have _much_ control over packaging in the project that
> I'm doing this for so my next step is to investigate non-package based
> class filters.
>
> Josh
>
> On Fri, Jul 29, 2011 at 5:14 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
>> I'm not sure what you are trying to do.
>>
>> You can use Plastic to create proxies around ordinary classes, as long
>> as the classes conform to an interface (this is what Tapestry IoC does
>> extensively).
>>
>> Plastic rewrites your classes and, among other things, adds a new
>> constructor, and invalidates the other constructors (they throw an
>> exception).  The new constructor gets a internal StaticContext object
>> that contains various injected things, and an InstanceContext object
>> that contains per-instance values ... in Tapestry terms, the
>> InstanceContext holds the ComponentResources object.
>>
>> BTW, I have a plan for extending Plastic (and therefore Tapestry) to
>> allow non-private fields ... public fields will still be verboten, but
>> other fields types will be allowed. It's just a matter of doing minor
>> rewrites on any incidental inner classes loaded with the top-level
>> (i.e., component) classes.
>>
>> On Fri, Jul 29, 2011 at 3:25 PM, Josh Canfield <jo...@gmail.com> wrote:
>>> Interesting. I imagine that your tests work because you're using
>>> groovy and not trying to access the class that you've transformed as
>>> it's actual type.
>>>
>>>  @Test
>>>    public void monitor() {
>>>        String rootPackage = "org.apache.tapestry5.monitor.test";
>>>        DefaultMonitorNameGenerator nameGenerator = new
>>> DefaultMonitorNameGenerator(rootPackage);
>>>        MonitoredProxyFactoryImpl monitoredProxyFactory = new
>>> MonitoredProxyFactoryImpl(nameGenerator, rootPackage);
>>>
>>> // The following results in
>>> // java.lang.ClassCastException:
>>> org.apache.tapestry5.monitor.test.FakeService cannot be cast to
>>> org.apache.tapestry5.monitor.test.FakeService
>>>        FakeService monitor = monitoredProxyFactory.monitor(FakeService.class);
>>>
>>>        monitor.publicMethod();
>>>    }
>>>
>>> Is what I'm trying to do feasible without replacing the context ClassLoader?
>>>
>>> On Fri, Jul 29, 2011 at 3:08 PM, Taha Hafeez <ta...@gmail.com> wrote:
>>>> Hi Josh
>>>>
>>>> This might help
>>>>
>>>> http://tawus.wordpress.com/category/plastic/
>>>>
>>>> regards
>>>> Taha
>>>>
>>>> On Fri, Jul 29, 2011 at 11:25 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
>>>>> On Fri, Jul 29, 2011 at 10:19 AM, Josh Canfield <jo...@gmail.com> wrote:
>>>>>> I'd like to integrate some advice used in a tapestry web project into
>>>>>> a non-tapestry web project. I can add a dependency on plastic, but not
>>>>>> a tapestry-ioc.
>>>>>>
>>>>>> I'm at the experimental stage at this point and I've hit a snag. I
>>>>>> figure I can wade around for a couple hours, or Howard can whip up in
>>>>>> example off the top of his head.
>>>>>>
>>>>>> Here's some pieces of code.
>>>>>>
>>>>>>        plasticManager = PlasticManager
>>>>>>                .withContextClassLoader()
>>>>>>                .packages(Collections.singleton(basePackage))
>>>>>>                .create();
>>>>>>
>>>>>>
>>>>>> public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {
>>>>>>
>>>>>>        return plasticManager.createClass(monitoredClass, new
>>>>>> PlasticClassTransformer() {
>>>>>>
>>>>>>            public void transform(PlasticClass plasticClass) {
>>>>>>
>>>>>> // At this point plasticclass has methodNames, but no methods so the
>>>>>> following fails to find my annotation.
>>>>>> // Do I need to manually override the methods?
>>>>>>
>>>>>>                List<PlasticMethod> methods =
>>>>>> plasticClass.getMethodsWithAnnotation(Monitor.class);
>>>>>>                for (PlasticMethod method : methods) {
>>>>>>                    final Stopwatch stopwatch =
>>>>>> createStopwatch(method.getAnnotation(Monitor.class), method);
>>>>>>                    logger.debug("Adding advice for {}", stopwatch.getName());
>>>>>>                    method.addAdvice(new MonitorAdvice(stopwatch));
>>>>>>                }
>>>>>>            }
>>>>>>
>>>>>>        }).newInstance();
>>>>>>    }
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>>>
>>>>>>
>>>>>
>>>>> I think what you are missing is that you don't create a class (that's
>>>>> for creating proxies and such, the monitoredClass you pass in becomes
>>>>> the base class).
>>>>>
>>>>> What you want to do is wrap your logic up your PlasticClassTransformer
>>>>> inside a StandardDelegate (a trivial internal class).
>>>>>
>>>>> You can then ask the manager to getClassInstantiator() and
>>>>> newInstance() that to get your advised and instantiated instance.
>>>>>
>>>>>
>>>>> --
>>>>> Howard M. Lewis Ship
>>>>>
>>>>> Creator of Apache Tapestry
>>>>>
>>>>> The source for Tapestry training, mentoring and support. Contact me to
>>>>> learn how I can get you up and productive in Tapestry fast!
>>>>>
>>>>> (971) 678-5210
>>>>> http://howardlewisship.com
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>
>>
>>
>> --
>> Howard M. Lewis Ship
>>
>> Creator of Apache Tapestry
>>
>> The source for Tapestry training, mentoring and support. Contact me to
>> learn how I can get you up and productive in Tapestry fast!
>>
>> (971) 678-5210
>> http://howardlewisship.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by Josh Canfield <jo...@gmail.com>.
> I'm not sure what you are trying to do.

Sorry, I wasn't very clear. I want to "advise" some services in an
application that isn't being built using tapestry. First usage will be
for adding performance monitoring, but other usages are envisioned.
The server is using ATG's Nucleus for service bean creation and
dependency injection.

I'm trying to work out a minimally intrusive way to build in some AOP
without resorting to a build time rewriting. I have a trivial case
working now, but it required moving my service interface out of the
plastic managed package.

Since I don't have _much_ control over packaging in the project that
I'm doing this for so my next step is to investigate non-package based
class filters.

Josh

On Fri, Jul 29, 2011 at 5:14 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
> I'm not sure what you are trying to do.
>
> You can use Plastic to create proxies around ordinary classes, as long
> as the classes conform to an interface (this is what Tapestry IoC does
> extensively).
>
> Plastic rewrites your classes and, among other things, adds a new
> constructor, and invalidates the other constructors (they throw an
> exception).  The new constructor gets a internal StaticContext object
> that contains various injected things, and an InstanceContext object
> that contains per-instance values ... in Tapestry terms, the
> InstanceContext holds the ComponentResources object.
>
> BTW, I have a plan for extending Plastic (and therefore Tapestry) to
> allow non-private fields ... public fields will still be verboten, but
> other fields types will be allowed. It's just a matter of doing minor
> rewrites on any incidental inner classes loaded with the top-level
> (i.e., component) classes.
>
> On Fri, Jul 29, 2011 at 3:25 PM, Josh Canfield <jo...@gmail.com> wrote:
>> Interesting. I imagine that your tests work because you're using
>> groovy and not trying to access the class that you've transformed as
>> it's actual type.
>>
>>  @Test
>>    public void monitor() {
>>        String rootPackage = "org.apache.tapestry5.monitor.test";
>>        DefaultMonitorNameGenerator nameGenerator = new
>> DefaultMonitorNameGenerator(rootPackage);
>>        MonitoredProxyFactoryImpl monitoredProxyFactory = new
>> MonitoredProxyFactoryImpl(nameGenerator, rootPackage);
>>
>> // The following results in
>> // java.lang.ClassCastException:
>> org.apache.tapestry5.monitor.test.FakeService cannot be cast to
>> org.apache.tapestry5.monitor.test.FakeService
>>        FakeService monitor = monitoredProxyFactory.monitor(FakeService.class);
>>
>>        monitor.publicMethod();
>>    }
>>
>> Is what I'm trying to do feasible without replacing the context ClassLoader?
>>
>> On Fri, Jul 29, 2011 at 3:08 PM, Taha Hafeez <ta...@gmail.com> wrote:
>>> Hi Josh
>>>
>>> This might help
>>>
>>> http://tawus.wordpress.com/category/plastic/
>>>
>>> regards
>>> Taha
>>>
>>> On Fri, Jul 29, 2011 at 11:25 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
>>>> On Fri, Jul 29, 2011 at 10:19 AM, Josh Canfield <jo...@gmail.com> wrote:
>>>>> I'd like to integrate some advice used in a tapestry web project into
>>>>> a non-tapestry web project. I can add a dependency on plastic, but not
>>>>> a tapestry-ioc.
>>>>>
>>>>> I'm at the experimental stage at this point and I've hit a snag. I
>>>>> figure I can wade around for a couple hours, or Howard can whip up in
>>>>> example off the top of his head.
>>>>>
>>>>> Here's some pieces of code.
>>>>>
>>>>>        plasticManager = PlasticManager
>>>>>                .withContextClassLoader()
>>>>>                .packages(Collections.singleton(basePackage))
>>>>>                .create();
>>>>>
>>>>>
>>>>> public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {
>>>>>
>>>>>        return plasticManager.createClass(monitoredClass, new
>>>>> PlasticClassTransformer() {
>>>>>
>>>>>            public void transform(PlasticClass plasticClass) {
>>>>>
>>>>> // At this point plasticclass has methodNames, but no methods so the
>>>>> following fails to find my annotation.
>>>>> // Do I need to manually override the methods?
>>>>>
>>>>>                List<PlasticMethod> methods =
>>>>> plasticClass.getMethodsWithAnnotation(Monitor.class);
>>>>>                for (PlasticMethod method : methods) {
>>>>>                    final Stopwatch stopwatch =
>>>>> createStopwatch(method.getAnnotation(Monitor.class), method);
>>>>>                    logger.debug("Adding advice for {}", stopwatch.getName());
>>>>>                    method.addAdvice(new MonitorAdvice(stopwatch));
>>>>>                }
>>>>>            }
>>>>>
>>>>>        }).newInstance();
>>>>>    }
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>>
>>>>>
>>>>
>>>> I think what you are missing is that you don't create a class (that's
>>>> for creating proxies and such, the monitoredClass you pass in becomes
>>>> the base class).
>>>>
>>>> What you want to do is wrap your logic up your PlasticClassTransformer
>>>> inside a StandardDelegate (a trivial internal class).
>>>>
>>>> You can then ask the manager to getClassInstantiator() and
>>>> newInstance() that to get your advised and instantiated instance.
>>>>
>>>>
>>>> --
>>>> Howard M. Lewis Ship
>>>>
>>>> Creator of Apache Tapestry
>>>>
>>>> The source for Tapestry training, mentoring and support. Contact me to
>>>> learn how I can get you up and productive in Tapestry fast!
>>>>
>>>> (971) 678-5210
>>>> http://howardlewisship.com
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
>
>
> --
> Howard M. Lewis Ship
>
> Creator of Apache Tapestry
>
> The source for Tapestry training, mentoring and support. Contact me to
> learn how I can get you up and productive in Tapestry fast!
>
> (971) 678-5210
> http://howardlewisship.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by Howard Lewis Ship <hl...@gmail.com>.
I'm not sure what you are trying to do.

You can use Plastic to create proxies around ordinary classes, as long
as the classes conform to an interface (this is what Tapestry IoC does
extensively).

Plastic rewrites your classes and, among other things, adds a new
constructor, and invalidates the other constructors (they throw an
exception).  The new constructor gets a internal StaticContext object
that contains various injected things, and an InstanceContext object
that contains per-instance values ... in Tapestry terms, the
InstanceContext holds the ComponentResources object.

BTW, I have a plan for extending Plastic (and therefore Tapestry) to
allow non-private fields ... public fields will still be verboten, but
other fields types will be allowed. It's just a matter of doing minor
rewrites on any incidental inner classes loaded with the top-level
(i.e., component) classes.

On Fri, Jul 29, 2011 at 3:25 PM, Josh Canfield <jo...@gmail.com> wrote:
> Interesting. I imagine that your tests work because you're using
> groovy and not trying to access the class that you've transformed as
> it's actual type.
>
>  @Test
>    public void monitor() {
>        String rootPackage = "org.apache.tapestry5.monitor.test";
>        DefaultMonitorNameGenerator nameGenerator = new
> DefaultMonitorNameGenerator(rootPackage);
>        MonitoredProxyFactoryImpl monitoredProxyFactory = new
> MonitoredProxyFactoryImpl(nameGenerator, rootPackage);
>
> // The following results in
> // java.lang.ClassCastException:
> org.apache.tapestry5.monitor.test.FakeService cannot be cast to
> org.apache.tapestry5.monitor.test.FakeService
>        FakeService monitor = monitoredProxyFactory.monitor(FakeService.class);
>
>        monitor.publicMethod();
>    }
>
> Is what I'm trying to do feasible without replacing the context ClassLoader?
>
> On Fri, Jul 29, 2011 at 3:08 PM, Taha Hafeez <ta...@gmail.com> wrote:
>> Hi Josh
>>
>> This might help
>>
>> http://tawus.wordpress.com/category/plastic/
>>
>> regards
>> Taha
>>
>> On Fri, Jul 29, 2011 at 11:25 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
>>> On Fri, Jul 29, 2011 at 10:19 AM, Josh Canfield <jo...@gmail.com> wrote:
>>>> I'd like to integrate some advice used in a tapestry web project into
>>>> a non-tapestry web project. I can add a dependency on plastic, but not
>>>> a tapestry-ioc.
>>>>
>>>> I'm at the experimental stage at this point and I've hit a snag. I
>>>> figure I can wade around for a couple hours, or Howard can whip up in
>>>> example off the top of his head.
>>>>
>>>> Here's some pieces of code.
>>>>
>>>>        plasticManager = PlasticManager
>>>>                .withContextClassLoader()
>>>>                .packages(Collections.singleton(basePackage))
>>>>                .create();
>>>>
>>>>
>>>> public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {
>>>>
>>>>        return plasticManager.createClass(monitoredClass, new
>>>> PlasticClassTransformer() {
>>>>
>>>>            public void transform(PlasticClass plasticClass) {
>>>>
>>>> // At this point plasticclass has methodNames, but no methods so the
>>>> following fails to find my annotation.
>>>> // Do I need to manually override the methods?
>>>>
>>>>                List<PlasticMethod> methods =
>>>> plasticClass.getMethodsWithAnnotation(Monitor.class);
>>>>                for (PlasticMethod method : methods) {
>>>>                    final Stopwatch stopwatch =
>>>> createStopwatch(method.getAnnotation(Monitor.class), method);
>>>>                    logger.debug("Adding advice for {}", stopwatch.getName());
>>>>                    method.addAdvice(new MonitorAdvice(stopwatch));
>>>>                }
>>>>            }
>>>>
>>>>        }).newInstance();
>>>>    }
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>
>>>>
>>>
>>> I think what you are missing is that you don't create a class (that's
>>> for creating proxies and such, the monitoredClass you pass in becomes
>>> the base class).
>>>
>>> What you want to do is wrap your logic up your PlasticClassTransformer
>>> inside a StandardDelegate (a trivial internal class).
>>>
>>> You can then ask the manager to getClassInstantiator() and
>>> newInstance() that to get your advised and instantiated instance.
>>>
>>>
>>> --
>>> Howard M. Lewis Ship
>>>
>>> Creator of Apache Tapestry
>>>
>>> The source for Tapestry training, mentoring and support. Contact me to
>>> learn how I can get you up and productive in Tapestry fast!
>>>
>>> (971) 678-5210
>>> http://howardlewisship.com
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by Josh Canfield <jo...@gmail.com>.
Interesting. I imagine that your tests work because you're using
groovy and not trying to access the class that you've transformed as
it's actual type.

  @Test
    public void monitor() {
        String rootPackage = "org.apache.tapestry5.monitor.test";
        DefaultMonitorNameGenerator nameGenerator = new
DefaultMonitorNameGenerator(rootPackage);
        MonitoredProxyFactoryImpl monitoredProxyFactory = new
MonitoredProxyFactoryImpl(nameGenerator, rootPackage);

// The following results in
// java.lang.ClassCastException:
org.apache.tapestry5.monitor.test.FakeService cannot be cast to
org.apache.tapestry5.monitor.test.FakeService
        FakeService monitor = monitoredProxyFactory.monitor(FakeService.class);

        monitor.publicMethod();
    }

Is what I'm trying to do feasible without replacing the context ClassLoader?

On Fri, Jul 29, 2011 at 3:08 PM, Taha Hafeez <ta...@gmail.com> wrote:
> Hi Josh
>
> This might help
>
> http://tawus.wordpress.com/category/plastic/
>
> regards
> Taha
>
> On Fri, Jul 29, 2011 at 11:25 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
>> On Fri, Jul 29, 2011 at 10:19 AM, Josh Canfield <jo...@gmail.com> wrote:
>>> I'd like to integrate some advice used in a tapestry web project into
>>> a non-tapestry web project. I can add a dependency on plastic, but not
>>> a tapestry-ioc.
>>>
>>> I'm at the experimental stage at this point and I've hit a snag. I
>>> figure I can wade around for a couple hours, or Howard can whip up in
>>> example off the top of his head.
>>>
>>> Here's some pieces of code.
>>>
>>>        plasticManager = PlasticManager
>>>                .withContextClassLoader()
>>>                .packages(Collections.singleton(basePackage))
>>>                .create();
>>>
>>>
>>> public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {
>>>
>>>        return plasticManager.createClass(monitoredClass, new
>>> PlasticClassTransformer() {
>>>
>>>            public void transform(PlasticClass plasticClass) {
>>>
>>> // At this point plasticclass has methodNames, but no methods so the
>>> following fails to find my annotation.
>>> // Do I need to manually override the methods?
>>>
>>>                List<PlasticMethod> methods =
>>> plasticClass.getMethodsWithAnnotation(Monitor.class);
>>>                for (PlasticMethod method : methods) {
>>>                    final Stopwatch stopwatch =
>>> createStopwatch(method.getAnnotation(Monitor.class), method);
>>>                    logger.debug("Adding advice for {}", stopwatch.getName());
>>>                    method.addAdvice(new MonitorAdvice(stopwatch));
>>>                }
>>>            }
>>>
>>>        }).newInstance();
>>>    }
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>
>> I think what you are missing is that you don't create a class (that's
>> for creating proxies and such, the monitoredClass you pass in becomes
>> the base class).
>>
>> What you want to do is wrap your logic up your PlasticClassTransformer
>> inside a StandardDelegate (a trivial internal class).
>>
>> You can then ask the manager to getClassInstantiator() and
>> newInstance() that to get your advised and instantiated instance.
>>
>>
>> --
>> Howard M. Lewis Ship
>>
>> Creator of Apache Tapestry
>>
>> The source for Tapestry training, mentoring and support. Contact me to
>> learn how I can get you up and productive in Tapestry fast!
>>
>> (971) 678-5210
>> http://howardlewisship.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by Taha Hafeez <ta...@gmail.com>.
Hi Josh

This might help

http://tawus.wordpress.com/category/plastic/

regards
Taha

On Fri, Jul 29, 2011 at 11:25 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
> On Fri, Jul 29, 2011 at 10:19 AM, Josh Canfield <jo...@gmail.com> wrote:
>> I'd like to integrate some advice used in a tapestry web project into
>> a non-tapestry web project. I can add a dependency on plastic, but not
>> a tapestry-ioc.
>>
>> I'm at the experimental stage at this point and I've hit a snag. I
>> figure I can wade around for a couple hours, or Howard can whip up in
>> example off the top of his head.
>>
>> Here's some pieces of code.
>>
>>        plasticManager = PlasticManager
>>                .withContextClassLoader()
>>                .packages(Collections.singleton(basePackage))
>>                .create();
>>
>>
>> public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {
>>
>>        return plasticManager.createClass(monitoredClass, new
>> PlasticClassTransformer() {
>>
>>            public void transform(PlasticClass plasticClass) {
>>
>> // At this point plasticclass has methodNames, but no methods so the
>> following fails to find my annotation.
>> // Do I need to manually override the methods?
>>
>>                List<PlasticMethod> methods =
>> plasticClass.getMethodsWithAnnotation(Monitor.class);
>>                for (PlasticMethod method : methods) {
>>                    final Stopwatch stopwatch =
>> createStopwatch(method.getAnnotation(Monitor.class), method);
>>                    logger.debug("Adding advice for {}", stopwatch.getName());
>>                    method.addAdvice(new MonitorAdvice(stopwatch));
>>                }
>>            }
>>
>>        }).newInstance();
>>    }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
> I think what you are missing is that you don't create a class (that's
> for creating proxies and such, the monitoredClass you pass in becomes
> the base class).
>
> What you want to do is wrap your logic up your PlasticClassTransformer
> inside a StandardDelegate (a trivial internal class).
>
> You can then ask the manager to getClassInstantiator() and
> newInstance() that to get your advised and instantiated instance.
>
>
> --
> Howard M. Lewis Ship
>
> Creator of Apache Tapestry
>
> The source for Tapestry training, mentoring and support. Contact me to
> learn how I can get you up and productive in Tapestry fast!
>
> (971) 678-5210
> http://howardlewisship.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Plastic

Posted by Howard Lewis Ship <hl...@gmail.com>.
On Fri, Jul 29, 2011 at 10:19 AM, Josh Canfield <jo...@gmail.com> wrote:
> I'd like to integrate some advice used in a tapestry web project into
> a non-tapestry web project. I can add a dependency on plastic, but not
> a tapestry-ioc.
>
> I'm at the experimental stage at this point and I've hit a snag. I
> figure I can wade around for a couple hours, or Howard can whip up in
> example off the top of his head.
>
> Here's some pieces of code.
>
>        plasticManager = PlasticManager
>                .withContextClassLoader()
>                .packages(Collections.singleton(basePackage))
>                .create();
>
>
> public <T> T monitor(Class<T> monitoredClass, Object... constructorParams) {
>
>        return plasticManager.createClass(monitoredClass, new
> PlasticClassTransformer() {
>
>            public void transform(PlasticClass plasticClass) {
>
> // At this point plasticclass has methodNames, but no methods so the
> following fails to find my annotation.
> // Do I need to manually override the methods?
>
>                List<PlasticMethod> methods =
> plasticClass.getMethodsWithAnnotation(Monitor.class);
>                for (PlasticMethod method : methods) {
>                    final Stopwatch stopwatch =
> createStopwatch(method.getAnnotation(Monitor.class), method);
>                    logger.debug("Adding advice for {}", stopwatch.getName());
>                    method.addAdvice(new MonitorAdvice(stopwatch));
>                }
>            }
>
>        }).newInstance();
>    }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

I think what you are missing is that you don't create a class (that's
for creating proxies and such, the monitoredClass you pass in becomes
the base class).

What you want to do is wrap your logic up your PlasticClassTransformer
inside a StandardDelegate (a trivial internal class).

You can then ask the manager to getClassInstantiator() and
newInstance() that to get your advised and instantiated instance.


-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org