<?xml version="1.0"?>





<rss version="2.0">
<channel>
  <title>Mark McLaren&#039;s Weblog</title>
  <link>http://cse-mjmcl.cse.bris.ac.uk/blog/</link>
  <description>JSP, JSTL, XML, XSLT and stuff</description>
  <language>en</language>
  <copyright>Mark McLaren</copyright>
  <lastBuildDate>Tue, 09 Mar 2010 14:14:25 GMT</lastBuildDate>
  <generator>Pebble</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>AJAX decoration: using jQuery Metadata for portlet AJAX </title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2010/03/09/1268144065141.html</link>
    
      
        <description>
          &lt;p&gt;
I recently examined a JSR 168 portlet which tackled the thorny area of &lt;a href=&#034;http://www.ja-sig.org/wiki/display/PLT/AJAX+in+a+Portlet&#034;&gt;AJAX in a Portlet&lt;/a&gt; ( incidentally the portlet I was looking at is the &lt;a href=&#034;http://www.jasig.org/portlets/calendar-portlet&#034;&gt;Jasig Calendar Portlet&lt;/a&gt; ).  The technique used makes all AJAX requests into POSTs which are then passed via the portal and a redirect to a servlet to acquire the desired response.  I am not new to JSR 168 portlets and have personally produced several portlets over the past few years. I have also solved the &lt;i&gt;AJAX in a Portlet&lt;/i&gt; problem but in a completely different way.  The solution I developed I am calling &lt;b&gt;AJAX decoration&lt;/b&gt; and relies on the &lt;a href=&#034;http://plugins.jquery.com/project/metadata&#034;&gt;jQuery Metadata plugin&lt;/a&gt;.  I think both techniques have their merits and possibly could be applicable to different scenarios.  I will attempt to illustrate my solution with a simple example app (note this example is not an actual portlet and will work in your servlet container of choice).
&lt;/p&gt;
&lt;p&gt;
The structure of each of my &#034;portlet&#034; JSPs in my example is something like this:
&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;b&gt;header.jsp&lt;/b&gt;&lt;/i&gt;&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery-1.3.2.min.js&amp;quot;&amp;gt;&amp;lt;!-- --&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.metadata.js&amp;quot;&amp;gt;&amp;lt;!-- --&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;div id=&amp;quot;content-area&amp;quot;&amp;gt;
&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;&lt;b&gt;footer.jsp&lt;/b&gt;&lt;/i&gt;&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;/div&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
    var pageContext = &amp;quot;${pageContext.request.contextPath}&amp;quot;;
    var targetArea = &amp;quot;div#content-area&amp;quot;;
&amp;lt;/script&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;ajaxDecorated.js&amp;quot;&amp;gt;&amp;lt;!-- --&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;&lt;b&gt;somepage.jsp&lt;/b&gt;&lt;/i&gt;&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;%@ include file=&amp;quot;header.jsp&amp;quot; %&amp;gt;
&amp;lt;h1&amp;gt;Some Page&amp;lt;/h1&amp;gt;

&amp;lt;!-- Content goes here --&amp;gt;

&amp;lt;%@ include file=&amp;quot;footer.jsp&amp;quot; %&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Notice that we will be using a consistent target area for our AJAX loaded content; in this case a div with the ID of &#034;content-area&#034;.  The jQuery Metadata plugin makes it possible to read data stored within CSS classes, doing this is a relatively clean way of storing data within our page.  In this approach the content links are decorated with data and in turn this data is used to assign appropriate AJAX behaviour.
&lt;/p&gt;
&lt;p&gt;
For a typical link using a GET we could use:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;a href=&amp;quot;page1.jsp&amp;quot; class=&amp;quot;ajaxRenderURL {link: &#039;/page1.jsp&#039;}&amp;quot;&amp;gt;page 1&amp;lt;/a&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
For form submission we could use:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;form action=&amp;quot;page2.jsp&amp;quot; class=&amp;quot;ajaxActionURL {link: &#039;/page2.jsp&#039;}&amp;quot;&amp;gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; /&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
The magic happens in the &#034;ajaxDecorated.js&#034; file.  When the page initially loads initPage() is invoked which attaches AJAX loading behaviour to links and forms.
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
$(document).ready(function() {
    initPage();
});

function setupLinks(){
    var data = $(this).metadata();
    var loadlink;
    var tsSeparator = &amp;quot;&amp;amp;&amp;quot;;
    if(data.link.indexOf(&amp;quot;?&amp;quot;) == -1){
        tsSeparator = &amp;quot;?&amp;quot;
    }
    if(!data.link.match(&amp;quot;^&amp;quot; + pageContext)){
        loadlink = pageContext + data.link + tsSeparator + new Date().getTime() + &amp;quot; &amp;quot; + targetArea;
    } else {
        loadlink = data.link + tsSeparator + new Date().getTime() + &amp;quot; &amp;quot; + targetArea;
    }
    $(targetArea).load(loadlink, null,
        function () {
            initPage();
    });
    return false;
}

function setupForms(){
    var data = $(this).metadata();
    var formdata = $(this).serializeArray();
    var loadlink;
    if(!data.link.match(&amp;quot;^&amp;quot; + pageContext)){
        loadlink = pageContext + data.link + &amp;quot; &amp;quot; + targetArea;
    } else {
        loadlink = data.link + &amp;quot; &amp;quot; + targetArea;
    }
    $(targetArea).load(loadlink, formdata,
        function(){
            initPage();
    });
    return false;
}

function initPage(){
    $(targetArea).find(&#039;a.ajaxRenderURL&#039;).click(setupLinks);
    $(targetArea).find(&#039;form.ajaxActionURL&#039;).submit(setupForms);
}
&lt;/pre&gt;
&lt;p&gt;
This technique adds AJAX to a web page in an unobtrusive way.  In this simple example the URLs loaded by AJAX and those that would be loaded without are the same (page1.jsp, index.jsp etc).  However, in an environment where the AJAX URLs and web page URLs are likely to be different or even change dynamically (such as in a portal) then this technique is equally applicable.
&lt;/p&gt;
&lt;link href=&#034;http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css&#034; type=&#034;text/css&#034; rel=&#034;stylesheet&#034; /&gt;
    &lt;script src=&#034;http://www.google.com/apis/maps/include/prettify.js&#034; type=&#034;text/javascript&#034;&gt;&lt;/script&gt;
    &lt;script type=&#034;text/javascript&#034;&gt;
    var calledprettyprint;

    function addLoadPrettyPrintEvent(func) {
    if (calledprettyprint) return;
    calledprettyprint = true;

    var oldonload = window.onload;
    if (typeof window.onload != &#039;function&#039;) {
        window.onload = func;
        } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
    }

    addLoadPrettyPrintEvent(prettyPrint);
&lt;/script&gt;

        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2010/03/09/1268144065141.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2010/03/09/1268144065141.html</guid>
    <pubDate>Tue, 09 Mar 2010 14:14:25 GMT</pubDate>
  </item>
  
  <item>
    <title>DisplayTag: Producing Decorators for Lists of Maps</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2010/02/25/1267097615880.html</link>
    
      
        <description>
          &lt;p&gt;
I have been using the &lt;a href=&#034;http://displaytag.sourceforge.net/1.2/&#034;&gt;Display tag library&lt;/a&gt; (hereafter referred to as displaytag) heavily on a big in-house web application project.  Displaytag helped me make relatively short work of pagination and data export, the resulting pages look very professional to boot.  Best of all using displaytag meant I was able to concentrate my efforts on other parts of the project.
&lt;/p&gt;&lt;p&gt;
Much of the time I use the displaytag in the &lt;i&gt;typical&lt;/i&gt; way (e.g. to display collections of beans).  However, the changeable nature of some of the reports required lead me to produce some of the screens using Lists of Maps.  Lists of Maps are very easily acquired from a database (e.g. Spring&#039;s ColumnMapRowMapper).  Once you configure displaytag to display lists of maps then you have the flexibility to make changes to the report contents purely by changing an SQL statement.  Also using the SQL &#034;AS&#034; keywords means that user friendly table column names are easily supported.  So by making use of lists of maps you can produce very flexible and easily extensible reporting screens.
&lt;/p&gt;&lt;p&gt;
&lt;i&gt;So far, so good...&lt;/i&gt;
&lt;/p&gt;
&lt;h2&gt;Formatting data and adding extra display columns&lt;/h2&gt;
&lt;p&gt;
By default whether you are passing a collection of beans or a list of maps to displaytag it just works!  e.g.
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;display:table name=&#034;dummyData&#034;&gt;&amp;lt;/display:table&gt;
&lt;/pre&gt;
&lt;p&gt;
Elsewhere where I have used the &lt;i&gt;typical&lt;/i&gt; way (collections of beans) of supplying data to displaytag.  Sometimes I need to reformat a date, currency column or change some columns into links.  You can do most of this inside the JSP page (messy!) or alternatively you can make use of the displaytag TableDecorator.  You can also use TableDecorator&#039;s to add new columns that don&#039;t exist in the data, however, once you start specifying custom generated columns then you need to explicitly specify all the columns you would like to see in the displaytag table, e.g.:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;display:table name=&#034;dummyData&#034; decorator=&#034;edu.bristol.SampleDecorator&#034;&gt;
     &amp;lt;display:column property=&#034;id&#034; /&gt;
     &amp;lt;display:column property=&#034;date&#034; /&gt;
     &amp;lt;display:column property=&#034;url&#034; /&gt;
     &amp;lt;display:column property=&#034;columnPopulatedByTableDecorator&#034; title=&#034;Decorator generated&#034;/&gt;
&amp;lt;/display:table&gt;
&lt;/pre&gt;
&lt;p&gt;
I wanted to do this with a list of maps.  After a little research on the forums, I found that you could still make use of TableDecorator generated custom columns by explicitly specifying the columns in your map.  As the data originates from the database, each list row will contain a map with the same set of map keys.  First you retrieve the map keyset from the first result row, store this keyset in the request and then you can explicitly iterate through the map columns:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;display:table name=&#034;dummyData&#034; decorator=&#034;edu.bristol.SampleDecorator&#034;&gt;
       &amp;lt;c:forEach items=&#034;${keyset}&#034; var=&#034;key&#034;&gt;
             &amp;lt;display:column property=&#034;${key}&#034; /&gt;
       &amp;lt;/c:forEach&gt;
     &amp;lt;display:column property=&#034;columnPopulatedByTableDecorator&#034; title=&#034;Decorator generated&#034;/&gt;
&amp;lt;/display:table&gt;
&lt;/pre&gt;
&lt;p&gt;
At this point, the &#034;columnPopulatedByTableDecorator&#034; will be generated but the TableDecorator doesn&#039;t do any re-formatting of the existing data columns.
&lt;/p&gt;
&lt;h2&gt;Producing Decorators for Lists of Maps&lt;/h2&gt;
&lt;p&gt;
So now we come to the &lt;b&gt;novel aspect&lt;/b&gt; of this blog entry!  The next question is how I reformat the data in my list of maps for displaytag purposes.  I searched through the forums and couldn&#039;t get an answer for this, so I came up with a solution.  Internally, displaytag uses &lt;a href=&#034;http://commons.apache.org/beanutils/&#034;&gt;Commons BeanUtils&lt;/a&gt; for data manipulation.  We have seen that it can handle lists of maps without any additional configuration - so it must be doing something clever!  The TableDecorator test code for displaytag includes tests for accessing mapped data, so I knew that it should be possible to access the mapped data in my own TableDecorator. Part of the difficulty in accessing the data is that my key names were not easily transformed into Java method names, the keys contain spaces, capitalization and other characters that are not permitted in a method name.  My solution was to alter my list of maps to become a list of &#034;wrapped maps&#034;.  Where a wrapped map looks something like:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
public class WrappedMap {

    Map m;

    public WrappedMap(Map m) {
        this.m = m;
    }

    public Object getMap(String propertyName) {
        return m.get(propertyName);
    }
}
&lt;/pre&gt;
&lt;p&gt;
My decorator for this list of wrapped maps could now access my oddly named keys, such as:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
public class WrappedMapDecorator extends TableDecorator{

    final private String startDate = &#034;Start Date&#034;;
    final private String websiteAddress = &#034;Website Address&#034;;

    public String getMap(String propertyName){
        WrappedMap wm = (WrappedMap) getCurrentRowObject();
        Object propertyValue = wm.getMap(propertyName);
        if(startDate.equals(propertyName)){
            // Implement appropriate formatting/decoration
            return &#034;&amp;lt;i&gt;&#034; + propertyValue.toString() + &#034;&amp;lt;/i&gt;&#034;;
        }
        if(websiteAddress.equals(propertyName)){
            // Implement appropriate formatting/decoration
            return &#034;&amp;lt;b&gt;&#034; + propertyValue.toString() + &#034;&amp;lt;/b&gt;&#034;;
        }
        return propertyValue.toString();
    }

}
&lt;/pre&gt;
&lt;p&gt;
Finally, my JSP would need to be tweaked to produce the appropriate table:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;display:table name=&#034;dummyData&#034; decorator=&#034;edu.bristol.WrappedMapDecorator&#034;&gt;
    &amp;lt;c:forEach items=&#034;${keyset}&#034; var=&#034;key&#034;&gt;
           &amp;lt;display:column property=&#034;map(${key})&#034; title=&#034;${key}&#034; /&gt;
    &amp;lt;/c:forEach&gt;
    &amp;lt;display:column property=&#034;columnPopulatedByTableDecorator&#034; title=&#034;Decorator generated&#034; /&gt;
&amp;lt;/display:table&gt;
&lt;/pre&gt;
&lt;p&gt;
I am happy with this solution.  I maintain the flexibility that the List of Maps technique makes possible but it is also possible to decorate the supplied data.
&lt;/p&gt;
&lt;p&gt;
Incidentally, you may have noticed that I haven&#039;t been blogging much in recent times.  I am now the father of 2 pre-schoolers and so what free time I get is usually spent collapsed in a heap rather than blogging!  Anyway I hope you find this useful...
&lt;/p&gt;
&lt;link href=&#034;http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css&#034; type=&#034;text/css&#034; rel=&#034;stylesheet&#034; /&gt;
    &lt;script src=&#034;http://www.google.com/apis/maps/include/prettify.js&#034; type=&#034;text/javascript&#034;&gt;&lt;/script&gt;
    &lt;script type=&#034;text/javascript&#034;&gt;
    var calledprettyprint;

    function addLoadPrettyPrintEvent(func) {
    if (calledprettyprint) return;
    calledprettyprint = true;

    var oldonload = window.onload;
    if (typeof window.onload != &#039;function&#039;) {
        window.onload = func;
        } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
    }

    addLoadPrettyPrintEvent(prettyPrint);
&lt;/script&gt;

        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2010/02/25/1267097615880.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2010/02/25/1267097615880.html</guid>
    <pubDate>Thu, 25 Feb 2010 11:33:35 GMT</pubDate>
  </item>
  
  <item>
    <title>Spring Web Flow 2 Web Development: Book Review</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/08/21/1250891389312.html</link>
    
      
        <description>
          &lt;p&gt;
I have been keeping the good people of &lt;a href=&#034;http://www.packtpub.com/&#034;&gt;PACKT Publishing&lt;/a&gt; waiting some time for this short review; they were kind and prompt in sending me their book - in my defence I have been very busy lately.  I had hoped to include some example code but as I am still short on time right now I thought I would release a general review first and maybe something with some example code at a later date when I&#039;ve had some more time to digest the book further.  To business, &lt;a href=&#034;http://www.packtpub.com/develop-powerful-web-applications-with-spring-web-flow-2/book&#034;&gt;Spring Web Flow 2 Web Development&lt;/a&gt; is a book for &lt;a href=&#034;http://www.springsource.org/about&#034;&gt;Spring&lt;/a&gt; developers who want to give &lt;a href=&#034;http://www.springsource.org/webflow&#034;&gt;Spring Web Flow&lt;/a&gt; a try.  So what is Spring Web Flow?  As described in following excerpts from the book &lt;i&gt;(page 9)&lt;/i&gt;:
&lt;/p&gt;
&lt;p&gt;
&#034;...- you can use Spring Web Flow to create conversational web applications.  ...For example, take an application with a wizard-like interface.  ...You are working in a predefined &lt;i&gt;flow&lt;/i&gt; with a specific goal, such as ordering a book or creating a new user account.  ...Spring Web Flow makes it very easy to create flows.  Flows created by Spring Web Flow are not only decoupled from the applications logic, but are also re-usable in different applications.  A &lt;b&gt;flow&lt;/b&gt; in Spring Web Flow is a sequence of steps, that is, with &lt;i&gt;states&lt;/i&gt; and &lt;i&gt;transitions&lt;/i&gt; between them.  There are also &lt;i&gt;actions&lt;/i&gt; that can be executed on various points, for example, when the flow starts or the web page is rendered...&#034;
&lt;/p&gt;
&lt;p&gt;
This book is intended to describe Spring Web Flow 2 and any book which ties itself to a particular version is going to date quickly but that said you are bound to learn something new reading this book.
&lt;/p&gt;
&lt;p&gt;
The book contains the obligatory chapters describing the Spring Web Flow 2 concepts and exploring what has changed since the last version, the distribution and example code.  It also has a chapter on setting up your development environment be that Ant, Maven, Eclipse (and Spring IDE) or NetBeans.  A chapter on &lt;a href=&#034;http://java.sun.com/javaee/javaserverfaces/&#034;&gt;JavaServerFaces&lt;/a&gt; integration using the Spring Web Flow 2 &#034;Spring Faces&#034; module, as I&#039;ve never properly explored JavaServerFaces I&#039;m not qualified to comment on this.  The sample project that is used throughout the book is a simple bug tracking system (&#034;flow.trac&#034;).  With this project there is plenty of scope to explore what Spring Web Flow 2 has to offer in terms of user interface (JavaScript including AJAX, &lt;a href=&#034;http://tiles.apache.org/&#034;&gt;Apache Tiles&lt;/a&gt; etc.), testing (&lt;a href=&#034;http://www.junit.org/&#034;&gt;JUnit&lt;/a&gt; and &lt;a href=&#034;http://easymock.org/&#034;&gt;EasyMock&lt;/a&gt;) and security (&lt;a href=&#034;http://static.springsource.org/spring-security/site/index.html&#034;&gt;Spring Security&lt;/a&gt;) whilst utilising a JPA (&lt;a href=&#034;https://www.hibernate.org/&#034;&gt;Hibernate&lt;/a&gt;) database layer.  In the appendix it shows you how you can run your code on the &lt;a href=&#034;http://www.springsource.com/products/dmserver&#034;&gt;SpringSource dm Server&lt;/a&gt; and gives a gentle introduction to making you application work in an OSGi environment.  The source code for the book is available @ &lt;a href=&#034;http://www.packtpub.com/files/code/5425_Code.zip&#034;&gt;www.packtpub.com/files/code/5425_Code.zip&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
One of my early impressions was one of surprise,  I admit to raising an eyebrow that they chose to use &lt;a href=&#034;http://www.microsoft.com/express/sql/default.aspx&#034;&gt;Microsoft SQL Server 2008 Express Edition&lt;/a&gt; as their example database server. I would expect something more lightweight such as &lt;a href=&#034;http://www.h2database.com/&#034;&gt;H2&lt;/a&gt; or &lt;a href=&#034;http://db.apache.org/derby/&#034;&gt;Apache Derby&lt;/a&gt; -e.g. something you could easily bundle as part of a Maven build.
&lt;/p&gt;
&lt;p&gt;
The intended audience for this book seems to be people who plan to use the triumvirate of Spring, Spring MVC and Spring Web Flow.  From my perspective Spring is a gimme and you are obviously interested in Spring Web Flow (that is why you bought the book!) but I&#039;m not so sure you are always free to choose Spring MVC.  To be fair the book goes to great pains to say that Spring Web Flow can be used with other technologies but as my main web development MVC framework is still good ole&#039; Struts 1.3, it would be nice to see some examples of using Spring Web Flow 2 with something other than Spring MVC or maybe even in a more exotic environment like inside a Spring MVC portlet.  Spring Web Flow 2 includes components for integrating with JavaScript.  I have to admit to being a jQuery devotee so any alternate JavaScript technology seems a bit unnecessary to me but I can see it could be useful to some folks.
&lt;/p&gt;
&lt;p&gt;
All in all and so far, I have found reading this book to be very enjoyable.  It is nice to see how other developers go about things, I found myself reading and thinking &#034;I wouldn&#039;t do it that way&#034; AND &#034;That&#039;s clever, I&#039;d never have thought of that!&#034;.  The experience of reading this book is very much like shadowing some very experienced senior Java developers.
&lt;/p&gt;

        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/08/21/1250891389312.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/08/21/1250891389312.html</guid>
    <pubDate>Fri, 21 Aug 2009 21:49:49 GMT</pubDate>
  </item>
  
  <item>
    <title>Coming soon:  Spring Web Flow 2 Web Development: Book Review</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/08/11/1249996759792.html</link>
    
      
        <description>
          &lt;a href=&#034;http://www.packtpub.com&#034;&gt;PACKT Publishing&lt;/a&gt; very kindly sent me a copy of &lt;a href=&#034;http://www.packtpub.com/develop-powerful-web-applications-with-spring-web-flow-2/book&#034;&gt;Spring Web Flow 2 Web Development&lt;/a&gt; to review.  I have been very busy with work so I have not yet gotten around to reviewing it but I plan to publish something in the not too distant future.  So watch this space!
        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/08/11/1249996759792.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/08/11/1249996759792.html</guid>
    <pubDate>Tue, 11 Aug 2009 13:19:19 GMT</pubDate>
  </item>
  
  <item>
    <title>jQuery UI ThemeRollerReady IFRAME in a dialog box</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/07/02/1246571226133.html</link>
    
      
        <description>
          &lt;p&gt;
Whilst making preparations for upgrading to &lt;a href=&#034;http://www.jasig.org/uportal&#034;&gt;uPortal&lt;/a&gt; 3.1.x from uPortal 2.5.x I came into contact with &lt;a href=&#034;http://jqueryui.com/home&#034;&gt;jQuery UI&lt;/a&gt; themes.
&lt;/p&gt;
&lt;p&gt;
In our previous portal the help screens were displayed using Cody Lindley&#039;s &lt;a href=&#034;http://jquery.com/demo/thickbox/&#034;&gt;ThickBox&lt;/a&gt;.  At the time I first introduced Thickbox to our portal I thought this was pretty revolutionary, clicking on a link made the background dim and the help text (via an IFRAME) was shown in sharp contrast in the foreground of the page (very Web 2.0!).
&lt;/p&gt;&lt;p&gt;
The interface for uPortal 3.1 has evolved considerably from 2.5.x.  The &lt;a href=&#034;http://wiki.fluidproject.org/display/fluid/Fluid-uPortal+Collaboration+2.0&#034;&gt;fluid project have worked on uPortal 3.1&lt;/a&gt; to improve the user experience.  jQuery and jQuery UI have become an integral part of the uPortal interface.  uPortal 3.1 makes good use of jQuery UI dialog and tab widgets.  (Incidentally, when did dialogue become dialog?)  As the range of jQuery UI widgetry expands so too will the options for portal improvement, all with a common look and feel and easily themed.
&lt;/p&gt;&lt;p&gt;
In our new portal I would still like to access the help pages in a Thickbox/IFRAME style.  However, the jQuery plugins that provide this kind of functionality (Thickbox and &lt;a href=&#034;http://dev.iceburg.net/jquery/jqModal/&#034;&gt;jqModal&lt;/a&gt;) do not look like the other jQuery UI widgets and are not themeable via jQuery UI themes.  I could probably map the plugin CSS to the jQuery UI theme CSS but this seems like it could be a lot of work.  So I have produced my own version of a dialog using an IFRAME.
&lt;/p&gt;&lt;p&gt;
I am fairly new to jQuery and what I have produced isn&#039;t quite up to jQuery plugin standard but if somebody out there wanted to apply some finishes touches to it I would be happy to contribute it back to the jQuery effort.  The main difference between my effort and Thickbox and jqModal is that I am only interested in IFRAMEs and my version should be &lt;a href=&#034;http://jqueryui.com/docs/Theming/ThemeRollerReady&#034;&gt;ThemeRollerReady&lt;/a&gt;.  All being well you should see the themeswitcher below and a click on the link below that should give you a dalog box IFRAME:
&lt;/p&gt;&lt;p&gt;
&lt;script type=&#034;text/javascript&#034; src=&#034;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&#034;&gt;&lt;!-- --&gt;&lt;/script&gt;
&lt;script type=&#034;text/javascript&#034; src=&#034;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js&#034;&gt;&lt;!-- --&gt;&lt;/script&gt;
&lt;script type=&#034;text/javascript&#034; src=&#034;http://jqueryui.com/themeroller/themeswitchertool/&#034;&gt;&lt;!-- --&gt;&lt;/script&gt;
&lt;script type=&#034;text/javascript&#034; src=&#034;http://cse-mjmcl.cse.bris.ac.uk/blog/helpbox.js&#034;&gt;&lt;!-- --&gt;&lt;/script&gt;
&lt;script type=&#034;text/javascript&#034;&gt;
$(document).ready(function(){
    $(&#039;#switcher&#039;).themeswitcher({&#039;loadTheme&#039;: &#039;Smoothness&#039;});
});
&lt;/script&gt;
&lt;div id=&#034;switcher&#034;&gt;&lt;/div&gt;
&lt;a href=&#034;http://cse-mjmcl.cse.bris.ac.uk/blog/Loremipsum.html&#034; title=&#034;Lorem ipsum&#034; class=&#034;helpbox&#034;&gt;jQuery UI IFRAME dialog box example&lt;/a&gt;
&lt;/p&gt;&lt;p&gt;
alternatively you can see it at &lt;a href=&#034;http://cse-mjmcl.cse.bris.ac.uk/blog/helpbox-theme.html&#034;&gt;standalone jQuery UI IFRAME dialog box example&lt;/a&gt;.
&lt;/p&gt;
        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/07/02/1246571226133.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/07/02/1246571226133.html</guid>
    <pubDate>Thu, 02 Jul 2009 21:47:06 GMT</pubDate>
  </item>
  
  <item>
    <title>Fetching Servlet Filter init parameters from a properties file with Spring</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/06/08/1244470220815.html</link>
    
      
        <description>
          &lt;p&gt;
A while ago I wrote some code and thought it a neat trick but totally forgot about it until recently.  I am writing this blog entry, firstly to share the idea and secondly to see if somebody can give an alternative using only Spring classes (which would deepen my understanding).  Using Spring&#039;s DelegatingFilterProxy it is possible to hand off creation of servlet filters to a bean defined in Spring&#039;s application context.  This is very useful if you want to inject Spring managed beans into a your servlet filters.
&lt;/p&gt;
&lt;p&gt;
I am using the DelegatingFilterProxy mechanism to configure my servlet filter init parameters and in conjunction using PreferencesPlaceholderConfigurer so these come from a properties file.  This means I can easily switch between prod and dev environments without having to modify web.xml (I could even change the authenticationFilter implementation to something else but that is a different story!).  My aim here is to minimize the number of files I need to edit when moving between prod and dev environments.  I hope somebody else finds this approach useful.
&lt;/p&gt;
&lt;p&gt;
As an example I am &lt;a href=&#034;http://www.ja-sig.org/wiki/display/CASC/Using+CAS+with+Java&#034;&gt;using the Yale CAS Filter&lt;/a&gt;, which is considered a little long in the tooth now (but I&#039;m still using it).  It makes for a good example because there are numerous filter properties that you might want to change.  Even the most up to date version of the &lt;a href=&#034;http://www.ja-sig.org/wiki/display/CASC/Configuring+the+JA-SIG+CAS+Client+for+Java+in+the+web.xml&#034;&gt;JA-SIG CAS Filter&lt;/a&gt; could potentially benefit from using my approach.
&lt;/p&gt;
&lt;h3&gt;web.xml&lt;/h3&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
    ...
    &amp;lt;filter&amp;gt;
        &amp;lt;filter-name&amp;gt;authenticationFilter&amp;lt;/filter-name&amp;gt;
        &amp;lt;filter-class&amp;gt;org.springframework.web.filter.DelegatingFilterProxy&amp;lt;/filter-class&amp;gt;
    &amp;lt;/filter&amp;gt;
    &amp;lt;filter-mapping&amp;gt;
        &amp;lt;filter-name&amp;gt;authenticationFilter&amp;lt;/filter-name&amp;gt;
        &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;
    &amp;lt;/filter-mapping&amp;gt;
    ...
&lt;/pre&gt;
&lt;h3&gt;applicationContext.xml&lt;/h3&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;?xml version=&#034;1.0&#034; encoding=&#034;UTF-8&#034;?&amp;gt;
&amp;lt;beans xmlns=&#034;http://www.springframework.org/schema/beans&#034;
	   xmlns:xsi=&#034;http://www.w3.org/2001/XMLSchema-instance&#034;
	   xsi:schemaLocation=&#034;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&#034;&amp;gt;

    &amp;lt;bean class=&#034;org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer&#034;&amp;gt;
        &amp;lt;property name=&#034;location&#034; value=&#034;/WEB-INF/dev.properties&#034; /&amp;gt;
    &amp;lt;/bean&amp;gt;

    &amp;lt;bean id=&#034;authenticationFilter&#034; class=&#034;edu.bristol.web.ParameterPassingFilterProxy&#034;&amp;gt;
        &amp;lt;property name=&#034;parameters&#034;&amp;gt;
            &amp;lt;map&amp;gt;
                &amp;lt;entry key=&#034;edu.yale.its.tp.cas.client.filter.loginUrl&#034; value=&#034;${cas.loginUrl}&#034; /&amp;gt;
                &amp;lt;entry key=&#034;edu.yale.its.tp.cas.client.filter.validateUrl&#034; value=&#034;${cas.validateUrl}&#034; /&amp;gt;
                &amp;lt;entry key=&#034;edu.yale.its.tp.cas.client.filter.wrapRequest&#034; value=&#034;${cas.wrapRequest}&#034; /&amp;gt;
                &amp;lt;entry key=&#034;edu.yale.its.tp.cas.client.filter.serverName&#034; value=&#034;${cas.serverName}&#034; /&amp;gt;
                &amp;lt;entry key=&#034;edu.yale.its.tp.cas.client.filter.renew&#034; value=&#034;${cas.renew}&#034; /&amp;gt;
            &amp;lt;/map&amp;gt;
        &amp;lt;/property&amp;gt;
        &amp;lt;property name=&#034;targetBeanName&#034; value=&#034;casFilter&#034; /&amp;gt;
    &amp;lt;/bean&amp;gt;

    &amp;lt;bean id=&#034;casFilter&#034; class=&#034;edu.yale.its.tp.cas.client.filter.CASFilter&#034;/&amp;gt;

&amp;lt;/beans&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
The extra class that I added to make this work is available from &lt;a href=&#034;http://cse-mjmcl.cse.bris.ac.uk/blog/ParameterPassingFilterProxy.java&#034;&gt;ParameterPassingFilterProxy.java&lt;/a&gt; you are free to use it as you wish.
&lt;/p&gt;
&lt;link href=&#034;http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css&#034; type=&#034;text/css&#034; rel=&#034;stylesheet&#034; /&gt;
    &lt;script src=&#034;http://www.google.com/apis/maps/include/prettify.js&#034; type=&#034;text/javascript&#034;&gt;&lt;/script&gt;
    &lt;script type=&#034;text/javascript&#034;&gt;
    var calledprettyprint;

    function addLoadPrettyPrintEvent(func) {
    if (calledprettyprint) return;
    calledprettyprint = true;

    var oldonload = window.onload;
    if (typeof window.onload != &#039;function&#039;) {
        window.onload = func;
        } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
    }

    addLoadPrettyPrintEvent(prettyPrint);
&lt;/script&gt;

        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/06/08/1244470220815.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/06/08/1244470220815.html</guid>
    <pubDate>Mon, 08 Jun 2009 14:10:20 GMT</pubDate>
  </item>
  
  <item>
    <title>What would Hibernate do?  Avoidance of hbm2ddl.auto=update in production</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/05/14/1242335931598.html</link>
    
      
        <description>
          &lt;p&gt;
For the past 9 months I have been using &lt;a href=&#034;https://www.hibernate.org/397.html&#034;&gt;Hibernate Annotations&lt;/a&gt; (along with the Spring Framework) to build my development database.  Although I have a reasonably strong grasp of SQL I would still not consider myself fluent.  Hibernate does all the hard work for me.  Truth be told, there is a substantial learning curve to mastering mapping entity bean associations/relationships with Hibernate annotations.  However once you finally develop an understanding (or find enough examples of what you want) it is very easy to continually evolve a data model throughout system development.  Using annotations in conjunction with a suitably crafted import.sql file I can go from zero to system in no time.  So after several months&#039; development I am on the brink of having a production ready database containing precious real data that I do not want to loose.  Should I take the risk of using &lt;a href=&#034;http://docs.jboss.org/hibernate/stable/core/reference/en/html/configuration-optional.html#configuration-misc-properties&#034;&gt;&#034;hbm2ddl.auto=update&#034;&lt;/a&gt; to incorporate last minute database changes?  I think not.  I would not do this and this is not because I don&#039;t trust Hibernate but more because I do not trust myself.  If I accidentally used hbm2ddl.auto=create (as I do regularly in my development instance) then bang goes my production database and all the data it contains.
&lt;/p&gt;
&lt;p&gt;
So I wrote a small program to let me see what Hibernate would do if I ran an update without the associated risk of actually performing an update.  I have a Spring applicationContext.xml which contains something like the following (note the complete absence of hbm2ddl.auto property setting):
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;bean class=&#034;org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer&#034;&amp;gt;
    &amp;lt;property name=&#034;location&#034; value=&#034;jdbc.prod.properties&#034; /&amp;gt;
&amp;lt;/bean&amp;gt;
&amp;lt;bean id=&#034;dataSource&#034; class=&#034;org.apache.commons.dbcp.BasicDataSource&#034; destroy-method=&#034;close&#034;&amp;gt;
    &amp;lt;property name=&#034;driverClassName&#034;&amp;gt;
        &amp;lt;value&amp;gt;${jdbc.driverClassName}&amp;lt;/value&amp;gt;
    &amp;lt;/property&amp;gt;
    &amp;lt;property name=&#034;url&#034;&amp;gt;
        &amp;lt;value&amp;gt;${jdbc.url}&amp;lt;/value&amp;gt;
    &amp;lt;/property&amp;gt;
    &amp;lt;property name=&#034;username&#034;&amp;gt;
        &amp;lt;value&amp;gt;${jdbc.username}&amp;lt;/value&amp;gt;
    &amp;lt;/property&amp;gt;
    &amp;lt;property name=&#034;password&#034;&amp;gt;
        &amp;lt;value&amp;gt;${jdbc.password}&amp;lt;/value&amp;gt;
    &amp;lt;/property&amp;gt;
&amp;lt;/bean&amp;gt;
&amp;lt;bean id=&#034;sessionFactory&#034;
    class=&#034;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&#034;&amp;gt;
    &amp;lt;property name=&#034;hibernateProperties&#034;&amp;gt;
        &amp;lt;props&amp;gt;
            &amp;lt;prop key=&#034;hibernate.dialect&#034;&amp;gt;${hibernate.dialect}
            &amp;lt;/prop&amp;gt;
        &amp;lt;/props&amp;gt;
    &amp;lt;/property&amp;gt;
    &amp;lt;property name=&#034;annotatedClasses&#034;&amp;gt;
        &amp;lt;list&amp;gt;
            &amp;lt;value&amp;gt;edu.bristol.entity.Tom&amp;lt;/value&amp;gt;
            &amp;lt;value&amp;gt;edu.bristol.entity.Dick&amp;lt;/value&amp;gt;
            &amp;lt;value&amp;gt;edu.bristol.entity.Harry&amp;lt;/value&amp;gt;
        &amp;lt;/list&amp;gt;
    &amp;lt;/property&amp;gt;
    &amp;lt;property name=&#034;dataSource&#034; ref=&#034;dataSource&#034; /&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Now for my program, one thing that I have noticed is that update scripts that Hibernate would create will add new fields to a database but will not delete existing columns (even if these have been removed from the annotated entity).  It makes a good deal of sense not to delete columns from a production database without knowing exactly what you are doing so this is a good thing!
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

public class AcquireScripts {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(&#034;applicationContext.xml&#034;);
        // ampersand is very significant - it means get me an
        // instance of the beanfactory and not give me a bean
        LocalSessionFactoryBean sfb = (LocalSessionFactoryBean) context.getBean(&#034;&amp;sessionFactory&#034;);
        Configuration cfg = sfb.getConfiguration();
        Dialect dialect = Dialect.getDialect(cfg.getProperties());
        //printDropSchemaScript(cfg, dialect);
        //printSchemaCreationScript(cfg, dialect);
        printSchemaUpdateScript((SessionFactory) sfb.getObject(), cfg, dialect);
    }

    private static void printSchemaCreationScript(final Configuration cfg, final Dialect dialect) {
        String[] schemaCreationScript = cfg.generateSchemaCreationScript(dialect);
        for (String stmt : schemaCreationScript) {
            System.out.println(stmt + &#034;;&#034;);
        }
    }

    private static void printDropSchemaScript(final Configuration cfg, final Dialect dialect) {
        String[] dropSchemaScript = cfg.generateDropSchemaScript(dialect);
        for (String stmt : dropSchemaScript) {
            System.out.println(stmt + &#034;;&#034;);
        }
    }

    private static void printSchemaUpdateScript(final SessionFactory sf, final Configuration cfg, final Dialect dialect) {
        HibernateTemplate hibernateTemplate = new HibernateTemplate(sf);
        hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
        hibernateTemplate.execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Connection con = session.connection();
                        Dialect dialect = Dialect.getDialect(cfg.getProperties());
                        DatabaseMetadata metadata = new DatabaseMetadata(con, dialect);
                        String[] schemaUpdateScript = cfg.generateSchemaUpdateScript(dialect, metadata);
                        for (String stmt : schemaUpdateScript) {
                            System.out.println(stmt + &#034;;&#034;);
                        }
                        return null;
                    }
                });
    }
}
&lt;/pre&gt;
&lt;p&gt;
Notice the cunning use of the ampersand to acquire an instance of the LocalSessionFactoryBean rather than the actual SessionFactory itself.  So there we have it, all of the cleverness of Hibernate annotations working out what SQL we might need to update our database but none of the risk.
&lt;/p&gt;
&lt;link href=&#034;http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css&#034; type=&#034;text/css&#034; rel=&#034;stylesheet&#034; /&gt;
    &lt;script src=&#034;http://www.google.com/apis/maps/include/prettify.js&#034; type=&#034;text/javascript&#034;&gt;&lt;/script&gt;
    &lt;script type=&#034;text/javascript&#034;&gt;
    var calledprettyprint;

    function addLoadPrettyPrintEvent(func) {
    if (calledprettyprint) return;
    calledprettyprint = true;

    var oldonload = window.onload;
    if (typeof window.onload != &#039;function&#039;) {
        window.onload = func;
        } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
    }

    addLoadPrettyPrintEvent(prettyPrint);
&lt;/script&gt;
        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/05/14/1242335931598.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/05/14/1242335931598.html</guid>
    <pubDate>Thu, 14 May 2009 21:18:51 GMT</pubDate>
  </item>
  
  <item>
    <title>I am experimenting with a hosted blog</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/05/06/1241598748190.html</link>
    
      
        <description>
          My &#034;other blog&#034; is located at &lt;a href=&#034;http://markmclaren.blogs.ilrt.org/&#034;&gt;http://markmclaren.blogs.ilrt.org/&lt;/a&gt;.  I&#039;m looking to see how I get on with a more restricted environment!
        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/05/06/1241598748190.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/05/06/1241598748190.html</guid>
    <pubDate>Wed, 06 May 2009 08:32:28 GMT</pubDate>
  </item>
  
  <item>
    <title>An alternative to req:isUserInRole</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/01/15/1232020602585.html</link>
    
      
        <description>
          &lt;p&gt;
For me, HttpServletRequest&#039;s &lt;a href=&#034;http://java.sun.com/webservices/docs/1.5/api/javax/servlet/http/HttpServletRequest.html#getRemoteUser()&#034;&gt;getRemoteUser()&lt;/a&gt; and &lt;a href=&#034;http://java.sun.com/webservices/docs/1.5/api/javax/servlet/http/HttpServletRequest.html#isUserInRole(java.lang.String)&#034;&gt;isUserInRole(role)&lt;/a&gt; have always felt like the right place to obtain user information in a web application.  My experience with &lt;a href=&#034;http://www.ja-sig.org/products/cas/&#034;&gt;JASIG CAS&lt;/a&gt; filter and &lt;a href=&#034;http://securityfilter.sourceforge.net/&#034;&gt;SecurityFilter&lt;/a&gt; has taught me that it is easy to create a servlet filter and drop in a request wrapper (&lt;a href=&#034;http://java.sun.com/webservices/docs/1.5/api/javax/servlet/http/HttpServletRequestWrapper.html&#034;&gt;HttpServletRequestWrapper&lt;/a&gt;) and create a method to set the remoteUser value.  Couple this filter with something like Spring&#039;s &lt;a href=&#034;http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/filter/DelegatingFilterProxy.html&#034;&gt;DelegatingFilterProxy&lt;/a&gt; and it is easy obtain user details from some dependency injected external database.
&lt;/p&gt;
&lt;p&gt;
Once the remoteUser value exists in the wrapped request you can acquire the details in a JSP page using JSTL e.g:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;c:out value=&#034;${pageContext.request.remoteUser}&#034;/&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
or with simple expression language alone (JSP 2.4+), e.g:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
${pageContext.request.remoteUser}
&lt;/pre&gt;
&lt;p&gt;
The question is how to obtain the role information for a user.  Jakarta&#039;s &lt;a href=&#034;http://jakarta.apache.org/taglibs/doc/request-doc/intro.html&#034;&gt;Request tag library&lt;/a&gt; enables you to make use of role information.
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
&amp;lt;req:isUserInRole role=&#034;admin&#034;&amp;gt;
	&amp;lt;h1&amp;gt;Hello Admin User&amp;lt;/h1&amp;gt;
&amp;lt;/req:isUserInRole&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
For some reason the request tag library is now deprecated.  Of course you can still use it (and it is in the Maven repository) but there is something of a stigma associated with using deprecated code!  So I have come up with an alternative.  The issue is that it is not easy to invoke methods with parameter arguments from expression language.  You cannot simply call:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
${pageContext.request.isUserInRole(&#039;admin&#039;)}
&lt;/pre&gt;
&lt;p&gt;
Using expression language you can access scoped variables, arrays, lists and maps.  Most of the time this data will be acquired and stored in some scope prior to loading the JSP page (such as in a typical MVC application).  However, I have discovered that you can acquire runtime generated responses from a Map using Commons Collection&#039;s &lt;a href=&#034;http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/LazyMap.html&#034;&gt;LazyMap&lt;/a&gt; and this technique can be used to effectively perform a user role lookup.  I added the following method to my request wrapper (and made sure the request wrapper was publically accessible):
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
public Map getIsUserInRole() {
    Transformer factory = new Transformer() {
        public Object transform(Object mapKey) {
            return isUserInRole((String) mapKey);
         }
    };
    Map lazyMap = MapUtils.lazyMap(new HashMap(), factory);
    return lazyMap;
}
&lt;/pre&gt;
&lt;p&gt;
and this means I can now access role information in a way that looks very much like a direct call to request.isUserInRole(role).
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
${pageContext.request.isUserInRole[&#039;admin&#039;]}
&lt;/pre&gt;
&lt;p&gt;
Using an idea that &lt;a href=&#034;https://issues.apache.org/bugzilla/show_bug.cgi?id=22895&#034;&gt;Matthew Sgarlata had for the request tag library&lt;/a&gt; it is even possible to include support for multiple role queries.
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
public Map getIsUserInRole() {
    final String ROLE_DELIMITERS = &#034;, \t\n\r\f&#034;;
    Transformer factory = new Transformer() {

    public Object transform(Object mapKey) {
           StringTokenizer tokenizer = new StringTokenizer((String) mapKey, ROLE_DELIMITERS);
           boolean result = false;
           while (tokenizer.hasMoreTokens() &amp;&amp; !result) {
               if (isUserInRole(tokenizer.nextToken())) {
                   result = true;
               }
           }
           return result;
           }
     };
     Map lazyMap = MapUtils.lazyMap(new HashMap(), factory);
     return lazyMap;
}
  &lt;/pre&gt;
and the expression language becomes:
  &lt;pre class=&#034;prettyprint&#034;&gt;
${pageContext.request.isUserInRole[&#039;admin,staff&#039;]}
&lt;/pre&gt;
&lt;p&gt;
and this can be easily negated:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
${!pageContext.request.isUserInRole[&#039;admin,staff&#039;]}
&lt;/pre&gt;
&lt;p&gt;
&lt;a href=&#034;http://cse-mjmcl.cse.bris.ac.uk/IsUserInRoleAuthFilter.zip&#034;&gt;Sample WAR with source code available&lt;/a&gt; (Assuming Java 1.5+ and Maven build &#034;&lt;i&gt;mvn compile war:war&lt;/i&gt;&#034;).
&lt;/p&gt;
&lt;link href=&#034;http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css&#034; type=&#034;text/css&#034; rel=&#034;stylesheet&#034; /&gt;
    &lt;script src=&#034;http://www.google.com/apis/maps/include/prettify.js&#034; type=&#034;text/javascript&#034;&gt;&lt;/script&gt;
    &lt;script type=&#034;text/javascript&#034;&gt;
    var calledprettyprint;

    function addLoadPrettyPrintEvent(func) {
    if (calledprettyprint) return;
    calledprettyprint = true;

    var oldonload = window.onload;
    if (typeof window.onload != &#039;function&#039;) {
        window.onload = func;
        } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
    }

    addLoadPrettyPrintEvent(prettyPrint);
&lt;/script&gt;
&lt;h3&gt;Update&lt;/h3&gt;
&lt;p&gt;
As an update to this, I am now using the above technqie with what I call a &lt;a href=&#034;http://cse-mjmcl.cse.bris.ac.uk/blog/ClearingLazyMap.java&#034;&gt;ClearingLazyMap&lt;/a&gt;.  This class implements the LazyMap interface but does not cache any values when they are fetched.  This is necessary in order to pick up the most recent database updates, see also &lt;a href=&#034;http://www.mail-archive.com/user@commons.apache.org/msg03235.html&#034;&gt;LazyMap that always calls transform, good idea?&lt;/a&gt;
&lt;/p&gt;
        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2009/01/15/1232020602585.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2009/01/15/1232020602585.html</guid>
    <pubDate>Thu, 15 Jan 2009 11:56:42 GMT</pubDate>
  </item>
  
  <item>
    <title>Re-inventing ORM, annotations re-invented for databases!</title>
    <link>http://cse-mjmcl.cse.bris.ac.uk/blog/2008/11/05/1225887924894.html</link>
    
      
        <description>
          &lt;p&gt;
I am working on a web application where one of the requirements is that I use an in-house Java database access framework.  Personally I would prefer to use something that complements Spring and that makes my life easier such as &lt;a href=&#034;http://www.hibernate.org/397.html&#034;&gt;Hibernate Annotations&lt;/a&gt;, &lt;a href=&#034;http://ibatis.apache.org/&#034;&gt;iBATIS&lt;/a&gt; or even &lt;a href=&#034;http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html&#034;&gt;Spring JDBCTemplates&lt;/a&gt; but requirements are requirements.
&lt;/p&gt;
&lt;p&gt;
One of the problems I face is that I need to re-invent a lot of &lt;a href=&#034;http://en.wikipedia.org/wiki/Object-relational_mapping&#034;&gt;ORM&lt;/a&gt; wheels.  The framework is very similar in essence to iBATIS.
All the SQL is stored in external XML configuration files.  The framework conducts the mappings conversion from ResultSets to lists of beans (using commons-beanutils).  As such, like iBATIS there is plenty of scope to generate most of the repetitive Java code (interfaces, implementations, facades) and configuration files (SQL &lt;a href=&#034;http://en.wikipedia.org/wiki/CRUD_(acronym)&#034;&gt;CRUD&lt;/a&gt;).  I have previously experimented with the &lt;a href=&#034;http://boss.bekk.no/boss/middlegen/&#034;&gt;Middlegen&lt;/a&gt; code generation with iBATIS. Middlegen code is a little long in the tooth, as a project it seems to have stagnated back in 2005 (I suppose &lt;a href=&#034;http://ibatis.apache.org/ibator.html&#034;&gt;iBATOR&lt;/a&gt; and &lt;a href=&#034;http://www.hibernate.org/255.html&#034;&gt;Hibernate Tools&lt;/a&gt; have long since superseded it).  That said, because Middlegen is based on &lt;a href=&#034;http://velocity.apache.org/&#034;&gt;Velocity templates&lt;/a&gt; it is very flexible and perfect for this kind of thing.  I was able to port my previous iBATIS code generating templates to produce suitable Java code for use with the in-house framework.
&lt;/p&gt;
&lt;p&gt;
I have managed to get some quite sophisticated code generated.  Apparently &#034;&lt;a href=&#034;http://today.java.net/pub/a/today/2006/07/13/lazy-loading-is-easy.html&#034;&gt;Lazy Loading is easy&lt;/a&gt;&#034;, so I am attempting to implement that using &lt;a href=&#034;http://cglib.sourceforge.net/&#034;&gt;CGLIB&lt;/a&gt; in my DAO.  I have also produced a method that is similar to &lt;a href=&#034;http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#saveOrUpdate(java.lang.Object)&#034;&gt;Hibernates&#039;s SaveOrUpdate&lt;/a&gt; method.  A given entity may have two kinds of relationship;  &lt;i&gt;&lt;b&gt;target one&lt;/b&gt;&lt;/i&gt; (OneToOne, ManyToOne) or &lt;i&gt;&lt;b&gt;target many&lt;/b&gt;&lt;/i&gt; (OneToMany).  I have no ManyToMany relationships and this is deliberate!  
&lt;/p&gt;
&lt;p&gt;
How my SaveOrUpdate works is derived from some code I found in iBATIS in Action (By Clinton Begin, Brandon Goodin and Larry Meadors, Published by Manning).  In  section &lt;i&gt;5.3.2 Updating or deleting child records&lt;/i&gt; the following method is suggested:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
public void saveOrder(SqlMapClient sqlMapClient, Order order) 
throws SQLException {
  if (null == order.getOrderId()) {
    sqlMapClient.insert(&#034;Order.insert&#034;, order);
  } else {
    sqlMapClient.update(&#034;Order.update&#034;, order);
  }
  sqlMapClient.delete(&#034;Order.deleteDetails&#034;, order);
  for(int i=0;i&amp;lt;order.getOrderItems().size();i++) {
    OrderItem oi = (OrderItem) order.getOrderItems().get(i); 
    oi.setOrderId(order.getOrderId());
    sqlMapClient.insert(&#034;OrderItem.insert&#034;, oi);
  }
}
&lt;/pre&gt;
&lt;p&gt;
The above method addresses the &lt;i&gt;&lt;b&gt;target many&lt;/b&gt;&lt;/i&gt; relationships but does not address the &lt;i&gt;&lt;b&gt;target one&lt;/b&gt;&lt;/i&gt; relationships.  My method works more like this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Iterate through all the &lt;i&gt;&lt;b&gt;target one&lt;/b&gt;&lt;/i&gt; entities and SaveOrUpdate these.  Inserting any generated ids into the current entity.
&lt;li&gt;Save the current entity data fields
&lt;li&gt;Iterate through all the &lt;i&gt;&lt;b&gt;target many&lt;/b&gt;&lt;/i&gt; relationships and SaveOrUpdate these.
&lt;/ol&gt;
&lt;p&gt;
At this point I got a little stuck.  I found that relatively often my &lt;i&gt;&lt;b&gt;target one&lt;/b&gt;&lt;/i&gt; relationships should not be the subject of further SaveOrUpdates.  The characteristic of these relationships is when the data is fairly static and mostly provides the conent of drop-down lists.  When you make a selection from a such a list you do not usually need to update the list itself! 
&lt;/p&gt;
&lt;p&gt;
Essentially, Middlegen generates everything based on the contents of the database.  For these relationships I needed some way to designate the code generation behaviour for certain database columns.
&lt;/p&gt;
&lt;p&gt;
In Java Persistence API implementations, like Hibernate Annotations, this kind of behaviour is very easily represented by using &lt;a href=&#034;http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html&#034;&gt;@Column(insertable=false, updatable=false)&lt;/a&gt;.  I needed a way for Middlegen to acquire this information from the database.  Then I had a &lt;b&gt;brainwave&lt;/b&gt;, in the most recent version of Middlegen (from the CVS) you can &#034;getRemarks&#034; for tables and columns.  Database remarks can be acquired from &lt;a href=&#034;http://java.sun.com/j2se/1.4.2/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[])&#034;&gt;DatabaseMetaData&lt;/a&gt;.  The unfortunate thing is that I am using an Oracle database which AFAICT uses a non-standard treatment of &#034;remarks&#034;.  It turns out that the default behaviour of Oracle drivers is not to populate the DatabaseMetaData with remarks. In Oracle remarks are contained inside two special tables; &lt;i&gt;user_tab_comments&lt;/i&gt; and  &lt;i&gt;user_col_comments&lt;/i&gt;.  You can override the default behaviour of Oracle driver to populate remarks, this can be done at the BasicDataSource (using &lt;i&gt;addConnectionProperty(&#034;remarksReporting&#034;, &#034;true&#034;)&lt;/i&gt;) or on a OracleConnection (using &lt;i&gt;setRemarksReporting(true)&lt;/i&gt;).
&lt;/p&gt;
&lt;p&gt;
Armed with this information I can &lt;i&gt;&lt;b&gt;&#034;annotate&#034;&lt;/b&gt;&lt;/i&gt; database columns, so I can use something like this:
&lt;/p&gt;
&lt;pre class=&#034;prettyprint&#034;&gt;
COMMENT ON COLUMN &amp;lt;table&gt;.&amp;lt;column&gt; IS &#039;insertable=false, updatable=false&#039;;
&lt;/pre&gt;
&lt;p&gt;
I tweaked MiddlegenPopulator to allow it to acquire remarks for Oracle from the DatabaseMetaData.  The database now contains all the information I need to generate my DAO code.  I can now tweak my DAO generation templates to miss out the SaveOrUpdate code for &lt;i&gt;&lt;b&gt;target one&lt;/b&gt;&lt;/i&gt; relationships which match the column comment of &#039;insertable=false, updatable=false&#039;.
&lt;/p&gt;
&lt;p&gt;
Simple huh?  Or maybe just plain scary... annotations with databases!
&lt;/p&gt;
   &lt;link href=&#034;http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css&#034; type=&#034;text/css&#034; rel=&#034;stylesheet&#034; /&gt;
    &lt;script src=&#034;http://www.google.com/apis/maps/include/prettify.js&#034; type=&#034;text/javascript&#034;&gt;&lt;/script&gt;
    &lt;script type=&#034;text/javascript&#034;&gt;
    var calledprettyprint;

    function addLoadPrettyPrintEvent(func) {
    if (calledprettyprint) return;
    calledprettyprint = true;

    var oldonload = window.onload;
    if (typeof window.onload != &#039;function&#039;) {
        window.onload = func;
        } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
    }

    addLoadPrettyPrintEvent(prettyPrint);
    &lt;/script&gt;
        </description>
      
      
    
    
    
    <comments>http://cse-mjmcl.cse.bris.ac.uk/blog/2008/11/05/1225887924894.html#comments</comments>
    <guid isPermaLink="true">http://cse-mjmcl.cse.bris.ac.uk/blog/2008/11/05/1225887924894.html</guid>
    <pubDate>Wed, 05 Nov 2008 12:25:24 GMT</pubDate>
  </item>
  
  </channel>
</rss>
