<?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>Spackos62-Java Blog &#187; Other</title>
	<atom:link href="http://java.randgestalten.de/index.php/category/other/feed/" rel="self" type="application/rss+xml" />
	<link>http://java.randgestalten.de</link>
	<description>java related stuff</description>
	<lastBuildDate>Fri, 02 Oct 2009 02:08:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bounded beans in IntelliJ IDEA</title>
		<link>http://java.randgestalten.de/index.php/2009/06/bounded-beans-in-intellij-idea/</link>
		<comments>http://java.randgestalten.de/index.php/2009/06/bounded-beans-in-intellij-idea/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 08:17:02 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=125</guid>
		<description><![CDATA[Well, for some reason I lost my Intellij IDEA configuration. I had a nice live template to create a firePropertyChange statement. This is not as nice as the bound setter plugin for eclipse as it does not create the method but the methods body.
Basically create a new Live Template and set the name to something [...]]]></description>
			<content:encoded><![CDATA[<p>Well, for some reason I lost my Intellij IDEA configuration. I had a nice live template to create a firePropertyChange statement. This is not as nice as the <a href="http://java.randgestalten.de/index.php/2008/10/generate-bounded-setters-plugin/">bound setter plugin</a> for eclipse as it does not create the method but the methods body.</p>
<p>Basically create a new <a href="http://blogs.jetbrains.com/idea/2006/03/defining-a-live-template-for-null-check/" target="_blank">Live Template</a> and set the name to something like &#8220;fpc&#8221;. The template is</p>
<pre>
<code>firePropertyChange("$name$", $name$, this.$name$ = $name$);</code>
</pre>
<p>and the variable definition for $name$ is <code>variableOfType("Object")</code>. Finally set the default value to &#8220;name&#8221; and you are ready to go. Let the IDE create a setter, remove the body, type fpc and hit tab. This will expand to the firePropertyChange statement and if you are lucky, the variable name is correct <img src='http://java.randgestalten.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Of course, the class has to be some sort of <a href="http://java.randgestalten.de/index.php/2008/10/a-simple-abstractbean/">AbstractBean</a> and provide a <code>firePropertyChange</code></p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2009/06/bounded-beans-in-intellij-idea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get the current SVN revision number in from Ant</title>
		<link>http://java.randgestalten.de/index.php/2009/05/get-the-current-svn-revision-number-in-from-ant/</link>
		<comments>http://java.randgestalten.de/index.php/2009/05/get-the-current-svn-revision-number-in-from-ant/#comments</comments>
		<pubDate>Thu, 14 May 2009 14:06:46 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=119</guid>
		<description><![CDATA[Another minor Problem I struggled with the last couple times I was updating one of my build scripts was resolving the current SVN revision number from within ant. Google points out a few solutions,  but most of them require optional SVN tasks or do an exec. Someone (sorry, I would put a link if [...]]]></description>
			<content:encoded><![CDATA[<p>Another minor Problem I struggled with the last couple times I was updating one of my build scripts was resolving the current SVN revision number from within ant. Google points out a few solutions,  but most of them require optional SVN tasks or do an exec. Someone (sorry, I would put a link if I could remember) came up with the simple solution of using the stored SVN information from the filesystem. So:</p>
<pre>
<code>    &lt;loadfile property="svn.revision" srcFile="./.svn/entries"&gt;
        &lt;filterchain&gt;
        &lt;headfilter lines="1" skip="3"/&gt;
        &lt;deletecharacters chars="\n"/&gt;
        &lt;/filterchain&gt;
   &lt;/loadfile&gt;</code>
</pre>
<p>loads the current revision number into a property called <code>svn.revision</code>. I use this with svn 1.4 and 1.5 repositories and it works just fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2009/05/get-the-current-svn-revision-number-in-from-ant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Set JPA entities at runtime</title>
		<link>http://java.randgestalten.de/index.php/2009/02/set-jpa-entities-at-runtime/</link>
		<comments>http://java.randgestalten.de/index.php/2009/02/set-jpa-entities-at-runtime/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 13:41:19 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[EclipseLink]]></category>
		<category><![CDATA[EPoS]]></category>
		<category><![CDATA[JPA]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=82</guid>
		<description><![CDATA[We use JPA as persistence mechanism for EPoS. The JPA implementation for the next release will be EclipseLink and I was adding that over the weekend. One important thing: I don&#8217;t know the entity classes at compile time. That makes it impossible to add the class names to the persistence.xml directly. Somehow we need a [...]]]></description>
			<content:encoded><![CDATA[<p>We use JPA as persistence mechanism for <a href="http://bio.informatik.uni-jena.de/epos">EPoS</a>. The JPA implementation for the next release will be <a href="http://www.eclipse.org/eclipselink/">EclipseLink</a> and I was adding that over the weekend. One important thing: I don&#8217;t know the entity classes at compile time. That makes it impossible to add the class names to the persistence.xml directly. Somehow we need a way to specify the classes at runtime (well, at least at initialization time, when we boto up the EntityManagerFactory). That is something taht seems to be not part of the JPA spec for Java SE environments ( in Java EE, you can let it <a href="http://java-persistence.blogspot.com/2008/02/testing-eclipselink-jpa-in-javase.html">search the jar files for entity classes</a>).</p>
<p>Ok, so we need a way to manipulate the content of the persistence.xml at runtime to add the class entries and I found way, hacking into the mechanism that reads the content of the persistence.xml.<br />
Basically, what we do is, we occupy the stream that represents the file and manipulate it at runtime. To do so, we have to extend the initialization process, but, thanks to protected variables used by the EclipseLink team, this works without any manipulation of the EclipseLink source code. We have to extend some classes, to make it work, but thats it. And it turned out that the changes are rather minimal and the hack works straight forward. So, lets jump into the code and get things running. Still, keep in mind, we work on top of internal initialization code ! There is a good chance that this breaks with a new release, although I think it will be straight forward to keep things running&#8230;at least as long as the EclipseLink team sticks to protected access modifiers for some of their variables.</p>
<p><span id="more-82"></span></p>
<p>The interface we have to implement is org.eclipse.persistence.internal.jpa.deployment.Archive. This defines the access mechanism used by EclipseLink and the implementations are used to access the content of the persistence.xml via  the getEntry method:</p>
<pre><code>/**
* Returns the InputStream for the given entry name. Returns null if no such
* entry exists. The entry name must be relative to the root of the module.
*
* @param entryPath the file name relative to the root of the module.
* @return the InputStream for the given entry name or null if not found.
*/
InputStream getEntry(String entryPath) throws IOException;</code></pre>
<p>Well, we don&#8217;t want to provide a complete implementation, we just want to know when the persistence.xml is accessed. Therefore we implemented a WrapperArchive that delegates to a parent archive and checks for access to persistence.xml.</p>
<pre><code>...
public InputStream getEntry(String entryPath) throws IOException {
    if (entryPath.equals("META-INF/persistence.xml")) {
        return createPersitenceString(parent
                .getEntry("META-INF/persistence.xml"));
    }
    return parent.getEntry(entryPath);
}
...
protected InputStream createPersitenceString(InputStream in) {
    BufferedReader bi = new BufferedReader(new InputStreamReader(in));
    StringBuffer b = new StringBuffer();
    String l = null;
    try {
        l = bi.readLine();
        while (l != null) {
            if (l.trim().startsWith("<!--$$CLASSES$$-->")) {
                if (entityCalsses != null) {
                    for (String clazz : entityCalsses) {
                        b.append("");
                        b.append(clazz);
                        b.append("");
                        b.append("\n");
                    }
                }
            } else {
                b.append(l);
            }
            l = bi.readLine();
        }
    } catch (IOException e) {
        Logger.getLogger(getClass()).error("Error while creating dynamic persistence.xml", e);
    }
    return new ByteArrayInputStream(b.toString().getBytes());
}
...</code></pre>
<p>All other methods delegate directly to the parent archive and we intercept access to <code>META-INF/persistence.xml</code>. If someone tries to get a stream to the persistence.xml, we intercept that call, read the original stream, manipulate it and return the changed content. Well, we just look for &lt;!&#8211;$$CLASSES$$&#8211;&gt; somewhere in the original, and replace that with the class definitions. I know, I know, there are better ways, but this works for now.<br />
Now, all we have to do is make sure that the WrapperArchive is used. To achieve this, we extend the PersistenceProvider:</p>
<pre><code>public class DynamicPersistenceProvider extends PersistenceProvider{
    private DynamicPersistenceProvider() {
        super();
        initializationHelper = new DynamicInitializationHelper();
    }
    public DynamicPersistenceProvider(String[] classes) {
        this();
        WrapperArchive.setEntityCalsses(Arrays.asList(classes));
    }
}</code></pre>
<p>The dynamic provider is initialized with the set of entity classes and we can use the openness of the EclipseLink implementation (thanks for using protected variables !!!). Internally they use an initialization helper that is used to get access to a JPAInitializer that has a method <code>initialize(Map, PersistenceInitializerHelper)</code> that creates the archives. To get that in, we implement the a DynamicInitializationHelper that extends PersistenceInitializationHelper:</p>
<pre><code>public class DynamicInitializationHelper extends PersistenceInitializationHelper{
    public JPAInitializer getInitializer(ClassLoader classLoader, Map m) {
        return DynamicJavaSECMPInitializer.getJavaSECMPInitializer();
    }
}</code></pre>
<p>This returns our own JPAInitializer</p>
<pre><code>public class DynamicJavaSECMPInitializer extends JavaSECMPInitializer {
    public static synchronized DynamicJavaSECMPInitializer getJavaSECMPInitializer() {
        if (javaSECMPInitializer == null || !(javaSECMPInitializer instanceof DynamicJavaSECMPInitializer)) {
           javaSECMPInitializer = new DynamicJavaSECMPInitializer();
        }
        initializeTopLinkLoggingFile();
        return (DynamicJavaSECMPInitializer) javaSECMPInitializer;
    }
    public void initialize(Map m, PersistenceInitializationHelper persistenceHelper) {
        initializeClassLoader(persistenceHelper);
        final Set pars = PersistenceUnitProcessor.findPersistenceArchives(initializationClassloader);
        for (Archive archive: pars) {
            AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize", archive);
            initPersistenceUnits(WrapperArchive.createWrapperArchive(archive), m, persistenceHelper);
        }

    }
}</code></pre>
<p>where we override the <code>initialize</code> method. The content is copy pasted from the original implementation except that we make sure that we create the WrapperArchives here. Also, again, thanks to the protected variables, we change the static reference to the JavaSECMPInitializer singleton to our dynamic version.</p>
<p>At the end of the day its not much code to achieve the runtime manipulation and we can do something like this</p>
<pre><code>...
String[] classes = new String[]{Person.class.getName()};
EntityManagerFactory emf = new DynamicPersistenceProvider(classes)
    .createEntityManagerFactory(persistenceUnitName, loadConfiguration());</code></pre>
<p>and end up with a persistence unit that contains a Person entity.<br />
There is only one little drawback that I discovered so far. When using dynamic weaving ( -javaagent:eclipselink.jar ), the JavaSECMPInitializer singleton seems to be initialized somewhere else. My workaround for now, I close the EntityManagerFactory immediately after the first creation and recreate a new one. That way lazy loading works and I am happy for the moment. I have to digg a bit deeper to find out why we need to do that, but for the moment I am happy <img src='http://java.randgestalten.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here is the <a href="http://java.randgestalten.de/files/dynamicPersistence.zip">source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2009/02/set-jpa-entities-at-runtime/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>BeansBinding ELProperty as simple template engine</title>
		<link>http://java.randgestalten.de/index.php/2008/10/beansbinding-elproperty-as-simple-template-engine/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/beansbinding-elproperty-as-simple-template-engine/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 00:55:55 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[BeansBinding]]></category>
		<category><![CDATA[EL]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=77</guid>
		<description><![CDATA[I just read an article about using adding EL support on your projects and I was looking for a nice alternative to Velocity anyways.
I know EL basics from BeansBinding, which I use in most of my UI projects. EL itself looks pretty interesting, but, well, sort of complicated. Even finding the proper jar files seems [...]]]></description>
			<content:encoded><![CDATA[<p>I just read an article about using <a href="http://weblogs.java.net/blog/felipeal/archive/2008/10/adding_el_suppo.html">adding EL support on your projects</a> and I was looking for a nice alternative to Velocity anyways.</p>
<p>I know EL basics from <a href="https://beansbinding.dev.java.net/">BeansBinding</a>, which I use in most of my UI projects. EL itself looks pretty interesting, but, well, sort of complicated. Even finding the proper jar files seems to be a problem, or at least <em>&#8220;another of life&#8217;s obscure mysteries&#8221;</em> and using EL for a simple search and replace example implies implementing some  <em>internals</em>. I was looking for something that I can just use.</p>
<p>Luckily, the first comment mentions the ELProperty class from BeansBinding, and that jar is just a few mouse clicks away, so I thought I give it a try&#8230;and&#8230;well&#8230;it works <img src='http://java.randgestalten.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><span id="more-77"></span></p>
<pre class="code"><code>public static void main(String[] args) {
    Person mom = new Person("Ann", null);
    Person son = new Person("Dave", mom);
    Dog dog = new Dog("Foxy");

    ELProperty el = ELProperty
            .create("${name}'s mom is ${mother.name}");
    System.out.println(el.getValue(son));

    ELProperty mapEL = ELProperty
            .create("${mother.name}'s dog is ${dog.name}");
    Map map = new HashMap();
    map.put("mother", mom);
    map.put("dog", dog);
    System.out.println(mapEL.getValue(map));
}</code></pre>
<p>ELProperty.create() creates a new instance from a template string. You can fill the template with values using getValue() method. This one takes either an Object with proper getter methods or a Map to resolve variables. And this is the result of the simple example:</p>
<pre><code>Dave's mom is Ann
Ann's dog is Foxy</code></pre>
<p><a href="http://java.randgestalten.de/files/ELBeansTest.java">Full Sample Code</a> ( You need the beansbinding jar in the classpath)</p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/beansbinding-elproperty-as-simple-template-engine/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>combine multiple jar files and remove signatures</title>
		<link>http://java.randgestalten.de/index.php/2008/10/combine-multiple-jar-files-and-remove-signatures/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/combine-multiple-jar-files-and-remove-signatures/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 22:56:48 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[signature]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=65</guid>
		<description><![CDATA[I just came across a situation where I had to join multiple jar files and my own classes into one jar bundle. Ant comes in handy:

&#60;jar destfile="${app.id}-${app.version}.jar"
  index="true"
  filesetmanifest="merge"&#62;
  &#60;fileset dir="${classes.dir}"/&#62;
  &#60;zipgroupfileset dir="${lib.dir}" includes="*.jar" /&#62;
&#60;/jar&#62;

This takes all the files in ${classes.dir}, packs them in a jar and add the content of [...]]]></description>
			<content:encoded><![CDATA[<p>I just came across a situation where I had to join multiple jar files and my own classes into one jar bundle. Ant comes in handy:</p>
<pre>
<code>&lt;jar destfile="${app.id}-${app.version}.jar"
  index="true"
  filesetmanifest="merge"&gt;
  &lt;fileset dir="${classes.dir}"/&gt;
  &lt;zipgroupfileset dir="${lib.dir}" includes="*.jar" /&gt;
&lt;/jar&gt;</code>
</pre>
<p>This takes all the files in <code>${classes.dir}</code>, packs them in a jar and add the content of all the jar files in <code>${lib.dir}</code>. In this case, we merge multiple manifests, but this can be changed according to the jar <a href="http://ant.apache.org/manual/CoreTasks/jar.html">task documentation</a>.<br />
<span id="more-65"></span><br />
This works as long as none of your files is signed. You know that you are in trouble when you get messages like</p>
<pre>
<code>java.lang.SecurityException: no manifiest section for signature file entry ....</code>
</pre>
<p>I found a solution <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&#038;f=65&#038;t=000103">here</a>, and the say that one has to remove the SUN_MICR files. This can be done manually, but keep it in mind when ever you update your libraries. I find it more elegant when ant just removes the files. Unfortunately it is to possible to specify excludes for zipgroupfileset. The alternative to zipgroupfileset is zipfileset, which allows exclude patterns and extraction of zip files via the src parameter. And thats the problem. You can only specify exactly one file as src. I don&#8217;t want to specify all my jars manually. Therefore I creates a single jar first, using zipgroupfileset and then move this file through a zip/zipfileset where we can exclude specific files. </p>
<pre>
<code>&lt;jar destfile="${app.id}-${app.version}.jar"
  index="true"
  filesetmanifest="merge"&gt;
  &lt;fileset dir="${classes.dir}"/&gt;
  &lt;zipgroupfileset dir="${lib.dir}" includes="*.jar" /&gt;
&lt;/jar&gt;
&lt;zip destfile="temp-jar-file.jar"&gt;
  &lt;zipfileset src="${app.id}-${app.version}.jar" excludes="META-INF/*.RSA, META-INF/*.DSA, META-INF/*.SF" /&gt;
&lt;/zip&gt;
&lt;move file="temp-jar-file.jar" tofile="${app.id}-${app.version}.jar"/&gt;</code>
</pre>
<p>You end up with a single, signature free jar file. This is not the most elegant solution, which would be either to enable multiple src files in zipfileset or an excludes parameter for zipgroupfileset that matches against the content of files, not the input files. But still, its small and does not need any third party ant tasks. </p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/combine-multiple-jar-files-and-remove-signatures/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Add Copyright to Java Sources</title>
		<link>http://java.randgestalten.de/index.php/2008/10/add-copyright-to-java-sources/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/add-copyright-to-java-sources/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 20:57:34 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Other]]></category>
		<category><![CDATA[QAlign2]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=59</guid>
		<description><![CDATA[Just found a nice Eclipse plugin that adds license information to a all the files (well, you can select them) in a project. You can specify the license, enter your own, customize the output &#8230;. nice !
Just reminds me, we had a java class that did that for QAlign 2 Panta Rei source code distribution.
Yeah, [...]]]></description>
			<content:encoded><![CDATA[<p>Just found a <a href="http://www.wdev91.com/?p=cpw_ug">nice Eclipse plugin</a> that adds license information to a all the files (well, you can select them) in a project. You can specify the license, enter your own, customize the output &#8230;. nice !</p>
<p>Just reminds me, we had a java class that did that for <a href="http://gi.cebitec.uni-bielefeld.de/qalign">QAlign 2 Panta Rei</a> source code distribution.</p>
<p>Yeah, <a href="http://java.randgestalten.de/files/AppendDisclaimer.java">there it is</a>, May 21, 2005 written in Michas old apartment in Barcelona while working on QAlign2.</p>
<div id="attachment_60" class="wp-caption aligncenter" style="width: 310px"><a href="http://java.randgestalten.de/wp-content/uploads/2008/10/barca.jpg"><img src="http://java.randgestalten.de/wp-content/uploads/2008/10/barca-300x225.jpg" alt="The QAlign2 crew" title="barca" width="300" height="225" class="size-medium wp-image-60" /></a><p class="wp-caption-text">The QAlign2 crew</p></div>
<p><span id="more-59"></span></p>
<p>And &#8211; do never ever trust Micha when he says that he never copied a &#8220;Start Wars &#8211; Episode I&#8221; script into a static final String variable in one of the source code files <img src='http://java.randgestalten.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<pre><code>import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/*
 * Created on May 21, 2005
 */

/**
 * Class scans given directory for sourcefiles and appends a disclaimer
 *
 * This is a quick hack version of a good appending methods and it is NOT memory efficient. Thats
 * why "Start Wars - Episode I" will not work as disclaimer. The disclaimer itself should NOT be inlined
 * in sourcecode comment tags, this is done by {@see #readDiscalimer()} (Let it be the pure disclaimer, without any \/* *\/).
 * Also the size of source files is limited to the heap size of the vm. If one uses this with large source
 * or disclaimer files extend the VM max heap size (e.g. -Xmx512m).
 *
 * Remember, the scanFile() method automatically appends "\r\n" to each line while writing the files.
 *
 * parameters
 * -d  The source directory
 * -f  The disclaimer
 * -v &amp;lt;0..2&amp;gt; verbose output
 * @author Thasso
 */
public class AppendDisclaimer {
    /**
     * the input directory
     */
    public static String inputDir = ".";
    /**
     * Filename of the disclaimer
     */
    public static String disclaimerFile = null;
    /**
     * String representation of the disclaimer
     */
    public static String disclaimer;

    /**
     * verbose level
     */
    public static int verbose = 0;

    public static void main(String[] args) {
        if(args.length &amp;lt; 1){
            printUsage();
        }
        // Parse arguments
        for (int i = 0; i &amp;lt; args.length; i++) {
            if(args[i].equalsIgnoreCase("-d")){
                inputDir = args[i+1];
            }
            if(args[i].equalsIgnoreCase("-v")){
                verbose = args[i+1] != null ? Integer.parseInt(args[i+1]):2;
            }
            if(args[i].equalsIgnoreCase("-f")){
                disclaimerFile = args[i+1];
            }
        }
        if(disclaimerFile == null)
            printUsage("Discalimer file not set ! Use -f to define a disclaimer");

        readDiscalimer();        

        // read filelist from directory
        File input = new File(inputDir);
        if(!input.isDirectory() ||!input.canRead()){
            printUsage("Can not read from directory " + input.getAbsolutePath());
        }
        if(verbose &amp;gt; 0)
            System.out.println("Scanning Directory :\n\t"+input.getAbsolutePath());

        List javaFiles = readDirectory(input, ".java");
        //Scan files and append disclaimer
        for (Iterator it = javaFiles.iterator(); it.hasNext();) {
            File f = (File) it.next();
            scanFile(f);
        }
    }

    /**
     * read the disclaimer from file
     */
    private static void readDiscalimer() {
        File f = new File(disclaimerFile);
        StringBuffer d = new StringBuffer();
        if(!f.exists())
            printUsage("Disclaimer " + disclaimerFile + " not found");
        if(!f.canRead())
            printUsage("Disclaimer " + disclaimerFile + " not readable");

        try {
            BufferedReader in = new BufferedReader(new FileReader(f));
            d.append("/*\r\n");
            String l = in.readLine();
            while(l != null){
                d.append(" * "+l+"\r\n");
                l = in.readLine();
            }
            d.append("*/\r\n");
            disclaimer = d.toString();
            in.close();
        } catch (FileNotFoundException e) {
            printUsage("Disclaimer " + disclaimerFile + " not found");
        } catch (IOException e) {
            printUsage("Error while reading from file " + disclaimerFile);
        }
    }

    /**
     * Memory consuming scna file method.
     * Creates a buffer and appends the disclaimer. Than reads the file and appends it to
     * the buffer, line by line, adding \\r\\n to each line.
     * When done, writes out the file with a simple and absolute inefficent buffer.toString() call.
     *
     * @param f
     */
    private static void scanFile(File f){
        BufferedReader in = null;
        StringBuffer content = new StringBuffer();
        content.append(disclaimer+"\r\n"+"\r\n");
        if(verbose &amp;gt; 1)
            System.out.println("Reading file " + f.getName());

        try {
            in = new BufferedReader(new FileReader(f));
            String line = in.readLine();
            while(line != null){
                content.append(line+"\r\n");
                line = in.readLine();
            }
            in.close();
        } catch (FileNotFoundException e) {
            printUsage("File  not found " + f.getName());
        } catch (IOException e) {
            printUsage("Could not read file " + f.getName());
        }finally{
            try {
                in.close();
            } catch (IOException e1) {}
        }
        if(verbose &amp;gt; 1)
            System.out.println("Writing file " + f.getName());

        BufferedWriter w = null;
        try {
            w = new BufferedWriter(new FileWriter(f));
            w.write(content.toString());
            w.close();
        } catch (IOException e) {
            printUsage("Could not write file " + f.getName());
        }finally{
            try {
                w.close();
            } catch (IOException e) {}
        }
    }

    /**
     * Recurcive scan directory for files with given suffix
     *
     * @param dir directory to scan
     * @param suffix files suffix
     */
    protected static List readDirectory(File dir, String suffix) {
        ArrayList files = new ArrayList();
        if(dir.isFile())
            return null;

        if(verbose &amp;gt; 1)
            System.out.println("Scanning Directory :\n\t"+dir.getAbsolutePath());

        File[] all = dir.listFiles();
        ArrayList dirs = new ArrayList();
        for (int i = 0; i &amp;lt; all.length; i++) {
            if(all[i].isFile()){
                if(all[i].getName().endsWith(suffix)){
                    files.add(all[i]);
                }
            }else if(all[i].isDirectory()){
                dirs.add(all[i]);
            }
        }
        for (Iterator it = dirs.iterator(); it.hasNext();) {
            File d = (File) it.next();
            List l = readDirectory(d, suffix);
            files.addAll(l);
        }
        return files;
    }

    /**
     * print string and the usage
     * @param string
     */
    private static void printUsage(String string) {
        System.err.println(string + "\n\n");
        printUsage();
    }

    /**
     * print the usage
     */
    private static void printUsage() {
        System.err.println(""
                +" This is a quick hack version of a good appending methods and it is NOT memory efficient. \r\n"
                + " Thats why "Start Wars - Episode I" will not work as disclaimer.\r\n"
                + " The disclaimer itself should NOT be inlined\r\n"
                + " in sourcecode comment tags, this is done automaticaly (Let it be the pure disclaimer, without any /* */) \r\n"
                + " Also the size of source files is limited to the heap size of the vm. If one uses this with large source\r\n"
                + " or disclaimer files extend the VM max heap size (e.g. -Xmx512m).\r\n"
                + " \r\n"
                + " Remember, the scanFile() method automatically appends "\\r\\n" to each line while writing the files.\r\n"
                + " \r\n"
                + " parameters\r\n"
                + " -d  The source directory\r\n"
                + " -f  The disclaimer\r\n"
                + " -v &amp;lt;0..2&amp;gt; verbose output\r\n"

        );
        System.exit(1);
    }

}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/add-copyright-to-java-sources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ANT build.xml template</title>
		<link>http://java.randgestalten.de/index.php/2008/10/ant-buildxml-template/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/ant-buildxml-template/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 09:37:02 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[ant]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=56</guid>
		<description><![CDATA[Just that I have it at one place. This is my ant build.xml template. It assumes a simple project structure:

src/ - all java soources in here
lib/
  devel/ - development libraries
  runtime/ - runtime libraries

If your project follows this struicture, you can get the following tasks out of the box:

Buildfile: build.xml

Main targets:

 clean  [...]]]></description>
			<content:encoded><![CDATA[<p>Just that I have it at one place. This is my ant build.xml template. It assumes a simple project structure:</p>
<pre>
src/ - all java soources in here
lib/
  devel/ - development libraries
  runtime/ - runtime libraries
</pre>
<p>If your project follows this struicture, you can get the following tasks out of the box:</p>
<pre>
Buildfile: build.xml

Main targets:

 clean          Clean
 compile        Compile all source files
 compile-tests  Compile all source files
 dist           Build library and pack jar file
 dist-one       Build library and pack jar file
 doc            Create Javadoc
 html-reports   Creates HTML from test reports
 test           Run JUnit Tests
Default target: dist
</pre>
<p>You can modify the properties to adapt different names or a different project structure. The script creates a <code>target</code> directory that contains the results.</p>
<p>Compiling and packing should work out of the box, but the test sections expects a junit and ant-junit jars  in <code>lib/devel</code>. They can also be placed in one of Ants lib directories.</p>
<p><a href="http://java.randgestalten.de/files/build.xml">The build file</a></p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/ant-buildxml-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WTF per minute</title>
		<link>http://java.randgestalten.de/index.php/2008/10/wtf-per-minute/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/wtf-per-minute/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 10:06:08 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=53</guid>
		<description><![CDATA[Yes, that is the right code quality measurement
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.osnews.com/story/19266/WTFs_m">Yes, that is the right code quality measurement</a></p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/wtf-per-minute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Packagesorter Sources</title>
		<link>http://java.randgestalten.de/index.php/2008/10/packagesorter-sources/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/packagesorter-sources/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 22:19:08 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=43</guid>
		<description><![CDATA[Here are the sources to the package sorter Eclipse plugin. The plugin is released under the EPL license and is packed as Eclipse Project. Unzip and import the directory into your Eclipse workspace.
Download
]]></description>
			<content:encoded><![CDATA[<p><a href="http://java.randgestalten.de/files/de.randgestalten.packagesorter-src-1.0.zip">Here are the sources</a> to the <a href="http://java.randgestalten.de/?p=6">package sorter Eclipse plugin</a>. The plugin is released under the <a href="http://www.eclipse.org/legal/epl-v10.html">EPL license</a> and is packed as Eclipse Project. Unzip and import the directory into your Eclipse workspace.</p>
<p><a href="http://java.randgestalten.de/files/de.randgestalten.packagesorter-src-1.0.zip">Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/packagesorter-sources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Java 6 Update 10 on Ubuntu 8.04</title>
		<link>http://java.randgestalten.de/index.php/2008/10/install-java-6-update-10-on-ubuntu-804/</link>
		<comments>http://java.randgestalten.de/index.php/2008/10/install-java-6-update-10-on-ubuntu-804/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 14:26:30 +0000</pubDate>
		<dc:creator>Thasso</dc:creator>
				<category><![CDATA[Other]]></category>

		<guid isPermaLink="false">http://java.randgestalten.de/?p=31</guid>
		<description><![CDATA[I had to install Java 6 Update 10 on Ubuntu 8.04. The thing is straight forward if you do not want to integrate the new JVM into Ubuntus alternatives system (and do not need teh new Java Plugin in your browser). Download, install, modify your PATH, JAVA_HOME and JDK_HOME, done. Google helped to find this [...]]]></description>
			<content:encoded><![CDATA[<p>I had to install <a href="https://jdk6.dev.java.net/" target="_blank">Java 6 Update 10</a> on Ubuntu 8.04. The thing is straight forward if you do not want to integrate the new JVM into Ubuntus alternatives system (and do not need teh new Java Plugin in your browser). Download, install, modify your PATH, JAVA_HOME and JDK_HOME, done. Google helped to find this <a href="http://coffeecokeandcode.blogspot.com/2008/09/installing-java-6-update-10-on-ubuntu.html">really good description of the process</a>, including moving the JVM to /usr/lib/jvm, where Ubuntu expects the JVMs.</p>
<p>The tricky part is the integration into Ubuntus alternatives system. It provides an easy way to switch between VMs and is usable even by &#8220;non geek&#8221; users and <code>update-java-alternatives</code> will also switch your java browser plugin. </p>
<p>We have to have Java 6 installed from the Ubuntu repositories:</p>
<pre>
<code>sudo apt-get install sun-java6-jdk</code>
</pre>
<p>Now, follow <a href="http://coffeecokeandcode.blogspot.com/2008/09/installing-java-6-update-10-on-ubuntu.html">Caspers</a> first two steps to install the VM. <a href="http://java.sun.com/javase/downloads/ea/6u10/6u10rcDownload.jsp#6u10JDKs">Download</a> Update 10 and execute the script</p>
<pre>
<code>sh jdk-6u10-rc2-bin-b32-linux-amd64-12_sep_2008.bin</code>
</pre>
<p>Move the directory to <code>/usr/lib/jvm</code>, where Ubuntu expects JVMs</p>
<pre>
<code>sudo mv jdk1.6.0_10 java-6-sun-1.6.0.10
sudo mv jdk1.6.0_10/ /usr/lib/jvm</code>
</pre>
<p>And create a symlink</p>
<pre>
<code>cd /usr/lib/jvm
sudo ln -s java-6-sun-1.6.0.10 java-6.10-sun</code>
</pre>
<p>The directory should look more or less like this</p>
<pre>
<code>lrwxrwxrwx   1 root   root      23 2008-10-07 19:01 java-1.5.0-sun -&gt; java-1.5.0-sun-1.5.0.15
drwxr-xr-x  10 root   root    4096 2008-10-08 11:34 java-1.5.0-sun-1.5.0.15
-rw-r--r--   1 root   root    2165 2008-03-26 02:28 .java-1.5.0-sun.jinfo
<strong>lrwxrwxrwx   1 root   root      19 2008-10-08 12:21 java-6.10-sun -&gt; java-6-sun-1.6.0.10</strong>
lrwxrwxrwx   1 root   root      19 2008-10-07 19:00 java-6-sun -&gt; java-6-sun-1.6.0.06
drwxr-xr-x   8 root   root    4096 2008-10-08 11:35 java-6-sun-1.6.0.06
<strong>drwxr-xr-x  10 root   root    4096 2008-10-08 11:40 java-6-sun-1.6.0.10</strong>
-rw-r--r--   1 root   root    2341 2008-10-08 11:47 .java-6-sun.jinfo</code>
</pre>
<p>To integrate the new VM into the alternatives system, we use <code>update-alternatives --install </code> to get new alternatives and provide a .jinfo file used by <code>update-java-alternatives</code>. For simplicity, I (and the script) assume that you have Java 6 installed. We need the Java 6 .jinfo file as a template. Basically, we h clone and modify the file, such that all the links point to the appropriate directory. Well, that can be done with your favourite text editor. The ugly part is, you have to call <code>udpate-alternatives --install</code> for ALL the commands. That calls for a <a href="http://java.randgestalten.de/files/create-jvm-alternative-6.10.pl" target="_blank">script<br />
</a>.</p>
<pre>
<code>#!/usr/bin/perl
use strict;

# use this as template - we assume that you have java 6 installed
# its in the repo !
my $jvm_template="java-6-sun";
my $jvm_template_path="/usr/lib/jvm/java-6-sun";

# this is the new jvm
my $jvm_name="java-6-sun-1.6.0.10";
my $jvm_alias="java-6.10-sun";
my $jvm_path="/usr/lib/jvm/java-6.10-sun";
my $jvm_priority=64;
my $jvm_section="non-free";

open(JINFO_OUT, "&gt;/usr/lib/jvm/.$jvm_alias.jinfo") or die "Can not write jinfo file !";

#write header
print JINFO_OUT "name=$jvm_name\n";
print JINFO_OUT "alias=$jvm_alias\n";
print JINFO_OUT "priority=$jvm_priority\n";
print JINFO_OUT "section=$jvm_section\n\n";

my @lines = ();
open(IN, "/usr/lib/jvm/.$jvm_template.jinfo") or die "Can note read jinfo template!";
@lines = &lt;IN&gt;;
close(IN);

my @new_config = ();
foreach(@lines){
    if($_=~ /$jvm_template_path/){
        chomp($_);
        $_ =~ s/$jvm_template_path/$jvm_path/;
        push(@new_config, $_);
        print JINFO_OUT "$_\n";
    }
}
close(JINFO_OUT);

foreach(@new_config){
    my @split = split(' ', $_);
    system("update-alternatives --install /usr/bin/@split[1]  @split[1]  @split[2] $jvm_priority");
}</code>
</pre>
<p><a href="http://java.randgestalten.de/files/create-jvm-alternative-6.10.pl" target="_blank">Download the script</a> and execute it (with sudo/as root &#8211; we have to write to <code>/usr/lib/jvm</code>) after you have moved the update 10 installation to <code>/usr/lib/jvm/java-6-sun-1.6.0.10</code> and created the symlink to <code>java-6.10-sun</code> as described above. (The script is quick and dirty. The names are defined in the first lines. If you have used other directory/alias names, you have to modify the script). </p>
<pre>
<code>sudo perl create-jvm-alternative-6.10.pl</code>
</pre>
<p>This generates a .jinfo file in /usr/lib/jvm and it calls <code>update-alternatives --install</code> for all the things mentioned in the template .jinfo. The <code>update-alternatives</code> takes a generic name as first argument (thanks Jonathan &#8211; I should read man pages more often). This is created as a symlink pointing to the actual name in /etc/alternatives. /etc/alternatives then points to the currently chosen alternative. SO, for example, at some point the script calls:</p>
<pre>
<code>update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-6.10/bin/java</code>
</pre>
<p>This creates two symlinks</p>
<pre>
<code>/usr/bin/java -&gt; /etc/alternatives/java
/etc/alternatives/java -&gt; /usr/lib/jvm/java-6.10/bin/java</code>
</pre>
<p>The latter might already exist and point to some java alternative (i.e. java v1.5). As far as I&#8217;ve seen this is not overwritten. The latter symlink is the one that is actually changed when you switch alternatives.</p>
<p>When everything is done, you should get something like:</p>
<pre>
<code>thasso@antef:~&gt; update-java-alternatives -l
java-1.5.0-sun 53 /usr/lib/jvm/java-1.5.0-sun
<strong>java-6.10-sun 64 /usr/lib/jvm/java-6.10-sun</strong>
java-6-sun 63 /usr/lib/jvm/java-6-sun</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://java.randgestalten.de/index.php/2008/10/install-java-6-update-10-on-ubuntu-804/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
