Saturday, October 8, 2011

Building GWT with Ant

build.properties 

gwt.path=c:/gwt-windows-1.7.1
build.dir=c:/build


build.xml

<project name="projectname" default="compile-all" basedir=".">
    <property file="build.properties"/>

    <property name="war.name" value="projectname.war"/>
    <property name="gwt.module.name" value="com.projectname.Main"/>

    <property name="proj.web-inf.dir" value="${basedir}/WebContent/WEB-INF"/>
    <property name="proj.src.dir" value="${basedir}/src"/>
    <property name="proj.lib.dir" value="${proj.web-inf.dir}/lib"/>

    <property name="build.deploy.dir" value="${build.dir}"/>
    <property name="build.deploy.classes.dir" value="${build.deploy.dir}/${gwt.module.name}/WEB-INF/classes"/>

    <path id="classpath">
        <pathelement location="${gwt.path}/gwt-user.jar"/>
        <pathelement location="${gwt.path}/gwt-dev-windows.jar"/>
        <pathelement location="${proj.src.dir}"/>
        <fileset dir="${proj.lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <target name="init">
        <echo message="=========================================="/>
        <echo message="OS Name: ${os.name}                       "/>
        <echo message="Java Home: ${java.home}                   "/>
        <echo message="Ant java version: ${ant.java.version}     "/>
        <echo message="Java vendor: ${java.vendor}               "/>
        <echo message="Java Version: ${java.version}             "/>
        <echo message="=========================================="/>
    </target>

    <target name="clean-deploy">
        <delete dir="${build.deploy.dir}/${gwt.module.name}"/>
        <delete dir="${build.deploy.dir}/${gwt.module.name}-aux"/>
        <delete dir="${build.deploy.dir}/${war.name}"/>
    </target>

    <target name="compile-to-js" depends="init" description="Compiles client code and generates relevent JavaScripts">

        <java classname="com.google.gwt.dev.GWTCompiler" fork="true">

            <classpath refid="classpath"/>
            <jvmarg value="-Xmx1G" />
            <jvmarg value="-Xss1024k"/>
            <jvmarg value="-Xms64M"/>
            <jvmarg value="-Dgwt.nowarn.legacy.tools"/>
            <arg value="-out"/>
            <arg value="${build.deploy.dir}"/>
            <arg value="-localWorkers"/>
            <arg value="5"/>
            <arg value="${gwt.module.name}"/>

            <arg value="-style"/>
            <arg value="pretty"/>

        </java>

        <delete dir="${build.deploy.dir}/${gwt.module.name}-aux"/>

    </target>

    <target name="compile">

        <echo message="=========================================="/>
        <echo message="Compiling source files…                 "/>
        <echo message="=========================================="/>

        <mkdir dir="${build.deploy.classes.dir}"/>

        <javac destdir="${build.deploy.classes.dir}"
               excludes="**/client/*.java"
               classpathref="classpath"
               verbose="off"
               debug="on"
               deprecation="false">
            <src path="${proj.src.dir}"/>
        </javac>

        <echo message="=========================================="/>
        <echo message="Copying application files…                   "/>
        <echo message="=========================================="/>

        <copy todir="${build.deploy.classes.dir}">
            <fileset dir="${proj.src.dir}" includes="**/*.xml"/>
            <fileset dir="${proj.src.dir}" includes="**/*.properties"/>
         </copy>
    </target>

    <target name="copy-web-content" description="Copies WebContent contents to WAR">

        <echo message="=========================================="/>
        <echo message="Copying Web content files…                   "/>
        <echo message="=========================================="/>

        <copy todir="${build.deploy.dir}/${gwt.module.name}">
            <fileset dir="${basedir}/WebContent">
            </fileset>
        </copy>
    </target>




</project>



Customize message box creation -GWT

public class CustomizeMessageBox {

    private static final String MSG_TITLE = "Customized";

    /**
     * Displays a message
     *
     * @param message  Message text
     * @param title    Message title
     * @param icon     MessageBox.INFO, MessageBox.ERROR, MessageBox.QUESTION,  MessageBox.WARNING
     * @param callback task to perform after response
     */
    public static void alert(final String message, final String title, final String icon, final         MessageBox.PromptCallback callback) {
        MessageBox.show(
                new MessageBoxConfig() {
                    {
                        setMsg(message);
                        setTitle(title);
                        setIconCls(icon);
                        setButtons(MessageBox.OK);
                        setCallback(callback);
                    }
                }
        );

    }

    /**
     * Displays a message
     *
     * @param message Message text
     * @param title   Message title
     * @param icon    MessageBox.INFO, MessageBox.ERROR, MessageBox.QUESTION, MessageBox.WARNING
     */
    public static void alert(String message, String title, String icon) {
        alert(message, title, icon, null);
    }

    /**
     * Displays a message
     *
     * @param message Message text
     * @param icon    MessageBox.INFO, MessageBox.ERROR, MessageBox.QUESTION, MessageBox.WARNING
     */
    public static void alert(String message, String icon) {
        alert(message, MSG_TITLE, icon);
    }

    /**
     * Displays a message
     *
     * @param message Message text
     */
    public static void alert(String message) {
        alert(message, MessageBox.INFO);
    }

    /**
     * Displays a message
     *
     * @param message  Message text
     * @param callback task to perform after response
     */
    public static void alert(String message, MessageBox.PromptCallback callback) {
        alert(message, MSG_TITLE, MessageBox.INFO, callback);
    }

}

Attached native javascript to GWT

private native boolean submit(Document frameDocument)/*-{
            var isValidate = false ;
            if (frameDocument.frmValidate())
            {
             isValidate = true;
             frameDocument.document.forms[0].submit();

            }
             return isValidate;
        
    }-*/;

Friday, October 7, 2011

GWT Spring Hibernate

How to wiring GWT with Spring and Hibernate
 Web.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
        'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/controller-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>controller</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

      <welcome-file-list>
        <welcome-file>Main.html</welcome-file>
    </welcome-file-list>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
  
</web-app>


projectname .gwt.xml


<?xml version="1.0" encoding="UTF-8"?>
<module>

    <!-- Inherit the core Web Toolkit stuff.                  -->
    <inherits name="com.google.gwt.user.User"/>
    <inherits name="com.google.gwt.user.ClippedImage"/>
    <inherits name="com.gwtext.GwtExt"/>
    <inherits name="com.google.gwt.core.Core"/>
    <inherits name="com.google.gwt.user.History"/>
    <inherits name="com.google.gwt.i18n.I18N"/>
    <inherits name="com.gwtext.Pagebus"/>
    <inherits name="com.gwtext.SyntaxHighlighter"/>
    <inherits name="com.gwtextux.GwtExtUx"/>
    <inherits name="com.google.gwt.user.ClippedImage"/>
    <stylesheet src="themes/green/css/xtheme-green.css"/>
    <stylesheet src="themes/slate/css/xtheme-slate.css"/>
    <stylesheet src="themes/indigo/css/xtheme-indigo.css"/>
    <stylesheet src="themes/silverCherry/css/xtheme-silverCherry.css"/>

    <!-- Specify the app entry point class.   -->
    <entry-point class="com.gwtproject.client.Main"/>
    <!-- RPC service servlet declarations          -->
    <servlet class="com.gwtproject.server.service.TestServiceImpl"
             path="/testService.do"/>
   
    <!-- uncomment below "user.agent" and "locale" properties to speedup gwt compiler
    (do only in development environment)

    For Firefox: <set-property name="user.agent" value="gecko1_8" />
    For IE: <set-property name="user.agent" value="ie6" />
    For Both: <set-property name="user.agent" value="ie6,gecko1_8" />

    <extend-property name="locale" values="en_UK" />
    -->

    <!--<set-property name="user.agent" value="ie6"/>-->
    <extend-property name="locale" values="en_UK"/>


</module>



controller-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <!-- The application context definition for the DispatcherServlet -->

    <!-- Maps the request through to a concrete controller instance -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <map>
                <entry key="/testService.do" value-ref="testServiceController"/>
           </map>
        </property>
        <property name="interceptors">
            <list>
                <ref bean="openSessionInViewInterceptor"/>
            </list>
        </property>
    </bean>


    <!-- Function End -->

     <bean id="testServiceController"
          class="com.projectname.server.service.TestServiceImpl">
        <property name="testDao" ref="testDaoImpl"/>
    </bean>

    <bean id="testDaoImpl" class="com.projectname.server.dao.TestDaoImpl">
        <property name="sessionFactory" ref="factory"/>
    </bean>


    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>
    <!--  Multipart form data handler -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="10000000"/>
    </bean>


    <bean name="openSessionInViewInterceptor"
          class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="factory"/>
    </bean>


    <import resource="classpath:applicationContext.xml"/>

</beans>



applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


    <import resource="classpath:spring-dao-config.xml"/>

    <bean id="testDaoImpl" class="com.projectname.server.dao.impl.TestDaoImpl"
          scope="prototype">
        <property name="sessionFactory">
            <ref bean="factory"/>
        </property>
    </bean>

</beans>


spring-dao-config.xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>WEB-INF/db.properties</value>
        </property>
    </bean>


    <!--Data base  configured to c3po pool.in this case we are using postgres and mssql server databses-->
    <bean id="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass"><value>${db.driver_class}</value></property>
        <property name="jdbcUrl"><value>${db.url}</value></property>
        <property name="user"><value>${db.username}</value></property>
        <property name="password"><value>${db.password}</value></property>
    </bean>


    <!-- FactoryBean that creates a Hibernate SessionFactory.
         This is the usual way to set up a shared Hibernate SessionFactory in a Spring application context;
         the SessionFactory can then be passed to Hibernate-based DAOs via dependency injection.
     -->
    <bean id="factory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
             <!-- configure your hbm.xml file here-->
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.default_schema">${db.default_schema}</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
                <prop key="hibernate.c3p0.minPoolSize">20</prop>
                <prop key="hibernate.c3p0.maxPoolSize">100</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.max_statement">0</prop>
                <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
                <prop key="hibernate.c3p0.acquire_increment">5</prop>
                <prop key="hibernate.c3p0.maxIdleTime">5</prop>
                <!-- enable second level cash using EHcash bundle -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>

            </props>
        </property>


        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="factory"/>
        </property>

        <property name="nestedTransactionAllowed" value="true"/>
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <!--
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>
     -->
    <!--Enable annotation base transaction and
        explicitly directing  to exsisting TRN manager -->
    <tx:annotation-driven transaction-manager="txManager"/>

</beans>



do you have any issues about this please send email to my email address (saneera@gmail.com)