<?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>Technotrance, Illusions and Perspectives &#187; google-guice</title>
	<atom:link href="http://myblog.shriharisc.com/tag/google-guice/feed/" rel="self" type="application/rss+xml" />
	<link>http://myblog.shriharisc.com</link>
	<description>A dose of everyday bruises with Java/JEE</description>
	<lastBuildDate>Mon, 12 Jul 2010 17:55:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Integrating Hibernate Search on PrimeFaces using Google Guice and JBoss Seam</title>
		<link>http://myblog.shriharisc.com/2009/11/14/integrating-hibernate-search-on-primefaces-using-google-guice-and-jboss-seam/</link>
		<comments>http://myblog.shriharisc.com/2009/11/14/integrating-hibernate-search-on-primefaces-using-google-guice-and-jboss-seam/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 18:58:27 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[google-guice]]></category>
		<category><![CDATA[hibernate-search]]></category>
		<category><![CDATA[jboss-seam]]></category>
		<category><![CDATA[primefaces]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=133</guid>
		<description><![CDATA[Recently I have been working on an application involving PrimeFaces and required search capabilities directly on the persisted data. Though I can use Hibernate search for implementing search services, the Google Guice, the underlying IoC framework, which PrimeFaces provide does not provide me the out-of-the-box integration. I am extending from my previous blog post which [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been working on an application involving <a href="http://primefaces.prime.com.tr/en/">PrimeFaces</a> and required search capabilities directly on the persisted data. Though I can use Hibernate search for implementing search services, the Google Guice, the underlying IoC framework, which PrimeFaces provide does not provide me the out-of-the-box integration. I am extending from my <a href="http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/">previous blog post</a> which talked about using Google Guice to inject JPA EntityManager. This post builds on the previous post and explores the option of search. Here are the following sequence of steps I followed to ensure PrimeFaces widgets are Hibernate Search compliant:</p>
<p>1)  Follow the steps mentioned to get an EntityManager instance fro m JBoss Seam&#8217;s component xml and register in the Google Guice injector using a Provider  implementation. Make a little change where the Provider&#8217;s getter method is made static.  Following is the EntityManagerProvider code snippet to do that:</p>
<pre class="brush: java">
class EntityManagerProvider implements Provider
{       static EntityManagerFactory entityManagerFactory = null;
        public static EntityManager get()
        {   return entityManagerFactory.createEntityManager();
        }
}
</pre>
<p>2) Implement Guice injection Provider for constructing a FullTextEntityManager from EntityManager</p>
<pre class="brush: java">
public class FullTextEntityManagerProvider implements Provider
{    static FullTextEntityManager fullTextManager = null;
     private static EntityManager entityManager;

     public static void setEntityManager(EntityManager em)
     {   entityManager = em;
     }

      public FullTextEntityManager get()
      {   if(fullTextManager==null)
             fullTextManager = Search.getFullTextEntityManager(entityManager);
           return fullTextManager;
      }
}
</pre>
<p>3) Modify the custom module (now refactored as MySearchEntityManagerModule) to construct the FullTextEntityManager using EntityManager instance as shown below.</p>
<pre class="brush: java">
public class MySearchEntityManagerModule extends AbstractModule
{      public void configure()
       {   Expressions expressions = Expressions.instance();
           ValueExpression emfVE = expressions.createValueExpression(&quot;#{my_persistence_unit}&quot;);
           EntityManagerFactory emf = (EntityManagerFactory)emfVE.getValue();
           EntityManagerProvider.setEntityManagerFactory(emf);
           FullTextEntityManagerProvider.setEntityManager(provider.get());

            //bind EntityManager and FullTextEntityManager for both pure database and search specific integration
            bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(Scopes.SINGLETON);
            bind(FullTextEntityManager.class).toProvider(FullTextEntityManagerProvider.class).in(Scopes.SINGLETON);
     }
}
</pre>
<p>3) Inject the FullTextEntityManager in the dataservice (DAO) for searching for text tokens using Hibernate Search.</p>
<pre class="brush: java">
@Name(&quot;hybrid&quot;)
@Guice
public class HybridDAO
{  @Inject FullTextEntityManager searchManager;
    //other DAO methods to perform search using Hibernate Search and Lucene
}
</pre>
<p>This quick tip provides us a capability of enhancing Primefaces widgets to use the power of Hibernate Search for search and much more extensible features.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/11/14/integrating-hibernate-search-on-primefaces-using-google-guice-and-jboss-seam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading Spring Context from Google Guice</title>
		<link>http://myblog.shriharisc.com/2009/09/15/loading-spring-context-from-google-guice/</link>
		<comments>http://myblog.shriharisc.com/2009/09/15/loading-spring-context-from-google-guice/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 15:26:21 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[google-guice]]></category>
		<category><![CDATA[guiceyfruit]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=114</guid>
		<description><![CDATA[Using Spring framework in any application opens a plethora of opportunities with regard to resolving complex requirement needs in lieu of the number of extensible components available based on the Spring framework. Some of the most preferred specification based stacks (such as Apache CXF)  are coupled with Spring Framework, and it would call for a [...]]]></description>
			<content:encoded><![CDATA[<p>Using Spring framework in any application opens a plethora of opportunities with regard to resolving complex requirement needs in lieu of the number of extensible components available based on the Spring framework. Some of the most preferred specification based stacks (such as Apache CXF)  are coupled with Spring Framework, and it would call for a need to figure out options of getting the best of breed application stacks.</p>
<p>This particular entry looks at loading Spring Framework from Google Guice container. There are two approaches I have across till now, which I will try covering in breif:</p>
<p><span style="text-decoration:underline;"><strong>1) Using Guice&#8217;s SpringIntegration</strong></span></p>
<p>In this approach you need to download guice-spring.jar (version 1.0) or if your project is maven based, add the following dependency.</p>
<pre class="brush: xml">
&lt;dependency&gt;
     &lt;groupId&gt;com.google.inject.integration&lt;/groupId&gt;
     &lt;artifactId&gt;guice-spring&lt;/artifactId&gt;
     &lt;version&gt;1.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
<p>You need to generalize an AbstractModule and use the com.google.inject.spring.SpringIntegration to load the Spring context into the Guice&#8217;s container.</p>
<pre class="brush: java">
import com.google.inject.spring.SpringIntegration;
import com.google.inject.AbstractModule;
//other imports...
public class SpringContextModule extends AbstractModule
{ @Override
   protected void configure()
   { ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;appcontext-config.xml&quot;);
     SpringIntegration.bindAll(binder(), applicationContext);
    }
}
</pre>
<p><strong><span style="text-decoration:underline;">2) Using GuiceyFruit&#8217;s SpringModule</span></strong></p>
<p>In this approach,  the dependency injection is based on JSR-250 common annotations specification and is resolved using the Spring annotation @Autowired (i.e.) all the beans have to be modified, introducing the annotation wherever the injection is required. This approach is suitable for all the beans for which we have control on the source code, and may not be a viable option for integration proven Spring modular components. In case if you have maven project, you need to include the below dependency:</p>
<pre class="brush: xml">
 &lt;dependency&gt;
      &lt;groupId&gt;org.guiceyfruit&lt;/groupId&gt;
      &lt;artifactId&gt;guiceyfruit-spring&lt;/artifactId&gt;
      &lt;version&gt;2.0-beta-6&lt;/version&gt;
    &lt;/dependency&gt;
</pre>
<p>and create a Guice injector using</p>
<pre class="brush: java">
Injector injector = Guice.createInjector(new SpringModule());
</pre>
<p>More details can be looked at <a href="http://code.google.com/p/guiceyfruit/wiki/Spring">Guicey-Spring integration wiki page.</a></p>
<p>Approach 1 is prefered for integration of various most commonly used module component common both between Guice and Spring, as the configuration context can be reused, while the other approach is good enough for custom modules in Spring to be injected in Google Guice.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/09/15/loading-spring-context-from-google-guice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using FreemarkerServlet in Google Guice to inject Configuration</title>
		<link>http://myblog.shriharisc.com/2009/09/05/using-freemarkerservlet-in-google-guice-to-inject-configuration/</link>
		<comments>http://myblog.shriharisc.com/2009/09/05/using-freemarkerservlet-in-google-guice-to-inject-configuration/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 19:01:01 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[freemarker]]></category>
		<category><![CDATA[google-guice]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=95</guid>
		<description><![CDATA[Freemarker is a fantastic template parsing framework , and has its own avantages over Apache Velocity.  Google Guice is a straightforward injection framework which injects abstraction-driven ,instance-driven, or annotation driven module classes using bindings.  In order 2 get the best of both these frameworks, the following snippet would aid in doing so.
1) Modify the WEB-INF/web.xml [...]]]></description>
			<content:encoded><![CDATA[<p>Freemarker is a fantastic template parsing framework , and has its own avantages over Apache Velocity.  Google Guice is a straightforward injection framework which injects abstraction-driven ,instance-driven, or annotation driven module classes using bindings.  In order 2 get the best of both these frameworks, the following snippet would aid in doing so.</p>
<p>1) Modify the WEB-INF/web.xml to define FreemarkerServlet</p>
<pre class="brush: xml">
&lt;servlet&gt;
 &lt;servlet-name&gt;freemarker&lt;/servlet-name&gt;
 &lt;servlet-class&gt;com.ts.guicefmkr.web.TemplateServlet&lt;/servlet-class&gt;
 &lt;init-param&gt;
 &lt;param-name&gt;TemplatePath&lt;/param-name&gt;
 &lt;param-value&gt;/WEB-INF/ftl&lt;/param-value&gt;
 &lt;/init-param&gt;
 &lt;init-param&gt;
 &lt;param-name&gt;NoCache&lt;/param-name&gt;
 &lt;param-value&gt;true&lt;/param-value&gt;
 &lt;/init-param&gt;
 &lt;init-param&gt;
 &lt;param-name&gt;ContentType&lt;/param-name&gt;
 &lt;param-value&gt;text/html&lt;/param-value&gt;
 &lt;/init-param&gt;
 &lt;init-param&gt;
 &lt;param-name&gt;template_update_delay&lt;/param-name&gt;
 &lt;param-value&gt;0&lt;/param-value&gt;
 &lt;/init-param&gt;
 &lt;init-param&gt;
 &lt;param-name&gt;default_encoding&lt;/param-name&gt;
 &lt;param-value&gt;ISO-8859-1&lt;/param-value&gt;
 &lt;/init-param&gt;
 &lt;init-param&gt;
 &lt;param-name&gt;number_format&lt;/param-name&gt;
 &lt;param-value&gt;0.##########&lt;/param-value&gt;
 &lt;/init-param&gt;
 &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
 &lt;/servlet&gt;
</pre>
<p>2) Extend the FreemarkerServlet and implement to Guice&#8217;s Module to bind the created  Configuration object</p>
<pre class="brush: java">
import com.google.inject.Binder;
import com.google.inject.Injector;
import com.google.inject.Module;
import javax.servlet.ServletException;

import freemarker.ext.servlet.FreemarkerServlet;
import freemarker.template.Configuration;
public class TemplateServlet extends FreemarkerServlet implements Module
{   private Configuration templateConfig;
    public Configuration getTemplateConfig()
    {      return templateConfig;
    }

     public void setTemplateConfig(Configuration templateConfig) {
        this.templateConfig = templateConfig;
     }

    public void init() throws ServletException
     {   super.init();
         templateConfig = getConfiguration();
        Injector injector = com.google.inject.Guice.createInjector(this);
       }

      public void configure(Binder binder)
     {   binder.bind(Configuration.class).toInstance(templateConfig);
     }
}
</pre>
<p>Thus you could use Configuration object anywhere in your application using @Inject from Guice.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/09/05/using-freemarkerservlet-in-google-guice-to-inject-configuration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injecting JPA EntityManager in Google Guice through JBoss Seam</title>
		<link>http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/</link>
		<comments>http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 18:17:44 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[google-guice]]></category>
		<category><![CDATA[jboss-seam]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=87</guid>
		<description><![CDATA[This cool tip describes a 3 step process to use JPA&#8217;s EntityManager initialized in JBoss Seam inside the Google Guice environment.
1). Define Seam&#8217;s components.xml with entity-manager-factory component as below

&#60;components&#62;
    &#60;persistence:entity-manager-factory name=&#34;my_persistence_unit&#34;/&#62;
    &#60;guice:init injector=&#34;#{myInjector}&#34;/&#62;
    &#60;guice:injector name=&#34;myInjector&#34;&#62;
        &#60;guice:modules&#62;
    [...]]]></description>
			<content:encoded><![CDATA[<p>This cool tip describes a 3 step process to use JPA&#8217;s EntityManager initialized in JBoss Seam inside the Google Guice environment.</p>
<p>1). Define Seam&#8217;s components.xml with entity-manager-factory component as below</p>
<pre class="brush: xml">
&lt;components&gt;
    &lt;persistence:entity-manager-factory name=&quot;my_persistence_unit&quot;/&gt;
    &lt;guice:init injector=&quot;#{myInjector}&quot;/&gt;
    &lt;guice:injector name=&quot;myInjector&quot;&gt;
        &lt;guice:modules&gt;
           &lt;value&gt;mypackage.MyEntityManagerModule&lt;/value&gt;
        &lt;/guice:modules&gt;
    &lt;/guice:injector&gt;
&lt;/components&gt;</pre>
<p>where my_persistence_unit is the persistence unit name under META-INF/persistence.xml.</p>
<p>2).   Define a Google Guice Module to wire-up Seam&#8217;s EnitiyManagerFactory by looking-up based on the expression-value and binding it to Guice context using a Provider</p>
<pre class="brush: java">
public class MyEntityManagerModule extends AbstractModule
{   public void configure()
    {   Expressions expressions = Expressions.instance();
        ValueExpression emfVE = expressions.createValueExpression(&quot;#{my_persistence_unit}&quot;);
        EntityManagerFactory emf = (EntityManagerFactory)emfVE.getValue();
        EntityManagerProvider.setEntityManagerFactory(emf);
        bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(Scopes.SINGLETON);
     }
 }
 </pre>
<pre class="brush: java">
class EntityManagerProvider implements Provider
    {   static EntityManagerFactory entityManagerFactory = null;
        public EntityManager get()
        {   return entityManagerFactory.createEntityManager();
        }
    }
 </pre>
<p>3)   One could use the EntityManager anywhere in the Seam-Guice hybrid component:</p>
<pre class="brush: java">
@Name(&quot;hybrid&quot;)
@Guice
public class HybridDAO
{  @Inject EntityManager entityManager;
   //other DAO methods.
}
</pre>
<p>This makes Guice components use EntityManager normally use Seam established EntityManagerFactory</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Method entry-exit logging in Google Guice through AOP</title>
		<link>http://myblog.shriharisc.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/</link>
		<comments>http://myblog.shriharisc.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 17:59:50 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[google-guice]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=83</guid>
		<description><![CDATA[This cool tip explains 2 step logic to log all methods entry and exit information with profiling in Guice with AOP (aop-alliance)
1) Define a Guice Module with a method level logging Interceptor binding at all class levels and method levels.

public class MyModule extends AbstractModule
{     public void configure()
     [...]]]></description>
			<content:encoded><![CDATA[<p>This cool tip explains 2 step logic to log all methods entry and exit information with profiling in Guice with AOP (aop-alliance)</p>
<p>1) Define a Guice Module with a method level logging Interceptor binding at all class levels and method levels.</p>
<pre class="brush: java">
public class MyModule extends AbstractModule
{     public void configure()
      {      bindInterceptor(Matchers.any(), Matchers.any(), new LoggingMethodInterceptor());
      }
}
</pre>
<p>2) Define a method level logging interceptor to log entry and exit messages with profiled information.</p>
<pre class="brush: java">
public class LoggingMethodInterceptor implements MethodInterceptor
{   public Object invoke(MethodInvocation invocation) throws Throwable
    {    Object returnValue = null;
    	 long start = System.currentTimeInMillis();
         System.out.println(&quot;Method &quot;+invocation.getMethod()+&quot; entered from &quot;+invocation.getThis()+&quot; with arguments &quot;+invocation.getArguments());
         returnValue = invocation.proceed();
         long end = System.currentTimeInMillis();
         System.out.println(&quot;Method &quot;+invocation.getMethod()+&quot; exited in &quot;+((end-start)/1000L)+&quot; seconds&quot;);
         return returnValue;
    }
}
</pre>
<p>One could use this logic to extend and introduce many cross-cutting concerns such as Transaction Management, secure access to method </p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Custom injection example in Google Guice</title>
		<link>http://myblog.shriharisc.com/2009/07/10/custom-injection-example-in-google-guice/</link>
		<comments>http://myblog.shriharisc.com/2009/07/10/custom-injection-example-in-google-guice/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 12:11:19 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[google-guice]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=64</guid>
		<description><![CDATA[Google Guice supports custom injections using 2 endpoints: type listener and injection listener and registeration of end-points. You may read the whole theory behind here, where a Logging Injection using Log4J is explained.
Lets try to implement that and see what more is required. In order to test the whole exercise, we could embrace TestNG to [...]]]></description>
			<content:encoded><![CDATA[<p>Google Guice supports custom injections using 2 endpoints: type listener and injection listener and registeration of end-points. You may read the <a title="Google Guice Custom Injections" href="http://code.google.com/p/google-guice/wiki/CustomInjections">whole theory behind here</a>, where a Logging Injection using Log4J is explained.</p>
<p>Lets try to implement that and see what more is required. In order to test the whole exercise, we could embrace TestNG to write a sample unit test-case using a dummy class have a method.   Lets write a simple class which uses the Log4J Logger instance, injected using InjectLogger.</p>
<pre class="brush: java">
public class LoggingUsage  
{   @InjectLogger org.apache.log4j.Logger log;
     public int doAddition(int a,int b)
     {      int c = a+b;
           log.info(&quot;Added result: &quot;+c);
           return c;
     }
}
</pre>
<p>Lets us now a write a TestNG testcase to try and test the same.<br />
public class CustomLoggerInjectionTest<br />
{    LoggingUsage logUsage;<br />
     @BeforeMethod<br />
     public void initLogUsage()<br />
    {     logUsage = Guice.createInjector(new LoggerModule(),new MyLoggerModule()).getInstance(LoggingUsage.class);<br />
    }</p>
<p>    @Test<br />
    public void testLogUsage()<br />
    {   int value = logUsage.doAddition(20, 15);<br />
        assertEquals(value,35);<br />
    }</p>
<p>   static class MyLoggerModule extends AbstractModule<br />
   {   @Override<br />
        protected void configure()<br />
        {      bind(LoggingUsage.class);<br />
        }<br />
    }<br />
}<br />
[/sourcecode]<br />
Lets examine this testcase. Just before test execution, we register the LoggerModule, which binds the LOG4J specific listeners (type/injections) and custom module to bind our custom class which needs Logger injection we wrote above, and instantiate the class that uses this Logger instance. When the test executes, the usage-class would already have a injected Logger and would execute without issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/07/10/custom-injection-example-in-google-guice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
