<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>CloudMe Blog</title>
	
	<link>http://blog.cloudme.org</link>
	<description>Blog of the CloudMe Project by Moritz Petersen</description>
	<lastBuildDate>Sun, 27 Jun 2010 07:38:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/cloudme-blog" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="cloudme-blog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Optimizing for GAE: Objectify and Guice</title>
		<link>http://blog.cloudme.org/2010/06/optimizing-for-gae-objectify-and-guice/</link>
		<comments>http://blog.cloudme.org/2010/06/optimizing-for-gae-objectify-and-guice/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 07:38:30 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdo]]></category>
		<category><![CDATA[objectify]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=132</guid>
		<description><![CDATA[When I started designing my webgallery application, I chose standard frameworks in order to keep the application (potentially) portable with minimal refactoring effort. I was not totally focused on Google App Engine, instead I was ignoring typical characteristics of its architecture. That is a totally valid approach and has on its positive side the advantage, [...]]]></description>
			<content:encoded><![CDATA[<p>When I started designing my <a href="http://photos.moritzpetersen.de">webgallery</a> application, I chose standard frameworks in order to keep the application (potentially) portable with minimal refactoring effort. I was not totally focused on <a href="http://code.google.com/appengine/">Google App Engine</a>, instead I was ignoring typical characteristics of its architecture. That is a totally valid approach and has on its positive side the advantage, that you can almost completely write normal Java applications that run on GAE (except some standard classes cannot be used). However, a big disadvantage of this approach is, that the application cold start time was uncomfortably long, as a lot of libraries have to be loaded before the application starts.</p>
<p>Therefore, I removed all references to the <a href="http://www.springsource.org/">Spring framework</a> and the <a href="http://www.datanucleus.org/">Datanucleus JDO</a> implementation. The migration started on the persistence layer. Luckily I added a DAO layer to my design, so all I needed was changing annotations in the model classes and writing new DAO implementations (interfaces remained the same). When using Spring I made use of the &#8220;JdoSupport&#8221; classes, which provide convenience methods for using transactions etc. As I don&#8217;t use the full scope in my application, it was easy to implement the part that I actually needed in my own framework.</p>
<p>Then I removed all references to the Spring framework. My initial thought was that I just get rid of DI at all until I find a better solution, but that appeared to be not practical and testing started to get annoying. Therefore I decided to use <a href="http://code.google.com/p/google-guice/">Guice</a>. Migration was pretty simple. I chose the approach to use member injection, so only @Inject annotations to the member classes are required. As Guice doesn&#8217;t use XML configuration, a Module is required, that contains the configuration. Sounds a bit strange at first glance, but it turned out to be very comfortable and actually very type safe (as it is configuration in a Java class.<br />
Next part was integration in <a href="http://www.stripesframework.org/">Stripes</a>: I was looking for some available implementations but quickly found out that integrating it into my application was just a matter of a very small class &#8211; all I needed was dependency injection in my ActionBeans. So I decided to write that integration by my own:</p>
<pre class="chili"><code class="Java""">
@Intercepts( { LifecycleStage.ActionBeanResolution } )
public class GuiceInterceptor implements Interceptor {
    private Injector injector;

    public GuiceInterceptor() {
        injector = Guice.createInjector(new WebgalleryModule());
    }

    @Override
    public Resolution intercept(ExecutionContext context) throws Exception {
        Resolution resolution = context.proceed();
        injector.injectMembers(context.getActionBean());
        return resolution;
    }
}
</code></pre>
<p>The point is to write a <a href="http://www.stripesframework.org/display/stripes/Intercept+Execution">Stripes interceptor</a> that intercepts the lifecycle stage of ActionBean resolution &#8211; therefore the annotation of the class. The interceptor first creates the Guice injector in the constructor &#8211; with referencing to my module (that&#8217;s the benefit of using a custom implementation &#8211; I don&#8217;t have to care about too much configuration). The main part is the intercept() method, which lets the interceptor inject the members in the ActionBean.<br />
The GuiceInterceptor just has to be placed into the extension package of my application in order to get recognized by Stripes (since version 1.5). That was already configured in the web.xml deployment descriptor:</p>
<pre class="chili"><code class="xml""">
    &lt;init-param&gt;
      &lt;param-name&gt;Extension.Packages&lt;/param-name&gt;
      &lt;param-value&gt;org.cloudme.webgallery.stripes.extensions&lt;/param-value&gt;
    &lt;/init-param&gt;
</code></pre>
<p>The application runs overall faster now and uses a lot less external libraries than before. I can recommend everyone who implements for Google App Engine to try to reduce the application size as much as possible by removing unnecessary complex dependencies. With the right application design, switching between technologies can be very easy. My initial concern that, when using very GAE specific technologies (such as Objectify), I get stuck in that platform and can never escape, is irrelevant, as migration to a different technology can be very simple. Just ensure you have sufficient test cases.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/06/optimizing-for-gae-objectify-and-guice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating iPhone webapps with Vaadin Touchkit</title>
		<link>http://blog.cloudme.org/2010/04/creating-iphone-webapps-with-vaadin-touchkit/</link>
		<comments>http://blog.cloudme.org/2010/04/creating-iphone-webapps-with-vaadin-touchkit/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 20:21:00 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[touchkit]]></category>
		<category><![CDATA[vaadin]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=106</guid>
		<description><![CDATA[In this post I&#8217;d like to write down my experiences (so far) with creating iPhone webapps with Vaadin TouchKit. For those of you who are not familiar with TouchKit: TouchKit is a tool kit that lets you develop applications that look and feel like native iPhone applications using only Vaadin. That means in plain English: [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;d like to write down my experiences (so far) with creating <a href="http://www.apple.com/iphone/">iPhone</a> webapps with <a href="http://vaadin.com/home">Vaadin</a> <a href="http://vaadin.com/wiki/-/wiki/Main/Vaadin%20TouchKit%20-%20Create%20iPhone%20applications%20using%20Vaadin">TouchKit</a>. For those of you who are not familiar with TouchKit:</p>
<blockquote><p>TouchKit is a tool kit that lets you develop applications that look and feel like native iPhone applications using only Vaadin.</p></blockquote>
<p>That means in plain English: Creating iPhone webapps in pure Java with a <a href="http://code.google.com/webtoolkit/">GWT</a>-based, elegant web framework and no HTML, JavaScript or other technology required.</p>
<p>Vaadin provides an <a href="http://www.eclipse.org/">Eclipse</a> <a href="http://vaadin.com/eclipse">plugin</a>, which is the recommended way of developing a Vaadin application, but that would only be half the fun. I want to go two steps further:</p>
<ul>
<li>Use <a href="http://maven.apache.org">Maven</a> as build / project management tool &#8211; but still using Eclipse as IDE.</li>
<li>Use Google <a href="http://code.google.com/appengine/">Appengine</a> as hosting environment.</li>
</ul>
<p>I wrote already about <a href="http://blog.cloudme.org/2010/04/mavenizing-my-project/">mavenizing a Google Appengine project</a>.</p>
<p><strong>Create a Vaadin Maven project</strong></p>
<ol>
<li>Vaadin already has a good <a href="http://vaadin.com/wiki/-/wiki/Main/Using%20Vaadin%20with%20Maven">Maven plugin</a>. For TouchKit it is required to have the option to compile the Vaadin widgetset. Therefore we use the Maven archetype to create a project which includes the GWT plugin already:
<pre class="chili"><code class="xml""""""""">
mvn archetype:generate \
-DarchetypeGroupId=com.vaadin \
-DarchetypeArtifactId=vaadin-archetype-sample \
-DarchetypeVersion=LATEST \
-DgroupId=your.company \
-DartifactId=project-name \
-Dversion=1.0.0 \
-Dpackaging=war
</code></pre>
</li>
<li>Optional: run the application with <code>mvn jetty:run</code>. You can access the application at localhost:8080/project-name</li>
<li>Optional: Create the Eclipse project files to import the project in Eclipse with <code>mvn eclipse:eclipse</code>. Of course this is not required, and instead of Eclipse you can use your IDE of choice.</li>
<li>The created pom.xml file doesn&#8217;t reference the latest versions of Vaadin and GWT, unfortunately. For TouchKit at least Vaadin 6.3 is required. Therefore change Vaadin to version 6.3.0 and GWT to version 2.0.3:
<pre class="chili"><code class="xml""""""""">
&lt;dependency&gt;
&lt;groupId&gt;com.vaadin&lt;/groupId&gt;
&lt;artifactId&gt;vaadin&lt;/artifactId&gt;
&lt;version&gt;6.3.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- This is also used by gwt-maven-plugin to deduce GWT version number. --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.gwt&lt;/groupId&gt;
&lt;artifactId&gt;gwt-user&lt;/artifactId&gt;
&lt;version&gt;2.0.3&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
</code></pre>
</li>
</ol>
<p><strong>Add the TouchKit widgetset</strong></p>
<ol>
<li>Add the TouchKit dependency to the classpath:
<pre class="chili"><code class="xml""""""""">
&lt;dependency&gt;
&lt;groupId&gt;org.vaadin&lt;/groupId&gt;
&lt;artifactId&gt;vaadin-touchkit&lt;/artifactId&gt;
&lt;version&gt;0.5&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li>Unfortunately, TouchKit is not available in a public Maven repository. You have two choices now: either add the TouchKit jar to your local repository using <code><a href="http://maven.apache.org/plugins/maven-install-plugin/usage.html">mvn install:install-file</a></code> or you put it into your own remote repository. The advantage of the second option is obvious: you don&#8217;t need to install the file on all your development machines locally. Add your remote repository to pom.xml:
<pre class="chili"><code class="xml""""""""">
&lt;repository&gt;
&lt;id&gt;cloudme&lt;/id&gt;
&lt;url&gt;http://cloudme.googlecode.com/svn/maven&lt;/url&gt;
&lt;/repository&gt;
</code></pre>
</li>
<li>Update web.xml file and change the servlet class to <code>org.vaadin.touchkit.mobileapplication.MobileApplicationServlet</code>, as <a href="http://vaadin.com/wiki/-/wiki/Main/Vaadin%20TouchKit%20-%20Create%20iPhone%20applications%20using%20Vaadin">described here</a>.</li>
<li style="text-align: left;">Unfortunately, there is a <a href="http://jira.codehaus.org/browse/MGWT-147">bug</a> in the current gwt-maven-plugin, and therefore a workaround is required to use the TouchKit widgetset: create your own widgetset which inherits TouchKit. It is important to set the entry point, otherwise the Maven plugin will not compile it correctly:
<pre class="chili"><code class="xml""""""""">
&lt;module&gt;
&lt;entry-point class=&quot;com.vaadin.terminal.gwt.client.DefaultWidgetSet&quot; /&gt;
&lt;inherits name=&quot;com.vaadin.terminal.gwt.DefaultWidgetSet&quot; /&gt;
&lt;inherits name=&quot;org.vaadin.touchkit.widgetset.TouchKitWidgetset&quot; /&gt;
&lt;/module&gt;
</code></pre>
</li>
<li style="text-align: left;">Change the widgetset in web.xml:
<pre class="chili"><code class="xml""""""""">
&lt;init-param&gt;
&lt;param-name&gt;widgetset&lt;/param-name&gt;
&lt;param-value&gt;com.example.gwt.MyWidgetset&lt;/param-value&gt;
&lt;/init-param&gt;
</code></pre>
</li>
</ol>
<p>Now create a simple application using TouchKit widgets as described <a href="http://vaadin.com/wiki/-/wiki/Main/Vaadin%20TouchKit%20-%20Create%20iPhone%20applications%20using%20Vaadin">here</a>, update the application parameter in web.xml and perform a clean build: <code>mvn gwt:clean jetty:run</code></p>
<p><strong>Enable Google Appengine</strong></p>
<p>Please note that the following steps describe only the basic, most necessary steps required to run the TouchKit application in Google Appengine.</p>
<ol>
<li>Add the GAE version to the properties section of the pom.xml:
<pre class="chili"><code class="xml""""""">
&lt;gae.version&gt;1.3.2&lt;/gae.version&gt;
</code></pre>
</li>
<li>Add required plugins:
<pre class="chili"><code class="xml""""""">
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
&lt;version&gt;2.1-beta-1&lt;/version&gt;
&lt;configuration&gt;
&lt;webResources&gt;
&lt;resource&gt;
&lt;directory&gt;src/main/webapp&lt;/directory&gt;
&lt;filtering&gt;true&lt;/filtering&gt;
&lt;includes&gt;
&lt;include&gt;**/appengine-web.xml&lt;/include&gt;
&lt;/includes&gt;
&lt;/resource&gt;
&lt;/webResources&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;!--
The actual maven-gae-plugin. Type &quot;mvn gae:run&quot; to run project,
&quot;mvn gae:deploy&quot; to upload to GAE.
--&gt;
&lt;plugin&gt;
&lt;groupId&gt;net.kindleit&lt;/groupId&gt;
&lt;artifactId&gt;maven-gae-plugin&lt;/artifactId&gt;
&lt;version&gt;0.5.7&lt;/version&gt;
&lt;/plugin&gt;
&lt;!--
Upload application to the appspot automatically, during
release:perform
--&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-release-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;goals&gt;gae:deploy&lt;/goals&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
</code></pre>
</li>
<li>And, of course, the plugin repository:
<pre class="chili"><code class="xml""""""">
&lt;pluginRepository&gt;
&lt;id&gt;maven-gae-plugin-repo&lt;/id&gt;
&lt;name&gt;maven-gae-plugin repository&lt;/name&gt;
&lt;url&gt;http://maven-gae-plugin.googlecode.com/svn/repository&lt;/url&gt;
&lt;/pluginRepository&gt;
</code></pre>
</li>
<li>Create a <code>appengine-web.xml</code> file in WEB-INF:
<pre class="chili"><code class="xml""""">
&lt;appengine-web-app
xmlns=&quot;http://appengine.google.com/ns/1.0&quot;&gt;
&lt;application&gt;project-name&lt;/application&gt;
&lt;version&gt;1&lt;/version&gt;
&lt;/appengine-web-app&gt;
</code></pre>
</li>
</ol>
<p>Now you can run the application with <code>mvn gae:run</code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/04/creating-iphone-webapps-with-vaadin-touchkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mavenizing my project</title>
		<link>http://blog.cloudme.org/2010/04/mavenizing-my-project/</link>
		<comments>http://blog.cloudme.org/2010/04/mavenizing-my-project/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 07:38:17 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=99</guid>
		<description><![CDATA[I started working on a Google App Engine project the usual way: using the Eclipse plugin. However, unfortunately the update to the latest Eclipse plugin broke it and I haven&#8217;t found a fix yet. So I decided to try the maven-gae-plugin, once again; with Maven everything runs builds better anyway, right? So far I had [...]]]></description>
			<content:encoded><![CDATA[<p>I started working on a Google App Engine project the usual way: using the Eclipse plugin. However, unfortunately the update to the latest Eclipse plugin broke it and I haven&#8217;t found a fix yet. So I decided to try the <a href="http://code.google.com/p/maven-gae-plugin/">maven-gae-plugin</a>, once again; with Maven everything <del datetime="2010-04-06T06:56:00+00:00">runs</del> builds better anyway, right? So far I had only made some minor tests with the plugin.</p>
<h3>Setup</h3>
<p>For mavenizing the project I went the safe route: I created a new project with the maven archetype plugin:</p>
<pre class="chili"><code class="xml""">
mvn archetype:create\
 -DarchetypeGroupId=net.kindleit\
 -DarchetypeArtifactId=gae-archetype-gwt\
 -DarchetypeVersion=0.5.0\
 -DgroupId=your.groupId\
 -DartifactId=your-artifactId\
 -DremoteRepositories=http://maven-gae-plugin.googlecode.com/svn/repository
</code></pre>
<p>The project itself was only temporary, I was interested in the pom.xml file. I updated that file accordingly (e.g. the plugin version of the file was not the latest), removed GWT sections as I don&#8217;t use GWT in my project and changed some minor settings and added all required dependencies. Finally I copied the pom.xml into my project directory.</p>
<h3>Subversion trouble</h3>
<p>Then I made a lot of modifications within Eclipse &#8211; moved sources to standard Maven locations, removed JAR files etc. and was about to commit everything &#8211; but unfortunately Subversion detected a bunch of tree conflicts. Bummer. While resolving these conflicts seem to be hard, I decided to check out the project into another directory and start changing the structure from scratch &#8211; this time not using Eclipse but Subversion command line tools. That worked perfectly.</p>
<h3>Running</h3>
<p>With running <code>mvn gae:run</code> I started the development server. Startup was really smooth. However, by default the server is started on localhost only, but in my project I need a specific IP address as I need to access the server from the iPhone, too. Therefore I had to set the <code>gae.address</code> property. Of course it can be defined in pom.xml, but then it is the same for all development machines. I don&#8217;t want that. Therefore it must be defined in the users&#8217;s ~/.m2/settings.xml:</p>
<pre class="chili"><code class="xml""">
  &lt;profiles&gt;
    &lt;profile&gt;
      &lt;id&gt;gae&lt;/id&gt;
      &lt;properties&gt;
        &lt;gae.address&gt;192.168.178.24&lt;/gae.address&gt;
      &lt;/properties&gt;
    &lt;/profile&gt;
  &lt;/profiles&gt;
  &lt;activeProfiles&gt;
    &lt;activeProfile&gt;gae&lt;/activeProfile&gt;
  &lt;/activeProfiles&gt;
</code></pre>
<p>Now the server runs on the right IP address.</p>
<h3>Debugging</h3>
<p>Now with using Maven I did not want to get rid of the ability to debug my application. How can this be done? Easy. First of all, the plugin provides the <code>mvn gae:debug</code> goal. When running this goal, the development server starts in debug mode.</p>
<p>In Eclipse a new <del datetime="2010-04-06T07:04:59+00:00">run</del> debug configuration has to be created.</p>
<ol>
<li>In the Console run <code>mvn gae:debug</code>; Maven will compile, execute tests and start the development server in debug mode.</li>
<li>Wait until the server waits for the remote debugger; you will see the following output: &#8220;Listening for transport dt_socket at address: 8000&#8243;. The address is in this case the port, which needs to be set in the Eclipse debug configurations.</li>
<li>In Eclipse go to Run > Debug Configurations &#8230;</li>
<li>Create a new &#8220;Remote Java Application&#8221; configuration</li>
<li>Give it a good name, choose Connection Type &#8220;Standard (Socket Attach)&#8221; and set the Connection Properties (in my case Host: 192.168.178.24, Port: 8000)</li>
<li>Click on &#8220;Debug&#8221;. Now you see that the development server in the console continues.</li>
</ol>
<h3>Reloading webpages</h3>
<p>Now everything works as good as when using the Eclipse plugin, right? Not quite. There is one thing that doesn&#8217;t work: dynamic reload of webpages, such as JSP or CSS files. Jetty allows dynamic reload, but when using Maven, Jetty does not use the src/main/webapp folder as working directory; instead it uses its own directory somewhere in target/&#8230;</p>
<p>To avoid long edit / build / deploy / run cycles, the recommended way at the moment is to run <code>mvn gae:run</code> in one console window and <code>mvn cli:execute</code> in another window. The command line interface allows you to quickly execute Maven goals. Run <code>compile war</code> to update the webpages in the development server&#8217;s working directory.</p>
<p>While this is not quite as simple as with Eclipse, it is a workaround that speeds up the development process significantly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/04/mavenizing-my-project/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery dynamic height of an element</title>
		<link>http://blog.cloudme.org/2010/03/jquery-dynamic-height-of-an-element/</link>
		<comments>http://blog.cloudme.org/2010/03/jquery-dynamic-height-of-an-element/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 17:38:41 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=88</guid>
		<description><![CDATA[This article describes how to set the height of an element dynamically based on the children of the element. Normally, you would use CSS to set the height of an element, and you would expect that the element will automatically resize based on the content of the element. However, if the element&#8217;s height should automatically [...]]]></description>
			<content:encoded><![CDATA[<p>This article describes how to set the height of an element dynamically based on the children of the element.</p>
<p>Normally, you would use CSS to set the height of an element, and you would expect that the element will automatically resize based on the content of the element. However, if the element&#8217;s height should automatically adapt to the height of one or many elements outside of this element, the dynaheight jQuery plugin might be handy.</p>
<p>Here is simple jQuery code to achieve that easily:</p>
<pre class="chili"><code class="js""""""""">
$.fn.dynaHeight = function(elements) {
  $(this).each(function() {
	var top = $(this).offset().top;
	var maxBottom = 0;
	$(elements).each(function() {
	  var pos = $(this).offset();
	  var height = $(this).outerHeight();
	  var bottom = pos.top + height;
	  if (bottom &gt; maxBottom) {
	    maxBottom = bottom;
	  }
	});
    $(this).css({&quot;height&quot;: (maxBottom - top) + &quot;px&quot;});
  });
  return this;
};
</code></pre>
<p>Example:</p>
<blockquote><p>
$(element).dynaHeight(&#8220;#item&#8221;);
</p></blockquote>
<p>As argument you pass the selector for the element that are used for determining the height.</p>
<p>What is it doing?</p>
<p>This piece of code first captures the top coordinate and then iterates through all given elements to find the max bottom coordinate. Finally it changes the CSS attribute &#8220;height&#8221;.</p>
<p>Download <a href='http://blog.cloudme.org/wp-content/uploads/2010/03/dynaheight.js.zip'>dynaheight.js.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/03/jquery-dynamic-height-of-an-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cache strategies for Google App Engine</title>
		<link>http://blog.cloudme.org/2010/02/cache-strategies-for-google-app-engine/</link>
		<comments>http://blog.cloudme.org/2010/02/cache-strategies-for-google-app-engine/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 09:23:24 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=79</guid>
		<description><![CDATA[Google App Engine (GAE) provides distributed in-memory cache, called Memcache. Due to quite rigid quotas, it might be necessary to extensively use the cache. In my example, a web based photo gallery, a lot of image scaling is performed. For example, when an album is loaded, all images are shown as thumbnails. These thumbnails are [...]]]></description>
			<content:encoded><![CDATA[<p>Google App Engine (GAE) provides distributed in-memory cache, called <a title="GAE Docs - Memcache" href="http://code.google.com/appengine/docs/java/memcache/">Memcache</a>. Due to quite rigid quotas, it might be necessary to extensively use the cache.</p>
<p>In my example, a web based photo gallery, a lot of image scaling is performed. For example, when an album is loaded, all images are shown as thumbnails. These thumbnails are generated with the <a title="GAE Docs - Images API" href="http://code.google.com/appengine/docs/java/images/">Images service</a> and more sooner than later an <tt>OverQuotaException</tt> is thrown. Although a few minutes after an <tt>OverQuotaException</tt> image transformation can be resumed, it is still annoying for the user. My application catches the exception and shows a &#8220;sorry, over quota&#8221; default image.</p>
<p>In order to reduce the number of image transformations, all transformed data is put into the cache. For this I am using a very simple (custom) API consisting of a<tt> CacheProducer</tt>, which creates the data and a <tt>CacheService</tt>, which checks if the data is already in cache or needs to be created.</p>
<pre class="chili"><code class="java""">
public interface CacheProducer&lt;T&gt; {
    T produce();
}
public interface CacheService {
    &lt;T&gt; T cache(CacheProducer&lt;T&gt; producer, Serializable... params);
    void invalidate();
}
</code></pre>
<p>The <tt>Serializable... params</tt> parameter represents the components of the cache key.</p>
<p>Now let&#8217;s have a look at how to use this API. First we look at the PhotoDataService (which is used to get the actual image data from the datastore):</p>
<pre class="chili"><code class="java""">
public class PhotoDataService {
...
    public byte[] getPhotoData(final Long photoId, final ImageFormat format, final ContentType type) {
        return cacheService.cache(new CacheProducer&lt;byte[]&gt;() {
            public byte[] produce() {
                PhotoData photoData = photoDataRepository.find(photoId);
                byte[] input = photoData.getDataAsArray();
                return imageService.process(input, format, type);
            }
        }, photoId, format, type);
    }
...
}
</code></pre>
<p>For this example I removed all code that is not related to caching. What you don&#8217;t see here: the <tt>CacheService</tt> first looks into the cache whether the data has been cached already. If not, the <tt>produce()</tt> method is called to produce the data, the <tt>CacheService</tt> puts it into the cache and returns the data. With this approach, I don&#8217;t need to care about the actual cache mechanisms, and I reduce clutter in the actual business logic. The cache API is reusable and can be used for any other data and key parameters.</p>
<p>However, the first run still produces a lot of requests to the Images service and here <tt>OverQuotaExceptions</tt> are still thrown. Next I&#8217;ll explain how to use a background task to prepopulate the cache in order to reduce the exceptions even further.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/02/cache-strategies-for-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>First run Maven GAE Plugin</title>
		<link>http://blog.cloudme.org/2010/01/first-run-maven-gae-plugin/</link>
		<comments>http://blog.cloudme.org/2010/01/first-run-maven-gae-plugin/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 22:19:51 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=77</guid>
		<description><![CDATA[Until now I used the Eclipse plugin to develop and test Google App Engine applications. Today I tried the Maven GAE Plugin, in order to become more flexible and independent from Eclipse. My first observations were: As I started with the Maven GAE Archetype, the package declaration of the generated code did not match the [...]]]></description>
			<content:encoded><![CDATA[<p>Until now I used the Eclipse plugin to develop and test Google App Engine applications. Today I tried the <a href="http://code.google.com/p/maven-gae-plugin/">Maven GAE Plugin</a>, in order to become more flexible and independent from Eclipse. My first observations were:</p>
<ul>
<li>As I started with the Maven GAE Archetype, the package declaration of the generated code did not match the actual file location. That needed to get corrected first.</li>
<li>I changed the compiler settings from 1.6 (default settings of the archetype) to 1.5 and had to remove some @Override annotations.</li>
<li>The gae.home property is undefined (which makes sense), but I had to define it to run the development server.</li>
</ul>
<p>Overall, I&#8217;m quite pleased with the first run. Although Maven adds a lot of overhead, and the actual build process took a bit too long initially to download all dependencies, the overall impression is very good.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/01/first-run-maven-gae-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Multiple return values as class?</title>
		<link>http://blog.cloudme.org/2010/01/multiple-return-values-as-class/</link>
		<comments>http://blog.cloudme.org/2010/01/multiple-return-values-as-class/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 21:32:58 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=71</guid>
		<description><![CDATA[Recently I struggled again with the issue, that Java does not support multiple return values of a function. Usually, this is not a problem, but sometimes it is just not very elegant. In this case, I was writing a method that computes the bounds of a crop rectangle (in percent of the original image size). [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I struggled again with the issue, that Java does not support multiple return values of a function. Usually, this is not a problem, but sometimes it is just not very elegant.</p>
<p>In this case, I was writing a method that computes the bounds of a crop rectangle (in percent of the original image size). Please note, that I was not working in the java.awt.* context, thus I did not want to use Rectangle or any other AWT class to store the return value.</p>
<p>The most simple solution is to use an array.</p>
<p><code>
<pre>float[] bounds = computeCrop(...)</pre>
<p></code></p>
<p>While this works in this example, it becomes crude when the return values are not all of the same type. Additionally, you have to guess which index represents which coordinate (top? left? bottom? right? width? &#8230;).</p>
<p>An alternative is to use a dedicated value class, such as Rectangle:</p>
<p><code>
<pre>public class Rectangle {
    public float x;
    public float y;
    ...
}

Rectangle bounds = computeCrop(...);
</pre>
<p></code></p>
<p>However while implementing I thought that this approach could be optimized further. Why not let the class&#8217; constructor do the job of computing the values? This has the advantage, that the member fields can be final, and the code for computing the crop bounds is in one place. It would look like this:</p>
<p><code>
<pre>public class Crop {
    public final float x;
    public final float y;
    public final float width;
    public final float height;

    public Crop(float w1, float h1, float w2, float h2) {
        float r1 = w1 / h1;
        float r2 = w2 / h2;
        if (r1 < r2) {
            width = 1;
            height = r1 / r2;
        }
        else {
            width = r2 / r1;
            height = 1;
        }
        x = (1f - width) / 2;
        y = (1f - height) / 2;
    }
}</pre>
<p></code></p>
<p>Apart from coupling the logic tightly with the data (logic = constructor, data = member fields), is there any other drawback for using this approach?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2010/01/multiple-return-values-as-class/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>First Google App Engine application</title>
		<link>http://blog.cloudme.org/2009/12/first-ga-application/</link>
		<comments>http://blog.cloudme.org/2009/12/first-ga-application/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 17:06:49 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=66</guid>
		<description><![CDATA[As a coding exercise, I started writing a small application for the Google App Engine environment. It is a very simple web photo gallery, managing photos in albums. The main purpose of the application was to get my hands on some new frameworks, and also learn about deployment on the Google App Engine Environment. You [...]]]></description>
			<content:encoded><![CDATA[<p>As a coding exercise, I started writing a small application for the Google App Engine environment. It is a very simple web photo gallery, managing photos in albums. The main purpose of the application was to get my hands on some new frameworks, and also learn about deployment on the Google App Engine Environment.</p>
<p>You can look at the application here: <a href="http://photos.moritzpetersen.de">http://photos.moritzpetersen.de</a> and browse the source code <a href="http://code.google.com/p/cloudme/source/browse/webgallery/trunk/webgallery/">here</a>.</p>
<p>The application&#8217;s architecture is a simple 3-tier architecture:</p>
<ul>
<li>The repository tier is responsible for persistence. I chose JDO as ORM technology.</li>
<li>The service tier is responsible for handling business logic. I tried to encapsulate as much logic as possible here. However, there are some exceptions, but I wouldn&#8217;t consider it business logic. For some features I implemented JSP EL functions (e.g. showing the right copyright date at the footer of each page).</li>
<li>The web tier is manages the web requests and interaction with the forms. As web framework I have chosen Stripes, which was a good choice. From the Stripes framework, I used templating, validation and error handling and file upload.</li>
</ul>
<p>All layers are stitched together using Spring 3.0. Here I used mostly annotation based configuration of the container. I also used the JdoTemplate to easily implement the JDO repositories. Using Spring was a lot of fun, as it allowed me to implement some parts of the application very generic first: the service tier was completely generic for the first CRUD use cases, and I could easily extend the generic services later. Spring simply stitched together the right classes (emmitting a lot of warnings as I use(d) autowiring by type).</p>
<p>For security I used the GAE specific application security configured in the deployment descriptor. That caused the first problem when deploying the application to the App Engine: I had to use my Gmail user, not my Google Apps user for administration of the application &#8211; and I&#8217;m still not sure what the actual problem was (but maybe I should just RTFM).</p>
<p>The next problem was also caused by my laziness: I didn&#8217;t create any index definitions &#8211; and promptly the application threw a lot of errors because of missing indexes. Again: I should better RTFM.</p>
<p>But after a while, GAE automatically generated indexes and the application ran without throwing exceptions. But I immediately noticed &#8211; confirmed by a quick look at the administration dashboard: the application is too slow. It consumes a lot of CPU time for scaling images (e.g. creating thumbnails). As a quick next step I implemented a cache layer. Architecturally, the cache layer itself is a service used by the PhotoService. I am not quite confident about this implementation, but it does its job well. My original idea was to implement the cache layer as a layer between repository tier and service tier, or implement it directly into the repository tier. But during development I figured out that the cache is quite closely related to the business logic (scaling of images) and not to the persistence.</p>
<p>Finally I am quite satisfied with web application development on the GAE platform. The development environment is very easy to use and deployment is almost trivial. It is very easy to bring an application in production. On the other hand, my initial impression of the performance is quite mixed; loading pages takes very long and measuring the performance indicates that the GAE platform itself is not very responsive.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2009/12/first-ga-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Handling multipart form data with Stripes on GAE</title>
		<link>http://blog.cloudme.org/2009/11/handling-multipart-form-data-with-stripes-on-gae/</link>
		<comments>http://blog.cloudme.org/2009/11/handling-multipart-form-data-with-stripes-on-gae/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 14:42:47 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[multipart]]></category>
		<category><![CDATA[stripes]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=59</guid>
		<description><![CDATA[Stripes is a simple and elegant web framework that aims at simplifying Java web development. File uploads are handled elegantly and simply for the developer. A MultipartWrapper wraps the request and provides easy access to the uploaded files. Stripes provides two different implementations, but both share the same problem: they write the file to a [...]]]></description>
			<content:encoded><![CDATA[<p>Stripes is a simple and elegant web framework that aims at simplifying Java web development. File uploads are handled elegantly and simply for the developer. A MultipartWrapper wraps the request and provides easy access to the uploaded files. Stripes provides two different implementations, but both share the same problem: they write the file to a temporary directory on the server. This is not supported by the Google App Engine. Therefore, a better solution is necessary.</p>
<p>Luckily, the default CommonsMultipartWrapper implementation is almost right. The biggest issue is hidden in this line:</p>
<p><code>            List&lt;FileItem&gt; items = upload.parseRequest(request);<br />
</code></p>
<p>Replacing this with <code>upload.getItemIterator(request)</code> changes the game completely. getItemIterator() contains FileItemStream objects, objects, which refer to the HTTP stream directly without saving data to disk. The stream needs to be buffered in memory until it gets processed by the application.</p>
<p>Here is the complete code of that class:</p>
<pre><code>
/**
 * An implementation of MultipartWrapper that uses Jakarta Commons FileUpload (from apache)
 * to parse the request parts. This implementation requires that both commons-fileupload and
 * commons-io be present in the classpath.  While this implementation does introduce additional
 * dependencies, it's licensing (ASL 2.0) is significantly less restrictive than the licensing
 * for COS - the other alternative provided by Stripes. This implementation allows handling
 * uploads on the Google App Engine, as it does not rely on storing the files temporarily
 * on the local file system.
 *
 * @author Moritz Petersen
 */
public class GaeMultipartWrapper implements MultipartWrapper {
    /**
     * Ensure this class will not load unless Commons FileUpload is on the
     * classpath.
     */
    static {
        FileUploadException.class.getName();
    }

    private final Hashtable&lt;String, FileBean&gt; files = new Hashtable&lt;String, FileBean&gt;();
    private final Hashtable&lt;String, String[]&gt; parameters = new Hashtable&lt;String, String[]&gt;();

    /**
     * Pseudo-constructor that allows the class to perform any initialization
     * necessary.
     *
     * @param request
     *            an HttpServletRequest that has a content-type of multipart.
     * @param tempDir
     *            a File representing the temporary directory that can be used
     *            to store file parts as they are uploaded if this is desirable
     * @param maxPostSize
     *            the size in bytes beyond which the request should not be read,
     *            and a FileUploadLimitExceeded exception should be thrown
     * @throws IOException
     *             if a problem occurs processing the request of storing
     *             temporary files
     * @throws FileUploadLimitExceededException
     *             if the POST content is longer than the maxPostSize supplied.
     */
    public void build(HttpServletRequest request, File tempDir, long maxPostSize) throws IOException, FileUploadLimitExceededException {
        try {
            String charset = request.getCharacterEncoding();
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setRepository(tempDir);
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(maxPostSize);
            Map&lt;String, List&lt;String&gt;&gt; params = new HashMap&lt;String, List&lt;String&gt;&gt;();

            for (FileItemIterator it = upload.getItemIterator(request); it.hasNext();) {
                FileItemStream item = it.next();
                // If it's a form field, add the string value to the list
                final byte[] buffer = IOUtils.toByteArray(item.openStream());
                if (item.isFormField()) {
                    List&lt;String&gt; values = params.get(item.getFieldName());
                    if (values == null) {
                        values = new ArrayList&lt;String&gt;();
                        params.put(item.getFieldName(), values);
                    }
                    values.add(charset == null ? new String(buffer) : new String(buffer, charset));
                }
                // Else store the file param
                else {
                    files.put(item.getFieldName(), new FileBean(null, item.getContentType(), item.getName() ) {
                        @Override
                        public long getSize() {
                            return buffer.length;
                        }

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return new ByteArrayInputStream(buffer);
                        }

                        @Override
                        public void save(File toFile) throws IOException {
                            throw new UnsupportedOperationException();
                        }

                        @Override
                        public void delete() throws IOException {
                            throw new UnsupportedOperationException();
                        }
                    });
                }
            }

            // Now convert them down into the usual map of String->String[]
            for (Map.Entry&lt;String, List&lt;String&gt;&gt; entry : params.entrySet()) {
                List&lt;String&gt; values = entry.getValue();
                parameters.put(entry.getKey(), values.toArray(new String[values.size()]));
            }
        }
        catch (FileUploadBase.SizeLimitExceededException slee) {
            throw new FileUploadLimitExceededException(maxPostSize, slee.getActualSize());
        }
        catch (FileUploadException fue) {
            IOException ioe = new IOException("Could not parse and cache file upload data.");
            ioe.initCause(fue);
            throw ioe;
        }

    }

    /**
     * Fetches the names of all non-file parameters in the request. Directly
     * analogous to the method of the same name in HttpServletRequest when the
     * request is non-multipart.
     *
     * @return an Enumeration of all non-file parameter names in the request
     */
    public Enumeration&lt;String&gt; getParameterNames() {
        return parameters.keys();
    }

    /**
     * Fetches all values of a specific parameter in the request. To simulate
     * the HTTP request style, the array should be null for non-present
     * parameters, and values in the array should never be null - the empty
     * String should be used when there is value.
     *
     * @param name
     *            the name of the request parameter
     * @return an array of non-null parameters or null
     */
    public String[] getParameterValues(String name) {
        return parameters.get(name);
    }

    /**
     * Fetches the names of all file parameters in the request. Note that these
     * are not the file names, but the names given to the form fields in which
     * the files are specified.
     *
     * @return the names of all file parameters in the request.
     */
    public Enumeration&lt;String&gt; getFileParameterNames() {
        return files.keys();
    }

    /**
     * Responsible for constructing a FileBean object for the named file
     * parameter. If there is no file parameter with the specified name this
     * method should return null.
     *
     * @param name
     *            the name of the file parameter
     * @return a FileBean object wrapping the uploaded file
     */
    public FileBean getFileParameterValue(String name) {
        return files.get(name);
    }
}
</code></pre>
<p>To use the GaeMultipartWrapper, it needs to be added to the StripesFilter in web.xml:</p>
<pre><code>
    &lt;init-param&gt;
      &lt;param-name&gt;MultipartWrapper.Class&lt;/param-name&gt;
      &lt;param-value&gt;org.cloudme.stripes.GaeMultipartWrapper&lt;/param-value&gt;
    &lt;/init-param&gt;
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2009/11/handling-multipart-form-data-with-stripes-on-gae/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some ODF links</title>
		<link>http://blog.cloudme.org/2009/09/some-odf-links/</link>
		<comments>http://blog.cloudme.org/2009/09/some-odf-links/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 14:41:00 +0000</pubDate>
		<dc:creator>Moritz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[odf]]></category>

		<guid isPermaLink="false">http://blog.cloudme.org/?p=54</guid>
		<description><![CDATA[A collection of links related to the OpenDocument Format (ODF) and Java development: http://opendocument.xml.org/ (the official community gathering place and information resource for the OpenDocument Format) http://odftoolkit.org/ (a home for libraries that ease the development of applications that support ODF) http://odftoolkit.org/projects/odfdom/pages/Home (ODFDOM is a free ODF library for Java) http://www.jopendocument.org/ (a pure Java library for [...]]]></description>
			<content:encoded><![CDATA[<p>A collection of links related to the OpenDocument Format (ODF) and Java development:</p>
<ul>
<li><a href="http://opendocument.xml.org/">http://opendocument.xml.org/</a> (the official community gathering place and information resource for the OpenDocument Format)</li>
<li><a href="http://odftoolkit.org/">http://odftoolkit.org/</a> (a home for libraries that ease the development of applications that support ODF)</li>
<li><a href="http://odftoolkit.org/projects/odfdom/pages/Home">http://odftoolkit.org/projects/odfdom/pages/Home</a> (ODFDOM is a free ODF library for Java)</li>
<li><a href="http://www.jopendocument.org/">http://www.jopendocument.org/</a> (a pure Java library for OASIS Open Document files manipulation)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.cloudme.org/2009/09/some-odf-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
