2009年4月28日火曜日

[rome-fetcher]how to get rss data from the basic authenticated web site

Steps to get rss data from the basic authenticated web site like the twitter.

  1. Create a new class implemented the CredentialSupplier interface. this class stores the username and password for the basic authentication and returns the Credentials object of the apache http client. following code is a sample class implemented the CredentialSupplier interface
    import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.CredentialSupplier;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.Credentials;
    public class AuthCredentialSupplier implements CredentialSupplier{
    private String username=null;
    public void setUsername(String username){
    this.username=username;
    }
    public String getUsername(){
    return this.username;
    }

    private String password=null;
    public void setPassword(String password){
    this.password=password;
    }
    public String getPassword(){
    return this.password;
    }

    public AuthCredentialSupplier(){
    }
    public AuthCredentialSupplier(String username,String password){
    setUsername(username);
    setPassword(password);
    }
    public Credentials getCredentials(String realm, String host){
    String username=getUsername();
    String password=getPassword();
    return new UsernamePasswordCredentials(username,password);
    }
    }
  2. Access to the rss url and get rss data from the basic authenticated web site. When accessing to the rss url, we need to use the HttpClientFeedFetcher instead of HttpURLFeedFetcher.
     //sample program
    //rss url
    String url="http://twitter.com/statuses/friends_timeline.atom";

    //username and password
    String username="username";
    String password="password";

    try{
    //create and initialize CredentialSupplier Object
    AuthCredentialSupplier authCredentials=new AuthCredentialSupplier(username,password);
    //create HttpClientFeedFetcher object
    //(we can not use HttpURLFeedFetcher with basic authentication)
    FeedFetcher feedFetcher=new HttpClientFeedFetcher(null,authCredentials);
    List result=(feedFetcher).retrieveFeed(new URL(url)).getEntries();
    //get response.
    if(result!=null){
    .....
    }
    else{
    System.out.println("ERROR")
    }
    }catch(FetcherException e){
    int responseCode=e.getResponseCode();
    System.out.println("ERROR, response code="+responseCode+", error="+e);
    }catch(Exception e){
    System.out.println("Unexpected Exception, e="+e)
    }

2009年4月24日金曜日

[Grails]Grails1.1 Setup fo the Apache Log4j Logging

Sample Setup fo the Apache Log4j Logging on Grails 1.1, A new Log4j DSL is available on Grails 1.1.
  • References
  • Sample Setup in grails-app/conf/Config.goovy file
    This sample setup is modified based on the document(http://d.hatena.ne.jp/nobeans/20090323/1237826907, this documentation is written in Japanese).
    // log4 setup
    log4j = {
    appenders {
    //override the setuf of the default console out
    console(name:'stdout', layout:pattern(conversionPattern: '%d{HH:mm:ss} [%p] (%c{2}) %m%n'))

    //override the setup of the default log
    rollingFile(name:'file', file:'logs/debug.log', maxFileSize:'10MB', maxBackupIndex:5, layout:pattern(conversionPattern: '%d{HH:mm:ss} [%p] (%c{2}) %m%n'))

    //override the setup of the default error stack
    rollingFile(name:'stacktrace', file:'logs/stacktrace.log', maxFileSize:'10MB', maxBackupIndex:5, layout:pattern(conversionPattern: '%d{yyyy-MM-dd HH:mm:ss} [%p] (%c{2}) %m%n'))

    //daily rolling log
    appender new org.apache.log4j.DailyRollingFileAppender(name:'dailyRollingFile', datePattern:"'.'yyyy-MM-dd",layout:pattern(conversionPattern: '%d{HH:mm:ss} [%p] (%c{2}) %m%n'), file:'logs/daily.log');
    }

    root {
    error 'stdout', 'file'
    additivity = false
    }

    //controller
    error 'org.codehaus.groovy.grails.web.servlet'
    //gsp
    error 'org.codehaus.groovy.grails.web.pages'
    //layouts
    error 'org.codehaus.groovy.grails.web.sitemesh'
    //url mapping filter
    error 'org.codehaus.groovy.grails."web.mapping.filter'
    //url mapping
    error 'org.codehaus.groovy.grails."web.mapping'
    //core, classloader
    error 'org.codehaus.groovy.grails.commons'
    //plugins
    error 'org.codehaus.groovy.grails.plugins'
    //hibernate integration
    error 'org.codehaus.groovy.grails.orm.hibernate'
    error 'org.springframework'
    //info 'org.springframework.security'
    //hibernate
    error 'org.hibernate'

    //jetty
    warn 'org.mortbay.log'

    //error stack
    error(
    additivity:false
    //,stdout:"StackTrace"
    ,stacktrace:"StackTrace"
    )

    //debug for my my app
    //info dailyRollingFile:"grails.app.controller.TestController"
    info(
    additivity:false
    //,stdout:"grails.app.controller"
    ,dailyRollingFile:"grails.app.controller"
    )
    info(
    additivity:false
    ,dailyRollingFile:"grails.app.service"
    )
    info(
    additivity:true
    ,dailyRollingFile:"grails.app.task"
    )
    }



2009年4月17日金曜日

[Grails]Grails Upgrade Steps from 1.0.x to 1.1

Grails Upgrade Steps from 1.0.x to 1.1
  • References
  • Upgrade Steps
    • Backup all application files
    • Add following statements into the grails-app/conf/Config.groovy file
         //upgrade
      grails.views.enable.jsessionid=false
      //grails.project.plugins.dir="./plugins"
    • Create a file grails-app/conf/BuildConfig.groovy file if this file doesn't exist and add following statements into this file.
       //upgrade
      //grails.views.enable.jsessionid=false
      grails.project.plugins.dir="./plugins"
    • if you use some plugins, you need to re-install plugins.
      Remove all plugins files and cleanup files stored under plugins directory
         mv plugins/* /tmp/
    • Run the upgrade command as follows
      (After running this commands, grails will create a new plugin named hibernate-1.1 under the plugins directory automatically. we don't need to re-install this hibernate-1.1 plugin.)
      grails upgrade
    • re-install plugins by using "grails install-plugins" command
      (for example)
      grails install-plugin /tmp/grails-quartz-0.3.1.zip
  • TroubleShooting
  • Plugins TroubleShooting
    • quartz-0.3.1 plugin
      • Error Description
        After starting the grails application, following error appears while starting up the quartz plugin
      • Error Stack
        2009-04-14 16:12:39,907 [main] ERROR context.ContextLoader  - Context initialization failed
        org.springframework.beans.factory.access.BootstrapException: Error executing bootstraps; nested exception is org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingPropertyException: No such property: startDelay for class: QuartzGrailsPlugin
        at org.codehaus.groovy.grails.web.context.GrailsContextLoader.createWebApplicationContext(GrailsContextLoader.java:74)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
      • Cause
        This error raises from following statements of the configureJobBeans closure in the QuartzGrailsPlugin.groovy file because of the "this" clause.
              // registering triggers
        jobClass.triggers.each {name, trigger ->
        "${name}Trigger"(trigger.clazz) {
        jobDetail = ref("${fullName}JobDetail")
        trigger.properties.findAll {it.key != 'clazz'}.each {
        this["${it.key}"] = it.value /* here */
        }
        }
        }
      • Resolution
        I changed this clause from "this" to "delegate", quartz plugin works fine.

2009年4月7日火曜日

[grails]upgraded the plugin for grails to execute native query

I created and upgraded the grails plugin named native-query(Version 0.2) to execute native query statement like ORACLE's query hint, mysql match-against statement on the grails framework.

Installation Steps
  1. download zip file named grails-native-query-0.2.zip via http://groups.google.com/group/taapps-sourcecode-libraries/web/grails-native-query-0.2.zip
  2. installing plugin into the grails application
    grails install-plugin grails-native-query-0.2.zip
How to use
  • use DomainObject.executeJdbcQuery method injected into the Domain Class
    • Description
      This executeJdbcQuery method is static method injected into the Domain Class object.
    • Return Value
      This method returns the List object and each objects stored into the List are Domain Objects of the fetched rows.
    • Arguments
      1. Required String type, where, order by, match against clauses(No need to enter the "select * from " statement)
      2. Optional String type, select option like the "SQL_CALC_FOUND_ROWS(mysql)" or hint text(ORACLE)
      3. List object or Map object stored the bind values
    • Example
      Following examples are using the Domain Class named "Test", this is a test domain class.
      • use the standard select statement
        def result=Test.executeJdbcQueryMap("where id=160")
      • use positonal parameter
        def result=Test.executeJdbcQueryMap("where id=?",[160])
      • use named map parameter
        def result=Test.executeJdbcQueryMap("where id=:id",[id:160])
      • use the positonal parameter wit
        def result=Test.executeJdbcQueryMap("where id=?","SQL_CALC_FOUND_ROWS",[160])
      • use the List object as the member of the positional parameters
        def result=Test.executeJdbcQueryMap("where id in (?)","SQL_CALC_FOUND_ROWS",[[160,161,162,163,164]])

  • use NativeQueryUtil.executeJdbcQueryMap method to execute free sql statement(not need to be related to the Domain Class)
    • Return Value
      This method returns the List object and each objects stored into the List are Map object stored the fetched column name and the value, map key is column name
    • Arguments
      1. String type, Select statement
      2. List object or Map object stored the bind values
    • Example
      • use the standard select statement
        def result=NativeQueryUtil.executeJdbcQueryMap("select * from test where id=103")
      • use positional map
        def result=NativeQueryUtil.executeJdbcQueryMap("select * from test where id=?",[103])
      • use namedmap parameter
        def result=NativeQueryUtil.executeJdbcQueryMap("select * from test where id=:id",[id:103])
      • use the List object as the member of the positional parameters
        def result=NativeQueryUtil.executeJdbcQueryMap("select * from test where id in (?)",[[103.104,105,106]])
  • use the NativeQueryUtil.executeNativeQuery method(execute sql statement via Hibernate's createQuery method)
    • Description
      This method executes the select statement via Hibernate's session.createQuery method.
    • Return Value
      Domain Object or List object stored the domain objects of the fetched rows.
    • Arguments
      1. String Type, select statement
      2. Map type, mapping the table name and domain class
      3. List object or Map object stored the bind values
    • Example
      • def result=NativeQueryUtil.executeNativeQuery("select /* query */ tbl.* from test tbl where id=?",[tbl:Test],[101])


2009年4月4日土曜日

[Grails]Upgraded Split Write Database Connection Plugin for master/slave configuration(Alpha version 0.3)

Upgraded the Grails Split Write Database Connection Plugin for master-slave configuration (Alpha version 0.3, like the magic multi connection for rails framework, multiple datasource)
I fixed some bugs and added new methods, changed the source code as follows.

Version 0.3
  • change the flush mode for the base session to avoid saving the changed domain class automatically into the base database. The flush mode for the base session is changed to the manual.
  • add new methods named discardXXX, attachXXX, refreshXXX for the save session.
  • added new CustomSavePersistentMethod and use this class when saving
  • added new method validateAndDiscard and discardAll which sync between the base session and save session.
Download
http://groups.google.com/group/taapps-sourcecode-libraries/web/grails-split-write-db-0.3.zip