<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ivan's agile thinkings &#187; AuxiliaryEnhancer</title>
	<atom:link href="http://ivansthunks.com/blog/tag/auxiliaryenhancer/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivansthunks.com/blog</link>
	<description>My thoughts and opinions on all things agile</description>
	<lastBuildDate>Sat, 02 Jul 2011 20:21:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Adding an auxiliary enchancer to OpenJPA</title>
		<link>http://ivansthunks.com/blog/2008/12/02/adding-an-auxiliary-enchancer-to-openjpa/</link>
		<comments>http://ivansthunks.com/blog/2008/12/02/adding-an-auxiliary-enchancer-to-openjpa/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 06:28:38 +0000</pubDate>
		<dc:creator>ivan</dc:creator>
				<category><![CDATA[Constant Learning]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[]]></category>
		<category><![CDATA[AuxiliaryEnhancer]]></category>
		<category><![CDATA[Enhancer]]></category>
		<category><![CDATA[OpenJpa]]></category>

		<guid isPermaLink="false">http://ivansthunks.com/blog/2008/12/02/adding-an-auxiliary-enchancer-to-openjpa/</guid>
		<description><![CDATA[A while back I attempted to create a visualization of the relations that OpenJPA creates between the Java classes that it enhances &#8211; the intent was to help better understand a particularly intertwined persistent object graph and see if I couldn&#8217;t figure out why we were getting a few strange problems. The initial approach I [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I attempted to create a visualization of the relations that OpenJPA creates between the Java classes that it enhances &#8211; the intent was to help better understand a particularly intertwined persistent object graph and see if I couldn&#8217;t figure out why we were getting a few strange problems.</p>
<p>The initial approach I took was a hilarious trip into the land of sed, perl and graphviz.  It kinda looked like it worked, but didn&#8217;t actually <img src='http://ivansthunks.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Time passed&#8230;&#8230;more time passed&#8230;&#8230;I learned more&#8230;&#8230;more time passed&#8230;&#8230;.and then I had the wacky notion of tying directly into the Enhancer that OpenJPA uses to create the persistence enabled classes.  I haven&#8217;t done the visualization yet, but this is what I learned about actually tying in an AuxiliaryEnhancer.</p>
<h2>1. Create the AuxiliaryEnhancer</h2>
<p>The following code is the minimal you will need.  Enter this and then compile &#8211; you will need OpenJPA and serp on your classpath.</p>
<pre>
<pre class="brush: java; title: ; notranslate">package com.ij;

import org.apache.openjpa.enhance.PCEnhancer.AuxiliaryEnhancer;
import serp.bytecode.BCClass;
import serp.bytecode.BCMethod;
import org.apache.openjpa.meta.ClassMetaData;

public class Grapher implements AuxiliaryEnhancer {

    public void run(BCClass bc, ClassMetaData meta) {
        System.out.println(&quot;Running Enhancement&quot;);
    }

    public boolean skipEnhance(BCMethod m) {
         System.out.println(&quot;skipEnhance?&quot;);
         return false;
    }
}
</pre>
</pre>
<h2>2. Register the enhancer using jar based services</h2>
<p>This is something I was only barely aware off &#8211; registering concrete implementations of services through jar file meta-data.</p>
<ul>
<li>Create a file in a <strong>META-INF/services</strong>/ directory called <strong>org.apache.openjpa.enhance.PCEnhancer$AuxiliaryEnhancer</strong>
<ul>
<li>Note the dollar sign! That one caught me out for a while: I couldn&#8217;t decide whether the dot notation or the dollar notation should be used to reference the nested class.</li>
</ul>
</li>
<li>This file should simply contain the fully qualified name of the Enhancer you wrote: com.ij.Grapher</li>
<li>Jar this directory and the class file up and you should end up with a jar listing like this</li>
</ul>
<pre>
<pre class="brush: java; title: ; notranslate">
ivan-jensens-computer:jar ivanjensen$ jar tvf ../grapher.jar
     0 Tue Dec 02 21:56:42 PST 2008 META-INF/
    60 Tue Dec 02 21:56:42 PST 2008 META-INF/MANIFEST.MF
     0 Tue Dec 02 21:54:28 PST 2008 META-INF/services/
    16 Tue Dec 02 21:30:44 PST 2008 META-INF/services/org.apache.renamed.openjpa.enhance.PCEnhancer$AuxiliaryEnhancer
     0 Tue Dec 02 21:55:16 PST 2008 com/
     0 Tue Dec 02 21:55:38 PST 2008 com/ij/
   771 Tue Dec 02 21:55:38 PST 2008 com/ij/Grapher.class
   465 Tue Dec 02 21:54:46 PST 2008 com/ij/Grapher.java
</pre>
</pre>
<h2>3. Add the new jar file to the enhancer classpath</h2>
<p>I did this by adding my jar file to the classpath element of my ant script &#8211; pretty simple.</p>
<h2>4. Re-enhance your classes</h2>
<p>Re-enhance your classes and you will see the System.out.println statements all over your console.</p>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] Running Enhancement</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<pre> [openjpac] skipEnhance?</pre>
<p>One thing I noticed about this: I had to delete my persistent classes and re-enhance, only then would the &#8216;run&#8217; method be called.  The skipMethod is called whether I delete the classes or not.  I don&#8217;t know what that means yet, but I&#8217;m sure I am about to find out.</p>
<h2>Notes:</h2>
<p>I was actually using a patched version of OpenJpa 1.0.1 (for various, non-amusing, reasons), but the patches shouldn&#8217;t affect this tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivansthunks.com/blog/2008/12/02/adding-an-auxiliary-enchancer-to-openjpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

