<?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>public static final &#187; java</title>
	<atom:link href="http://www.publicstaticfinal.de/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.publicstaticfinal.de</link>
	<description>the constant Java blog</description>
	<lastBuildDate>Fri, 25 Feb 2011 14:48:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Weak References in Java</title>
		<link>http://www.publicstaticfinal.de/2011/02/25/weak-references-in-java/</link>
		<comments>http://www.publicstaticfinal.de/2011/02/25/weak-references-in-java/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 14:48:53 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[Weak Reference]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=169</guid>
		<description><![CDATA[Ever heard of Weak References in Java? Well, me neither. But it really sounds useful and you should give it a try.]]></description>
			<content:encoded><![CDATA[<p>Ever heard of <a title="Weak References in Java" href="http://weblogs.java.net/blog/2006/05/04/understanding-weak-references" target="_blank">Weak References</a> in Java? Well, me neither. But it really sounds useful and you should give it a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2011/02/25/weak-references-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FOP: Embedding fonts from classpath</title>
		<link>http://www.publicstaticfinal.de/2011/01/26/fop-embedding-fonts-from-classpath/</link>
		<comments>http://www.publicstaticfinal.de/2011/01/26/fop-embedding-fonts-from-classpath/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 07:37:33 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[FOP]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=163</guid>
		<description><![CDATA[If you&#8217;re using Apache FOP to create PDF/A you&#8217;re forced to embed all fonts (even the base 14 fonts).  Generally there are two options how to embed the fonts: Let FOP auto-detect the fonts Create a font metrics file and tell FOP where to find it Auto-detection is a pretty nice feature since FOP does [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using <a title="Apache FOP" href="http://xmlgraphics.apache.org/fop/" target="_blank">Apache FOP</a> to create PDF/A you&#8217;re forced to embed all fonts (even the base 14 fonts).  Generally there are two options how to embed the fonts:</p>
<ol>
<li>Let FOP auto-detect the fonts</li>
<li>Create a font metrics file and tell FOP where to find it</li>
</ol>
<p><span id="more-163"></span>Auto-detection is a pretty nice feature since FOP does the job to find and interpret the font.  The major drawback of this solution is that FOP scans different places for fonts (the default font folders of the OS,  every folder in &#8220;.&#8221; and it looks for special entries in MANIFEST.MF). This is done every time you create a new Fop instance. You can guess how that might effect an application if it&#8217;s creating lots of PDF files.</p>
<p>To improve the performance you can create font metrics files an tell FOP where to find them. Compared to auto-detection this is a cheap operation. But there&#8217;s also a drawback: URIs to the files have to absolute paths. Especially if you&#8217;re using different environments  (Development, Testing, Pre-Production etc.) absolute paths are hard to find.</p>
<p>Wouldn&#8217;t it be nice if you can put your font files in a JAR and FOP just finds it? FOP can&#8217;t do it by default but you can teach FOP how to. First you have to implement an URIResolver:</p>
<pre class="brush: java; title: ; notranslate">
import java.io.InputStream;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

public class ClasspathUriResolver implements URIResolver
{

  public Source resolve(String href, String base) throws TransformerException
  {
    Source source = null;
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(href);
    if (inputStream != null)
    {
      source = new StreamSource(inputStream);
    }
    return source;
  }
}
</pre>
</pre>
<p>The next step is to register this URIResolver as a custom URIResolver. You can access the default URIResolver via the FopFactory:</p>
<pre class="brush: java; title: ; notranslate">
FopFactory fopFactory = FopFactory.newInstance();
FOURIResolver uriResolver = (FOURIResolver) fopFactory.getURIResolver();

uriResolver.setCustomURIResolver(new ClasspathUriResolver());
</pre>
</pre>
<p>FOP now tries to find the font and metrics file with its built in feature and if no files are found the ClasspathUriResolver is called. If still no font can be found an PDFConformanceException is thrown.</p>
<p>The final step is to put your files somewhere in the classpath (best choice would be a JAR file). If you're putting them in a JAR file in the folder fonts the appropriate FOP config would look like:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;renderers&gt;
 &lt;renderer mime=&quot;application/pdf&quot;&gt;
   &lt;fonts&gt;
     &lt;!-- Arial  --&gt;
     &lt;font
       metrics-url=&quot;\fonts\arial.xml&quot;
       kerning=&quot;yes&quot;
       embed-url=&quot;\fonts\arial.ttf&quot;&gt;
       &lt;font-triplet name=&quot;Arial&quot; style=&quot;normal&quot; weight=&quot;normal&quot; /&gt;
       &lt;font-triplet name=&quot;ArialMT&quot; style=&quot;normal&quot; weight=&quot;normal&quot; /&gt;
     &lt;/font&gt;
   &lt;/fonts&gt;
 &lt;/renderer&gt;
</pre>
</pre>
<p>FOP now finds the font and can embed it. Since the file is loaded into the Classloader ever subsequent lookup is very fast. If you don't want to open an InputStream every time and you don't have many fonts you can cache the contents in a byte[] and put an ByteArrayInputStream in the StreamSource.</p>
<p>For comparison: When I use auto-detection creating a new Fop instance takes about 300 ms. Using predefined metrics files the instance is created &lt; 20 ms.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2011/01/26/fop-embedding-fonts-from-classpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String concatenation</title>
		<link>http://www.publicstaticfinal.de/2010/08/21/string-concatenation/</link>
		<comments>http://www.publicstaticfinal.de/2010/08/21/string-concatenation/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 12:09:05 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=148</guid>
		<description><![CDATA[One of the first things developers learn about String concatenation in Java is that the &#8220;+&#8221; operation does the magic. In this case the magician is the compiler. Lets look how the following code snippet gets translated by the compiler. Every time the String concatenation is invoked a new StringBuilder instance is created and but [...]]]></description>
			<content:encoded><![CDATA[<p>One of the first things developers learn about String concatenation in Java is that the &#8220;+&#8221; operation does the magic. In this case the magician is the compiler. Lets look how the following code snippet gets translated by the compiler.</p>
<pre class="brush: java; title: ; notranslate">
// Sample.java
String buffer = &quot;&quot;;
for (String string : listOfStrings)
{
  buffer += string;
}
</pre>
<pre class="brush: java; title: ; notranslate">
// Sample.class
String buffer = &quot;&quot;;
for (String string : listOfString)
{
  buffer = new StringBuilder(buffer).append(string).toString();
}
</pre>
</pre>
<p>Every time the String concatenation is invoked a new StringBuilder instance is created and but never assigned. Hence it is immediately eligible for Garbace Collection. What a waste of Memory. If you really need to concat Strings in a loop you better do it directly with a StringBuilder.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/08/21/string-concatenation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RIP Sun</title>
		<link>http://www.publicstaticfinal.de/2010/01/22/rip-sun/</link>
		<comments>http://www.publicstaticfinal.de/2010/01/22/rip-sun/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 11:49:32 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Sun]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=141</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.publicstaticfinal.de/wp-content/uploads/2010/01/SunRIPsmall.jpg" rel="thumbnail"><img class="alignnone size-medium wp-image-140" title="SunRIPsmall" src="http://www.publicstaticfinal.de/wp-content/uploads/2010/01/SunRIPsmall-300x234.jpg" alt="" width="581" height="452" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/01/22/rip-sun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Collections Library 1.0 available</title>
		<link>http://www.publicstaticfinal.de/2010/01/04/google-collections-library-1-0-available/</link>
		<comments>http://www.publicstaticfinal.de/2010/01/04/google-collections-library-1-0-available/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 08:56:12 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Framework]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=128</guid>
		<description><![CDATA[Last year on Dec, 30th Google released version 1.0 of it&#8217;s Collections framework. Although Apache Commons Collections provide a rich set of enhancements to the standard Collections framework Google&#8217;s implementation has two big advantages: Based on Java 1.5 and hence using Generics More standard compliant (Commons Collections occasionally break standard behavior) Go to their website [...]]]></description>
			<content:encoded><![CDATA[<p>Last year on Dec, 30th Google released version 1.0 of it&#8217;s Collections framework. Although Apache Commons Collections provide a rich set of enhancements to the standard Collections framework Google&#8217;s implementation has two big advantages:</p>
<ul>
<li>Based on Java 1.5 and hence using Generics</li>
<li>More standard compliant (Commons Collections occasionally break standard behavior)</li>
</ul>
<p>Go to their <a title="Google Collections" href="http://code.google.com/p/google-collections/" target="_blank">website</a> and make sure to read the <a title="Google Collections FAQ" href="http://code.google.com/p/google-collections/wiki/Faq" target="_blank">FAQ</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/01/04/google-collections-library-1-0-available/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Closures in Java</title>
		<link>http://www.publicstaticfinal.de/2009/12/14/closures-in-java/</link>
		<comments>http://www.publicstaticfinal.de/2009/12/14/closures-in-java/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 10:37:27 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Closures]]></category>
		<category><![CDATA[JSE7]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=117</guid>
		<description><![CDATA[One of the most discussed (and most controversial) topics in the last years concerning the future of Java was the introduction of Closures. After 2008&#8242;s Devoxx Closures were off the Java7 release. One year later at the same event Mark Reinhold revoked it and announced that Closures will be in Java7 (and hence delaying Java [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most discussed (and most controversial) topics in the last years concerning the future of Java was the introduction of <a title="Wikipedia: Closures" href="http://en.wikipedia.org/wiki/Closure_(computer_science)" target="_blank">Closures</a>. After 2008&#8242;s Devoxx Closures were off the Java7 release. One year later at the same event Mark Reinhold revoked it and announced that Closures will be in Java7 (and hence <a title="Java7 will be delayed" href="http://mail.openjdk.java.net/pipermail/jdk7-dev/2009-November/001054.html" target="_blank">delaying Java 7 for a few months</a>).<span id="more-117"></span></p>
<p>For days ago, Reinhold published a<a href="http://cr.openjdk.java.net/~mr/lambda/straw-man/" target="_blank"> straw-man proposal</a> of how closures (or more formal: lambda expressions) could look like in Java. I&#8217;m still not sure whether I&#8217;m happy about closures in Java or not. On the one hand it definitely helps to reduce the number of lines of code since it drops redundant information (e.g. the decleration of a Runnable).</p>
<pre class="brush: java; title: ; notranslate">
Thread th = new Thread(new Runnable() {
                         public void run() {
                            doSomeStuff();
                            doMoreStuff();
                         }
                       });

Thread th = new Thread(#(){ doSomeStuff(); doMoreStuff(); } )
</pre>
<p>On the other hand it increases the complexity of the JVM itself and makes maintenance a bit harder. This will affect only few developers but it&#8217;s another stop to the grave of Java (death by non-maintainability).</p>
<p>I think time will tell how Closures will affect the Java world and if it&#8217;s a good idea to include it or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/12/14/closures-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Be aware: Constants get inlined at compile time</title>
		<link>http://www.publicstaticfinal.de/2009/10/13/be-aware-constants-get-inlined-at-compile-time/</link>
		<comments>http://www.publicstaticfinal.de/2009/10/13/be-aware-constants-get-inlined-at-compile-time/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 07:27:54 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Constants]]></category>
		<category><![CDATA[Memory]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=59</guid>
		<description><![CDATA[Most books tell you that new String(&#8220;value&#8221;) should not be used since it creates two objects although only one is needed. But what the books almost never tell you is that there is one case in which you should use it: Java constants aka &#8220;public static final&#8221;.The reason for this is that constants (Strings or [...]]]></description>
			<content:encoded><![CDATA[<p>Most books tell you that new String(&#8220;value&#8221;) should not be used since it creates two objects although only one is needed. But what the books almost never tell you is that there is one case in which you should use it: Java constants aka &#8220;public static final&#8221;.<span id="more-59"></span>The reason for this is that constants (Strings or primitive types) get inlined by the compiler at compile time. This means that the value of the constant is not resolved when the class is loaded but copied when the sources are compiled. If you&#8217;re not using new String(&#8230;) you have to recompile all of your code otherwise there&#8217;s no guarantee that you code runs as expected.</p>
<p>This gets even more important if your code is not used exclusivly by you but provided as a 3rd party lib. Dozens of programs might be broken after an updte just because a constant has been changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/10/13/be-aware-constants-get-inlined-at-compile-time/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Authentication via LDAP</title>
		<link>http://www.publicstaticfinal.de/2009/08/07/authentication-via-ldap/</link>
		<comments>http://www.publicstaticfinal.de/2009/08/07/authentication-via-ldap/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 11:09:52 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=48</guid>
		<description><![CDATA[Yesterday I was looking for a neat and clean solution for authenticating a user via LDAP. Most of the solutions I found read the user from LDAP and compared the credentials with each other. This might be useful if yohave to do other stuff with the User but just for authenticating its to bloated. So [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I was looking for a neat and clean solution for authenticating a user via LDAP. Most of the solutions I found read the user from LDAP and compared the credentials with each other. This might be useful if yohave to do other stuff with the User but just for authenticating its to bloated.</p>
<p><span id="more-48"></span>So I thought about it it came to my mind: Just connect to the LDAP server with the credentials provided and look if everything is fine:</p>
<pre class="brush: java; title: ; notranslate">
public boolean authenticate(final String username, final String passwd) {
  final Hashtable&lt;String, String&gt; env = new Hashtable&lt;String, String&gt;();
  env.put(Context.INITIAL_CONTEXT_FACTORY, &quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
  env.put(Context.PROVIDER_URL, &quot;ldap://myldap:383&quot;);
  env.put(Context.SECURITY_PRINCIPAL, &quot;CN=&quot; + username + &quot;,CN=Users,DC=Test&quot;);
  env.put(Context.SECURITY_CREDENTIALS, passwd);
  try {
    new InitialLdapContext(env, null).close();
  } catch (AuthenticationException ae) {
    return false;
  } catch (NamingException ne) {
    throw new RuntimeException(ne);
  }
  return true;
}
</pre>
<p>Et voila, user can be authenticated without much effort.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 27px; width: 1px; height: 1px;">[10:59:25] Daniel Munzinger: 1. Anwendung vom Server entfernen<br />
2. Server neustarten<br />
3. Clean Tomcat Work Directory<br />
4. Anwendung wieder auf den Server deployen</div>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/08/07/authentication-via-ldap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSR-303: Bean Validation</title>
		<link>http://www.publicstaticfinal.de/2009/05/29/jsr-303-bean-validation/</link>
		<comments>http://www.publicstaticfinal.de/2009/05/29/jsr-303-bean-validation/#comments</comments>
		<pubDate>Fri, 29 May 2009 07:14:12 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[bean]]></category>
		<category><![CDATA[jsr-303]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=17</guid>
		<description><![CDATA[In the last few days I created some JPA entities which are used in a Web Application and filled by the user. So what&#8217;s next? Input validation. I don&#8217;t like JSF&#8217;s f:validator tag since it displays only one error message which might be useful if you put the error message next to the input filed. [...]]]></description>
			<content:encoded><![CDATA[<p>In the last few days I created some JPA entities which are used in a Web Application and filled by the user. So what&#8217;s next? Input validation. I don&#8217;t like JSF&#8217;s f:validator tag since it displays only one error message which might be useful if you put the error message next to the input filed. But if you display all error messages bundled on the top of the main content the messages are non-sense for the user.<span id="more-17"></span></p>
<p>The next possible solution would be the validator attribute of a JSF component. While this is appropriate for small forms it creates a lot of overhead in big forms (assume you have 10 input fields which follow different constraints). Also it leaves a &#8220;Do Repeat Yourself&#8221; taste in the delevoper&#8217;s mouth. Okay, so what&#8217;s the one and only solution.</p>
<p>As always I don&#8217;t think there is any but one that comes pretty close: <a title="JSR-303" href="http://jcp.org/en/jsr/detail?id=303" target="_blank">JSR-303 aka Bean Validation</a>. Emerged from Hibernate Validation this JSR aims to simplify bean validation. The JSR is currently in its final proposal stage hence the final release 1.0.0 should be near.</p>
<p>Instead of writing you own code you annotate your beans and tell the Validator what to look for. @NotNull e.g. tells the Validator that the value must not be null. The Rest of the validation is very very very easy:</p>
<pre class="brush: java; title: ; notranslate">
final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
final Validator addressValidator = factory.getValidator();
final Set&gt; violations = addressValidator.validate(beanToValidate);
</pre>
<p>And with this set of violations you can display whatever error messages you want.</p>
<p>You can download it from the <a title="Hibernate Validator" href="http://sourceforge.net/project/showfiles.php?group_id=40712&amp;package_id=225206" target="_blank">Sourceforge page</a> of Hibernate Validator which also contains a very good &#8220;Getting started&#8221; guide which was (co-)written by <a href="http://musingsofaprogrammingaddict.blogspot.com/" target="_blank">Gunnar Morling</a>. At his blog you can find two good introductions, too (<a href="http://musingsofaprogrammingaddict.blogspot.com/2009/01/getting-started-with-jsr-303-beans.html" target="_blank">Part1</a> and <a href="http://musingsofaprogrammingaddict.blogspot.com/2009/02/getting-started-with-jsr-303-bean.html" target="_blank">Part2</a>). For further information just follow the blog of <a href="http://in.relation.to/" target="_blank">Gavin King et. al</a>.</p>
<p>Or if you use Maven you can download it from JBoss&#8217; Maven repository:</p>
<p><a href="http://repository.jboss.com/maven2/org/hibernate/hibernate-validator/" target="_blank">Hibernate Validator</a><br />
<a href="http://repository.jboss.com/maven2/javax/validation/validation-api/" target="_blank">Bean Validation API</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/05/29/jsr-303-bean-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

