Sunday, 27 October 2013

ExtJS-JSplumb Integration - Flowchart Demo

ExtJS 4 is a very popular javascript framework to design Rich Internet Applications. It has its own drawing mechanism to draw charts but it doesn't support flowchart mechanism out-of the box. There are some plugins available to draw flowcharts to some extent.
JSPlumb is one of the famous drawing framework to draw shapes/components and connect them. It is build on top of jquery, so we can make use of all the advantages of jquery as well.

While I was trying to draw flowcharts in ExtJS, I found JSplumb is one of the suitable frameworks for my requirements, but I couldn't get a complete post/material to integrate these two. Finally I had to buy some time to integrate them and here comes the post.

There are wide-variety of javascript libraries available on net(Raphael, Processing.js...) but none of them support diamond(Decision) box as as component. Since JSplumb is built on jquery, we can play with html+CSS and get components as per our wish. 

In the following fiddle, go to the Result tab, right click on the pane and play-around with it. I have added some jquery events to prove some of the jquery actions.



Here is the sample fiddle  and following is the sample snap-shot of it.

Here is the source code : Extjs-Jsplumb-html

Saturday, 30 March 2013

Configure Mail Server In Liferay : Simplest Way

Intended users for this post are developers. As we have to work on restricted environment, where we cannot configure complete mail server and test mails,we need a simplest way to test mailing applications.

Recently I need to test forgot functionality in liferay 6.1.1 for which we need a mail server. Google's smtp free server (smtp.gmail.com: 465) is not allowed in our environment. So I have configured my own smtp server on local machine and tested the functionality. It is really simplest way to test your application.

Step 1: First configure your own mail server on your machine.
Step 2: Login to liferay as admin user, go to control panel\Server Administration\Mail and enter following details(There are default details, any way).
  • Incoming POP Server: localhost
  • Incoming Port: 110
  • User Name: root
  • Password: root
  • Outgoing SMTP Server: localhost
  • Outgoing Port: 25
  • User Name: root
  • Password: root 
and save them. 
Step 3: Click on forgot password, enter captcha, password hint and send-email-link. 
Step 4: Verify your mail here where you can see password update link.
JAMES_HOME\apps\james\var\mail\outgoing\XXXXXXXXXXXXXXXXXX.Repository.FileStreamStore 


Note: If you are using liferay 6.1.1 GA2 and default configuration of apache-james-2-3-2 then link pattern will be some thing like this,
http://localhost:8080/c/portal/update_password?p_l_id=10591&ticketKey=b594ea37-718a-44d0-b46d-f6f9cefe3c09
Remove amp;from the above link, other wise forgot password link will redirect user to home page. It will not ask user for new password.

How To Setup Own Mail Server For Your Application

There are plenty of mail servers available in the market for free, hmailserver, apache-james.. are some of them. In the following post, I am going to write about configuring mail server(apache-james 2.3.2) on your own machine. This might be useful for developers to test their application in the restricted environment.
(For simplicity, I am ignoring configuration settings, and going to write about main steps, for more info you can read in apache-james site).
  1. Download apache-james 2.3.2 and unpack on your machine(Here after james-2.3.2 folder will be referred as JAMES_HOME.
  2. Go to JAMES_HOME\bin and click on run.bat, if the mail server runs successfully, following screen can be seen
    .
  3.  You are almost there. To test whether you have configured your mail server properly, open command prompt and try this command. telnet localhost 25, then you can see a scree like this.
  4. Now use out going smtp server as localhost or 127.0.0.1 and port as 25. (You can modify these details in conf.xml and host file). 
  5. Send a mail from your application and check your out box/inbox here.
    JAMES_HOME\apps\james\var\mail\inboxes
    JAMES_HOME\apps\james\var\mail\outgoing
Done. 
          

Wednesday, 13 February 2013

Compile and Run Multiple Java Classes From Command Prompt.

It sounds silly to write a post for java file compilation and execution. But I took one hour to find the problem.

If you have multiple java files and all of them are in different packages. Then how can you compile them and run main class among them. Let me take an example
Consider two classes in a package com.test.
com.test.MainClass
com.test.UtilityClass


Compilation: If you go inside and compile using javac mainclass, you will be thrown an error
MainClass : Cannot find location

Similarly if you have class files in a package and if you try to execute,using java mainclass command, then system will throw following error.

Exception in thread "main" java.lang.NoClassDefFoundError: <ClassName> (wrong nam
e: com/test/<ClassName>)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Problem here is we go inside the folder com/test and try to compile or run files. But
Never go inside packaged folder(com or test) and try to run the files. 
Come out of the package folder and run the commands. 
Compilation: 
javac ./com/test/MainClass.java
javac ./com/test/Utilities.java

Execution:
java com.test.MainClass.java
 

Friday, 8 February 2013

Ant For Beginners


  • Download Apache Ant Binary Distributions from here
  • Set path to %ANT_HOME%/bin (Type ant -version to test whether path is set or not).
  • Now we need a build.xml to create folder hierarchy.
  • Here is a basic build.xml which will create src, resources, build folders. These are not standard folders. One can create their own folders and define the folder hierarchy by modifying build.xml. In the following build.xml, I have created src folder to put source code(Some times properties files also), resources folder to keep all the resources(images, data files etc) and build folder to keep class files and JAR file.
  • Just copy following code in a file and save it as build.xml. Go to the path and type
      ant run
  • If you have a source code already then copy your source code in src folder, resources in resources folder and re- run ant.


<?xml version="1.0" encoding="UTF-8"?>
<project name="InfraTransform" default="build-jar" basedir=".">
    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="resources.dir" value="resources" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="com.test.MainClass" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="*.jar" />
    </path>

    <pathconvert property="manifest.classpath" pathsep=" ">
        <path refid="classpath" />
        <mapper>
            <chainedmapper>
                <flattenmapper />
                <globmapper from="*.jar" to="${lib.dir}/*.jar" />
            </chainedmapper>
        </mapper>
    </pathconvert>

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <target name="compile" depends="clean">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
    </target>

    <target name="build-jar" depends="compile">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <fileset dir="${resources.dir}" includes="**/*.*" />
            <fileset dir="${src.dir}" excludes="**/*.java" />
            <manifest>
                <attribute name="Built-By" value="${user.name}" />
                <attribute name="Main-Class" value="${main-class}" />
                <attribute name="Class-Path" value="${manifest.classpath}" />
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath" />
                <path location="${jar.dir}/${ant.project.name}.jar" />
            </classpath>
        </java>
    </target>


</project>

Let's try to understand the build.xml a bit.
  • In general build.xml starts with project tag, and property tags are used to define some constants, so that we can refer those names in the rest of the code.( In our example, we used properties to define folder paths).
  • If you have more properties, then keep all the properties in a file and name the files as build.properties. Refer this file as property in build.xml.
          <property file="build.properties"/>
  • Let's see a sample build.properties
         src.dir = src
         build.dir = build
        resources.dir = resources
        classes.dir = ${build.dir}/classes
        jar.dir = ${build.dir}/jar
       lib.jar = lib
       main-class = com.test.MainClass


  • target in build.xml is like function in traditional programming. Write your logic(Creating directories/files, deleting them, running instructions etc) in a block and name that block as target name = "somename". You can call function from command prompt.
      ant <targetname>
  • depends name it self tells us that before this function runs, there is a dependent function which needs to be run.
I think, this is enough to start ant, if you understand a bit, then go through official material and other sources.

Further Reading:
Tutorial: Hello World with Apache Ant
Apache Ant - Tutorial

Saturday, 2 February 2013

Ant Script: How To Pack Multiple Jars In A Single Script

As we know that, pack200 is the best compression tool to compress java classes(In my experience, it compresses one third to one ninth of its original size). Sometimes we need to pack a bundle of jar files(Especially library files), but it is hard to use command line syntax to bundle them all in a single shot. But ant will help us to automate this, even if you don't know ant , don't worry. Just follow these steps.

Steps to pack multiple jars
  • Download ant from here
  • Set %ANT_HOME% as path variable and add ant/bin to path.
  • Create a file and name it build.xml. (If you don't know any thing about build.xml, just copy following code and save it as build.xml).
<project>
<property name="dir.jar" value="
Source_Folder"/>
<apply executable="pack200" parallel="false" dest="
Destination_Folder">
        <arg value="--modification-time=latest"/>
        <arg value="--deflate-hint=true"/>
        <arg value="--segment-limit=-1"/>
        <targetfile/>
        <srcfile/>
        <fileset dir="${dir.jar}/" includes="**/*.jar" />
    <mapper type="glob" from="*" to="*.pack.gz" />
 </apply>
</project>


In the preceding code snippet, Source_Folder and Destination_Folder are user defined folders. Make sure that your jar files are in source_folder and these two folders should be in the same folder as build.xml .
  • Final step is, type ant. 
Done... Your packed jars with extenstion pack.gz will be created in Destination_Folder. 






    

Saturday, 26 January 2013

Basic introduction and usage of log4j

Log4j is used to log the data in a format that will be understandable and useful. Here is the documentation. 

I am going to explain how to start a simple logger using log4j for you project with a basic example. 


Download the file from Apache log4j.
  1. Create a project. (I have created a java project).
  2. Create a file with name log4j.properties(Now it can be log4j.xml also). 
  3. If it is a java project, copy properties file into src folder.
  4. If it is a web project then copy this project under WEB-INF/classes
  5. Add following entries to the file. (You can add more entries, see. Documentation)
# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout(console)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


Run your project now and check your log file/ console.....
 

Saturday, 19 January 2013

How To Disable Automatic Updates By Service.xml with Database in Liferay

Even though liferay service layer is an excellent and efficient tool to work with database and it generates automatic hibernate spring framework, it has some drawback in terms of customization. model-hints.

portlet-model-hints.xml and portlet-model-hints-ext.xml entries are work around for customizing database schema. Still if you face any problem and want to continue without liferay service layer database scheme, you can just disable this update with database.

  1. Add service-ext.properties in resource folder(If you are not using maven, then locate the path of service.properties and keep extension file in the same path.)
  2. Enter following entry and restart the server. 
               build.auto.upgrade=false

How To Refresh Page From Vaadin Application

We can refresh webpage from vaadin using javascript. Try following code snippet to reload webpage.

getMainWindow().executeJavaScript("window.location.reload();");

Liferay-Maven: Installation For Beginners

1. Install maven(I have used 3.0 ) and set path for M2_HOME, if you don't know how to install maven, check my previous post here)

2. Take a backup of .m2 and copy whole folder in <documents and settings>/<User>/.m2 folder

3.  Open command prompt( Preferably open liferay SDK folder in command prompt) and type the following instructions

    -> mvn archetype:generate
    -> Select appropriate number for liferay-portlet-archetype(in 3.0 version it is 45)
    -> Enter groupID : com.tcs.srl.etransform
    -> artifactId     : etransform-portlet
   
4. It will create a folder structure with artificatID consisting of src folder and pom.xml. (Replace them with your src folder and pom.xml, if  you have already src file and your customized pom.xml).

5. goto artifact(Here etransform-portlet) and run following instruction
    -> mvn -o package
    It creates target folder and packaged structure

6. mvn clean liferay:deploy to deploy liferay libraries in to portlet.

7. Other liferay maven commands
    mvn liferay:deploy
    mvn liferay:build-lang
    mvn liferay:build-wsdd
    mvn liferay:build-thumbnail   
    mvn liferay:theme-merge
    mvn liferay:build-css
    mvn liferay:build-db
    mvn liferay:build-service
    mvn liferay:direct-deploy   
    mvn liferay:build-ext


8. Other maven commands
    mvn clean    - will delete target directory
    mvn validate    - validate whether project is correct and necessary information is available
    mvn compile    - compile source code and place classes in target/classes folder
    mvn test    - run tests using a suitable unit testing framework
    mvn package    - take the compile code and package it in distributabel format(WAR/JAR as metioned in pom.xml)
    mvn verify    - run any checks to verify the package is valid and meets quality criteria
    mvn install    - install the package into the local repository, for use as a dependency in other projects locally
    mvn deploy    - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects
   
9. maven eclipse commands
    mvn eclipse:eclipse           
        - create metainformation: meta files for eclipse are created, can be used for project import
    mvn -Dwtpversion=2.0 eclipse:eclipse   
        - create metainformation with wtp: same like create metainformation + WTP plugin infos(To convert into eclipse web project)
    mvn -Declipse.workspace=/path/to/workspace eclipse:add-maven-repo
        - tell eclipse where local repository is located
10. maven release project
    mvn release:prepare
        - prepare release: informations about versions number are collected
    mvn release:clean
        - clean release: rollback to snapshot versions
    mvn release:perform
        - perform release: deploy project to remote repository and make tag in version control system.
            username and password for version control system are taken from server informations in
            ~/.m2/settings.xml. serverID is same like defined in deploymentServer ... this behaviour is not whished
    mvn release:perform -Dusername=foo -Dpassword=bar
        - perform release with username and password for authentication on version control system

   

    mvn deploy:deploy-file -Dfile=/path/to/jar/file -DrepositoryId=repos-server -Durl=http://repos.company.org/test -DgroupId=javax -DartifactId=mail -Dpackaging=jar-Dversion=1.0.1


11. maven tomcat integration(You need to enter tomcat manger ID in pom.xml).
    mvn tomcat:deploy
    mvn tomcat:redeploy
    mvn tomcat:undeploy
    mvn tomcat:stop
    mvn tomcat:start



Friday, 18 January 2013

Maven: Installation For Beginners

 Follow steps 1 to 4 to create a sample web project using maven.
  Download apache maven here.
  1.  Set path for maven till /bin and JAVA_HOME/bin.
  2.  To test maven is properly configured or not, open command prompt, type mvn --version it will show the path variable.
  3. Type following instruction at the command prompt (Replace groupID with your package structure and artificatID with your projectname).
mvn archetype:generate -DgroupId=com.test -DartifactId=sampleWebApp -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Tuesday, 15 January 2013

Creating WebService Using Liferay Service.xml


Steps to create webservice using liferay service.xml

1. Add remote-service="true" in service.xml for the perticular entity.
    eg: 
<entity name="Customer" local-service="true" remote-service="true" cache-enabled="false">

2. Build service. It will create <entityName>ServiceImpl.java file in XXX.portal.service.impl
    eg:
CustomerServiceImpl.java in com.test.portal.service.impl.
3. Create a method in <entityName>ServiceImpl.java and return some value.
    eg:
        CustomerServiceImpl.java extends CustomerServiceBaseImpl{
               public String getCustomerName(String customerName){
           
                //TO DO, Write your logic here and based on logic return your details   
                return customerName;
               }
       
        }


4. Build service and web-service. It will create required classes. You can check your project entry in WEB-INF/server-config.wsdd.
    eg:
        <parameter name="allowedMethods" value="getSquareOf"/>
       
       
5. Check whether your web service is running fine or not by checking wsdl file.
    eg:
        http://localhost:8080/Customer-portlet/api/axis/Plugin_Customer_CustomerService?wsdl

Steps to validate webservice

6. Use service definition given in step 5 and create a webservice client.
                or
7. Use any webservice testing tool to validate webservice.
    eg:
    http://storm.codeplex.com/

Saturday, 12 January 2013

Jboss: Connect To Server From Remote Client

Use following command to allow all the clients(Any IP) to access your Jboss server.

Open %JBOSS_HOME% /bin in command prompt and type

run --host -b 0.0.0.0

Another simple technique can be to create a batch file for this.

Open a text file, type following entry

@Echo off
call %JBOSS_HOME%/bin/run.bat --host 0.0.0.0

Save it as sameText.bat(Where ever you want) and click on it. 


Liferay: Unable To Communicate With Repository

Generally, we(Software Developers) ignore exceptions, but when it comes to production, people are particular about each and every exception, even though it is ignorable, people need explanation to ignore it.

Recently one thing happened to me while using liferay. They use proxy servers in their production servers, hence people are getting following exception.  

Unable to communicate with repository http://plugins.liferay.com/enterprise

Even though it is ignorable, they don't want this exception in their log files. 

Following entry in portlet-ext.properties does the trick. 
(Keep the values black) . 
plugin.repositories.trusted=
plugin.repositories.untrusted=


And restart the server, then this exception will never appear in log files. 

Eclipse: Cannot Find The Class Problem

Recently I have faced Cannot find the class problem in eclipse. I remember my project worked fine last month. Suddenly it is crying to locate some java classes when I compile this project. Still interesting this is that, those classes are there in the mentioned package. Even googling didn't help me.

Finally I found a work around.

Simple, I copied whole class file into new java class file, deleted the original java class file and renamed the new class name to older java class name.

It worked well for me, if you face similar problem try this small tip, it may works.

Weird things happen in eclipse some times....

Allow All WebSerice Clients To Use Liferay Webservice

As we know, there is an entry that we need to enter into portlet-ext.properties to access webservice from the client.

axis.servlet.hosts.allowed=192.168.100.100,....

But if we want to allow every client to use these webservices created by liferay i.e. if we don't want any restriction to access liferay webservice then keep the entry blank in portlet-ext.properties.

axis.servlet.hosts.allowed=