<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="/templates/default/atom.css" type="text/css" ?>

<feed 
   xmlns="http://www.w3.org/2005/Atom"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/">
    <link href="http://javaporn.cestdelamerde.com/feeds/atom10.xml" rel="self" title="JavaPorn - Pornographic Java" type="application/atom+xml" />
    <link href="http://javaporn.cestdelamerde.com/"                        rel="alternate"    title="JavaPorn - Pornographic Java" type="text/html" />
    <link href="http://javaporn.cestdelamerde.com/rss.php?version=2.0"     rel="alternate"    title="JavaPorn - Pornographic Java" type="application/rss+xml" />
    <title type="html">JavaPorn - Pornographic Java</title>
    <subtitle type="html"></subtitle>
    <icon>http://javaporn.cestdelamerde.com/templates/default/img/s9y_banner_small.png</icon>
    <id>http://javaporn.cestdelamerde.com/</id>
    <updated>2008-06-12T20:52:29Z</updated>
    <generator uri="http://www.s9y.org/" version="1.3.1">Serendipity 1.3.1 - http://www.s9y.org/</generator>
    <dc:language>en</dc:language>

    <entry>
        <link href="http://javaporn.cestdelamerde.com/archives/11-Cute-format-on-javaporn.html" rel="alternate" title="Cute format on javaporn ?" />
        <author>
            <name>François Guerry</name>
                    </author>
    
        <published>2008-06-12T12:44:27Z</published>
        <updated>2008-06-12T20:52:29Z</updated>
        <wfw:comment>http://javaporn.cestdelamerde.com/wfwcomment.php?cid=11</wfw:comment>
    
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://javaporn.cestdelamerde.com/rss.php?version=atom1.0&amp;type=comments&amp;cid=11</wfw:commentRss>
    
    
        <id>http://javaporn.cestdelamerde.com/archives/11-guid.html</id>
        <title type="html">Cute format on javaporn ?</title>
        <content type="xhtml" xml:base="http://javaporn.cestdelamerde.com/">
            <div xmlns="http://www.w3.org/1999/xhtml">
                I wrote only 2 posts on this wonderful blog, and I'm already upset by the dumb formating options.<br />
I thought : and if I was able to copy/paste the nice formatted code of an eclipse editor directly in the blog post, without having any edition to do about format itself... ?<br />
<br />
Yeah...<br />
<br />
Just made it <img src="http://javaporn.cestdelamerde.com/templates/default/img/emoticons/wink.png" alt=";-)" style="display: inline; vertical-align: bottom;" class="emoticon" /><br />
<br />
Look at my beautiful previous posts with gorgeous syntax highlight ! What a dream !! Rendered with my own tool !<br />
You want to do it all the same ?<br />
Come on, just check this code in, and with slight adaptations to match your blog engine it could work nice...<br />
<br />
<a href="http://javaporn.cestdelamerde.com/uploads/Analysis.java" title="Analysis.java" target="_blank">Analysis.java</a> (ahh, of course the code itself is a perfect piece of crap)<br />
<br />
Small howto :<br />
<br />
* be sure to use Java 6 !!!<br />
* download &amp; compile the code<br />
* run it as a main class <br />
* copy some code from an arbitrary eclipse editor (java, xml, whatever...)<br />
* paste it in the big window<br />
* click the export button<br />
* copy all the ugly code produced<br />
* paste it to your naughty blog<br />
<br />
It takes in account bold, italic, and colors. So feel free to configure eclipse with funny styles to use it as well ! (I'm pretty proud about my pink strings, in fact...)<br />
<br />
Enjoy <img src="http://javaporn.cestdelamerde.com/templates/default/img/emoticons/wink.png" alt=";-)" style="display: inline; vertical-align: bottom;" class="emoticon" /> 
            </div>
        </content>
        
    </entry>
    <entry>
        <link href="http://javaporn.cestdelamerde.com/archives/10-Closeup-on-object-encapsulation-violation.html" rel="alternate" title="Closeup on object encapsulation violation" />
        <author>
            <name>François Guerry</name>
                    </author>
    
        <published>2008-06-11T16:38:33Z</published>
        <updated>2008-06-12T12:59:41Z</updated>
        <wfw:comment>http://javaporn.cestdelamerde.com/wfwcomment.php?cid=10</wfw:comment>
    
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://javaporn.cestdelamerde.com/rss.php?version=atom1.0&amp;type=comments&amp;cid=10</wfw:commentRss>
    
    
        <id>http://javaporn.cestdelamerde.com/archives/10-guid.html</id>
        <title type="html">Closeup on object encapsulation violation</title>
        <content type="xhtml" xml:base="http://javaporn.cestdelamerde.com/">
            <div xmlns="http://www.w3.org/1999/xhtml">
                Do you like Spring and Hibernate ? Personally, I do... but today I faced a strange issue... I tried to make the two incriminated guys auto generate a diff script for my DB schema...<br />
<br />
To make it short, I wrote this kind of code :<br />
<blockquote><br />
<pre>        ApplicationContext context = <FONT COLOR="#7f0055"><B>new</B></FONT> ClassPathXmlApplicationContext(<FONT COLOR="#ff7b92">"conf/data-repository-conf.xml"</FONT>);<BR />        LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) context.getBean(<FONT COLOR="#ff7b92">"&amp;mySessionFactory"</FONT>);<BR />        SchemaUpdate update = <FONT COLOR="#7f0055"><B>new</B></FONT> SchemaUpdate(lsfb.getConfiguration());<BR />        update.execute(<FONT COLOR="#7f0055"><B>true</B></FONT>, <FONT COLOR="#7f0055"><B>false</B></FONT>);</pre><br />
</blockquote><br />
And the associated conf/data-repository-conf.xml :<br />
<blockquote><br />
<pre>  <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">bean</FONT> <FONT COLOR="#7f007f">id</FONT>=<FONT COLOR="#2a00ff"><I>"myDataSource"</I></FONT> <FONT COLOR="#7f007f">class</FONT>=<FONT COLOR="#2a00ff"><I>"org.apache.commons.dbcp.BasicDataSource"</I></FONT> <FONT COLOR="#7f007f">destroy-method</FONT>=<FONT COLOR="#2a00ff"><I>"close"</I></FONT><BR />  	<FONT COLOR="#7f007f">p:validationQuery</FONT>=<FONT COLOR="#2a00ff"><I>"SELECT COUNT(*) FROM DUAL"</I></FONT><BR />    <FONT COLOR="#7f007f">p:driverClassName</FONT>=<FONT COLOR="#2a00ff"><I>"com.mysql.jdbc.Driver"</I></FONT><BR />    <FONT COLOR="#7f007f">p:url</FONT>=<FONT COLOR="#2a00ff"><I>"jdbc:mysql://host/schema"</I></FONT><BR />    <FONT COLOR="#7f007f">p:username</FONT>=<FONT COLOR="#2a00ff"><I>"***"</I></FONT><BR />    <FONT COLOR="#7f007f">p:password</FONT>=<FONT COLOR="#2a00ff"><I>"***"</I></FONT><FONT COLOR="#008080">/&gt;</FONT><BR /><BR />  <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">bean</FONT> <FONT COLOR="#7f007f">id</FONT>=<FONT COLOR="#2a00ff"><I>"mySessionFactory"</I></FONT> <FONT COLOR="#7f007f">class</FONT>=<FONT COLOR="#2a00ff"><I>"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</I></FONT><BR />    <FONT COLOR="#7f007f">p:dataSource-ref</FONT>=<FONT COLOR="#2a00ff"><I>"myDataSource"</I></FONT><FONT COLOR="#008080">&gt;</FONT><BR />    <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">property</FONT> <FONT COLOR="#7f007f">name</FONT>=<FONT COLOR="#2a00ff"><I>"mappingResources"</I></FONT><FONT COLOR="#008080">&gt;</FONT><BR />      <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">list</FONT><FONT COLOR="#008080">&gt;</FONT><BR />        <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">value</FONT><FONT COLOR="#008080">&gt;</FONT>mappings.hbm.xml<FONT COLOR="#008080">&lt;/</FONT><FONT COLOR="#3f7f7f">value</FONT><FONT COLOR="#008080">&gt;</FONT><BR />      <FONT COLOR="#008080">&lt;/</FONT><FONT COLOR="#3f7f7f">list</FONT><FONT COLOR="#008080">&gt;</FONT><BR />    <FONT COLOR="#008080">&lt;/</FONT><FONT COLOR="#3f7f7f">property</FONT><FONT COLOR="#008080">&gt;</FONT><BR />    <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">property</FONT> <FONT COLOR="#7f007f">name</FONT>=<FONT COLOR="#2a00ff"><I>"hibernateProperties"</I></FONT><FONT COLOR="#008080">&gt;</FONT><BR />      <FONT COLOR="#008080">&lt;</FONT><FONT COLOR="#3f7f7f">value</FONT><FONT COLOR="#008080">&gt;</FONT><BR />        hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect<BR />      <FONT COLOR="#008080">&lt;/</FONT><FONT COLOR="#3f7f7f">value</FONT><FONT COLOR="#008080">&gt;</FONT><BR />    <FONT COLOR="#008080">&lt;/</FONT><FONT COLOR="#3f7f7f">property</FONT><FONT COLOR="#008080">&gt;</FONT><BR />  <FONT COLOR="#008080">&lt;/</FONT><FONT COLOR="#3f7f7f">bean</FONT><FONT COLOR="#008080">&gt;</FONT></pre><br />
</blockquote><br />
Little explanation here : <br />
* first it creates the AppContext<br />
* then it lookup for the LocalSessionFactoryBean (not the resulting <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/SessionFactory.html">SessionFactory</a>, but its creator, the original Spring <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/FactoryBean.html">FactoryBean</a>) : for that I use the special char "&" at the beginning of the bean's name <br />
* the LocalSessionFactoryBean is needed to retrieve the underlying Configuration for Hibernate, and it creates with it a SchemaUpdate object<br />
* finally, it runs the update stuff..<br />
<br />
This is yet at a nice level of pornography, because of course an equivalent of such a code could be integrated in the bean declaration.<br />
But it doesn't deserve my purpose for some reason... whatever, let's go further !<br />
Here is the result at runtime: <br />
<blockquote><br />
<pre><FONT COLOR="#000080">org.hibernate.HibernateException</FONT>: No local DataSource found for configuration - 'dataSource' property must be set on LocalSessionFactoryBean<BR />	at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.configure(<FONT COLOR="#000080">LocalDataSourceConnectionProvider.java:50</FONT>)<BR />	at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(<FONT COLOR="#000080">ConnectionProviderFactory.java:124</FONT>)<BR />	at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(<FONT COLOR="#000080">ConnectionProviderFactory.java:56</FONT>)<BR />	at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(<FONT COLOR="#000080">ManagedProviderConnectionHelper.java:27</FONT>)<BR />	at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(<FONT COLOR="#000080">SchemaUpdate.java:127</FONT>)</pre><br />
</blockquote><br />
Hmmm... I googled this insane stack and found this is an old Spring issue :<br />
<a href="http://jira.springframework.org/browse/SPR-1547" title="http://jira.springframework.org/browse/SPR-1547">http://jira.springframework.org/browse/SPR-1547</a><br />
I'm using Spring 2.5.1 so I shouldn't have the problem... ?? The fact is : I have the problem...<br />
<br />
After a small investigation, I discovered that the datasource is passed to the Update stuff via a strange ThreadLocal trick, but this trick failed here for some unobvious reasons... To waste no time, and as long as this small program is a one-shot test stuff, I decided to solve the problem with the ugly code following.<br />
First we have a look inside the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/LocalSessionFactoryBean.html">problematic class</a> code to see how to fix that :<br />
<blockquote><br />
<pre><FONT COLOR="#7f0055"><B>public</B></FONT> <FONT COLOR="#7f0055"><B>class</B></FONT> LocalSessionFactoryBean <FONT COLOR="#7f0055"><B>extends</B></FONT> AbstractSessionFactoryBean <FONT COLOR="#7f0055"><B>implements</B></FONT> BeanClassLoaderAware {<BR /><BR />	<FONT COLOR="#7f0055"><B>private</B></FONT> <FONT COLOR="#7f0055"><B>static</B></FONT> <FONT COLOR="#7f0055"><B>final</B></FONT> ThreadLocal <FONT COLOR="#0000c0"><I>configTimeDataSourceHolder</I></FONT> = <FONT COLOR="#7f0055"><B>new</B></FONT> ThreadLocal();</pre>        <br />
</blockquote><br />
And now the fixed code itself - enjoy it:<br />
<blockquote><br />
<pre>        ApplicationContext context = <FONT COLOR="#7f0055"><B>new</B></FONT> ClassPathXmlApplicationContext(<FONT COLOR="#ff7b92">"conf/data-repository-conf.xml"</FONT>);<BR />        LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) context.getBean(<FONT COLOR="#ff7b92">"&amp;mySessionFactory"</FONT>);<BR />        SchemaUpdate update = <FONT COLOR="#7f0055"><B>new</B></FONT> SchemaUpdate(lsfb.getConfiguration());<BR />        <FONT COLOR="#7f0055"><B>try</B></FONT> {<BR />            Field declaredField = LocalSessionFactoryBean.<FONT COLOR="#7f0055"><B>class</B></FONT>.getDeclaredField(<FONT COLOR="#ff7b92">"configTimeDataSourceHolder"</FONT>);<BR />            declaredField.setAccessible(<FONT COLOR="#7f0055"><B>true</B></FONT>);<BR />            ThreadLocal&lt;DataSource&gt; tl = (ThreadLocal&lt;DataSource&gt;)(declaredField.get(<FONT COLOR="#7f0055"><B>null</B></FONT>));<BR />            tl.set((DataSource)context.getBean(<FONT COLOR="#ff7b92">"myDataSource"</FONT>));<BR />        } <FONT COLOR="#7f0055"><B>catch</B></FONT> (Throwable e) {<BR />            e.printStackTrace();<BR />        }<BR />        update.execute(<FONT COLOR="#7f0055"><B>true</B></FONT>, <FONT COLOR="#7f0055"><B>false</B></FONT>);</pre><br />
</blockquote><br />
<br />
Huh, believe me or not, it simply works now <img src="http://javaporn.cestdelamerde.com/templates/default/img/emoticons/laugh.png" alt=":-D" style="display: inline; vertical-align: bottom;" class="emoticon" /><br />
<br />
Small explanation:<br />
* get the declared (static) field of the ThreadLocal<br />
* make it accessible<br />
* get its content (null arg for static access)<br />
* put the DataSource in it<br />
* here we go !!! 
            </div>
        </content>
        
    </entry>
    <entry>
        <link href="http://javaporn.cestdelamerde.com/archives/9-Gorgeous-File-Manipulation.html" rel="alternate" title="Gorgeous File Manipulation" />
        <author>
            <name>philou</name>
                    </author>
    
        <published>2008-05-29T13:55:36Z</published>
        <updated>2008-05-29T13:55:36Z</updated>
        <wfw:comment>http://javaporn.cestdelamerde.com/wfwcomment.php?cid=9</wfw:comment>
    
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://javaporn.cestdelamerde.com/rss.php?version=atom1.0&amp;type=comments&amp;cid=9</wfw:commentRss>
    
    
        <id>http://javaporn.cestdelamerde.com/archives/9-guid.html</id>
        <title type="html">Gorgeous File Manipulation</title>
        <content type="xhtml" xml:base="http://javaporn.cestdelamerde.com/">
            <div xmlns="http://www.w3.org/1999/xhtml">
                The main purpose of the following code is to grab the directory of the package of a classpath resource and use it to create a new file. It is used in our unit tests where we assume that classpath resources are in real directories. It's of course pornographic...<br />
<blockquote><pre>File file = new File(new File(getClass().getResource("noel.jpg").getPath()).getParentFile(), info.getFileName());<br />
file.createNewFile();</pre></blockquote> 
            </div>
        </content>
        
    </entry>
    <entry>
        <link href="http://javaporn.cestdelamerde.com/archives/8-ClassLoader-brutal-JAR-insertion-at-runtime.html" rel="alternate" title="ClassLoader : brutal JAR insertion at runtime" />
        <author>
            <name>François Guerry</name>
                    </author>
    
        <published>2008-05-26T13:22:16Z</published>
        <updated>2008-06-12T12:06:34Z</updated>
        <wfw:comment>http://javaporn.cestdelamerde.com/wfwcomment.php?cid=8</wfw:comment>
    
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://javaporn.cestdelamerde.com/rss.php?version=atom1.0&amp;type=comments&amp;cid=8</wfw:commentRss>
    
    
        <id>http://javaporn.cestdelamerde.com/archives/8-guid.html</id>
        <title type="html">ClassLoader : brutal JAR insertion at runtime</title>
        <content type="xhtml" xml:base="http://javaporn.cestdelamerde.com/">
            <div xmlns="http://www.w3.org/1999/xhtml">
                I didn't remember where I found this, but I'd like to thank a lot the person who had invented it...<br />
I'd never had figured such a thing by myself, or maybe after long and desperate nights of headache, as a last chance...<br />
But the fact is : it works ! And it works fine !<br />
<br />
The idea is to insert new URLs in the system classloader, to allow some JARs to be part of it after startup... for me, it's was a manner to expose a kind of pluggable component feature : you just drop a JAR in a specific directory, and without modifying a single line of startup script or whatever, your jar comes to life automaticaly in the app.<br />
<br />
And now the beast, using a bit of reflective API, reaaaaally messy :<br />
<blockquote><pre>    <FONT COLOR="#7f0055"><B>private</B></FONT> <FONT COLOR="#7f0055"><B>static</B></FONT> <FONT COLOR="#7f0055"><B>final</B></FONT> Class[] <FONT COLOR="#0000c0"><I>parameters</I></FONT> = <FONT COLOR="#7f0055"><B>new</B></FONT> Class[]{URL.<FONT COLOR="#7f0055"><B>class</B></FONT>};<BR />    <BR />    <FONT COLOR="#7f0055"><B>public</B></FONT> <FONT COLOR="#7f0055"><B>static</B></FONT> <FONT COLOR="#7f0055"><B>void</B></FONT> addURL(URL u) <FONT COLOR="#7f0055"><B>throws</B></FONT> IOException {<BR />        URLClassLoader sysloader = (URLClassLoader)ClassLoader.<I>getSystemClassLoader</I>();<BR />        Class sysclass = URLClassLoader.<FONT COLOR="#7f0055"><B>class</B></FONT>;<BR />     <BR />        <FONT COLOR="#7f0055"><B>try</B></FONT> {<BR />            Method method = sysclass.getDeclaredMethod(<FONT COLOR="#ff7b92">"addURL"</FONT>,<FONT COLOR="#0000c0"><I>parameters</I></FONT>);<BR />            method.setAccessible(<FONT COLOR="#7f0055"><B>true</B></FONT>);<BR />            method.invoke(sysloader,<FONT COLOR="#7f0055"><B>new</B></FONT> Object[]{ u });<BR />        } <FONT COLOR="#7f0055"><B>catch</B></FONT> (Throwable t) {<BR />            t.printStackTrace();<BR />            <FONT COLOR="#7f0055"><B>throw</B></FONT> <FONT COLOR="#7f0055"><B>new</B></FONT> IOException(<FONT COLOR="#ff7b92">"Error, could not add URL to system classloader"</FONT>);<BR />        }<BR />    }</pre></blockquote> 
            </div>
        </content>
        
    </entry>
    <entry>
        <link href="http://javaporn.cestdelamerde.com/archives/7-Welcome.html" rel="alternate" title="Welcome" />
        <author>
            <name>philou</name>
                    </author>
    
        <published>2008-05-26T13:10:45Z</published>
        <updated>2008-05-28T07:50:07Z</updated>
        <wfw:comment>http://javaporn.cestdelamerde.com/wfwcomment.php?cid=7</wfw:comment>
    
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://javaporn.cestdelamerde.com/rss.php?version=atom1.0&amp;type=comments&amp;cid=7</wfw:commentRss>
    
    
        <id>http://javaporn.cestdelamerde.com/archives/7-guid.html</id>
        <title type="html">Welcome</title>
        <content type="xhtml" xml:base="http://javaporn.cestdelamerde.com/">
            <div xmlns="http://www.w3.org/1999/xhtml">
                Welcome to JavaPorn, a blog dedicated to horrible java code <img src="http://javaporn.cestdelamerde.com/templates/default/img/emoticons/tongue.png" alt=":-P" style="display: inline; vertical-align: bottom;" class="emoticon" /><br />
<br />
Here is the first piece of code ; it is used in a project to circumvent a parsing issue that throws OutOfMemoryError. Yes OutOfMemoryError !! (see <a href="https://issues.apache.org/jira/browse/SANSELAN-3">SANSELAN-3 on ASF Jira</a>)<br />
<br />
<blockquote><pre>try {<br />
    IImageMetadata metadata = Sanselan.getMetadata(new File(sourcePath));<br />
    // process stuff here <img src="http://javaporn.cestdelamerde.com/templates/default/img/emoticons/smile.png" alt=":-)" style="display: inline; vertical-align: bottom;" class="emoticon" /><br />
} catch (OutOfMemoryError oome) {<br />
    // do nothing here. see https://issues.apache.org/jira/browse/SANSELAN-3 for an explanation<br />
    log.warn("Please resolve cleanly ASF SANSELAN-3, catching an OOME is so crappy");<br />
}<br />
</pre></blockquote> 
            </div>
        </content>
        
    </entry>

</feed>