<?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>Schabby&#039;s Blog &#187; Java</title>
	<atom:link href="http://schabby.de/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://schabby.de</link>
	<description>OpenGL, Java, Cassandra and other stuff that totally makes the world go round</description>
	<lastBuildDate>Fri, 27 Jan 2012 18:48:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>OpenGL Picking in 3D</title>
		<link>http://schabby.de/picking-opengl-ray-tracing/</link>
		<comments>http://schabby.de/picking-opengl-ray-tracing/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 22:25:54 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=964</guid>
		<description><![CDATA[There are two common ways to accomplish picking: Switching OpenGL to "selection mode" and tracing a line shot from your 2D view screen through the 3D scene. In this opengl picking tutorial I cover the latter and provide a code example.]]></description>
			<content:encoded><![CDATA[<p>This blog post explains a common and versatile approach to OpenGL picking called &#8220;ray picking&#8221;. A code example is given below.</p>
<p>Picking is the process of finding objects in you scene based on user input. Most commonly, you want to determine what object a user has clicked with his mouse. In this case the 2D mouse coordinates serve as a reference on the view port to identify the projected objected that has been clicked. A similar scenario is a first person shooter, where the gun serves as the picking pointer, however in world space coordinates. If you shoot your gun, the trajectory of the bullet is traced through your scene and collisions are detected.<span id="more-964"></span></p>
<div id="attachment_996" class="wp-caption aligncenter" style="width: 473px"><img src="http://schabby.de/wp-content/uploads/2011/09/screenshot-opengl-picking.png" alt="Screenshot OpenGL Picking" title="Screenshot OpenGL Picking" width="463" height="363" class="size-full wp-image-996" /><p class="wp-caption-text">A screenshot showing a hex map in an LWJGL application with a selected tile as an example for 3D picking</p></div>
<h3>OpenGL Selection Mode vs. Ray Picking</h3>
<p>There are two common ways of implementing picking. The first one makes use of a special OpenGL feature in which you render your scene in a so-called &#8220;selection mode&#8221;. In selection mode, OpenGL does not render to the frame buffer but gathers depths information of the objects with is later read at the point where your picking occurs. The second approach is called &#8220;Ray Picking&#8221; and is independent from OpenGL and simulates a line that is shot through the scene until it hits an object. I personally prefer the second approach for the following reasons:</p>
<ul>
<li>You don&#8217;t need an additional render pass. A render pass can take some considerable amount of time and special clipping/culling code is required to only render the small part of the scene which you want to perform the picking on. In addition, you render a less complex scene (omitting textures, lighting, normals, etc) in selection mode if possible which can add a high degree of complexity to your drawing routines (imagine breaking up display lists). Ray picking obviously requires some code to trace the ray through your scene, but this is usually less complex.</li>
<li>Ray picking can be done independently from the rest of the game code as a simultaneous and &#8220;read-only&#8221; operation on the scene graph. In some environments such as AWT/JOGL where the input devices are handled by a separate thread, your picking code can run as part of the handler thread and does not impact the performance of the rest of the game. This comes in handy especially in scenarios where you need to perform picking very often (eg. mouse hovering, target laser on the in-game gun).</li>
<li>In pretty much all Java environments, calling OpenGL takes a considerable amount of time due to JNI. It is therefore beneficial to minimize the number OpenGL calls. Rendering in selection mode would mean additional OpenGL calls that I rather substitute with ray picking.</li>
</ul>
<p>However, there are scenarios where ray picking is simply inferior or impractical. Consider picking in a scene with a lot of small details (eg. tree with little branches and leaves) or if you need to know the individual polygon/triangle that you picked. I will therefore cover both approaches, starting with ray picking.</p>
<p><a href="http://schabby.de/wp-content/uploads/2011/09/ray-packing.png"><img class="alignright" title="Picking by Ray Tracing" src="http://schabby.de/wp-content/uploads/2011/09/ray-packing.png" alt="Picking by Ray Tracing" width="465" height="289" /></a></p>
<h3>Ray Picking</h3>
<p>Ray picking is the process of shooting a line (ie. &#8220;ray&#8221;) from the camera through the 2D viewscreen (where the 3D scene is projected on) into the scene until it hits an object. To do so, we need to know the camera and the point on the viewscreen (eg. mouse cursor position). The first point is trivial, however the second point is a bit more difficult to determine. Let&#8217;s say we have a 2D point on the viewscreen (x, y) and now want to map this point into world coordinates. One way would be to make use of the inverted viewing matrix, but inverting a matrix is usually an expensive undertaking. Instead, we compute the position of the plane of the viewscreen in world space and map the 2D point on this plane and from there into world space.</p>
<p>The algorithm in pseudo code follows below. Note that <tt>cameraLookAt</tt> is the 3D point where the camera looks at (as used on <tt>glLookat</tt>), <tt>cameraPosition</tt> is the current position of the camera in world space and <tt>cameraUp</tt> is the up vector of the camera.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">x = getMouseX() // scalar
y = getMouseY() // scalar

view = cameraLookAt - cameraPosition // 3D float vector
normalize view

h = crossProduct( view, cameraUp ) // 3D float vector
normalize h

v = crossProduct( h, view) // 3D float vector
normalize v</pre>
<p>ok, so far for <tt>h</tt> and <tt>v</tt>. We now need to compute the lengths of <tt>h</tt> and <tt>v</tt> in the view port. In my example I assume that you set up your frustum by a field of view angle in degrees (<tt>fovy</tt>) as commonly the case with <tt>glFrustum</tt>. Let <tt>nearClippingDistance</tt> be the distance to the near clipping plane and <tt>width/height</tt> the ratio of the view port width divided by the viewport heigth.</tt></tt></p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// convert fovy to radians
rad = fovy * PI / 180
vLength = tan( rad / 2 ) * nearClippingPlaneDistance
hLength = vLength * (width / height)

scale v by vLength
scale h by hLength</pre>
<p>The two scalars <tt>vLength</tt> and <tt>hLength</tt> in combinaton with <tt>v</tt> and <tt>h</tt> help spanning the view port plane extending from the center point. At this point it is worth mentioning that it may make sense to only compute <tt>h</tt> and <tt>v</tt> when you update the camera orientation because the normalization involves a square root which is a relatively expensive operation (actually <tt>tan</tt> may also be avoided).</p>
<p>Now it is time to map the 2D mouse coordinates onto the view port plane.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// translate mouse coordinates so that the origin lies in the center
// of the view port
x -= width / 2
y -= height / 2

// scale mouse coordinates so that half the view port width and height
// becomes 1
y /= (height / 2)
x /= (width / 2)

// linear combination to compute intersection of picking ray with
// view port plane
pos = cameraPos + view + h*x + v*y

// compute direction of picking ray by subtracting intersection point
// with camera position
dir = pos - cameraPos</pre>
<p>That's pretty much it. We now got <tt>pos</tt> as the intersection point of the picking ray in the view port plane and the picking ray direction <tt>dir</tt>. This describes a line that can now be used to intersect with the individual objects of the scene.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// brute force
for all objects in the scene
  test for intersection and keep closest
end for</pre>
<p>So by intersecting the picking ray with all your visible objects in your scene while keeping track of the distance, you determine the object that is hit by your mouse cursor.<br />
Checking all objects in our scene may not be very efficient though. So in a first attempt I recommend to test only objects that lie (partly) within your viewing frustum. Under normal circumstances you got that code already in your scene graph as part of the software culling routine when pumping obects into the render pipeline.</p>
<p>I hope this helped. If you liked the post or if you got questions, ideas hints or found a problem, please leave a comment. I am trying to answer as quickly as possible. Also, if you want me to send you the source code of this example please let me know, I am happy to share the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/picking-opengl-ray-tracing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JOGL Example &#8211; Tutorial</title>
		<link>http://schabby.de/jogl-example-hello-world/</link>
		<comments>http://schabby.de/jogl-example-hello-world/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 14:25:38 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=858</guid>
		<description><![CDATA[If you want to get your feed wet with JOGL examples, you may want to check out my simple JOGL tutorial. The JOGL tutorial provides up2date, stand-alone and self-contained example code.]]></description>
			<content:encoded><![CDATA[<p>This getting started tutorial is intended for JOGL users that are mere beginners. It helps to setup a recent JOGL installation in Eclipse on Windows.<br />
<span id="more-858"></span><br />
First you need to download JOGL. This is actually more difficult than it sounds because the precompiled binary packages are a bit hidden in the new project site. Fortunately, I figured out <a href="http://schabby.de/jogl-download/">where you can download recent JOGL binaries</a>.</p>
<p>For this tutorial, I am using</p>
<ul>
<li><tt>jogl-b633-2012-01-23_20-37-13</tt>, and</li>
<li><tt>gluegen-b480-2012-01-23_16-49-04</tt>.</li>
</ul>
<p>This is how your project setup should look like:</p>
<p><a href="http://schabby.de/wp-content/uploads/2010/07/JOGLProjectSetup.png"><img src="http://schabby.de/wp-content/uploads/2010/07/JOGLProjectSetup.png" alt="JOGL Project Setup" title="JOGL Project Setup" width="310" height="346" class="aligncenter size-full wp-image-1033" /></a></p>
<p>So one you got the binaries, unpack the ZIP file somewhere. You will notice the <tt>jar</tt> folder with a number of JAR files. All you need is in that <tt>jar</tt> folder, because the DLL binaries are included in the JARs.</p>
<p>Create a new eclipse project (name doesnt matter) and add the following JARs on the classpath:</p>
<ul>
<li><tt>gluegen-rt-natives-windows-amd64.jar</tt></li>
<li><tt>gluegen-rt.jar</tt></li>
<li><tt>jogl-all-natives-windows-amd64.jar</tt></li>
<li><tt>gluegen-rt.jar</tt></li>
</ul>
<p>These files are the native part of the OpenGL binding and are linked via Java Native Interface (JNI). Since the DLLs are precompiled as part of the JAR files, you are almost ready to go.</p>
<p>Once you got that set up, create a package <tt>de.schabby.jogl.helloworld</tt> and copypaste the following two classes in the newly created package.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.schabby.jogl.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowEvent</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLCapabilities</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.awt.GLCanvas</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JFrame</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> HelloWorld 
<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: #666666; font-style: italic;">// setup OpenGL Version 2</span>
    	GLProfile profile <span style="color: #339933;">=</span> GLProfile.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>GLProfile.<span style="color: #006633;">GL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	GLCapabilities capabilities <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLCapabilities<span style="color: #009900;">&#40;</span>profile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// The canvas is the widget that's drawn in the JFrame</span>
    	GLCanvas glcanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLCanvas<span style="color: #009900;">&#40;</span>capabilities<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	glcanvas.<span style="color: #006633;">addGLEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Renderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	glcanvas.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">300</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">JFrame</span> frame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JFrame</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Hello World&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        frame.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> glcanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// shutdown the program on windows close event</span>
        frame.<span style="color: #006633;">addWindowListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">WindowAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowClosing<span style="color: #009900;">&#40;</span><span style="color: #003399;">WindowEvent</span> ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        frame.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span> frame.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        frame.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">true</span> <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>The <rr>Render.java</tt> class looks like follows. It implements <tt>GLEventListener</tt> which is the call-back implementaiton by JOGL to do all OpenGL rendering.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.schabby.jogl.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GL2</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLAutoDrawable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.glu.GLU</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Renderer</span> <span style="color: #000000; font-weight: bold;">implements</span> GLEventListener 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> GLU glu <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLU<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> display<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glClear</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_COLOR_BUFFER_BIT</span> <span style="color: #339933;">|</span> GL2.<span style="color: #006633;">GL_DEPTH_BUFFER_BIT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glTranslatef</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.5f, 0.0f, <span style="color: #339933;">-</span>6.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glBegin</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_TRIANGLES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>0.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
        gl.<span style="color: #006633;">glTranslatef</span><span style="color: #009900;">&#40;</span>3.0f, 0.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glBegin</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_QUADS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
        gl.<span style="color: #006633;">glFlush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> displayChanged<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable, <span style="color: #000066; font-weight: bold;">boolean</span> modeChanged, <span style="color: #000066; font-weight: bold;">boolean</span> deviceChanged<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</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><span style="color: #0000ff;">&quot;displayChanged called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</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><span style="color: #0000ff;">&quot;init() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glClearColor</span><span style="color: #009900;">&#40;</span>0.0f, 0.0f, 0.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glShadeModel</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_FLAT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> reshape<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable, <span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y, <span style="color: #000066; font-weight: bold;">int</span> width, <span style="color: #000066; font-weight: bold;">int</span> height<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</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><span style="color: #0000ff;">&quot;reshape() called: x = &quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, y = &quot;</span><span style="color: #339933;">+</span>y<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, width = &quot;</span><span style="color: #339933;">+</span>width<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, height = &quot;</span><span style="color: #339933;">+</span>height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// avoid a divide by zero error!</span>
        <span style="color: #009900;">&#123;</span>
            height <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">float</span> h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> width <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> height<span style="color: #339933;">;</span>
&nbsp;
        gl.<span style="color: #006633;">glViewport</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, width, height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glMatrixMode</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_PROJECTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        glu.<span style="color: #006633;">gluPerspective</span><span style="color: #009900;">&#40;</span>45.0f, h, <span style="color: #cc66cc;">1.0</span>, <span style="color: #cc66cc;">20.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glMatrixMode</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_MODELVIEW</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dispose<span style="color: #009900;">&#40;</span>GLAutoDrawable arg0<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</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><span style="color: #0000ff;">&quot;dispose() called&quot;</span><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>By right-clicking on the <tt>main()</tt> method and chosing "run as application", Eclipse will execute the program. You should see something like this:</p>
<p><a href="http://schabby.de/wp-content/uploads/2010/07/JoglHelloWorld.png"><img src="http://schabby.de/wp-content/uploads/2010/07/JoglHelloWorld.png" alt="Jogle Hello World" title="Jogle Hello World" width="300" height="300" class="aligncenter size-full wp-image-1034" /></a></p>
<p>That's it so far. Please let me know if you have questions or comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/jogl-example-hello-world/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>JOGL Download</title>
		<link>http://schabby.de/jogl-download/</link>
		<comments>http://schabby.de/jogl-download/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 09:35:40 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=850</guid>
		<description><![CDATA[Just can't find the JOGL downloads (Java OpenGL bindings)? Well, I figured out where to download JOGLs most recent binary distribution such as 2011 releases :)]]></description>
			<content:encoded><![CDATA[<p>You are right in this page if you are on the quest for one of the following:</p>
<ul>
<li>recent JOGL binary distribution download</li>
<li>precompiled Windows Java OpenGL bindings</li>
<li>you just can not find the place where to download JOGL <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p><span id="more-850"></span><br />
I had a couple of hours on my own recently and though it would be a good thing to catch up with the latest developments in the Java+OpenGL environment. So I looked for the download of JOGL (Java Bindings for OpenGL) just to realize, that I is actually quite difficult to find a recent binary distribution of JOGL!</p>
<p><strong>Eventually, after a while I found it: <a href="http://jogamp.org/deployment/autobuilds/master/" rel="nofollow">http://jogamp.org/deployment/autobuilds/master/</a>.</strong></p>
<p>There you find an extensive list of directories containing builds, mostly for different binding projects such as <em>gluegen</em>, <em>joal</em>, etc. Search for &#8220;jogl&#8221; to jump to the JOGL builds. </p>
<p>The downloads are basically a list of (probably automated) builds. So I hope that this will be the place for the next months to get update-to-date JOGL binaries. The builds that have the suffix &#8220;-master&#8221; in their directory name appear to contain only documentation. </p>
<p>Please also note that for running JOGL you also need gluegen. I personally use the 2012-01-23 release at the moment. This is</p>
<ul>
<li>gluegen-b480-2012-01-23_16-49-04</li>
<li>jogl-b633-2012-01-23_20-37-13</li>
</ul>
<p>which seem to work.</p>
<p>So the guys at JogAmp.org do actually provide precompiled binaries, they just make it difficult to find them. <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now that you downloaded JOGL, you may be interested in my <a href="http://schabby.de/jogl-example-hello-world/">JOGL Hello World example</a> (includes how to set up the classpath and DLLs right).</p>
<p>Please leave a comment if it helps or my resource gets out of date.</p>
<p>2010-10-20 EDIT: updated download URL.<br />
2010-11-03 EDIT: updated download URL again to point to autobild directory.<br />
2010-11-19 EDIT: added reference to my hello world example with JOGL<br />
2011-07-27 EDIT: updated link to JOGL downloads<br />
2012-01-27 EDIT: updated link to JOGL downloads, added note about gluegen</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/jogl-download/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Keine Sessions in Tomcat / JSP</title>
		<link>http://schabby.de/keine-sessions-in-tomcat-jsp/</link>
		<comments>http://schabby.de/keine-sessions-in-tomcat-jsp/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 19:27:41 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=827</guid>
		<description><![CDATA[Ab einem gewissen Trafficvolumumen beginnt die Sessionstabelle die der Tomcat auf der Platte hält stetig zu wachsen und täglich größere Ausmaße anzunehmen. Da der Tomcat einen großen Teil der serialisierten Sessiontabelle im Speicher hält, kann das zu einem akkuten Crashrisiko werden, wenn der Tomcat bis zum Limit wächst (oder das OS zu swappen beginnt). Ausserdem [...]]]></description>
			<content:encoded><![CDATA[<p>Ab einem gewissen Trafficvolumumen beginnt die Sessionstabelle die der Tomcat auf der Platte hält stetig zu wachsen und täglich größere Ausmaße anzunehmen. Da der Tomcat einen großen Teil der serialisierten Sessiontabelle im Speicher hält, kann das zu einem akkuten Crashrisiko werden, wenn der Tomcat bis zum Limit wächst (oder das OS zu swappen beginnt). <span id="more-827"></span>Ausserdem ist es ineffizient für jede css-, JavaScript oder Bilddatei einen Sessionstempel im Header mitzusenden. Das gilt inbesondere für Seiten die stark und regelmäßig von Bots gecrawled werden, da Bots naturgemäß keine Session halten und der Tomcat bei jedem Zugriff des Bots von einem neuen User ausgehen muss und eine neue Session anlegt.</p>
<p>Es stellt sich also folgende Frage:</p>
<ul>
<li>Session unterdrücken in Tomcat / JSP</li>
<li>Tomcat keine Sessions</li>
<li>JSP / Tomcat ohne Sessions</li>
</ul>
<p>Im Prinzip ist die Lösung sehr einfach. Man muss nur zwei einfache Regeln befolgen.</p>
<ol>
<li>Mache nur nur <tt>request.getSession()</tt> calls wenn Du wirklich eine neue oder die bestehende Session brauchst (Login, etc.). Überlege Dir auch, ob nicht <tt>request.getSession(false)</tt> reicht (= nur eine bereits bestehende Session zurückgeben, aber keine neue Anlegen falls noch keine existiert).</li>
<li>Setze in allen JSPs die Direktive <tt><%@ page session="false" %></tt> um das automatische Erzeugen einer Session im JSP-Code zu unterbinden. Wenn Du die Session doch in einer JSP brauchen solltest, erstelle sie vorher in einem Controller.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/keine-sessions-in-tomcat-jsp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BufferedReader &#8211; kurz Erklärt</title>
		<link>http://schabby.de/bufferedreader-kurz-erklart/</link>
		<comments>http://schabby.de/bufferedreader-kurz-erklart/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:53:37 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=825</guid>
		<description><![CDATA[Der BufferedReader bereitet so manchem Java-Experten noch Kopfschmerzen. In diesem Artikel wird kurz und knapp der BufferedReader mit einem Beispiel erläutert sowie seine Funktionsweise erklärt.]]></description>
			<content:encoded><![CDATA[<p>Moin, moin! Heute nun eine kurze Einweisung in die richtige Verwendung von BufferedReaders und den Grund warum der BufferedReader so populär ist. Im Grunde gibt es zwei Gründe:<span id="more-825"></span></p>
<ol>
<li>BufferedReader verbessert die Lesegeschwindigkeit aus anderen Readern (z.B. FileReader um von der Festplatte zu lesen) drastisch,</li>
<li>BufferedReader hat praktischer Weise eine readLine() Methode, mit deren Hilfe man ganze Zeilen lesen kann.</li>
</ol>
<p>In Prinzip ist der BufferedReader auch nur ein Reader. Das heisst er hat die typischen read(&#8230;) Methoden. Intern aber, arbeitet der BufferedReader auf einem Speicherbereich (dem Buffer), den er erst füllt, bevor seine read(&#8230;) Methoden darauf arbeiten können. Das heisst, dass der BufferedReader größere Datenblöcke auf einmal lesen kann und die dann nach und nach an den äußeren Reader weiterleiten kann. Das ist insbesondere sinnvoll bei Readern die von wiederum von Low-Level Readern lesen, die jeweils bei größeren Datenblöcken performant sind, wie typischer Weise von der Festplatte oder vom Socket.</p>
<p>Ein typischer Gebrauch eines BufferedReaders sieht so aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">InputStreamReader</span> inputStreamReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span> <span style="color: #009900;">&#40;</span>textFile<span style="color: #009900;">&#41;</span>, encoding <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">BufferedReader</span> bufferedReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span> <span style="color: #009900;">&#40;</span>inputStreamReader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://schabby.de/bufferedreader-kurz-erklart/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java JDK Download</title>
		<link>http://schabby.de/java-jdk-download/</link>
		<comments>http://schabby.de/java-jdk-download/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:07:19 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=820</guid>
		<description><![CDATA[Kurze, klare Erklaerung welcher Java JDK der richtige zum herunterladen ist und was SE, EE und ME bedeutet. Der offizielle Download des Java JDKs gibt es naemlich nur an einer Stelle.]]></description>
			<content:encoded><![CDATA[<p>Seltsamer Weise scheinen viele meiner freundlichen Mitmenschen sich zu fragen, wo die richtige Quelle zum downloaden des Java JDKs bzw. des Java SDKs ist. Nun, da gibt es eine einfache Antwort: offiziell bei <span id="more-820"></span><a href="http://java.sun.com/">SUN</a> <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ok, neuerdings Oracle, da Oracle SUN mitsamt MySQL gekauft hat, aber das steht auf einem anderen Blatt.</p>
<p>Uebrigens, JDK heisst Java Development Kit, und SDK Software Development Kit. Es nimmt sich aber defacto nichts.</p>
<p>Ist man nun bei SUN gelandet, gibt es drei Varianten zum downloaden.</p>
<ul>
<li>Java SE: steht fuer Standard Edition ist ziemlich sicher das was Du suchst wenn Du das klassische Javapaket fuer den Deskop runterladen willst (JRE und JDK)</li>
<li>Java ME: steht fuer Mobile Edition und beinhaltet eine auf fuer Handies zugeschnittene Java Plattform (mit Compiler) mit reduzierten Bibliotheken</li>
<li>Java EE: steht fuer Enterprise Edition und ist deutlich umfangreicher als die Standard Edition und enthaelt zusaetzliche, business nahe Tools wie den Glassfish Application Server</li>
</ul>
<p>Ich hoffe das hilft.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/java-jdk-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven Tests Ignorieren</title>
		<link>http://schabby.de/maven-tests-ignorieren/</link>
		<comments>http://schabby.de/maven-tests-ignorieren/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 09:02:47 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=816</guid>
		<description><![CDATA[Es kommt auch in den besten Familien vor, dass mal ein unit test nicht läuft. Da muss sich nicht schämen, nur ein bisschen ärgern. Ähnliche Probleme sind Maven tests ausschalten mvn ignoriere test fehler Wenn man trotzdem seinen maven snapshot bauen möchte, müssen aber die tests failures ignoriert werden. Das wird mittels einer Property direkt [...]]]></description>
			<content:encoded><![CDATA[<p>Es kommt auch in den besten Familien vor, dass mal ein unit test nicht läuft. Da muss sich nicht schämen, nur ein bisschen ärgern. Ähnliche Probleme sind</p>
<ul>
<li>Maven tests ausschalten</li>
<li>mvn ignoriere test fehler</li>
</ul>
<p><span id="more-816"></span><br />
Wenn man trotzdem seinen maven snapshot bauen möchte, müssen aber die tests failures ignoriert werden. Das wird mittels einer Property direkt auf der Kommandezeile bewerkstelligt. Da ich dieses Property andauernd vergesse, möchte ich es mir hier selbst und der Allgemeinheit nochmals vor Augen führen:</p>
<p><tt><br />
mvn -Dmaven.test.failure.ignore=true install<br />
</tt></p>
<p>Hope that helps&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/maven-tests-ignorieren/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cassandra Installation and Configuration</title>
		<link>http://schabby.de/cassandra-installation-configuration/</link>
		<comments>http://schabby.de/cassandra-installation-configuration/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:44:42 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Cassandra]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=786</guid>
		<description><![CDATA[This is the second post on my little &#8220;Cassandra &#8211; Getting Started&#8221; series covering the installation and basic configuration of Cassandra. Cassandra is extremely easy to set up, especially compared to HBase. All you got to do is to download, extract, edit a single XML-file and run. But let us take it step by step. [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second post on my little &#8220;Cassandra &#8211; Getting Started&#8221; series covering the installation and basic configuration of Cassandra. Cassandra is extremely easy to set up, especially compared to <a href="http://hadoop.apache.org/hbase/">HBase</a>. All you got to do is to download, extract, edit a single XML-file and run. But let us take it step by step.<br />
<span id="more-786"></span><br />
You can <a href="http://incubator.apache.org/cassandra/">download Cassandra</a> directly from it&#8217;s (her?) website. At the time of the submission of this post, version 0.4.1 was the most recent stable. Note that you need <a href="http://java.sun.com/javase/downloads/index.jsp">Java 6</a> installed to run Cassandra which I assume here as properly installed. </p>
<p>After extracting Cassandra to some folder (on my Windows box I placed it directly in <tt>D:\cassandra</tt>), the only file you need to edit is <tt>conf/storage-conf.xml</tt>. While Cassandra is engineered to run on a large number of machines in a network, we start it here as a single node with the default parameter set, so that most of the settings are ok for now. </p>
<p>If your are <b>not</b> on a Unix-like system, you need to update the folders where Cassandra is supposed to store the data. If your using Windows (like me), then find the following lines in <tt>conf/storage-conf.xml</tt> and change the paths to something sensible</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/commitlog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/callouts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/bootstrap<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/staging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>like for example my settings:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/commitlog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/callouts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/bootstrap<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/staging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Let&#8217;s take Cassandra for a spin and check if she starts up correctly. For Mac OS, Linux, etc. users, simply change to the <tt>bin</tt> directory of Cassandra and run <tt>./cassandra</tt>. As an aside for the impatient, I start Cassanda with <tt>sudo</tt> to avoid trouble with the Cassandras <tt>system.log</tt>.</p>
<p>Windows users, however, that use the command line (meaning not <a href="http://www.cygwin.com/">Cygwin</a>) cannot start it just like that. The <tt>cassandra.bat</tt> didnt work for me on my Vista box if executed with <tt>bin</tt> being the current working directory (probably due to the CASSANDRA_HOME environment variable that get&#8217;s incorrectly set in the batch file). BUT it works perfect if you call <tt>bin\cassandra.bat</tt> from Cassandra&#8217;s main directory above <tt>bin</tt>. So if you are on Windows, change to the directory where you extracted Cassandra and execute <tt>bin\cassandra.bat</tt>.</p>
<p>Cassandras output on startup will look similar to this (here on Mac OS):</p>
<pre style="font-size:10px">
Schabbys-MacBook-Pro:bin johannes$ sudo ./cassandra
Schabbys-MacBook-Pro:bin johannes$ Listening for transport dt_socket at address: 8888
DEBUG - Loading settings from ./../conf/storage-conf.xml
DEBUG - Syncing log with a period of 1000
DEBUG - opening keyspace Keyspace1
DEBUG - adding Super1 as 0
DEBUG - adding Standard2 as 1
DEBUG - adding Standard1 as 2
DEBUG - adding StandardByUUID1 as 3
DEBUG - adding LocationInfo as 4
DEBUG - adding HintsColumnFamily as 5
DEBUG - opening keyspace system
DEBUG - INDEX LOAD TIME for /Users/johannes/cassandra/data/system/LocationInfo-1-Data.db: 0 ms.
DEBUG - INDEX LOAD TIME for /Users/johannes/cassandra/data/system/LocationInfo-2-Data.db: 0 ms.
DEBUG - INDEX LOAD TIME for /Users/johannes/cassandra/data/system/LocationInfo-3-Data.db: 0 ms.
INFO - Replaying /Users/johannes/cassandra/commitlog/CommitLog-1257980407451.log
DEBUG - Replaying /Users/johannes/cassandra/commitlog/CommitLog-1257980407451.log starting at 117
DEBUG - Reading mutation at 117
DEBUG - replaying mutation for system.L: {ColumnFamily(LocationInfo [Generation,])}
INFO - Flushing Memtable(LocationInfo)@228828460
DEBUG - Submitting LocationInfo for compaction
INFO - Completed flushing Memtable(LocationInfo)@228828460
INFO - Compacting [/Users/johannes/cassandra/data/system/LocationInfo-1-Data.db,/Users/johannes/cassandra/data/system/LocationInfo-2-Data.db,/Users/johannes/cassandra/data/system/LocationInfo-3-Data.db,/Users/johannes/cassandra/data/system/LocationInfo-4-Data.db]
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-1-Data.db   : 256
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-2-Data.db   : 512
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-3-Data.db   : 768
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-4-Data.db   : 1024
DEBUG - Expected bloom filter size : 1024
INFO - Compacted to /Users/johannes/cassandra/data/system/LocationInfo-5-Data.db.  0/255 bytes for 0/1 keys read/written.  Time: 150ms.
DEBUG - collecting Generation:false:4@3
DEBUG - collecting Token:false:16@0
INFO - Saved Token found: 160533723849634883377008460059010504450
DEBUG - Starting to listen on 127.0.0.1:7001
DEBUG - Binding thrift service to localhost:9160
</pre>
<p>I think that&#8217;s it. Leave a comment if you run in trouble or check the nice <a href="http://wiki.apache.org/cassandra/GettingStarted#if_something_goes_wrong">If Something Goes Wrong</a> page in the Cassandra Wiki.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/cassandra-installation-configuration/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Cassandra &#8211; Getting Started</title>
		<link>http://schabby.de/cassandra-getting-started/</link>
		<comments>http://schabby.de/cassandra-getting-started/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 13:58:11 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Cassandra]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=733</guid>
		<description><![CDATA[This post addresses Java developers who want to get their feet wet with Cassandra. This is the first post in a series of three in which I describe Cassandras data model as seen from the angle of a typical Java developer. By contributing a javaish view on the data model, I try to extend the [...]]]></description>
			<content:encoded><![CDATA[<p>This post addresses Java developers who want to get their feet wet with Cassandra. This is the first post in a series of three in which I describe Cassandras data model as seen from the angle of a typical Java developer. By contributing a javaish view on the data model, I try to extend the set of existing data model descriptions.<br />
<span id="more-733"></span><br />
The second post in this series will briefly describe how to install and configure Cassandra. The third post will provide several hands-on examples for Cassandra with Java.</p>
<p>I have been toying around with <a href="http://incubator.apache.org/cassandra/">Cassandra </a>for quite some time now. From all NOSQL databases I have seen (and there are <a href="http://blog.oskarsson.nu/2009/06/nosql-debrief.html">quite a few</a> already as <a href="http://www.franzkowiak.org/">Michael</a> pointed out to me earlier), Cassandra seems to be the most promising one to me for reasons that are definitely worth discussing, but are here be beyond the scope of this post.</p>
<h2>Data Model</h2>
<p>Cassandras data model has been described <a href="http://wiki.apache.org/cassandra/DataModel">more</a> <a href="http://arin.me/code/wtf-is-a-supercolumn-cassandra-data-model">than</a> <a href="http://blog.evanweaver.com/articles/2009/07/06/up-and-running-with-cassandra/">once</a>. In contrast to the descriptions above, I will try to follow a more javaish view which I find easiest and most powerful to work with. I thereby start describing Cassandras data model as nested hash maps. </p>
<p>The way in which data get&#8217;s stored in key/value based databases like Cassandra strongly resembles the use of ordinary hash maps. To recall, hash maps store data for a (unique) key. The key is also later used to retrieve the data from the hash map. For example, in order to map string keys to byte arrays you would write in Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>String, <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This principle stays the same with Cassandra. However, in Cassandra you do not have a single hash map but up to three layers of nested hash maps! What does the mean? Imagine you dont store your values in a single byte array for each key, but again in a hash map, like</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This way you would partition the data you want to store as key/value pairs that are first filled in the data hash map. The data hash map then gets inserted in the higher-order hash map for a given key string. Similarly, to retrieve a value, you would provide the key string and get the data hash map from which you would extract the value you are interested in. </p>
<p>Let us further assume that we dont want to store the key/value pairs as two individual values, but coupled in a class called &#8220;Column&#8221; so that our data model would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, Column<span style="color: #339933;">&gt;&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, Column<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Where <tt>Column</tt> is defined as:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Column <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> name<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> value<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">long</span> timestamp<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Column<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> name, <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> value<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;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">value</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">timestamp</span> <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentMillis</span><span style="color: #009900;">&#40;</span><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>This is already pretty close to what is called a <em>Column Family</em> in Cassandra. You need to restrain yourself from deriving something from the name &#8220;Column&#8221;. Also ignore <tt>timestamp</tt> which is used by Cassandra to avoid data inconsistency and which shall not bother us here.  </p>
<p>Before we go on, let us have a look on a concrete example on how you would need to work with this kind of data structure. Let us assume we want to store the profile data of a single user for some imaginary social networking website.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* data model to store user profiles */</span>
Map<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, Column<span style="color: #339933;">&gt;&gt;</span> user <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, Column<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* create a user 'schabby' */</span>
user.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, Column<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* fill in some profile data for user 'schabby' */</span>
Column age <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;age&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span> 27b <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>age.<span style="color: #006633;">name</span>, age<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Column realName <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;real name&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Johannes Schaback&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>realName.<span style="color: #006633;">name</span>, realName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Column nationality <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nationality&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;German&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>nationality.<span style="color: #006633;">name</span>, nationality<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Again, do not get confused by the use of the byte arrays where normal string would make more sense. This is to resemble the Cassandra data model as close as possible. You will later realize that it&#8217;s actually quite nifty to keep the inner hash map byte based for the price of manually converting everything to byte arrays.</p>
<p>If we want to retrieve values from our data structure, we would need to do as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">byte</span> age <span style="color: #339933;">=</span> user.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;age&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> realName  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>user.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;real name&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> nationality<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>user.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nationality&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this is it. There is not much more conceptual stuff to understand in order to use Cassandra. So we are now ready to project this structure to Cassanda terminology.</p>
<h3>Column Family</h3>
<p>Cassandra structures its data model in <em>keyspaces</em>, <em>Column Families</em> (CF), <em>Columns</em> and <em>SuperColumns</em>.</p>
<p>A keyspace is a namespace to group Column Families and can be compared to a <em>schema</em> or single <em>database</em> in the SQL world. A keyspace contains one or more Column Families.</p>
<p>A Column Family can be seen as a multidimensional hash map like the one in our example above. In the SQL analogy, you may see a Column Family as a single table that belongs to a schema, however this comparison will not take you far. It is really more a dynamically growing and shrinking hash map rather than a table with fixed columns. Still, in Cassandras terminology you speak of <em>rows</em> when you refer to the hash map that you get for a key string. </p>
<p>Rows are accessed by string keys and each row &#8211; which can be seen as a &#8220;data hash map&#8221; &#8211; has several <em>columns</em>. Each column within a row is a bundled pair of a byte array key (a.k.a <tt>name</tt>) and its byte array data field (a.k.a. <tt>value</tt>) very similar to our example.</p>
<p>Depending on your configuration, you can let Cassandra apply a sorting scheme to impose an order over your columns in a row. This enables to query ranges over columns. For example, imagine a telephone book from which you want to retrieve all names starting with &#8220;Smi&#8221;. In Java terms, this could be compared to using <tt>SortedMap</tt> instead of <tt>Map</tt>. But we sticked to <tt>Map</tt> for simplicity here.</p>
<h3>SuperColumns</h3>
<p>The cool thing about Cassandra is its support for an additional hash map layer. This additional layer is added to the Column layer and enables you to store and access your data as a hash map in a hash map in a hash map, or in other words, as a three dimensional hash map. This additional hash map is called a SuperColumn (SC) </p>
<p>In our Java-like example, a Column Family with SuperColumns look like</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, SuperColumn<span style="color: #339933;">&gt;&gt;</span> superColumn 
     <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, SuperColumn<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>where <tt>SuperColumn</tt> is again a hash map over columns like</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SuperColumn <span style="color: #000000; font-weight: bold;">extends</span> HashMap<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, Column<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, I want to point out that the actual SuperColumn definition in Cassandra is different and that this explanatory definition is not too accurate, but nicely serves the illustration purpose.</p>
<p>Similar to normal Columns, the values within a SuperColumn are also stored in an order depending on your configuration, enabling to cut out slices from your SuperColumns.</p>
<p>To continue our social networking site example, let us have a look on how SuperColumns are used to store the friend and relations of the user &#8216;schabby&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* create ColumnFamily with SuperColumns */</span>
Map<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, SuperColumn<span style="color: #339933;">&gt;&gt;</span> columnFamily <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Map<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, SuperColumn<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prepare a SuperColumn for 'schabby' */</span>
columnFamily.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, SuperColumn<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* create SC to store friend info */</span>
SuperColumn friends <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SuperColumn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* fill in some friends */</span>
Column friend1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;friend_1&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Merry&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
friends.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>friend1.<span style="color: #006633;">name</span>, friend1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Column friend2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;friend_2&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Robert&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
friends.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>friend2.<span style="color: #006633;">name</span>, friend2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Column friend3 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;friend_3&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Susan&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
friends.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>friend3.<span style="color: #006633;">name</span>, friend3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* finally store SC in Colunm Family */</span>
columnFamily.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;friends&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, friends<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We are free to create another SuperColumn in the same Column Family to store other list-like data for &#8216;schabby&#8217;, for example his inbox.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* ... continued example */</span>
&nbsp;
SuperColumn inbox <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SuperColumn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* add two mails to inbox */</span>
Column mail1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hi Schabby&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;I hope you are well! Cheers, Nick&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
inbox.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>mail1.<span style="color: #006633;">name</span>, mail1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Column mail2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Column<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;some message body&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
inbox.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>mail2.<span style="color: #006633;">name</span>, mail2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
columnFamily.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inbox&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, inbox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Retrieving the mails from the inbox is straight forward:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* continued example */</span>
&nbsp;
SuperColumn inbox <span style="color: #339933;">=</span> columnFamily.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schabby&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inbox&quot;</span>.<span style="color: #006633;">toBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> subject<span style="color: #339933;">:</span> inbox.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">String</span> body <span style="color: #339933;">=</span> inbox.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// do something with subject/body</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And this is it. I hope this enlightened your understanding of Cassandras data model. It&#8217;s not that difficult all in all, especially when you start using it.</p>
<p>Please leave some comments for corrections and feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/cassandra-getting-started/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Zanox SOAP API Java</title>
		<link>http://schabby.de/zanox-soap-api/</link>
		<comments>http://schabby.de/zanox-soap-api/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 13:26:15 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=693</guid>
		<description><![CDATA[In this post I am going to provide some example code to connect to Zanox revised API via Java and SOAP. The updated API got more coherent and consistent. Its changes mainly address the new authentication model and the application store in which you can feature your own applications based on Zanox data. Now for [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I am going to provide some example code to connect to Zanox revised API via Java and SOAP. </p>
<p>The updated API got more coherent and consistent. Its changes mainly address the new authentication model and the application store in which you can feature your own applications based on Zanox data.<br />
<span id="more-693"></span><br />
Now for the purpose of this post, I sum up how to connect to Zanox SOAP API using Java. There are several steps to do, mostly administrative in nature.</p>
<h2>Get the keys</h2>
<p>Zanox implements a shared key authentication model. Therefore, you need to get yourself the right keys (public and private) to get started. <a href="http://wiki.zanox.com/en/Zanox_Connect_Authentification_Model">Here&#8217;s the page that describes how to get them</a>. Once you created your own application, you will receive the following keys that are necessary prerequisites for your code</p>
<ul>
<li>zanox Application ID</li>
<li>Connect ID</li>
<li>Public key</li>
<li>zanox secret key (aka. private key)</li>
</ul>
<h2>Generate Zanox SOAP Beans From WSDL</h2>
<p>Once you have the keys, you need to generate the classes from the WSDL. For the purpose of this post, we used the (currently latest) WSDL on </p>
<pre>

http://api.zanox.com/wsdl/2009-07-01
</pre>
<p>with the endpoint at</p>
<pre>

http://api.zanox.com/soap/2009-07-01
</pre>
<p>We used <tt>wsimport</tt> to generate the classes as follows</p>
<pre>
wsimport -d bin -s src -p com.zanox.api.namespace._2009_07_01 http://api.zanox.com/wsdl/2009-07-01
</pre>
<p>This should generate you a bunch of class in in the src folder.</p>
<h2>Example Code to Connect to Zanox SOAP API</h2>
<p>In order to run our example code, you need to setup your classpath with the following libraries (actually I am not sure if they are really all necessary)</p>
<ul>
<li>XmlSchema-1.4.3.jar</li>
<li>asm-2.2.3.jar</li>
<li>cxf-2.1.4.jar</li>
<li>geronimo-activation_1.1_spec-1.0.2.jar</li>
<li>geronimo-annotation_1.0_spec-1.1.1.jar</li>
<li>geronimo-jaxws_2.1_spec-1.0.jar</li>
<li>geronimo-stax-api_1.0_spec-1.0.1.jar</li>
<li>geronimo-ws-metadata_2.0_spec-1.1.2.jar</li>
<li>jaxb-api-2.1.jar</li>
<li>jaxb-impl-2.1.9.jar</li>
<li>jaxb-xjc-2.1.9.jar</li>
<li>saaj-api-1.3.jar</li>
<li>saaj-impl-1.3.2.jar</li>
<li>wsdl4j-1.6.2.jar</li>
<li>wstx-asl-3.2.6.jar</li>
<li>xml-resolver-1.2.jar</li>
</ul>
<p>Alrighty, we got all we need to fill in the last bricks. The example code conncets to the API and retrieves the sales of the specified date. The API holds far more methods for different purposes, but they are quit similar in use and once you got the sale-example working (even though you might not generate any sales yet) you can easily infere the correct usage and parameter sets for all other methods. </p>
<p>Create two classes: <tt>ZanoxAuthenticationDto</tt> and <tt>SoapZanoxFacade</tt>. The first DTO is just to hold the authentication data and looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ZanoxAuthenticationDto
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> applicationId<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> connectId<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> sharedKey<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> privateKey<span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// ... according getter and setter methods</span>
 <span style="color: #666666; font-style: italic;">//  are left out for simplicity reasons</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the SoapZanoxFacade class we do the actual work and contain all following code snippets. </p>
<p>We first define a couple of helper methods. The most important one is certainly the one to compute the correct signature that is sent to Zanox for every request.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> createSoapSignature<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> serviceName, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> serviceMethod, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> timestamp, <span style="color: #003399;">String</span> nonce, <span style="color: #003399;">String</span> secreyKey<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> HMAC_SHA1_ALGORITHM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HmacSHA1&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Acquire an HMAC/SHA1 from the raw key bytes.</span>
        SecretKeySpec signingKey <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SecretKeySpec<span style="color: #009900;">&#40;</span>secreyKey.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, HMAC_SHA1_ALGORITHM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Acquire the MAC instance and initialize with the signing key.</span>
        Mac mac <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> 
        <span style="color: #009900;">&#123;</span>
            mac <span style="color: #339933;">=</span> Mac.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>HMAC_SHA1_ALGORITHM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mac.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span>signingKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> 
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;trouble while initiating/signing&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> serviceName.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> serviceMethod.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> timestamp <span style="color: #339933;">+</span> nonce<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Compute the HMAC on the digest, and set it.</span>
        <span style="color: #003399;">String</span> signature <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BASE64Encoder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>mac.<span style="color: #006633;">doFinal</span><span style="color: #009900;">&#40;</span>input.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> signature<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Another one to format the timestamp</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> createSoapTimestamp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> fmt <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd'T'HH:mm:ss.000'Z'&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">SimpleDateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>fmt, <span style="color: #003399;">Locale</span>.<span style="color: #006633;">US</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        df.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> date <span style="color: #339933;">=</span> df.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> date<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>one to converte java.util.Calendar to XmlGregorianCalendar</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> XMLGregorianCalendar toXmlGregorianCalendar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> c<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>c <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    	<span style="color: #009900;">&#123;</span>
    		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ZanoxWsException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Calendar instance must be of type GregorianCalendar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> DatatypeFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newXMLGregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#41;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> 
    	<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DatatypeConfigurationException e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
    		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ZanoxWsException<span style="color: #009900;">&#40;</span>e<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>and the last one to generate a random string of specified length (aka. nonce)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> createNonce<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> length<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> CHARS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;01234567890abcdefghijkmlmnopqrstuvwxyz&quot;</span><span style="color: #339933;">;</span>
    	StringBuilder sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<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><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> length<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    		sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>CHARS.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>CHARS.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> sb.<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: #009900;">&#125;</span></pre></div></div>

<p>The final code snipped finally retrieves the sales:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Retrieve the first 10K sales for day &lt;code&gt;calTime&lt;/code&gt;. 
	 * Paging is hard-wired to first page (in other words: no paging)
	 */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getSales<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> calTime, ZanoxAuthenticationDto auth<span style="color: #009900;">&#41;</span>  
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> timestamp <span style="color: #339933;">=</span> createSoapTimestamp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> nonce     <span style="color: #339933;">=</span> createNonce<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> signature <span style="color: #339933;">=</span> createSoapSignature<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PublisherService&quot;</span>, <span style="color: #0000ff;">&quot;GetSales&quot;</span>, timestamp, nonce, auth.<span style="color: #006633;">getPrivateKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        XMLGregorianCalendar date <span style="color: #339933;">=</span> toXmlGregorianCalendar<span style="color: #009900;">&#40;</span>calTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        GetSalesRequest getSalesRequest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GetSalesRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setConnectId</span><span style="color: #009900;">&#40;</span>auth.<span style="color: #006633;">getConnectId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setDate</span><span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span>timestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setPage</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setItems</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setSignature</span><span style="color: #009900;">&#40;</span>signature<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getSalesRequest.<span style="color: #006633;">setNonce</span><span style="color: #009900;">&#40;</span>nonce<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        GetSalesResponse response <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> 
        <span style="color: #009900;">&#123;</span>
            response <span style="color: #339933;">=</span> getPublisher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSales</span><span style="color: #009900;">&#40;</span>getSalesRequest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> 
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ZanoxWsException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error while fetching sales from Zanox SOAP API&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> 
&nbsp;
        <span style="color: #666666; font-style: italic;">// TODO: do something sensible with the data...</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> SaleItem item<span style="color: #339933;">:</span> response.<span style="color: #006633;">getSaleItems</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSaleItem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</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>item.<span style="color: #006633;">getProgram</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> &quot;</span><span style="color: #339933;">+</span>item.<span style="color: #006633;">getSubPublisher</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">+</span>item.<span style="color: #006633;">getClickId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">+</span>item.<span style="color: #006633;">getAmount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">+</span>item.<span style="color: #006633;">getLptId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">+</span>item.<span style="color: #006633;">getCommission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> PublisherSoapPortType getPublisher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> PublisherService pubs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PublisherService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> pubs.<span style="color: #006633;">getPublisherSoapPort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>To run the code, create a main methode where you set up you authentication data and call getSales on SoapZanoxFacade.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <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>
    	ZanoxAuthenticationDto dto <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ZanoxAuthenticationDto<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	dto.<span style="color: #006633;">setConnectId</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ASL22DKAS223SKD55KAgD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	dto.<span style="color: #006633;">setSharedKey</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JY89AFLLA(RKJZZ&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	dto.<span style="color: #006633;">setPrivateKey</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;asdkljah893qisdnfsyniweoi8ynw893nw&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	dto.<span style="color: #006633;">setApplicationId</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sdfs4w9p4uao8oadf9fusm9d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #003399;">Calendar</span> c <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	c.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2009</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	SoapZanoxFacade l <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapZanoxFacade<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	l.<span style="color: #006633;">getSales</span><span style="color: #009900;">&#40;</span>c, dto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This should get you going with the API. All other SOAP requests are built in a similar fashion. </p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/zanox-soap-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

