Monday, December 13, 2010

OpenID + Spring MVC 3 + Spring Security 3 + OpenID Selector

We decided to move our authentication into openID. Meanwhile, we upgrade our security from Spring Security 2 to 3. Adding OpenID to spring security is straightforward, add the following tag into "<http>" tag of security.xml
<openid-login login-page="/login.jsp" login-processing-url="/openlogin" user-service-ref="userDao"
                 authentication-failure-handler-ref="openIdAuthFailureHandler"/>

And OpenID will take over the authentication.

However, there are more to change for openID.

First and most obviously, we need a new login page takes user to openid provider instead of our own user/password checker. We chose openid-selector (1.2 currently) as the spring security choose it for demo. It is a fairly nice package. It allows one to choose from several openid providers (Google, Yahoo, AOL, OpenID, blogger, flicker, ...) Several things need to be done to use it:


  • However, it uses jQuery and has lots of conflict with prototype we are using. Fortunately, the conflict originates mostly from "$". I modify the two js file, replace all "$" with "jQuery" and add jQuery.noConflict(); in the beginning and firebug stops to complain.


  • There are several options in openid-jquery.js to play with. I use default mostly, except "no_sprite" to true as I have uncomment flickr and its not in the big sprite picture.


  • In the login page, add following into "<head"> tag (notice that openid-jquery.js should be add before openid-jquery-en.js)
    <script type="text/javascript" src="<c:url value="/scripts/jquery/openid-selector/js/openid-jquery.js" />"></script>
         <script type="text/javascript" src="<c:url value="/scripts/jquery/openid-selector/js/openid-jquery-en.js" />"></script>

    and add following into "<body">
    <script type="text/javascript">
            jQuery.noConflict();
            jQuery(document).ready(function(){
                openid.img_path="<c:url value='/scripts/jquery/openid-selector/images/'/>";
                openid.init("openid_identifier");
                jQuery("#openid_identifier").focus();
            });
        </script>

    Notice the openid.img_path has to be set outside if the openid-selector files are not put under the root.

These pretty much take care of login. But now we also need to modify signup process. Originally, you click signup and then put a new username/password. Now with openid, you first login with an openid from one of the providers, system found that you are not a user, and provide you a signup sheet. That is why in above <openid-login"> tag, we need a special "openIdAuthFailureHandler" for "authentication-failure-handler".

We can extend a spring handler for this purpose.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.imirsel.nema.webapp.security;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.openid.OpenIDAuthenticationStatus;
import org.springframework.security.openid.OpenIDAuthenticationToken;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

/**
 *  Customized {@link AuthenticationFailureHandler} that redirect to sign-up page
 * if the OpenID authentication succeeds, but the user name is not yet in local DB of the container
 * @author gzhu1
 */
public class OpenIDAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    static private Log logger = LogFactory.getLog(OpenIDAuthenticationFailureHandler.class);

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
            HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {
        logger.error(exception, exception);
        if (exception instanceof UsernameNotFoundException
                && exception.getAuthentication() instanceof OpenIDAuthenticationToken
                && ((OpenIDAuthenticationToken) exception.getAuthentication()).getStatus().equals(OpenIDAuthenticationStatus.SUCCESS)) {
            DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
            request.getSession(true).setAttribute("USER_OPENID_CREDENTIAL", exception.getAuthentication().getPrincipal());
            // redirect to create account page

            logger.info("user (" + exception.getAuthentication().getPrincipal() + "," + exception.getExtraInformation() + ") is not found and redirect to signup.");
            redirectStrategy.sendRedirect(request, response, "/signup.html");

        } else {
            super.onAuthenticationFailure(request, response, exception);
        }
    }
}

and declare the bean in applicationContext.xml.
<bean id="openIdAuthFailureHandler" class="org.imirsel.nema.webapp.security.OpenIDAuthenticationFailureHandler">
        <property name="defaultFailureUrl" value="/login.jsp"/>
    </bean>

Of course, now I do not really have a password, so I generate a random string for password, because a password is needed somewhere else.

That is it. Now your user can sign-in with their Google, Yahoo, AOL, open-id account directly.

Tuesday, December 7, 2010

My Review of HP G62-340US 15.6" Laptop

Originally submitted at Staples

Now you can do more and have more fun without spending more. The HP G62 notebook PC features the latest technology and enhanced security right out of the box for the perfect balance of performance, connectivity, and worry-free computing. With its cl ean design and textured HP Imprint finish in char...


Solid buy

By zggame from urbana, IL on 12/7/2010

 

5out of 5

Pros: Quiet, Quality Display

Best Uses: Web Browsing, Video, Word Processing

Describe Yourself: Tech Savvy

Primary use: Personal

I bought at the sale for $379-$50 coupon for my parents. It has HDMI and webcam. They mostly just use skype, web browsing and watch online streaming TV/movie. It is perfect for their usage. The thrown in office starter is not bad. HDMI is easy for them to connect TV to watch video, better than VGA+audio port. Webcam works fairly well during skype. Overall, it is plenty for them. Great buy. Not too many bloatware. Norton is annoying for the trial. I uninstalled it and put a free Avast Personal. Office starter should be more than enough for some basic use.

(legalese)

Wednesday, October 13, 2010

Set optional jetty-env.xml for JNDI in maven jetty plugins

I talked about how to set up jndi with jetty in last post. But I still like to use "jetty:run" in the maven jetty plugin. It uses an embed server that hard to configure. Later I found a way to specify a non-conventional position for jetty-env.xml in maven-jetty-plugin. So I put jetty-env.xml in that position and specify it in pom.xml. Now I can still use jetty:run. But when it packaged as war and deployed in web container. It cannot run without setting JNDI in server.

Here is my setting:
1. Put jetty-env.xml in WEB-INF/local/jetty-env.xml;
2. Add following into pom.xml

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.22</version>
<configuration>

<contextPath>/</contextPath>

<jettyEnvXml>src/main/webapp/WEB-INF/local/jetty-env.xml</jettyEnvXml>
.......
</configuration>
</plugin>


Well, if you really hate any settings in svn repository. You can put jetty-env.xml somewhere outside svn directory. Use

<jettyEnvXml>${jetty.setting}</jettyEnvXml>

Now when you start maven append the system property jetty.setting=directory.

mvn -Djetty.setting=**/jetty-env.xml jetty:run

Friday, October 8, 2010

Use JNDI with Spring/Jetty

We used to bury the settings (databases, remote calls urls, ...) in Maven's setting.xml. We keep several profiles for settings for different servers and use maven's profile selector to switch between them. This approach hide the sensitive information (username/password to databses...) in our local machines and switch between different profiles fairly effectively. However, it is painful to keep settings.xml sync between us, especially when we changes setting in server pretty frequently. So we decide to move to JNDI, so the setting goes with the servlet server (Jetty now, maybe some more powerful stuff later) and no more sync problem.

It took me three full days! Unfortunately, it becomes another typical experience with the Configuration Nightmare. One spends hours after hours read stuff all over internet and nothing makes sense, until finally, it makes sense and actually is QUITE SIMPLE!

Two pages are mostly useful. Jetty's JNDI page and this one (a little out-of-date and a few flaw).

So set up the resources in JNDI first. We need two types: DataSource for databases and normal Strings for remote call urls.

1. First of all, jetty by default does not support jndi. So we need to tell jetty to turn it on. (Modify your etc/jetty.xml file.)
<Array id="plusConfig" type="java.lang.String"> -->
   <Item>org.mortbay.jetty.webapp.WebInfConfiguration</Item>
   <Item>org.mortbay.jetty.plus.webapp.EnvConfiguration</Item>
   <Item>org.mortbay.jetty.plus.webapp.Configuration</Item>
   <Item>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</Item>
   <Item>org.mortbay.jetty.webapp.TagLibConfiguration</Item>
 </Array>

<Call name="addLifeCycle">
     <Arg>
       <New class="org.mortbay.jetty.deployer.WebAppDeployer">
         <Set name="contexts"><Ref id="Contexts"/></Set>
         <Set name="webAppDir"><SystemProperty name="jetty.home" default="."/>/webapps</Set>
   ......
        <Set name="configurationClasses"><Ref id="plusConfig"/></Set>
       </New>
     </Arg>
   </Call>


Alternatively, one can turn it on in one particular webApp, but that is kind of complicated, and we would like resources configured in server level. So this works fine for us.

2. Adding the resources.
Create a xml file (myjndi.xml) and put it along with jetty.xml in jetty.home/etc.
<Configure  id="Server" class="org.mortbay.jetty.Server">
<New id="dataSource" class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>jdbc/dataSource</Arg>
        <Arg>
            <New class="org.apache.commons.dbcp.BasicDataSource">
                <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
                <Set name="url">jdbc:mysql://localhost:3306/diy090?autoReconnect=true</Set>
                <Set name="username">user</Set>
                <Set name="password">pass</Set>
                <Set name="maxActive">100</Set>
                <Set name="maxWait">1000</Set>
                <Set name="poolPreparedStatements">true</Set>
                <Set name="defaultAutoCommit">true</Set>
            </New>
        </Arg>
    </New>
    
    <New class="org.mortbay.jetty.plus.naming.EnvEntry">
        <Arg>flowservice/url</Arg>
        <Arg type="java.lang.String">rmi://remote.service.call:1099/FlowService</Arg>
        <Arg type="boolean">true</Arg>
    </New>
.......
</configure>

This will add the resource ("java:comp/env/jdbc/dataSource", "java:comp/env/flowservice/url") in the server level that available to every web applications. Note that "id" attribute in configure tag needs to match "id" attribute in jetty.xml so jetty know they are for the same server.
Now instead of starting jetty by "java -jar start.jar", we need to tell jetty to use both the default setting (etc/jetty.xml) and extra xml (myjndi.xml), the command is "java -jar start.jar etc/jetty.xml etc/myjndi.xml". Or we can simply put the content of <configure> tag into jetty.xml. Then one can use the same old command "java -jar start.jar".

Alternatively one can created a xml file called jetty-env.xml with the resource and put it along with web.xml in WEB-INF. This supplies the info only for that particular web application. But now one should use instead

<Configure class="org.mortbay.jetty.webapp.WebAppContext">


Note: to make the data source work, relevant jars need to be in jetty's library jetty.home/lib/ext. For common dbcp with mysql. Here is the list:
commons-dbcp-1.2.1.jar
commons-collections-3.2.jar
commons-pool-1.2.jar
mysql-connector-java-5.1.6.jar



3. Now the normal objects (String, "java:comp/env/flowservice/url") are exposed to web application now. But datasource is not yet. (That took me more than a day to figure out/realize.) One has to declare it in web.xml of web application.

<resource-ref>
<res-ref-name>jdbc/dataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


4. Spring provided the new tag that is much easier to work with than previously.

<jee:jndi-lookup jndi-name="java:comp/env/jdbc/dataSource" id="dataSource" />

<jee:jndi-lookup jndi-name="java:comp/env/flowservice/url" id="flowserviceUrl" />

That is it. Now you have two beans dataSource(java.sql.DataSource) and flowserviceUrl(String) ready for anything you likes.

Friday, September 17, 2010

SVN Merge is Evil

It is really hard. I worked on a branch for a new feature maybe 2 months. The trunk is kept with some small enhancement and bug-fix. Today is the merge day. OMG, that is hard. I used the merge in Netbeans first. After 40 minutes I seem to get all the brown sign of conflicts out and committed it. Well, that is not so bad, I thought. But soon I discovered quite some new files are missing and some are not updated. Boom! I rolled things back to last reversion and recommitted. This time I tried to do it in Eclipse. Well, it seems more conflicts pop up and I guessed that Eclipse's SVN plugin was better. I was wrong after another half an hour. Still some files were showed updated but they are definitely old. What is wrong!

It turns out that neither one handles TREE-CONFLICTs well. And yes, it proliferates for my merge. And neither provides any good way to handle it. I have to done it one at a time in command line. SVN is really dumb for tree-conflict. I checked out a clean reversion again. I merged once. It stops at certain revision. I resolved conflicts (normal ones) in Eclispe. Then I tried to hand-mark those tree-conflict as resolved. And then I merged again. This time it progressed to some further revision with some new conflicts. I marked them resolved again, merged again. .... Five, six cycles later, I finally get to the HEAD. :) The stupid things about svn is that if I added one file and later removed it in the branch, merging stops at some revision with adding the file, then next time merging removes it. Odd price to pay with a revision system with historical approach.

That is really hard!

Note: A couple of days later, I found it missed a few files. OMG! (9/20)

Tuesday, August 31, 2010

Name Parameter in HQL of Hibernate

Name Parameter in HQL is nice, like the parameter statement in sql, it is more robust than build the hql in flight and for the special characters. But it is also a little picky. It took me about 30 minutes to figure out what is wrong and the correct combination.


Session session=getSession();
String hql="from Job where (ownerId=:userId) AND ((name LIKE :keyword) OR (description LIKE :keyword) "
+" OR (flow.name LIKE :keyword) OR (flow.description LIKE :keyword)"
+"OR (flow.typeName LIKE :keyword) OR (flow.keyWords LIKE :keyword))";
Query query=session.createQuery(hql);
query.setLong("userId", userId).setString("keyword", "%"+keyword+"%");


You cannot put flow.keyWords LIKE '%:keyword%' or flow.keyWords LIKE %:keyword% in the hql, it has to go in the setString().

Tuesday, August 24, 2010

Switch to Netbeans

I get fairly frustrated by Eclipse. It is nice and polished in most sense. However, a few things keep bothering me. A few essential tools in our work needs plugins. Svn and maven plugins are both problematic. Early this year, for some reason, svn plugin keeps freezing the eclipse. Maven plugin also feels somehow weird. I did not get it through until I start to use spring's Spring Tools Suite(STS), a customized eclipse from spring source. It is kind of nice with maven built-in. I still need to install svn plugin, which does not freeze anymore. But the svn commit often fails, not sure whether I should blame the plugin or the googlecode's svn repository though. But it is likely some suboptimal setting in svn plugin. However, maven plugin (M2eclipse) still runs sluggishly and occasionally it freezes STS, especially in the MacBook Pro (4G Ram, 2.4G T8300). It often takes 10 seconds or more to open a pom.xml files, even in a fairly powerful machine ( 2.4G Core Quad, 8G Ram). Our projects are reasonably complicated, dependency in the order of 100 things. It has some nice tool for maven, such as the search in dependency. But the speed is too slow. It takes even 10 seconds to switch to a window of pom.xml. Something seriously wrong with m2eclipse.

Recently Eclipse upgrade to 3.6. I tried to upgrade. But it does not work well for me. Some common shortcut key does not work such as Ctrl-Alt-C for commit.

I don't remember how it starts. I saw netbeans a few days ago. I used netbeans before eclipse. But it is a long time ago. In recent few projects, everyone else use s eclipse, so I use it as well. Well, I took a serious look at netbeans (6.9.1) this time, to my pleasure, it has maven and svn built-in. So it should be OK for my project. Our project is a multi-module maven project. I first thought the project is eclipse specified as we commit from eclipse. And I know for sure that there are some eclipse's setting in repository. I tried netbeans and it works. It actually works pretty better than I thought. It checkout the project into several projects, each module becomes a project. It actually is better, as now each project's default paths (src/test/web/res/..) are recognized automatically into the project viewer. In eclipse, it is one project and eclipse cannot recognize the default maven paths in modules. So they are all normal folders and it is quite inconvenient to look for the path I need within about 10 different folders. One plus. Well, soon I found a few other goodies. It automatically include the default ~/.m2/settings.xml into the project. This is fairly convenient because it does control quite some properties in the project and I often needs to modify it. And best of all, pom.xml opens fast. In the crude looking, it is treated as a simple xml file. And a close look shows that you can also open a dependency graph, which does take some time. But most time, I do not need to see that, I only need to work with it as simple xml.

Svn is not as powerful. It does not provide a svn repository view. However, you can checkout things and other normal tools like update, commit, merge, diff are there and that is enough for me.

And an extra plus is that it seems bundled with Spring. And I got some basic tools for spring out of box, such as bean editor...

Overall, netbeans seems to run faster. It does not looks as nice, swing is not as good as SWT. But it looks OK, and this is the secondary concerns for me. Things seems integrated more tightly in netbeans. Eclipse seems more module, everything comes in as plugin and there are lots of great plugin. But for my work, I only need those typical tools. And netbeans seems to serve me better.

I switched to netbeans and it works fine for me for a week now . We will see how it works out in more time.


Update: Dec. 16th, 2010
Two good things about Netbeans:
* It formated HTML corrected. You will definitely hate Eclipse/sprinesource when you try to auto-format html. Netbeans also take care of javascript in html <script> tag.
* Netbeans also understand javascript syntax to some degree and points out some obvious typo such as extra ";", "}", ... Javascript is nice, but hard to debug and often a small typo like that costs me half an hour as firebug/chrome developer tool does not realize it and reports some other errors and leads you everywhere but the correct place. That is a really benefit with Netbeans.

Sunday, July 18, 2010

Hibernate (2)

We use dbunit to populate the database with some data in testing phase. I tried to generate some new dbunit export with new "order" value. It always fail to load, I have to revert the data back to the time without any order data. It said it could not find the new column for "order". I finally decided to sit down and work on it. After maybe 20 minutes guessing, I figure it is probably the maven-hibernate3 plugin generated the ddl without those column. I delete this plug-in and it goes well.

I think I also found a bug in their documentation. In their example,
(3.5.1Final), the id field is declared non-nullable.

@Entity
public class Parent {
@OneToMany
@OrderColumn(name="order")
@JoinColumn(name="parent_id", nullable=false)
private List<Child> children;
...
}

@Entity
public class Child {
...
@ManyToOne
@JoinColumn(name="parent_id", insertable=false, updatable=false, nullable=false)
private Parent parent;
...
}

I have something very similar, a MirexSubmission with a list of MirexNote's. I also make the cascade from MirexSubmision.


@Entity
@Table(name="mirex_submission")
public class MirexSubmission implements Serializable {
...
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER)
@OrderColumn(name="note_order")
@JoinColumn(name="submission_id")
public List<MirexNote> getNotes() {
return notes;
}

.....
}



@Entity
@Table(name="mirex_note")
public class MirexNote implements Serializable{
...
@ManyToOne
@JoinColumn(name="submission_id",insertable=false, updatable=false,nullable=true)
public MirexSubmission getSubmission() {
return submission;
}
...
}


However, it always give me some error when I try save (Hibernate merge()) about "no default value for "submission_id" column. It does not matter I merge the MirexNote or MirexSubmission object, in which case it cascade to MirexNote. I check the schema, "submission_id" column does not allow null value, but with out the default. And obviously, it is no wonder the insert on this table fail. Finally I change to nullable=true and things worked out.

Correction (Aug. 3rd): It seems to work fine if both ends (many-to-many, one-to-many) set nullable=false.

Friday, July 16, 2010

Hibernate

Recently I start to build an sub system for MIREX submission. It is not too complicated, it should accept submission of programs with certain metadata, such as contributors, notes, etc... And Mirex runner (NEMA system, or a human runner) should be able to change the status, leave note and inform the submitter. It is not simple when you need to consider every detail. And I have already spent about two weeks on it.

One thing I need to learn is Hibernate. We used that already in our system. But Amit and others have implemented it and provided me with a DAO object. So I have no direct interaction with it. This time, I have to write every bit from the scratch. Well, that is not totally true. I have their old code for reference. I use the annotation mapping, and spring hibernate template. I spent quite some time to read "Java Persistence with Hibernate", not the best choice. The book was written by one of the authors of Hibernate, and is very detailed and not really good for the first-timer. After couple of days, I finished about half the books and started to read the hibernate documentation from their site. It turns out much more accessible. It has some good example and explains things pretty clearly and I really like it.

Today, I realize that I need to have bi-direction many-to-one and many-to-many association with list. The order is important. I looked though the book and again, the documentation gives me an easier answer. @OrderColumn. And here comes the problem. We are using a quite mixed version of hibernate, some modules use 3.2.6, some uses 3.3.0GA and some use 3.4.0GA, with JPA 1. None of them support @OrderColum. It took me a while to figure out that this is the new annotation for JPA2. I have to upgrade hibernate in order to use this feature. Otherwise, I can use some hibernate annotation. I figured to whatever, just upgrade. So the latest 3.5.1Final we go.

The first problem I run into is that I cannot find it in the main maven repoistory. After some search, I found it is only in JBoss's repository. I need to add it into our own proxy repository server. I have to ask Amit for the username/password for it. After some talk, I found out we have already had it in the proxy list. I SHOULD HAVE tried before ask...

And soon, I found the module structure changes. Hibernate-annotation depends on hibernate-core. So I only need to include hibernate-annotation. But soon I run into the next problem, Spring cannot build the session-factory for Hibernate, it cannot find some classes about slf4j. I am confused. Hibernate already has slf4j dependence. After some 20 minutes on google, I finally figured out that Hibernate has slf4j-api. But I also need to make my code depends on slf4j12. So include it and I got new different error.

This time is ehcache. The old code has hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider. I looked at hibernate-core.jar, and sure, it dose not have such class. After a while, I realized it might not in core. I check the repository, there is one module, hibernate-ehcache. Here we go. But again, new problem pops up.

It is something about assistance. I am confused again. Hibernate does have dependency on javaassistence. A close look says that is is in test scope. Maybe that is the reason. Add the new dependency, and finally my unit tests sort of run.

My old database schema does not work with the new ordercolumn. The project has dependency on hibernate3-maven plug-in. I used to just copy their output ddl into mysql to generate the new database. But this time after I import the ddl, the test always tell me some field "note_Orders" is missing. I am really confused. I gives the @OrderColumn a new name. After run the maven test several times and I started to read the ddl more carefully and sure, the order columns are missing. WTF!!! Well, it turns out that hibernate3-maven plug-in depends on hibernate-3.4.0GA. It is probably does not understand this new annotation.

I add hibernate.hbm2ddl.auto=update into the hibernate config. And it seems to update my schema fine.

WHEW, quite some work!

Thursday, July 1, 2010

Java enum

Java 5 enum is nice, but it is not terribly well documented. The useful information is scattered around. I did a careful study maybe 2 years ago but I am a bit loss when I try to look around for its information now. So maybe it is better to document everything I need here for later reference.


A few useful links:
Type-Safe Enumerations
Using the built-in enumeration methods: values( )
Sun's Tutorial
Spring MVC form tags for enum

Monday, June 28, 2010

XStream Map<String,Ojbect> is not a good idea for Multi-Models

I guessed in an earlier post that I might be able to get multi-model work with XStream using a Map<String,Ojbect>. Well, it does not work well. The ending result is something like

{"map":
[{"entry":[
{"string":"jobs","list":[""]}, {"string":"head","org.imirsel.nema.flowservice.config.SimpleMeandreServerProxyConfig": {"host":"nema.lis.uiuc.edu","port":11714,"maxConcurrentJobs":1,"username":"admin","password":"admin"}},
{"string":"workers","map":[{"entry":"org.imirsel.nema.flowservice.config.SimpleMeandreServerProxyConfig":{"host":"nema-c-1-2","port":11714,"maxConcurrentJobs":2,"username":"admin","password":"admin"},"org.imirsel.nema.flowservice.config.MeandreServerProxyStatus":{"numRunning":0,"numAborting":0}}}]}]}]}

Not nice. It is possible in javacript, but will need quite some codes to handle. Still, I would like to stick with something required minimal massaging in javascript side. Mine current solution seems better.

Sunday, June 27, 2010

Spring Webflow

I have been using it for maybe two months and probably can say a little about it now. It is not our first choice. That would be GWT. We started NEMA mostly with the traditional Spring MVC. Amit, the leading programmer in NEMA, familiar with it, although he leaved the binding/command part out of the picture. So we used it kind of strictly of MVC model. I did not use spring mvc before, but I used Struts 1 and Spring in last project and the concepts of MVC and Inverse of Control (IoC) is not unfamiliar. I mostly do the patching work for Amit, adding a new controller, modify the existing one, etc.

Later, we need a more wizard like function, and Amit envision a site with more Ajax-like functions, such as status bar, ... So he asked me to look into GWT. But I am not familiar with it and things get bogged down as I learned it. Mostly, I felt a bit uncomfortable with GWT because it lacks the mature frame work like Spring MVC, everything backs to JSP/servlet stage. Also, it depends on CSS heavily and none of us are comfortable with it. But if it is now, I might stick longer and be more patient with it and maybe we should use it. Anyway, we cut it loose and here comes Spring Webflow.

The other senior programmer, Andrew used webflow before and he likes it. So we adopted it for its natural fit for wizard function. And in some sense, we just want to try a new tool. The webflow journey started.

It is fun experience with it. A little rough at the beginning, the first few weeks I always feel close, but something keeps popping up. Amit was kind of pissed off by my promising and not delivering. But finally, we delivered. And now it works fairly well.

A few things that I would like to say about spring webflow:

  • It can use the POJO as actions, which makes me feel better and excellent for testing. Webflow provides quite some convenience to facilitate this. I rely heavily on POJO and shift quite some logic into the webflow definition. This makes the definition a bit clumsy, but it is probably a good price to pay.
  • The history function does not work from suflow. See my post in their forum. Spring forum is kind of frustrating. No reply, even no view.
  • Some small things that are not well-documented. Amit upgraded it to 2.1.0Milestone and does not tell me. It switched to Spring EL where it uses #() instead of $(). I dig around quite a while to figure it out.


It also comes a Spring JS by default bundled with Dojo, which is part of the reason I started Dojo. I am going to look into it.

Programming

I did a lot of programing recently as RA for the NEMA project. And I like it. I don't know, but I can work on it all day without checking any fun sites, deal sites, news, or even emails. Maybe deep down I like programing more? I do not know. Maybe it is just something that I feel fairly confident with and comfortable to work with.

I like programming. A computer is faithful and responsive. You put something in and it spits out according to it with no surprise. Well, there are plenty of surprise, or not-saying from computer, but after some working, debugging, they becomes reasonable. That is one reason I like programming and the one I often tell people. I am not sure whether there are deeper one. There might. I cannot speak out it clearly. So here it is.

A Good Site for This Blog--Escaping HTML

It is quite irritating to find all the < ... > go away in this blog, it takes them as tag. I have to escape them. When you get to code, xml or java generics, where there are lots of them, it becomes pain in ***. A quick google gives me a useful page that does it for me. Thanks.

Saturday, June 26, 2010

Command Pattern and Generics in Java - JSON Story (3)

So what I want to do is to put a list of the same objects into List<Map<String,String>>. I created some converters for convert single object into <Map<String,String>>;. When I start to write the list converter, I realize that I only need a generic one and use the command pattern.
Here is the list converter:


public class ConverterToList<T> {



public List<Map<String,String>> convertToList(Collection<T> collection, ConverterToMap<T> converter){

List<Map<String,String>> list=new ArrayList<Map<String,String>>();
for (T t:collection){
list.add(converter.convertToMap(t));
}
return list;
}

}

And the interface for command


public interface ConverterToMap<T> {

public Map<String,String> convertToMap(T t);

}

A sample implementation

public class ConverterToMapJob implements ConverterToMap<Job> {

@Override
public Map<String, String> convertToMap(Job job) {
Map<String, String> map = new HashMap<String, String>();

JsonHelper helper = new JsonHelper(map);

helper.addCheckNull(job.getId(), "id");

helper.addCheckNull(job.getSubmitTimestamp(), "submitTimestamp");
helper.addCheckNull(job.getScheduleTimestamp(), "scheduleTimestamp");
helper.addCheckNull(job.getEndTimestamp(), "endTimestamp");
helper.addCheckNull(job.getUpdateTimestamp(), "updateTimestamp");
helper.addCheckNull(job.getHost(), "host");
helper.addCheckNull(job.getName(), "name");
helper.addCheckNull(job.getPort(), "port");
//now I am free to put any bean. Even not a bean.
helper.addCheckNull(job.getJobStatus(), "status");
return map;
}

}


I am fairly happy with this. This structure only handles a flat object or an array of it, and not general enough for more complex multi-level objects. But that fits my needs now and in the foreesable future of this project. I probably will hang with it and leave the json problem in my back. It is generally not a good idea to reinvent the wheel, but when the wheel is too complicated for your car, maybe it is better to roll your own.

XStream - Json story (2)

Now I understand xstream much better after study their codes for maybe 20-30 hours. It is nice, but probably overkill for my purpose. Their documentation is good enough to get you going. But there are lots of more intricate that might be good to write.

One problems with XStream is that it is really designed for xml. For xml, it is fine to have something like


<birthday type="java.sql.Timestamp">
12-20-2004
<birthday>

It would be whole lots more complicated for json. Their solution (xstream 1.3.1, both jettison and there own driver) is

birthday:{
@class:java.sql.Timestamp,
$:12-20-2004
}

For my purpose, I only need the value, not the class information. I understand that it is useful information when you need to deserialize the json back into a java object. I have a object with type java.util.Date, with a value as java.sql.Timestamp, which is a subclass of java.util.Date. And xstream keeps spit out things like the above, while I only need a simple things like birthday:12-20-2004. When the object is really just a java.util.Date, and xstream generates the simple thing. These inconsistency completely messed up my code. I need a fairly complicated javascript code to handle the situation like such, while things are much easier to handle if xstream always convert it into the simple case. I tried all kinds of way to get around it but no luck. I looked into their engine, but it is quite complicated and I did not fully get it. And furthermore, their code (at least json driver) is under heavy development. I wa confused at first by very different code for the same class and finally realized that the eclipse show the jave source from maven repository (official 1.3.1 release) and I got the 1.3.1 developing trunk. Man, they are very different and it seems they are trying something quite different. Back to the topic,
I tried to register the the Date converter for java.sql.Timestamp.class and it does not work. Finally I found the way to work:

xstream.addDefaultImplementation(Date.class,java.sql.Timestamp.class);

Frankly, I do not quite get why it works. And this add some discomforting.

Another not-so-nice thing about xstream is that it does the reflection on fields only, not the common bean convention. So some fake beans does not get converted. I wrote a couple of converter for complex classes I have. And that is much easier to write it than to looking through all the source and documentation.

But the final problem comes with the multiple models. XStream can only do one at a time. So I give up and goes to the original approach and as I explain in the last post, things work out pretty well.

A Quest for Better Json

We use json for the ajax call in NEMA DIY. And it is an interesting journey for us to find a way to implement it.

For my last project, I used a small xml java generating library for AJAX. This time, we need to generate more and we need json instead. (Amit likes Json more.) In the very beginning, we rolled our own and use some library (XStream I think) to write directly to HttpServletResponse. After a while, it becomes quite tedious, so I looked around and find Spring-Json. It is kind of nice to use with Spring MVC, I only need to register a special view for Json and push models in the same view as the normal jstl/jsp way. We are happy for a couple of months.

We upgraded to Spring 3 in May and Amit switch to the Jackson Json view that bundled with Spring 3. It works in the same way as Spring-Json because Spring-json is based on Spring 2.5. But we met some problems soon. Jackson does not handle the circular reference very well. Spring-json's default engine SOJO simply break the reference in some level and that serves us well enough. Jackson can works with proper annotation in this situation but we do not like to modify the code for this purpose and furthermore, we might need two different versions of Json serialization for the same class. So I have to reinstate spring-json and puts lot of exclusion in the maven dependency description.

We are not very satisfied with the situation, especially Amit. He does not like spring-json because its dependency on spring 2.5, which might break down with our majorly spring 3 stuff. But he has to live with it as I am the guy doing the front-end now. But deep down, I am a little uncomfortable with it either. All these tools are a little over-engineering for us because they are designed for bi-direction purpose that supports both serialization and de-serialization. But we only need simple serialization in Java and deserialization happens in Javascript. While we do have some very complicated classes that needs to be sent via json, we only need part of the information, which can be in a fairly simple form, not the complicated model that those framework engineered for.

I started to look into Dojo and would like to switch to its datagrid. It can hook with its data stores and their reference store is Json one. Their documents are limited and little weird. I made some experiments and it seems that the dadagrid only handles the array of flat json objects. So I am back to square one and has to work on the json presentation. I tried to work within spring-json to filter out some more complicated but not useful. Not working! So back to XStream. It is hard. I tried several things, even looked into its source. That is an interesting story by itself. Now I back to write to response directly. I ended up wrote some customized converter to plug-in for certain types.

And it breaks down when I tried to write two or more models once. I just cannot get it work. (From the hindsight, I might be able to do it by push it as Map<Spring,Object>. Well, I tested it and it does not worked out well. ) Finally, I have to try the jsnoview. Now I decide to do some really basic thing. I push the object in as a Map<String,String>, and array as the list of it. it works just fine with jsonview. Actually it works better because now I can push in a processed value instead of the raw value to be processed in javascript. It occurs to me that maybe the Jackson json works with it as well. I tested it and it works just fine. Now we are using Jackson view and home-made pre-processing. Evething seems to work. I am fairly happy.

I guess the moral of it is that sometimes it is better to just do it by myself and it might be the simplest approach. I spent maybe 50hrs on the source, document of xstream, spring-json and it is kind of wasted. They are good for what they are set to do, but their focus often do not align with mine well and therefore hard to use. Next time looks out!

What a journey!

Nightmare of CSS,mesh

CSS is evil!

It is powerful, but confusing as hell! I added Dojo datagrid to some pages, but often things screw up. There are so many similar different styles in css, and there are no error messages when you have a typo or wrong tag. And there are too many things to learn, too many tricks. Especially when I works with the established css setting for our nema.

Coupled with the sitemesh in our project, css can do even more damage. Sitemesh is nice and intercepts all pages and decorate them. However, it is very sensitive to mismatch tags. I had one <div> without close tag, and it mess all things up with the help from CSS. Furthermore, the eclipse and browser cannot validate it well, especially when I have javascript code mixed in. Maybe I should always keep the javascript in separate files.

A few small tricks.

Center block

.mycenter {
margin-left:auto;
margin-right:auto;
}

A absolute value box sit in the center

div#page {
width: 780px;


/* start to use the new absolute position system
*/
position: absolute;
left: 50%;
margin-left: -390px;
top: 5px;
}

Dojo, GWT

I have been looking into Dojo for a about 10 days. It is better than the plain javascript. And widgets are nice. Well, if I think about it again now, I might stick with GWT. Javascript is really not good for any developing.

Firebug/Chrome developing is kind of nice, but you run into lots more simple bugs that require lots of time. A simple thing such as typo in variable names and strings is usually silently ignored by browser and takes forever to debug. There I really miss java. I originally think to push more into the dojo side, but now I feel that I should keep as much in java side as possible. Much easier to handle and debug. Next time, if I need a really nice javascript site, I will definitely go to GWT. The java simulator at the beginning must be much easier.

Something nice for javascript. The pervasiveness of anonymous class, inner class, closure, are really nice features and open my eyes for new stuff. And Dojo DataGrid looks much nicer and supports much more features than the simple JSP Displaytag from Appfuse.

NEMADIY

I have been working on NEMADIY for more than half a year. This summer I work full-time for it. It is a fairly large project. There are 7-10 people working on it at one moment. I am mostly working on the front-end, the DIY module. Amit started it with Appfuse, which in turn uses whole bunch of tools: Spring MVC, struts-menu, sitemesh, prototype.... At the beginning, I am just helped Amit to modify things a bit. But over the time, I started to take the DIY as my responsibilty.

We added Spring webflow for a widzard-like process. Amit knows nothing about it. Another programmer Andrew, who has some experience with webflow, help me. Recently I start to look into Dojo, a javascript framework (prototype does not have widget system), to implement a datagrid for better looking.

In March, I spent a month to investigate GWT, we thought about to move to it completely. But I am completely new on it, and others are the same. We finally ditched the idea as we are not sure it is good for a big system as ours. We instead, used webflow to implement some parts, which also takes me about a month to learn and implement.


Well more stories about each later.

Switch the Gear

This blog was set up as my photo diary. But I neither write diary recently or shoot photos daily. And recently I do have something like to jog down, mostly about programing because that is something I did most recently. I thought to start a new blog, but why not just reuse this old blog. So here I am again, a different title and description and some new posts.