<?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>Blog of Adam Warski</title>
	<atom:link href="http://www.warski.org/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.warski.org/blog</link>
	<description>Java and JBoss related stuff</description>
	<lastBuildDate>Sat, 21 Aug 2010 15:23:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Envers 1.2.3-hibernate-3.3 &#8211; backport release</title>
		<link>http://www.warski.org/blog/?p=257</link>
		<comments>http://www.warski.org/blog/?p=257#comments</comments>
		<pubDate>Sat, 21 Aug 2010 15:23:52 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Envers]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=257</guid>
		<description><![CDATA[Hernan Chanfreau backported some of the recent Envers changes and fixes from the 3.5 branch. The detailed list of the backported issues can be found in this forum post. 
I applied the patch and released a new Hibernate-3.3 compatible Envers version, it&#8217;s available in the JBoss Maven repository.
Thanks a lot to Hernan for the work [...]]]></description>
			<content:encoded><![CDATA[<p>Hernan Chanfreau backported some of the recent Envers changes and fixes from the 3.5 branch. The detailed list of the backported issues can be found in <a href="http://community.jboss.org/message/553486#553486">this forum post</a>. </p>
<p>I applied the patch and released a new Hibernate-3.3 compatible Envers version, it&#8217;s available in the <a href="https://repository.jboss.org/nexus/content/groups/public-jboss/org/jboss/envers/jboss-envers/1.2.3-hibernate-3.3/">JBoss Maven repository</a>.</p>
<p>Thanks a lot to Hernan for the work and providing the patch! :)</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=257</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New features in cdi-ext</title>
		<link>http://www.warski.org/blog/?p=249</link>
		<comments>http://www.warski.org/blog/?p=249#comments</comments>
		<pubDate>Mon, 02 Aug 2010 18:28:15 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Weld]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=249</guid>
		<description><![CDATA[Tomek Szymański just commited two new features to cdi-ext.
The first is the ability to secure JSF pages when using the Nav component to handle navigation (more on it here). If you want to make a page accessible only if a certain EL expression is true, when defining a page, you can write:


private final Page adminPage [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/szimano">Tomek Szymański</a> just commited two new features to <a href="http://github.com/adamw/cdiext">cdi-ext</a>.</p>
<p>The first is the ability to secure JSF pages when using the Nav component to handle navigation (more on it <a href="http://www.warski.org/blog/?p=185">here</a>). If you want to make a page accessible only if a certain EL expression is true, when defining a page, you can write:</p>
<pre name="code" class="java">

private final Page adminPage = new ViewIdPageBuilder(&quot;/admin.xhtml&quot;)
    .setRequiresLogin(true)
    .setSecurityEL(&quot;#{currentUser.isAdmin)&quot;).b();
</pre>
<p>If the condition is not met, the user will get 403 Forbidden. Also, Tomek extended handling of <code>setRequiresLogin(true)</code>, so that if a user is redirected to the login page, because he was not logged in and the page is secured, the page which the user tried to access is stored and after logging in, he is redirected back to the original page.</p>
<p>The second is the ability to specify the transaction timeout using a <code>@TransactionTimeout(timeout = 5)</code> annotation on a method.</p>
<p>Moreover, we have moved our repository infrastructure to Nexus. The new address where cdi-ext artifacts are deployed is <a href="http://tools.softwaremill.pl/nexus/content/repositories/snapshots">http://tools.softwaremill.pl/nexus/content/repositories/snapshots</a>.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=249</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails + CDI? Why not! Enter TorqueBox + Weld</title>
		<link>http://www.warski.org/blog/?p=245</link>
		<comments>http://www.warski.org/blog/?p=245#comments</comments>
		<pubDate>Tue, 27 Jul 2010 12:07:33 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TorqueBox]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Weld]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=245</guid>
		<description><![CDATA[I guess many people are often &#8220;unsatisfied&#8221; with how JSF works and how much time it sometimes takes to do a simple thing. That&#8217;s why we are trying out a new combination: RoR for the frontend and CDI for the backend. How?
Deploying RoR applications to JBoss AS is really easy thanks to the TorqueBox project. [...]]]></description>
			<content:encoded><![CDATA[<p>I guess many people are often &#8220;unsatisfied&#8221; with how JSF works and how much time it sometimes takes to do a simple thing. That&#8217;s why <a href="http://www.softwaremill.eu">we</a> are trying out a new combination: RoR for the frontend and CDI for the backend. How?</p>
<p>Deploying RoR applications to JBoss AS is really easy thanks to the <a href="http://www.torquebox.com/">TorqueBox</a> project. You just  deploy a <code>.yml</code> file using the provided <code>rake</code> tasks and you can develop the application &#8220;live&#8221; &#8211; no redeploys, instant changes, and so on.</p>
<p>Using RoR as a frontend to a CDI/Weld based application requries two more steps, so that RoR can see the business logic classes and share the same http session with CDI (so it&#8217;s possible to access <code>@SessionScoped</code> beans from RoR and CDI code). </p>
<p>First you need to deploy your application in the <code>DefaultDomain</code> (at least until <a href="https://jira.jboss.org/browse/TORQUE-85">TORQUE-85</a> is fixed). To do this, add a <code>jboss-classloading.xml</code> file to the <code>META-INF</code> directory with this content:</p>
<pre name="code" class="xml">

&lt;classloading xmlns=&quot;urn:jboss:classloading:1.0&quot;
              domain=&quot;DefaultDomain&quot;
              top-level-classloader=&quot;true&quot;
              export-all=&quot;NON_EMPTY&quot;
              import-all=&quot;true&quot;&gt;
&lt;/classloading&gt;
</pre>
<p>Secondly, you need to add a filter to RoR&#8217;s web application, so that Weld and RoR share the same session. Just edit <code>config/web.xml</code> in your RoR application (the magic in the RoR deployer will add it to the virtual .war deployment it creates) and add the following:</p>
<pre name="code" class="xml">

&lt;web-app&gt;
    &lt;listener&gt;
        &lt;listener-class&gt;org.jboss.weld.servlet.WeldListener&lt;/listener-class&gt;
    &lt;/listener&gt;
&lt;/web-app&gt;
</pre>
<p>Now RoR and CDI share the same session (so you can use <code>@SessionScoped</code> beans etc, probably also <code>@ConversationScoped</code>, but I haven&#8217;t tried that). You can lookup CDI beans from RoR code using e.g. the <a href="http://github.com/adamw/cdiext">BeanInject class from cdi-ext</a>, or just by writing a very simple utility method which lookups the <code>BeanManager</code>.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=245</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Initial valid-time support in Envers</title>
		<link>http://www.warski.org/blog/?p=239</link>
		<comments>http://www.warski.org/blog/?p=239#comments</comments>
		<pubDate>Fri, 02 Jul 2010 12:12:42 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Envers]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=239</guid>
		<description><![CDATA[I just commited initial support for valid-time auditing in Envers, a feature that a lot of users has been (directly or indirectly) asking for. It&#8217;s joint work, as Stephanie Pau contributed a patch with a large portion of those changes &#8211; thanks!
You can try it out by checking out Hibernate trunk source code.
What is valid-time [...]]]></description>
			<content:encoded><![CDATA[<p>I just commited initial support for valid-time auditing in Envers, a feature that a lot of users has been (directly or indirectly) asking for. It&#8217;s joint work, as Stephanie Pau contributed a patch with a large portion of those changes &#8211; thanks!</p>
<p>You can try it out by checking out <a href="http://anonsvn.jboss.org/repos/hibernate/core/trunk">Hibernate trunk</a> source code.</p>
<p>What is valid-time about? So far Envers only stored the revision at which a change was made. This information is enough to retrieve historical data, however the queries are quite complicated and in advanced use cases can be time-consuming. This can be improved when we store both the start and end revisions, that is information on when data was &#8220;valid&#8221;. For historic entities, both column are filled, and for &#8220;current&#8221; data, the end revision column is <code>null</code>.</p>
<p>Using the valid-time audit strategy, it will be possible to:</p>
<ul>
<li>speed up and simplify the queries to retrieve historical data</li>
<li>implement support for queries, which traverse relations</li>
<li>implement other types of queries, like latest changes</li>
</ul>
<p>To configure Envers to store the end-revision number, you have to specify a property in your configuration file:</p>
<pre name="code" class="xml">

&lt;property name=&quot;org.hibernate.envers.audit_strategy&quot;&gt;
   org.hibernate.envers.strategy.ValidTimeAuditStrategy
&lt;/property&gt;
</pre>
<p>Envers will then generate and additional <code>REVEND</code> column (next to the <code>REV</code> column) in every audit (<code>_AUD</code>) entity/table; however this column won&#8217;t be part of the primary key. You can change the name of the end-revision column by setting the <code>org.hibernate.envers.audit_strategy_valid_time_end_name</code> property value.</p>
<p>The value of the end-revision column can be calculated basing only on the original revision-changed columns, so using a couple of queries it will be possible to easily migrate existing data to the new audit strategy.</p>
<p>Please note that this feature is experimental, and can be changed in the future. The associated JIRA issue is <a href="http://opensource.atlassian.com/projects/hibernate/browse/HHH-3763">HHH-3763</a>.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=239</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NEnvers</title>
		<link>http://www.warski.org/blog/?p=237</link>
		<comments>http://www.warski.org/blog/?p=237#comments</comments>
		<pubDate>Thu, 01 Jul 2010 17:51:01 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Envers]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=237</guid>
		<description><![CDATA[If you are a .NET and NHibernate user, soon you&#8217;ll be able to use Envers in your project!
Simon Duduica has been working on an Envers to .NET port. This is still work in progress, but you can check out the current source code here: https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/sandbox/simondud/Envers.NET
A large portion was ported, but there&#8217;s also quite a lot [...]]]></description>
			<content:encoded><![CDATA[<p>If you are a .NET and <a href="http://community.jboss.org/wiki/NHibernateforNET">NHibernate</a> user, soon you&#8217;ll be able to use <a href="http://jboss.org/envers">Envers</a> in your project!</p>
<p>Simon Duduica has been working on an Envers to .NET port. This is still work in progress, but you can check out the current source code here: https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/sandbox/simondud/Envers.NET</p>
<p>A large portion was ported, but there&#8217;s also quite a lot left, so any help is appreciated. Big thanks to Simon and looking forward to a release :)</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=237</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Services in Scala</title>
		<link>http://www.warski.org/blog/?p=233</link>
		<comments>http://www.warski.org/blog/?p=233#comments</comments>
		<pubDate>Mon, 21 Jun 2010 16:39:46 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=233</guid>
		<description><![CDATA[Using Scala&#8217;s implicits it&#8217;s possible to implement Object Services in a much more &#8220;user-friendly&#8221; way. Just to remind, the goal is to  extend a class hierarchy with a method, polymorphically. E.g. we have:


trait Animal
class Elephant extends Animal
class Ant extends Animal

and we want to add a paint method, which has a different implementation for an [...]]]></description>
			<content:encoded><![CDATA[<p>Using Scala&#8217;s implicits it&#8217;s possible to implement <a href="http://www.warski.org/blog/?p=226">Object Services</a> in a much more &#8220;user-friendly&#8221; way. Just to remind, the goal is to  extend a class hierarchy with a method, polymorphically. E.g. we have:</p>
<pre name="code" class="java">

trait Animal
class Elephant extends Animal
class Ant extends Animal
</pre>
<p>and we want to add a <code>paint</code> method, which has a different implementation for an elephant, and for an ant (quite obviously :) ). In another words, we want to have a polymorphic extension method. One approach in Scala is to use pattern-matching. And in fact pattern-matching can be used to implement object services as described here (below is a method which uses reflection, but it can be easily swapped).</p>
<p>Before we&#8217;ll go to the actual implementation, here&#8217;s how you can use the object services. We already have the objects, so it&#8217;s time to create the services:</p>
<pre name="code" class="java">

trait PaintService[O &lt;: Animal] extends OS[O] {
   def paint(c: Canvas): Unit
}

class ElephantPaintService(elephant: Elephant) extends PaintService[Elephant] {
   def paint(c: Canvas): Unit { ... }
}

class AntPaintService(ant: Ant) extends PaintService[Ant] {
   def paint(c: Canvas): Unit { ... }
}
</pre>
<p>Now we need a place, where we register the services. In the Weld implementation, this was done automatically at container startup time. Here we&#8217;ll need one object (which could be auto-generated by a compiler plugin):</p>
<pre name="code" class="java">

object PaintServiceReg extends OSP[Animal, PaintService] {
   register[Elephant, ElephantPaintService]
   register[Ant, AntPaintService]
}
</pre>
<p>The type bound&#8217;s of the <code>register</code> method will make sure that you can register only appropriate services for appropriate object types. Finally, we can use our services. If we want to use the <code>PaintService</code>, we&#8217;ll need to import the content of the <code>PaintServiceReg</code> object. That way we have control, on what services are available when. Usage is quite simple and <strong>looks like a regular method invocation</strong>, as if the method was in the class hierarchy:</p>
<pre name="code" class="java">

import PaintServiceReg._

val animal1: Animal = new Elephant
animal1.paint(c)  // ElephantPaintService is called

val animal2: Animal = new Ant
animal2.paint(c)  // AntPaintService is called
</pre>
<p>What&#8217;s left to show is the actual code for <code>OS</code> and <code>OSP</code>:</p>
<pre name="code" class="java">

trait OS[O]

trait OSP[O &lt;: AnyRef, S[_ &lt;: O] &lt;: OS[_]] {
   implicit def oToS(obj: O): S[O] = {
      val bestService = findBestService(obj.getClass())
      bestService.getConstructors()(0).newInstance(obj).asInstanceOf[S[O]]
   }

   protected def register[RO &lt;: O, RS &lt;: S[RO]](implicit manifestRO: Manifest[RO],
            manifestRS: Manifest[RS]) {
      serviceMap += manifestRO.erasure -&gt; manifestRS.erasure
   }

   private var serviceMap: Map[Class[_], Class[_]] = Map()

   private def findBestService(objectCls: Class[_]): Class[_] = {
      serviceMap.foldLeft[(Class[_], Class[_])]((classOf[AnyRef], classOf[AnyRef]))
         ((curr, mapping) =&gt; {
         // Checking if the mapping is appropriate for the given object class
         // and more specific than the current one
         if (mapping._1.isAssignableFrom(objectCls) &amp;&amp;
                curr._1.isAssignableFrom(mapping._1))
            mapping
         else curr
      })._2
   }
}
</pre>
<p>The implementation can be improved e.g. to always return the same object service reference for a given object (something like <a href="http://www.eclipse.org/resources/resource.php?id=407">Eclipse Adapters</a> &#8211; thanks for the link), or to provide alternative ways to instantiate the services &#8211; not necessarily with a one-arg constructor.</p>
<p>The biggest problem here is that completeness is not checked &#8211; that is, if there&#8217;s a service missing for a class in the hierarchy, there will be a run-time error. But I think this also can be checked with a compiler plugin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=233</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Object Services, or bridging anemic and rich models, in CDI/Weld</title>
		<link>http://www.warski.org/blog/?p=226</link>
		<comments>http://www.warski.org/blog/?p=226#comments</comments>
		<pubDate>Thu, 27 May 2010 09:43:31 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=226</guid>
		<description><![CDATA[Rich domain models are certainly a nice, object-oriented idea, but I always had one problem with them: what if they become bloated with completely unrelated methods? For objects that are frequently used in a system, we may want to add various methods, which depend on the actual class of the object. Also, what if we&#8217;d [...]]]></description>
			<content:encoded><![CDATA[<p>Rich domain models are certainly a nice, object-oriented idea, but I always had one problem with them: what if they become bloated with completely unrelated methods? For objects that are frequently used in a system, we may want to add various methods, which depend on the actual class of the object. Also, what if we&#8217;d like to use some other (e.g. CDI) beans as part of the method logic? Normally in DI frameworks there&#8217;s no injection into model classes. Or we want to add a frontend-specific method, but we receive the instances from a backend service?</p>
<p><strong>Object Services</strong> try to address the issues above. Suppose we have a simple class hierarchy of animals:</p>
<pre name="code" class="java">

abstract class Animal
class Elephant extends Animal
class Ant extends Animal
</pre>
<p>and we want to implement a <code>paint</code> method, which paints a picture of the given animal on a canvas. Quite obviously, painting an elephant is different from painting an ant. There are several solutions:</p>
<ul>
<li>add a paint method to the <code>Animal</code> interface &#8211; problems outlined above</li>
<li>add a paint method, in which we check which <code>Animal</code> was passed using <code>instanceof</code> &#8211; quite ugly</li>
<li>use the visitor pattern &#8211; typesafe, but quite verbose</li>
</ul>
<p>I think the best solution would be to have type-safe &#8220;polymorphic extension methods&#8221;, so that in your code you could just add some methods to each class in a hierarchy, but unfortunately this isn&#8217;t supported by any Java (see also <a href="http://en.wikipedia.org/wiki/Multiple_dispatch">multiple dispatch</a>).</p>
<p>Another possibility is to use what I call &#8220;Object Services&#8221;. If we want to add some methods to a class hierarchy, we create a parallel hierarchy of &#8220;services&#8221; (which are normal classes):</p>
<pre name="code" class="java">

interface PaintService&lt;T extends Animal&gt; extends OS&lt;T&gt; {
   void paint(Canvas c);
}

// Implements OS&lt;Elephant&gt;
class ElephantPaintService implements PaintService&lt;Elephant&gt; {
   // Here we can store the object, for which the service was invoked
   void setServiced(Elephant e) { ... }
   void paint(Canvas c) { ... }
}

// Implements OS&lt;Ant&gt;
class AntPaintService implements PaintService&lt;Ant&gt; {
   // Injection works normally
   @Inject AnthillService anthill;

   void setServiced(Ant a) { ... }
   void paint(Canvas c) { ... }
}
</pre>
<p><code>OS</code> is an interface marking some beans as object services; the class, to which the service corresponds is given as a type parameter.</p>
<p>The <code>ObjectServiceExtension</code> will detect all beans that implement the <code>OS</code> interface, and register an <code>OSP</code> (Object Service Provider) bean which can be later injected to obtain a correct object service given an <code>Animal</code>:</p>
<pre name="code" class="java">

@Inject
OSP&lt;Animal, PaintService&lt;Animal&gt;&gt; paintService;

void paint(Animal a, Canvas c) {
   paintService.f(a).paint(c);
}
</pre>
<p>Each invocation of the <code>f</code> method will lookup the correct bean, based on the run-time type of the object passed, create a new instance of the found bean and set the object, for which the method was called. All beans created are CDI-managed, so injection etc works normally.</p>
<pre name="code" class="java">

void test(Canvas c) {
   // Will invoke paint(c) in AntPaintService
   paint(new Ant(), c);

   // Will invoke paint(c) in ElephantPaintService
   paint(new Elephant(), c);
}
</pre>
<p>The source code is available on GitHub in the <a href="http://github.com/adamw/cdiext">cdiext project</a>. To use it, just bundle the jar with your application.</p>
<p>Thanks to <a href="http://szimano.org/">Tomek Szymański</a> for discussing the implementation.</p>
<p>So what&#8217;s next? The code could use a couple of improvements, but the biggest next task is to add deploy-time checking if there&#8217;s an object service for each class in a hierarchy (e.g. we have a <code>PrintService</code> and an <code>ElephantPrintService</code>, but forget to add an <code>AntPrintService</code>).</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=226</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CDI &amp; Weld Extensions in Git</title>
		<link>http://www.warski.org/blog/?p=220</link>
		<comments>http://www.warski.org/blog/?p=220#comments</comments>
		<pubDate>Mon, 17 May 2010 19:37:46 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=220</guid>
		<description><![CDATA[Hello,
I&#8217;ve created a new cdiext project at github, initially with two extensions:
1. Stackable Security Interceptors, about which I blogged here and here. Example usage:


@SecureBinding
@Secure(&#34;#{loggedInUser.administrator}&#34;)
public @interface AdministratorOnly {
}

public class SecureBean {
    @AdministratorOnly
    @Secure(&#34;#{additionalSecurityCheck}&#34;)
    public void doSecret() { ... }
}

2. Injectable ELEvaluator, which works both during a faces [...]]]></description>
			<content:encoded><![CDATA[<p>Hello,</p>
<p>I&#8217;ve created a new <a href="http://github.com/adamw/cdiext"><code>cdiext</code> project at github</a>, initially with two extensions:</p>
<p>1. Stackable Security Interceptors, about which I blogged <a href="http://www.warski.org/blog/?p=197">here</a> and <a href="http://www.warski.org/blog/?p=211">here</a>. Example usage:</p>
<pre name="code" class="java">

@SecureBinding
@Secure(&quot;#{loggedInUser.administrator}&quot;)
public @interface AdministratorOnly {
}

public class SecureBean {
    @AdministratorOnly
    @Secure(&quot;#{additionalSecurityCheck}&quot;)
    public void doSecret() { ... }
}
</pre>
<p>2. Injectable ELEvaluator, which works both during a faces request and outside of one (e.g. during invocation of an MDB). Example usage:</p>
<pre name="code" class="java">

@Inject
private ELEvaluator elEvaluator;

void someMethod() {
    // ...
    Integer result = elEvaluator.evaluate(
            &quot;#{testParam1 + 10 + testParam2}&quot;, Integer.class, params);
    // ...
}
</pre>
<p>Thanks to <a href="http://in.relation.to/Bloggers/Dan">Dan Allen</a> for <a href="http://community.jboss.org/thread/148045?tstart=0">helping out</a> with this one.</p>
<p>There are also some tests done using <a href="http://community.jboss.org/en/arquillian">Arquillian</a> &#8211; looks like it&#8217;s going to be a great testing tool! :)</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=220</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSR-308, checkers framework and static analysis on GeeCON</title>
		<link>http://www.warski.org/blog/?p=216</link>
		<comments>http://www.warski.org/blog/?p=216#comments</comments>
		<pubDate>Mon, 26 Apr 2010 17:58:50 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Typestate]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[geecon]]></category>
		<category><![CDATA[jsr308]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=216</guid>
		<description><![CDATA[I&#8217;ll be speaking about the JSR-308 specification (annotations on java types), which will be part of Java 7, on the GeeCON conference, which will take place from the 13th till the 14th of May 2010 in Poznan, Poland.
Apart from an introduction to the new annotations, I will cover the checkers framework, and do a live [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be <a href="http://2010.geecon.org/speakerdetails/22">speaking</a> about the <a href="http://types.cs.washington.edu/jsr308/">JSR-308 specification</a> (annotations on java types), which will be part of Java 7, on the GeeCON conference, which will take place from the 13th till the 14th of May 2010 in <a href="http://en.wikipedia.org/wiki/Poznan">Poznan</a>, Poland.</p>
<p>Apart from an introduction to the new annotations, I will cover the checkers framework, and do a live demo of some of the bundled checkers (nullability, immutability), as well as of my <a href="http://www.warski.org/typestate.html">typestate checker</a>. I will also show how to implement a simple custom checker, using the framework.</p>
<p>If you&#8217;ll be there, be sure not to miss it. If you&#8217;ve not yet registered, visit <a href="http://2010.geecon.org">http://2010.geecon.org</a> :).</p>
<p>See you there,<br />
Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=216</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending the security interceptor for Weld/JSF2</title>
		<link>http://www.warski.org/blog/?p=211</link>
		<comments>http://www.warski.org/blog/?p=211#comments</comments>
		<pubDate>Tue, 13 Apr 2010 09:22:52 +0000</pubDate>
		<dc:creator>Adam Warski</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Weld]]></category>
		<category><![CDATA[jsf]]></category>

		<guid isPermaLink="false">http://www.warski.org/blog/?p=211</guid>
		<description><![CDATA[In my previous post, I described how to create a simple security interceptor, which checks conditions defined using EL expressions, e.g.:


@Secure(&#34;#{loggedInUser.name == arg0.name}&#34;)
public List&#60;Message&#62; listMessages(User owner) { ... }  

Now, it would be nice to be able to stack such annotations, so that they can be placed:
* on methods
* on classes &#8211; then the [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.warski.org/blog/?p=197">previous</a> post, I described how to create a simple security interceptor, which checks conditions defined using EL expressions, e.g.:</p>
<pre name="code" class="java">

@Secure(&quot;#{loggedInUser.name == arg0.name}&quot;)
public List&lt;Message&gt; listMessages(User owner) { ... }  
</pre>
<p>Now, it would be nice to be able to stack such annotations, so that they can be placed:<br />
* on methods<br />
* on classes &#8211; then the constraint applies to all methods<br />
* on other annotations, to create &#8220;security bindings&#8221;</p>
<p>An example usage could be:</p>
<pre name="code" class="java">

@Secure(&quot;#{loggedInUser != null}&quot;)
public class Messages {
   @AdminOnly
   public void deleteAllMessages() { ... }

   @Secure(&quot;#{loggedInUser.maxMessageListCount == count}&quot;)
   public List&lt;Message&gt; listMessages(@ELVar(&quot;user&quot;) User owner, @ELVar(&quot;count&quot;) int count) { ... }
}  
</pre>
<p>where <code>@AdminOnly</code> is defined as:</p>
<pre name="code" class="java">

@SecureBinding
@Secure(&quot;#{loggedInUser.isAdministrator}&quot;)
public @interface AdminOnly { }
</pre>
<p>This way common security constraints can be expressed as annotations or on the class. I&#8217;m also using an improvement suggested by <a href="http://in.relation.to/Bloggers/Dan">Dan Allen</a>, to name the method arguments using <code>@ELVar</code>, instead of naming them <code>arg0</code>, <code>arg1</code>, etc. This also allows to refer to method arguments in the &#8220;security binding&#8221; annotations, whatever the position of the argument is.</p>
<p>How to implement such annotations? Well, the first step is to create a portable extension (meaning it will work with any CDI implementation, not just Weld), which will gather, for each method, all the <code>@Secure</code> annotations, and their values, into one single annotation, <code>@InterceptSecure</code>. Having such a set of constraints to check for each method, we add the annotation to the method meta-data, using a utility class from <a href="http://www.seamframework.org/Documentation/WeldExtensions">Weld Extensions</a>: the <code>NewAnnotatedTypeBuilder</code>. As the <code>@InterceptSecure</code> is an interceptor binding, the security interceptor will be called whenever the method is invoked.</p>
<p>The extension observes the <code>ProcessAnnotatedType</code> event, which is fired for each bean type. If necessary, the type can be modified, to include the new annotations. The annotation is only added to Weld meta-data, not to the method itself (so there&#8217;s no bytecode manipulation or such).</p>
<p>One last obstacle to overcome is to get the value of the generated <code>@InterceptSecure</code> annotation in the interceptor. Currently this is not possible using e.g. <code>BeanManager</code>, but should be address in CDI Maintenance Release (see <a href="http://old.nabble.com/Retrieving-the-Bean-object-for-an-interceptor-td28147499.html">here</a>), so as a temporary solution all the generated annotations are stored in a map in the extension. All extensions are application-scoped beans, so the information can be accessed from the interceptor. One shortcoming of the solution is that one method may belong to several, differently annotated CDI beans.</p>
<p>The code for the annotation and extension:</p>
<pre name="code" class="java">

@InterceptorBinding
public @interface InterceptSecure {
    @Nonbinding
    String[]    value();
}

public class SecurityExtension implements Extension {
    private final Map&lt;Method, InterceptSecure&gt; interceptSecureForMethods = new HashMap&lt;Method, InterceptSecure&gt;();

    public InterceptSecure getInterceptSecure(Method m) {
        return interceptSecureForMethods.get(m);
    }

    public &lt;T&gt; void processAnnotatedType(@Observes ProcessAnnotatedType&lt;T&gt; event) {
        // A flag indicating if the builder was used to modify the annotations
        boolean used = false;
        NewAnnotatedTypeBuilder&lt;T&gt; builder = new NewAnnotatedTypeBuilder&lt;T&gt;(event.getAnnotatedType());

        // We need to read the values of the @Secure annotation that are present on:
        // 1. types (classes)
        // 2. methods
        // 3. arbitrarily nested on @SecureBinding annotations

        // Gathering the initial secure values from the type
        List&lt;String&gt; initialSecureValues = new ArrayList&lt;String&gt;();
        for (Annotation annotation : event.getAnnotatedType().getAnnotations()) {
            collectSecureValues(annotation, initialSecureValues);
        }

        for (AnnotatedMethod&lt;?&gt; m : event.getAnnotatedType().getMethods()) {
            // Gathering the secure values from the method
            final List&lt;String&gt; values = new ArrayList&lt;String&gt;(initialSecureValues);
            collectSecureValues(m, values);

            // If any values have been gathered, adding the annotation to the method and storing it
            // in the map.
            if (values.size() &gt; 0) {
                InterceptSecure is = new InterceptSecureImpl(values.toArray(new String[values.size()]));
                builder.addToMethod(m.getJavaMember(), is);
                used = true;

                interceptSecureForMethods.put(m.getJavaMember(), is);
            }
        }

        // Setting the new annotated type, if any changed were made
        if (used) {
            event.setAnnotatedType(builder.create());
        }
    }

    private void collectSecureValues(AnnotatedMethod m, List&lt;String&gt; values) {
        for (Annotation annotation : m.getAnnotations()) {
            collectSecureValues(annotation, values);
        }
    }

    private void collectSecureValues(Annotation annotation, List&lt;String&gt; values) {
        if (Secure.class.isAssignableFrom(annotation.annotationType())) {
            values.add(((Secure) annotation).value());
        } else {
            if (annotation.annotationType().getAnnotation(SecureBinding.class) != null) {
                for (Annotation nestedAnnotation : annotation.annotationType().getAnnotations()) {
                    collectSecureValues(nestedAnnotation, values);
                }
            }
        }
    }

    private static class InterceptSecureImpl extends AnnotationLiteral&lt;InterceptSecure&gt; implements InterceptSecure {
        private final String[] values;

        private InterceptSecureImpl(String[] values) {
            this.values = values;
        }

        @Override
        public String[] value() {
            return values;
        }
    }
}
</pre>
<p>And for the interceptor:</p>
<pre name="code" class="java">

@Interceptor
@InterceptSecure(&quot;&quot;)
public class SecurityInterceptor {
    @Inject
    private SecurityExtension se;

    @AroundInvoke
    public Object checkSecurity(InvocationContext ctx) throws Exception {
        // Getting the generated @InterceptSecure annotation for the method.
        // After the CDI Maintenance Release is released, it should be possible to get the annotated
        // type of the currently invoked bean, see:
        // http://old.nabble.com/Retrieving-the-Bean-object-for-an-interceptor-td28147499.html
        // For now, we just use a map in the extension. One limitation is that this doesn&#039;t allow
        // different security annotations for methods which are used in several beans.
        InterceptSecure is = se.getInterceptSecure(ctx.getMethod());
        String[] toCheck = is == null ? null : is.value();

        // Check the el conditions as in the previous post
        // (...)

        return ctx.proceed();
    }
}
</pre>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warski.org/blog/?feed=rss2&amp;p=211</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
