<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>GuruBlog : Articles about java</title>
    <description>local-guru.net</description>
    <link>http://www.local-guru.net/blog</link>
    <ttl>40</ttl>
    <item>
      <title>File Upload in Servlet 3.0</title>
      <link>http://www.local-guru.net//blog/2010/9/1/file-upload-in-servlet-3-0</link>
      <description>&lt;p&gt;I played around a bit with the new servlet-api 3.0 and tomcat7 and i really like it.
it has some nice features like servlet declaration using annotations or the support for
asynchornous requesthandling, but the feature that i really really love is the
support for file upload.&lt;/p&gt;

&lt;p&gt;To handle a file upload form like this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;form action="upload" enctype="multipart/form-data" method="post"&amp;gt;
&amp;lt;input type="file" name="filename"/&amp;gt;
&amp;lt;input type="submit" value="submit"/&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is to mark the servlet with an annotation and access the uploaded file using 'request.getPart("filename")' like this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package at.hpc.servlettest;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet(name="testUpload", urlPatterns={"/upload"})
@MultipartConfig
public class TestUpload extends HttpServlet {
    protected void doPost( HttpServletRequest req, HttpServletResponse res ) 
               throws ServletException, IOException {
        Part part = req.getPart("filename");

        res.setContentType( part.getContentType());
        res.setHeader( "disposition", "inline" );

        OutputStream out = res.getOutputStream();
        InputStream in = part.getInputStream();

        byte buffer[] = new byte[ 4048 ];
        int n = 0;
        while ((n = in.read( buffer )) &amp;gt; 0) {
            out.write( buffer, 0, n );
        }
        out.close();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a really silly example that just copies the uploaded file back to the browser, but it shows how clean and simple
handling fileuploads gets with this api - this is the feature i have been waiting for years!&lt;/p&gt;
</description>
      <pubDate>2010-09-01T19:35:00+02:00</pubDate>
    </item>
    <item>
      <title>OutOfIdeasException</title>
      <link>http://www.local-guru.net//blog/2010/6/22/outofideasexception</link>
      <description>&lt;p&gt;hmmmmmm - todays project for &lt;a href="http://www.30daysofcreativity.com"&gt;30daysofcreativity&lt;/a&gt; is...&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;guru.OutOfIdeasException()
    at guru.bodyparts.head.Brain.think()
    at.guru.bodyparts.head.Head()
    at.guru.body.Main().run()
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>2010-06-22T20:46:00+02:00</pubDate>
    </item>
    <item>
      <title>Deploying Processing apps using WebStart</title>
      <link>http://www.local-guru.net//blog/2010/1/11/deploying-processing-apps-using-webstart</link>
      <description>&lt;p&gt;To deploy a processing sketch via java webstart export the sketch as an application (i used my &lt;a href="http://www.local-guru.net/blog/2009/12/15/random-lines-in-processing"&gt;randomlines&lt;/a&gt; sketch and exported it as a linux
application).&lt;/p&gt;

&lt;p&gt;then i wrote a simple jnlp file and copied the files to my webserver. The only thing thats left is providing a
link to the jnlp file like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.local-guru.net/jnlp/randomlines/test.jnlp"&gt;click here to dowload and launch the application&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More info on jnlp and webstart can be found
&lt;a href="http://java.sun.com/docs/books/tutorial/deployment/webstart/deploying.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;test.jnlp&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;jnlp spec="1.0+" codebase="" href=""&amp;gt;
    &amp;lt;information&amp;gt;
        &amp;lt;title&amp;gt;Processing Test&amp;lt;/title&amp;gt;
        &amp;lt;vendor&amp;gt;guru&amp;lt;/vendor&amp;gt;
        &amp;lt;icon href="http://www.local-guru.net/jnlp/randomlines/randomlines.png"/&amp;gt;
        &amp;lt;desktop/&amp;gt;
        &amp;lt;offline-allowed/&amp;gt;
        &amp;lt;shortcut online="false"&amp;gt;
              &amp;lt;desktop/&amp;gt;
        &amp;lt;/shortcut&amp;gt;
    &amp;lt;/information&amp;gt;
    &amp;lt;resources&amp;gt;
        &amp;lt;j2se version="1.5+" href="http://java.sun.com/products/autodl/j2se"/&amp;gt;
        &amp;lt;jar href="http://www.local-guru.net/jnlp/randomlines/lib/randomlines.jar" main="true" /&amp;gt;
        &amp;lt;jar href="http://www.local-guru.net/jnlp/randomlines/lib/core.jar" main="true" /&amp;gt;
    &amp;lt;/resources&amp;gt;
    &amp;lt;application-desc
         name="ProcessinTestApp"
         main-class="randomlines"
         width="300"
         height="300"&amp;gt;
     &amp;lt;/application-desc&amp;gt;
     &amp;lt;update check="background"/&amp;gt;
&amp;lt;/jnlp&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>2010-01-11T11:22:00+01:00</pubDate>
    </item>
    <item>
      <title>Handassembled java bytecode</title>
      <link>http://www.local-guru.net//blog/2009/3/3/handassembled-java-bytecode</link>
      <description>&lt;p&gt;motivated by my &lt;a href="http://www.local-guru.net/blog/2009/02/25/machine-language-hello-world-in-120-bytes"&gt;hand written x86 machinecode&lt;/a&gt;, i decided
that i had to take it to the next level.&lt;/p&gt;

&lt;p&gt;i worte some handassembled java bytecode. run the file below as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./dump.sh &amp;gt; HelloAsm 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and run the programm with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;java HelloAsm
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>2009-03-03T04:25:00+01:00</pubDate>
    </item>
    <item>
      <title>switching java-versions in ubuntu</title>
      <link>http://www.local-guru.net//blog/2009/1/23/switching-java-versions-in-ubuntu</link>
      <description>&lt;p&gt;to switch between 2 installed versions of the jdk in ubuntu 2 commands are needed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo update-alternatives --config java
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is used to choose the java version&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo update-alternatives --config javac
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is used to change the javac version&lt;/p&gt;
</description>
      <pubDate>2009-01-23T15:19:00+01:00</pubDate>
    </item>
    <item>
      <title>Javadeus08</title>
      <link>http://www.local-guru.net//blog/2008/6/20/javadeus08</link>
      <description>&lt;p&gt;Yesterday i attended to the &lt;a href="http://at.sun.com/sunnews/events/2008/jun/javadeus08/"&gt;javadeus08&lt;/a&gt;, the first java-conference by sun
in austria.&lt;/p&gt;

&lt;p&gt;Most of the sessions were really interesting, and i definitly have to give &lt;a href="http://jmaki.com/"&gt;jmaki&lt;/a&gt; a try :-)&lt;/p&gt;

&lt;p&gt;The only thing, that annoyed me, is that there was no free wlan and that even the speakers had a veeeeeeeeery slow
connection for their demos&lt;/p&gt;
</description>
      <pubDate>2008-06-20T05:44:00+02:00</pubDate>
    </item>
    <item>
      <title>simple Java Set/Map initialization</title>
      <link>http://www.local-guru.net//blog/2008/4/20/simple-java-set-map-initialization</link>
      <description>&lt;p&gt;When you need a array in java filled with some data, initializing it is easy&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;String[] bla = new String[] { "Foo", "Bar", "Bla" };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and done.
but when you need a Map or a Set prefilled with some data it starts getting complicated.
you can either use a static initializer block like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Bla {
    static Set&amp;lt;String&amp;gt; dings;
    static {
        dings = new HashSet&amp;lt;String&amp;gt;();
        dings.add("foo");
        dings.add("bla");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or you fill your set or map in the contructor if it is still null (be carefull if you use this in a multithreaded application)&lt;/p&gt;

&lt;p&gt;another way i have learnd at the last jax is using this code&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Bla {
    static Set&amp;lt;String&amp;gt; dings = new HashSet&amp;lt;String&amp;gt;() {{ 
        add("Foo"); add("Bar"); add("Bla"); }};
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;neat isn't it?&lt;/p&gt;

&lt;p&gt;this also works with a Map&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Bla {
    static Map&amp;lt;String,String&amp;gt; dings = new HashMap&amp;lt;String,String&amp;gt;() {{ 
        put("k1", "Foo"); put("k2", "Bar"); put("k2", "Bla"); }};
}
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>2008-04-20T07:55:00+02:00</pubDate>
    </item>
    <item>
      <title>Using "functional"-Programming to map Resultsets to POJOs</title>
      <link>http://www.local-guru.net//blog/2007/9/26/using-funktional-programming-to-map-resultsets-to-pojos</link>
      <description>&lt;p&gt;When I use jdbc to develop a small tool, where using a OR-Mapper
like Hibernate is overkill, I always dislike that i have to dublicate
all the "getStatement" "execQuery" "while (resultSet.next())" stuff.&lt;/p&gt;

&lt;p&gt;i dont like to repeat stuff in my code. It would be very easy if
java had closures, but i dont want to wait until java7 to simplify
my code so I needet another idea.&lt;/p&gt;

&lt;p&gt;java has no closures but it has something that can be used in a similar
manner - Anonymous inner classes.&lt;/p&gt;

&lt;p&gt;so to simplify the following class&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Connection con = getConnection()
Statement st = con.createStatement();
ResultSet rs = st.executeQuery( "sqlstring");
List&amp;lt;Foo&amp;gt; result = new ArrayList&amp;lt;Foo&amp;gt;();
while (rs.next()) {
    Foo f = new Foo();
    f.setProperty1( rs.getString( "property1" ));
    f.setProperty2( rs.getString( "property2" ));
    result.add( f );
}
// imagine catch/finaly block here
st.close();
con.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;i define a interface called Function&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;interface Function&amp;lt;T&amp;gt; {
    public T valueOf( ResultSet rs ) throws SQLException;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then i define a QueryMapper like this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class QueryMapper&amp;lt;T&amp;gt; {
    private Database db;
    public QueryMapper( Database db ) {
        this.db = db;
    }

    List&amp;lt;T&amp;gt; listOf( Function&amp;lt;T&amp;gt; f, String query ) {
        List&amp;lt;T&amp;gt; res = new ArrayList&amp;lt;T&amp;gt;();
        Resultset rs = ... get resultset for query ..
        while ( rs.next() ) {
            res.add( f.valueOf( rs ));
        }
        // catch/finaly block here
        return res;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So all i have to do in my code now is calling&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Function&amp;lt;Foo&amp;gt; fn = new Function&amp;lt;Foo&amp;gt;() {
    public Foo valueOf( ResultSet rs) throws SQLException {
        Foo res = new Foo();
        res.setPropery1( rs.getString( "property1" ));
        res.setPropery2( rs.getString( "property2" ));
        return res;
    }
}

QueryMapper&amp;lt;Foo&amp;gt; qm = QueryMapper&amp;lt;Foo&amp;gt;( db );
List&amp;lt;Foo&amp;gt; res = qm.listOf( fn, "select * from foo");
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I can even recycle the Function objects ( if i query different object of the same type but with different ids for example )
thats more how i like it&lt;/p&gt;
</description>
      <pubDate>2007-09-26T20:40:00+02:00</pubDate>
    </item>
    <item>
      <title>Jax2007</title>
      <link>http://www.local-guru.net//blog/2007/4/26/jax2007</link>
      <description>&lt;p&gt;Im currently sitting in Wiesbaden at the Jax2007 (missing a keynote from SAP :-) )
i've seen a lot of new interessting tools and techniques which will keep me busy some
times.&lt;/p&gt;

&lt;p&gt;webtest from canoo, for example has evolved in a very interesting way since i gave it
the last try,&lt;/p&gt;

&lt;p&gt;one thing that many of the speakers are showing ist the tendency to use more languages
on the JVM and choose the right tool for the problem. and not let the language shape the
solution.&lt;/p&gt;

&lt;p&gt;there were some nice sessions showning how to generate textual DSL with ruby or
open architecture ware.&lt;/p&gt;

&lt;p&gt;this time there were some netbeans sessions where new features from the upcoming
netbeans 6 release where shown - realy nice&lt;/p&gt;

&lt;p&gt;and i heard abount ApacheDS a ldap server which is completely written in java
and can be embedded into other applications.&lt;/p&gt;

&lt;p&gt;ok the sap keynote is comming to an end - i will go to the next session now ...&lt;/p&gt;
</description>
      <pubDate>2007-04-26T11:25:00+02:00</pubDate>
    </item>
    <item>
      <title>the Future and Java 1.5</title>
      <link>http://www.local-guru.net//blog/2007/1/21/the-future-and-java-1-5</link>
      <description>&lt;p&gt;Java 1.5 introduced a bunch of new classes for handling multithreaded programming.
The one I realy like the most is Future.&lt;/p&gt;

&lt;p&gt;With future its possible to start the calculation of something you know you will need
at a later point in yout programm. When you reach that point you can call &lt;code&gt;Future.get()&lt;/code&gt;
to fetch the result of the calculation. If it is already avaliable the result is returned
immediately, if the calculation is still running or has not started yet &lt;code&gt;Future.get()&lt;/code&gt;
blocks until the results are here.&lt;/p&gt;

&lt;p&gt;one gets a future by submitting an object implementing Runnable or Callable to an ExecutorService.
This is an interface that is implemented by ThreadPoolExecutor.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ExecutorService e = Executors.newSingleThreadExecutor();
Future&amp;lt;String&amp;gt; future = e.submit(callable);
...
String result = future.get();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This Class implements a queue and a threadpool. using ExecutorService.submit() you can put some
Workload in an queue, from where the threads of the pool will fetch and execute them. the submit method returns an Future which can be used to fetch the results of the workload.&lt;/p&gt;

&lt;p&gt;There are various preconfigured Threadpools available from Executors Class. For example&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ExecutorService e = Executors.newSingleThreadExecutor(); 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;creates a degenerated Threadpool using just one thread. This is very usefull if one has to garantee
that no two instances of the submitted callable are executed at the same time. If you have to use an external service that is implemented by offering an api via webservice thats not threadsave beacause of a missing transaction handling, this SingleThreadExecutor can be used to serialize the requests comming from a webapplication.
All the clientrequests can be spooled in the executor service but all the calls to the service api are strictly searialized.&lt;/p&gt;

&lt;p&gt;Another very usefull class is&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ExecutorService e = Executors.newCachedThreadPool()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;this is what most people think of when they refere to a threadpool. The class generatets new
threads if needed but reuses existing ones if they do idle. Threads that are not used for 60 seconds are removed from the pool.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ExecutorService e = Executors.newFixedThreadPool(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;creates a Threadpool containing n threads. No new threads are generated an there also is no timeout.
SingleThreadExecutor is an fixedThreadPool with  n = 1.&lt;/p&gt;

&lt;p&gt;I think the java.util.concurrent package is the one of the best things that got introduced with java 1.5.&lt;/p&gt;
</description>
      <pubDate>2007-01-21T11:09:00+01:00</pubDate>
    </item>
  </channel>
</rss>

