<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"
>

<channel>
	<title>acidum.de &#187; Code Examples</title>
	<atom:link href="http://www.acidum.de/tag/code-examples/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.acidum.de</link>
	<description></description>
	<lastBuildDate>Sun, 08 Nov 2009 20:12:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Deploy a Grails Application into Glassfish</title>
		<link>http://www.acidum.de/2009/04/16/deploy-a-grails-application-into-glassfish/</link>
		<comments>http://www.acidum.de/2009/04/16/deploy-a-grails-application-into-glassfish/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 15:34:04 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Code Examples]]></category>
		<category><![CDATA[Glassfish]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=220</guid>
		<description><![CDATA[In most cases a Grails application uses a database to store its content. Fortunately Grails is tightly coupled with GORM to ease the use. Since Grails distinguish between different deployment environments, it uses a special configuration for development and production. Therefore you have to think of a database in production environment while you don not [...]]]></description>
			<content:encoded><![CDATA[<p>In most cases a <a title="Grails Site" href="http://www.grails.org/" target="_blank">Grails</a> application uses a database to store its content. Fortunately Grails is tightly coupled with GORM to ease the use. Since Grails distinguish between different deployment environments, it uses a special configuration for development and production. Therefore you have to think of a database in production environment while you don not in development stage.</p>
<p><a title="Glassfish Site" href="http://www.glassfish.org/" target="_blank">Glassfish</a> v2 and v3 Prelude comes with an internal database JavaDB (based on the Apache Derby) that could be used for first tests. It would be great to use the default database by default for deployments for Glassfish via JNDI. This article shows how to use the bundled database with an Grails application.</p>
<p>In preparation for the next steps you have to install <a title="Grails Installation" href="http://www.grails.org/Installation" target="_blank">Grails 1.1</a> and <a title="Glassfish v2 Installation Instructions" href="https://glassfish.dev.java.net/downloads/v2.1-b60e.html">Glassfish v2</a> or <a title="Glassfish v3 Prelude Installation Instructions" href="http://docs.sun.com/app/docs/doc/820-5968/ggssq?a=view">Glassfish v3 Prelude.</a></p>
<p>To create the demo app just run the following at command line:<br />
<code><br />
grails create-app glassfishapp<br />
cd glassfishapp<br />
grails create-domain-class Bookmark<br />
grails create-controller Bookmark<br />
</code></p>
<p>As the next step edit the domain class and the controller. E.g. open Eclipse with installed <a title="Eclipse Groovy Plugin" href="http://groovy.codehaus.org/Eclipse+Plugin" target="_blank">Groovy plugin.</a></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> BookmarkController <span style="color: #009900;">&#123;</span>
    def scaffold <span style="color: #339933;">=</span> Bookmark
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bookmark <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">String</span> url
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you are ready to test your app with</p>
<p><code><br />
grails run-app<br />
</code></p>
<p>To prepare the Grails application for Glassfish the data source for the production environment has to be changed. Open the <em>DataSource.groovy</em> and change</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">production <span style="color: #009900;">&#123;</span>
	dataSource <span style="color: #009900;">&#123;</span>
		dbCreate <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update&quot;</span>
		url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:hsqldb:file:prodDb;shutdown=true&quot;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">production <span style="color: #009900;">&#123;</span>
	dataSource <span style="color: #009900;">&#123;</span>
		dbCreate <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update&quot;</span>
		<span style="color: #666666; font-style: italic;">// do not use java:/jdbc as documented</span>
		jndiName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc/__default&quot;</span>
		username <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;APP&quot;</span>
		password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;APP&quot;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Do not use <code>jndiName = "java:jdbc/__default"</code> as <a href="http://www.grails.org/doc/1.1/guide/3.%20Configuration.html">documented</a>. Be aware that it may be dangerous to use <code>dbCreate= "update"</code> in production environments.</p>
<p>Now compile the web archive with </p>
<p><code>grails war</code></p>
<p>Start your database and the application server<br />
<code>./asadmin start-database<br />
./asadmin start-domain</code></p>
<p>Finally open the sun web interface at http://localhost:4848/ to deploy the war file</p>
<p><code>http://localhost:8080/glassfishapp-0.1/</code></p>
<p>I experienced some memory problems by using Grails with Glassfish. To fix that problem I added <code>-XX:MaxPermSize=256m</code> and <code>-Xmx1024m</code> as JVM option. JVM options can be changed via the web interface (Common Tasks -> Application Server -> JVM Settings -> JVM Options)</p>
<div id="attachment_225" class="wp-caption aligncenter" style="width: 160px"><a href="http://www.acidum.de/wp-content/uploads/2009/04/grails_glassfish.png" rel="lightbox"><img class="size-thumbnail wp-image-225" title="Grails running in Glassfish" src="http://www.acidum.de/wp-content/uploads/2009/04/grails_glassfish-150x150.png" alt="Grails running in Glassfish" width="150" height="150" /></a><p class="wp-caption-text">Grails running in Glassfish</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2009/04/16/deploy-a-grails-application-into-glassfish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Groovy Programming Examples</title>
		<link>http://www.acidum.de/2008/09/11/groovy-programming-examples/</link>
		<comments>http://www.acidum.de/2008/09/11/groovy-programming-examples/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 13:16:03 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Code Examples]]></category>
		<category><![CDATA[Metaprogramming]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=154</guid>
		<description><![CDATA[Java Iteration Example

import java.util.Date;
&#160;
public class Foo &#123;
    public static void main&#40;String&#91;&#93; args&#41; &#123;
        int start = 1;
        int summe = 0;
&#160;
        for &#40;int i = start; i &#60;= 5; i++&#41; &#123;
 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Java Iteration Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> start <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> summe <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            summe <span style="color: #339933;">+=</span> i<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">Date</span> now <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>now.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; Die Summe ist: &quot;</span> <span style="color: #339933;">+</span> summe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Groovy Iteration Example within Groovy Shell</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
        start <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>
        summe <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i in start..5<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            summe <span style="color: #339933;">+=</span> i
        <span style="color: #009900;">&#125;</span>
&nbsp;
        now <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        println <span style="color: #0000ff;">&quot;$now Die Summe ist: $summe&quot;</span></pre></div></div>

<p><strong>Groovy Map Handling with Closures</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">map <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">1</span>, <span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// inline closure</span>
map.<span style="color: #006633;">each</span> <span style="color: #009900;">&#123;</span> key, value <span style="color: #339933;">-&gt;</span>
    map<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span>
<span style="color: #009900;">&#125;</span>
println map
&nbsp;
<span style="color: #666666; font-style: italic;">// extern closure</span>
doubler <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> key, value <span style="color: #339933;">-&gt;</span>
    map<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span>
<span style="color: #009900;">&#125;</span>
map.<span style="color: #006633;">each</span><span style="color: #009900;">&#40;</span>doubler<span style="color: #009900;">&#41;</span>
println map</pre></div></div>

<p><strong>Groovy Beans</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Book</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> title
&nbsp;
    <span style="color: #003399;">Book</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> theTitle<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        title <span style="color: #339933;">=</span> theTitle
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">Book</span> plus<span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span> anotherBook<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Book</span><span style="color: #009900;">&#40;</span>title <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">+</span> anotherBook.<span style="color: #006633;">title</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">Book</span> gina <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Book</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Groovy in Action&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// getters are generated automatically </span>
println gina.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
println <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>gina <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Book</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Groovy for Experts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">title</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>Groovy Duck Typing</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #009900;">&#123;</span>
    def speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> println <span style="color: #0000ff;">&quot;wuff&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Cat <span style="color: #009900;">&#123;</span>
    def speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> println <span style="color: #0000ff;">&quot;miau&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
def saySomething<span style="color: #009900;">&#40;</span>somewhat<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    somewhat.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
saySomething<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
saySomething<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>Simple XML Example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> XmlBuilder <span style="color: #009900;">&#123;</span>
   def out
   XmlBuilder<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">out</span> <span style="color: #339933;">=</span> out <span style="color: #009900;">&#125;</span>
   def invokeMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       out <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000ff;">&quot;&lt;$name&gt;&quot;</span>
       <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> Closure<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">delegate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>
            args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#125;</span>
       <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
           out <span style="color: #339933;">&lt;&lt;</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#125;</span>
       out <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000ff;">&quot;&lt;/$name&gt;&quot;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
def xml <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlBuilder<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
xml.<span style="color: #006633;">html</span> <span style="color: #009900;">&#123;</span>
    head <span style="color: #009900;">&#123;</span>
        title <span style="color: #0000ff;">&quot;Hello World&quot;</span>
    <span style="color: #009900;">&#125;</span>
    body <span style="color: #009900;">&#123;</span>
        p <span style="color: #0000ff;">&quot;Welcome!&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
println xml.<span style="color: #006633;">out</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/09/11/groovy-programming-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.287 seconds -->
