<?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>Netphase &#187; java</title>
	<atom:link href="http://blog.netphase.com/category/blog/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netphase.com</link>
	<description>for a connected world</description>
	<lastBuildDate>Mon, 14 Dec 2009 21:40:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby on Rails with Spring, Hibernate and JPA</title>
		<link>http://blog.netphase.com/2009/01/23/ruby-on-rails-with-spring-hibernate-and-jpa/</link>
		<comments>http://blog.netphase.com/2009/01/23/ruby-on-rails-with-spring-hibernate-and-jpa/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 18:56:42 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.netphase.com/?p=248</guid>
		<description><![CDATA[Scott led a session at Bar Camp extolling the virtues of running your Ruby on Rails app in a pure Java environment, accessing the same resources as your programming brethren.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m leading a discussion on this topic at the <a href="http://barcampcharlotte.com/">Charlotte BarCamp</a> tomorrow.</p>
<p>Many companies who have invested a lot of time and money in Java technology could still benefit greatly from Ruby on Rails by using it to build new sites that make use of their backend Java based services.</p>
<p>Here&#8217;s an example of an <a href="http://rspec.info/">RSpec</a> test that uses a Java DAO, provided by <a href="http://www.springsource.org">Spring</a> to access a database with <a href="http://java.sun.com/javaee/technologies/persistence.jsp">JPA</a> / <a href="http://www.hibernate.org/">Hibernate</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">it <span style="color:#996600;">&quot;should retrieve an Account by ID&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  load_data <span style="color:#996600;">&quot;dao/accounts.xml&quot;</span>
  dao = spring.<span style="color:#9900CC;">getBean</span> <span style="color:#996600;">'accountDao'</span>
  acct = dao.<span style="color:#9900CC;">find</span> <span style="color:#996600;">&quot;5000&quot;</span>
  acct.<span style="color:#9900CC;">should_not</span> be_nil
  acct.<span style="color:#9900CC;">getName</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">&quot;Alpha&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It&#8217;s surprisingly easy to use a Spring context here by adding the following method to my spec_helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">import <span style="color:#996600;">'org.springframework.context.support.ClassPathXmlApplicationContext'</span>
import <span style="color:#996600;">'org.springframework.core.io.ClassPathResource'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> spring<span style="color:#006600; font-weight:bold;">&#40;</span>context_file=<span style="color:#996600;">&quot;beans.xml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  java.<span style="color:#9900CC;">lang</span>.<span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">current_thread</span>.<span style="color:#9900CC;">context_class_loader</span> =
      JRuby.<span style="color:#9900CC;">runtime</span>.<span style="color:#9900CC;">getJRubyClassLoader</span>
  <span style="color:#0066ff; font-weight:bold;">@ctx</span> <span style="color:#006600; font-weight:bold;">||</span>= ClassPathXmlApplicationContext.<span style="color:#9900CC;">new</span> context_file
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Using that helper I am able to access any component managed by Spring.  As designed, Spring will wire up any required dependencies, provide a pooled database connection, and handle any other configuration necessary so that the bean returned is ready to use.</p>
<p>My test also uses <a href="http://www.dbunit.org/">DBUnit</a> to load an XML fixture.  It can do that by adding this method to spec_helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">import <span style="color:#996600;">'org.dbunit.database.DatabaseConnection'</span>
import <span style="color:#996600;">'org.dbunit.dataset.xml.FlatXmlDataSet'</span>
import <span style="color:#996600;">'org.dbunit.operation.DatabaseOperation'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> load_data<span style="color:#006600; font-weight:bold;">&#40;</span>data_file<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#0066ff; font-weight:bold;">@dataSource</span> <span style="color:#006600; font-weight:bold;">||</span>= spring.<span style="color:#9900CC;">getBean</span> <span style="color:#996600;">'dataSource'</span>
    <span style="color:#0066ff; font-weight:bold;">@conn</span> <span style="color:#006600; font-weight:bold;">||</span>= DatabaseConnection.<span style="color:#9900CC;">new</span> <span style="color:#0066ff; font-weight:bold;">@dataSource</span>.<span style="color:#9900CC;">getConnection</span>
    <span style="color:#0066ff; font-weight:bold;">@dataset</span> <span style="color:#006600; font-weight:bold;">||</span>=
        FlatXmlDataSet.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassPathResource.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>data_file<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">getInputStream</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#6666ff; font-weight:bold;">DatabaseOperation::CLEAN_INSERT</span>.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span>@conn, <span style="color:#0066ff; font-weight:bold;">@dataset</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span>
    <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;load_data error: &quot;</span>, $!, <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>DBUnit provides a useful way to load data fixtures into a test database.  The reason I&#8217;m using DBUnit instead of the more common YAML format used in Rails apps, is so that I can make use of the fixtures already available in the Java project.</p>
<p>Here&#8217;s what my DBUnit fixture looks like:</p>
<p>This combination of technologies seems like a pretty powerful combination to me, especially for those companies who are entrenched in Java and are looking for a way to improve their productivity.</p>
<p>I&#8217;m interested to know who else is using this type of setup.  Leave a comment and describe your experience.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2009/01/23/ruby-on-rails-with-spring-hibernate-and-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing a Java AXIS web service with Ruby and SOAP</title>
		<link>http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/</link>
		<comments>http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/#comments</comments>
		<pubDate>Mon, 12 Feb 2007 19:04:38 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/</guid>
		<description><![CDATA[I&#8217;ve been working with soap4r in Ruby to access some Java web services which use AXIS 1.1.  The particular api I&#8217;m accessing requires an initial authentication call which returns a token that must be passed in all subsequent SOAP headers.  This is a pretty common scenario, but it took me awhile to get [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with soap4r in Ruby to access some Java web services which use AXIS 1.1.  The particular api I&#8217;m accessing requires an initial authentication call which returns a token that must be passed in all subsequent SOAP headers.  This is a pretty common scenario, but it took me awhile to get it working correctly.</p>
<p>There were basically two &#8220;tricks&#8221;.  The first which I found on a few other blogs entails creating a HeaderHandler (some suggested SimpleHeaderHandler, but that didn&#8217;t work for me because of trick #2).  Here&#8217;s what I ended up with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ClientAuthHeaderHandler <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">SOAP::Header::Handler</span>
  HEADER = 
    <span style="color:#6666ff; font-weight:bold;">XSD::QName</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://www.foo.com/SecurityPass'</span>, 
                             <span style="color:#996600;">'SecurityPass'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>token<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span>HEADER<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@token</span> = token
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> on_outbound
    el = <span style="color:#6666ff; font-weight:bold;">SOAP::SOAPElement</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>HEADER, <span style="color:#0066ff; font-weight:bold;">@token</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    el.<span style="color:#9900CC;">extraattr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'xsi:type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;xsd:string&quot;</span>
    <span style="color:#6666ff; font-weight:bold;">SOAP::SOAPHeaderItem</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>el, <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The second &#8220;trick&#8221; was in the on_outbound method.  What I found was that AXIS was requiring the xsi:type to be included in the header that I added or it wouldn&#8217;t be recognized.  Using extraattr was the only way I could figure out how to coerce it to be included.  I suspect at some point this will be unnecessary, but for now it did the trick.</p>
<p>Here&#8217;s a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FooService
  <span style="color:#008000; font-style:italic;"># pass STDOUT to wiredump to see messages</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>uri, wiredump=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@uri</span> = uri
    <span style="color:#0066ff; font-weight:bold;">@wiredump</span> = wiredump
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> svc<span style="color:#006600; font-weight:bold;">&#40;</span>ref<span style="color:#006600; font-weight:bold;">&#41;</span>
    ws = <span style="color:#6666ff; font-weight:bold;">SOAP::WSDLDriverFactory</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{@uri}#{ref}?wsdl&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">create_rpc_driver</span>
    ws.<span style="color:#9900CC;">wiredump_dev</span> = <span style="color:#0066ff; font-weight:bold;">@wiredump</span>
    ws.<span style="color:#9900CC;">generate_explicit_type</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> !@sec_token.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      ws.<span style="color:#9900CC;">headerhandler</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> ClientAuthHeaderHandler.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>@sec_token<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    ws
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> authenticate<span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    ws = svc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/services/AuthenticationService&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@sec_token</span> = ws.<span style="color:#9900CC;">authenticate</span><span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> lookup<span style="color:#006600; font-weight:bold;">&#40;</span>stuff<span style="color:#006600; font-weight:bold;">&#41;</span>
    ws = svc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/services/LookupService&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    ws.<span style="color:#9900CC;">lookup</span><span style="color:#006600; font-weight:bold;">&#40;</span>stuff<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
