Friday, November 20, 2009
Terracotta quick tutorial
I think the quickest way to grasp the concept and get started using Terracotta. Find it here Terracotta quick start
Spring 3 file upload
I could not find a consolidated and everything works kind of Spring fileupload example using Spring 2.5.x or 3.x. I have written tested and uploaded it here. Framework Factory blog on Spring 2.5/ Spring 3 Fileupload
Monday, November 2, 2009
Tuesday, October 27, 2009
Friday, August 21, 2009
Openfire 3.6.4 on mac - admin console login issue SOLUTION
I installed Openfire 3.6.4 on my mac and ran into admin console login problem without any 'google' solution. Do not know whether this is a mac only problem. Simply I was not able to log in using the password I provided during set up. Re-setup as suggested by others did not help. The solution ultimately I traded is to switch embedded database to mysql (other non embedded will work too). In order to switch from embedded to mysql I reinstalled open fire but it can be done by re-setup too and is discussed later in this post. Uninstalling is tricky and can be done in this way.
==========EITHER=========
Uninstalling openfire on Mac.
Remove the files/folders listed below
/Library/LaunchDaemons/org.jivesoftware.openfire.plist
/usr/local/openfire
/Library/PreferencePanes/Openfire.prefpan
Your openfire is kicked (uninstall) out of your Mac.
==========OR=============
Resetup can be done in this way.
open /usr/local/openfire/conf/openfire.xml
Change the line (hopefully last but one line) true to false
==========END EITHER/OR=======
Then open the admin console at http://localhost:9090/index.jsp or you can do it in Mac way by opening openfire in System Preference and click on "Open Admin Console".
Follow the instructions in the admin console. Select mysql (or oracle or something else other than embedded one) as the database. Provide all the required info make sure you create the database that you specify and credentials are correct. Complete the setup. I was not able to log in using the password I have provided for admin during setup. No worry. Go to terminal and run
mysql -u -p and use the database for that used by openfire. Execute this sql command update ofuser set plainpassword = 'admin123' where username = 'admin'; use password of your choice. Done. Restart openfire from "Openfire" System Preference (you will find an entry 'Openfire' in System Preference) by clicking 'Stop Openfire' button and then 'Start Openfire'. Login to admin console using the changed (in database) password with user name 'admin. Fin.
Thursday, June 11, 2009
Handy command to see whether a class file exists in your lib/dir
$find ~chiradip/.m2/repository/ -name *.jar -exec jar tvf {} \; egrep 'Spring'
Another command that does the similar thing
$for line in `ls *.jar`; do echo $line; jar tvf $line egrep 'Spring'; done
Another command that does the similar thing
$for line in `ls *.jar`; do echo $line; jar tvf $line egrep 'Spring'; done
Running JPA test cases in Maven
First thing first. The directory structure should look like this.
+- pom.xml
+- src
| +- main
| | +- java
| | | +- mypackage
| | | | +- MyEntity.java
| | +- resources
| | | +- META-INF
| | | | +- persistence.xml
| +- test
| | +- java
| | | +- mypackage
| | | | +- MyPersistenceTest.java
+- src
| +- main
| | +- java
| | | +- mypackage
| | | | +- MyEntity.java
| | +- resources
| | | +- META-INF
| | | | +- persistence.xml
| +- test
| | +- java
| | | +- mypackage
| | | | +- MyPersistenceTest.java
second thing second ;)
package mypackage;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class MyPersistenceTest {
@Test
public void testBasicPersistence() throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myunitname");
EntityManager manager = factory.createEntityManager();
@Test
public void testBasicPersistence() throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myunitname");
EntityManager manager = factory.createEntityManager();
String attributeOne = "AttributeOne";
String attributeTwo = "AttributeTwo";
String attributeTwo = "AttributeTwo";
MyEntity entity = new MyEntity();
entity.setAttributeOne( attributeOne );
entity.setAttributeTwo( attributeTwo );
entity.setAttributeTwo( attributeTwo );
manager.getTransaction().begin();
manager.persist( entity );
manager.getTransaction().commit();
manager.persist( entity );
manager.getTransaction().commit();
MyEntity result = ( entity ) manager
.createQuery("SELECT e FROM MyEntity e WHERE e.attributeOne = :attributeOne")
.setParameter("attributeOne", attributeOne )
.getSingleResult();
.createQuery("SELECT e FROM MyEntity e WHERE e.attributeOne = :attributeOne")
.setParameter("attributeOne", attributeOne )
.getSingleResult();
Assert.assertEquals(firstName, result.getAttributeOne());
Assert.assertEquals(lastName, result.getAttributeTwo());
Assert.assertEquals(lastName, result.getAttributeTwo());
manager.close();
factory.close();
}
}
factory.close();
}
}
Third thing. Make sure all the dependencies are in the pom like JPA, hibernate, or db pool (c3p0 kind of).
Thats it.
Regards, Chiradip
Tuesday, May 12, 2009
Friday, May 8, 2009
Jersey on embedded grizzly (another tomcat like server) and then on tomcat 6
<< It is about JAX-RS - JSR 311. Simply getting up and running with restful webservices quickly> >
<<remember running it on tomcat 5.x is a bit different> >
First thing first. Lets write the application first. To show the dependencies the library versions that worked for me is listed below in the form of POM file.
======================================================================
For NON MAVEN USERS: Do not worry about the pom content just look at the dependencies to get the jar files with right versions and this also says that my dev environment is Java 6 but it should work well with Java 5. You may skip the pom if you wish.
======================================================================
============================
POM.XML
============================
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>paperless-client-rest-api</groupId>
<artifactId>simple-ws-client-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Simple Client REST Aplis for Java
</description>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/
</url>
<layout>default</layout>
</repository>
<repository>
<id>maven-repository.dev.java.net</id>
<name>Java.net Maven 1 Repository (legacy)</name>
<url>http://download.java.net/maven/1
</url>
<layout>legacy</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.1.0-ea</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.1.0-ea</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver
</artifactId>
<version>1.9.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<!-- <executable>${env.JAVA_HOME}/bin/javac</executable> -->
<compilerVersion>1.6</compilerVersion>
<fork>true</fork>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project><
We will have only 2 files to start with a basic and a good and a complete sample
======================
CODE: The resource class - this class will decide what you get once you access the specific URI. The uri here is /helloworld on the server root. e.g. if your server runs on localhost:9998 then the resource will be accessible at http://localhost:9998/helloworld. If you open http://localhost:9998/helloworld on your browser you will see the message helloworld - of course you have to implement the service - it is just one block away.
======================
====================================
CODE: The RESTful service goes below. It uses the resource (HelloWorldResource).
====================================
Notice Line number 22. initParams.put( "com.sun.jersey.config.property.packages", "org.paperless.client.rest.example" ); The second part of this line is crucial. This the name of the package where all the resource (classes) will be available, either directly or under subpackage of this package.
Use your favorite browser (I dont use my favorite browser for this as safari is bad in handling XML output ;) ). The output in firefox is as below.
<simpleDescrption>
<myDescOne>My Description One</myDescOne>
<myDescTwo>My Description Two</myDescTwo>
</simpleDescrption>
<<remember running it on tomcat 5.x is a bit different> >
First thing first. Lets write the application first. To show the dependencies the library versions that worked for me is listed below in the form of POM file.
======================================================================
For NON MAVEN USERS: Do not worry about the pom content just look at the dependencies to get the jar files with right versions and this also says that my dev environment is Java 6 but it should work well with Java 5. You may skip the pom if you wish.
======================================================================
============================
POM.XML
============================
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>paperless-client-rest-api</groupId>
<artifactId>simple-ws-client-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Simple Client REST Aplis for Java
</description>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/
</url>
<layout>default</layout>
</repository>
<repository>
<id>maven-repository.dev.java.net</id>
<name>Java.net Maven 1 Repository (legacy)</name>
<url>http://download.java.net/maven/1
</url>
<layout>legacy</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.1.0-ea</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.1.0-ea</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver
</artifactId>
<version>1.9.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<!-- <executable>${env.JAVA_HOME}/bin/javac</executable> -->
<compilerVersion>1.6</compilerVersion>
<fork>true</fork>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project><
We will have only 2 files to start with a basic and a good and a complete sample
======================
CODE: The resource class - this class will decide what you get once you access the specific URI. The uri here is /helloworld on the server root. e.g. if your server runs on localhost:9998 then the resource will be accessible at http://localhost:9998/helloworld. If you open http://localhost:9998/helloworld on your browser you will see the message helloworld - of course you have to implement the service - it is just one block away.
======================
- package org.paperless.client.rest.example;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.Produces;
- /**
- * @Author Chiradip Mandal
- * @Copyright © Chiradip Mandal 2009
- * @License Apache License v2.0 http://www.apache.org/licenses/LICENSE-2.0
- *
- */
- // The Java class will be hosted at the URI path "/helloworld"
- @Path( "/helloworld" )
- public class HelloWorldResource {
- // The Java method will process HTTP GET requests
- @GET
- // The Java method will produce content identified by the MIME Media
- // type "text/plain"
- @Produces( "text/plain" )
- public String getMessage() {
- return "Hello World";
- }
- }
CODE: The RESTful service goes below. It uses the resource (HelloWorldResource).
====================================
- package org.paperless.client.rest.example;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import com.sun.grizzly.http.SelectorThread;
- import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
- /**
- * @Author Chiradip Mandal
- * @Copyright © Chiradip Mandal 2009
- * @License Apache License v2.0 http://www.apache.org/licenses/LICENSE-2.0
- *
- */
- public class Main {
- public static void main( String[] args ) throws IOException {
- final String baseUri = "http://localhost:9998/";
- final Map<> initParams = new HashMap<>();
- initParams.put( "com.sun.jersey.config.property.packages", "org.paperless.client.rest.example" );
- System.out.println( "Starting grizzly..." );
- SelectorThread threadSelector = GrizzlyWebContainerFactory.create( baseUri, initParams );
- System.out.println( String.format( "Jersey app started with WADL available at %sapplication.wadl\n"
- + "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri ) );
- System.in.read();
- threadSelector.stopEndpoint();
- System.exit( 0 );
- }
- }
Notice Line number 22. initParams.put( "com.sun.jersey.config.property.packages", "org.paperless.client.rest.example" ); The second part of this line is crucial. This the name of the package where all the resource (classes) will be available, either directly or under subpackage of this package.
Guess what? Just run the the above java and point your browser at http://localhost:9998/helloworld and voila you see the message appears on you browser "Hello World".
=======================
ENHANCEMENT OF THE ABOVE CODE
=======================
This is pretty static code. Lets add some dynamism here. Lets say if we access http://localhost:9998/helloworld/print/{myname} - substitute {myname} by your actual name - and we get our name printed on the browser or the caller http client.
Lets add the following lines in HelloWorldResource.java
@GET
@Produces( "text/plain" )
@Path( "print/{myname}" )
public String getTestReply( @PathParam( "myname" ) String name ) {
return "Your name: " + name;
}
One more thing is done. Lets jump into the next thing - returning a java object. Trick is here - at the server side we can return an object but at the browser or httpclient side java object does not make sense but XML does. Lets see how we can return a java object at the server side to get an XML at the client.
@GET
@Produces( "application/xml" )
@Path( "desc" )
public SimpleDescrption getDescription() {
SimpleDescrption desc = new SimpleDescrption();
desc.setMyDescOne( "My Description One" );
desc.setMyDescTwo( "My Description Two" );
return desc;
}
You guessed it right. If you access browser http://localhost:9998/helloworld/desc and you will see an XML appearing on your browser or curl or any httpclient.
One thing is left the SimpleDescription.java - it is simple just follow me below. It's a simple bean except it is marked as @XMLRootElement and thats it. Remember this marking is must to get this working.
package org.paperless.client.rest.example;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @Author Chiradip Mandal
* @Copyright © Chiradip Mandal 2009
* @License Apache License v2.0 http://www.apache.org/licenses/LICENSE-2.0
*
*/
@XmlRootElement
public class SimpleDescrption {
private String myDescOne;
private String myDescTwo;
public void setMyDescOne( String myDescOne ) {
this.myDescOne = myDescOne;
}
public String getMyDescOne() {
return myDescOne;
}
public void setMyDescTwo( String myDescTwo ) {
this.myDescTwo = myDescTwo;
}
public String getMyDescTwo() {
return myDescTwo;
}
}
=======================
ENHANCEMENT OF THE ABOVE CODE
=======================
This is pretty static code. Lets add some dynamism here. Lets say if we access http://localhost:9998/helloworld/print/{myname} - substitute {myname} by your actual name - and we get our name printed on the browser or the caller http client.
Lets add the following lines in HelloWorldResource.java
@GET
@Produces( "text/plain" )
@Path( "print/{myname}" )
public String getTestReply( @PathParam( "myname" ) String name ) {
return "Your name: " + name;
}
One more thing is done. Lets jump into the next thing - returning a java object. Trick is here - at the server side we can return an object but at the browser or httpclient side java object does not make sense but XML does. Lets see how we can return a java object at the server side to get an XML at the client.
@GET
@Produces( "application/xml" )
@Path( "desc" )
public SimpleDescrption getDescription() {
SimpleDescrption desc = new SimpleDescrption();
desc.setMyDescOne( "My Description One" );
desc.setMyDescTwo( "My Description Two" );
return desc;
}
You guessed it right. If you access browser http://localhost:9998/helloworld/desc and you will see an XML appearing on your browser or curl or any httpclient.
One thing is left the SimpleDescription.java - it is simple just follow me below. It's a simple bean except it is marked as @XMLRootElement and thats it. Remember this marking is must to get this working.
package org.paperless.client.rest.example;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @Author Chiradip Mandal
* @Copyright © Chiradip Mandal 2009
* @License Apache License v2.0 http://www.apache.org/licenses/LICENSE-2.0
*
*/
@XmlRootElement
public class SimpleDescrption {
private String myDescOne;
private String myDescTwo;
public void setMyDescOne( String myDescOne ) {
this.myDescOne = myDescOne;
}
public String getMyDescOne() {
return myDescOne;
}
public void setMyDescTwo( String myDescTwo ) {
this.myDescTwo = myDescTwo;
}
public String getMyDescTwo() {
return myDescTwo;
}
}
Use your favorite browser (I dont use my favorite browser for this as safari is bad in handling XML output ;) ). The output in firefox is as below.
<simpleDescrption>
<myDescOne>My Description One</myDescOne>
<myDescTwo>My Description Two</myDescTwo>
</simpleDescrption>
Tuesday, May 5, 2009
Running 2 tomcats on a single machine
Very simple thing but very important and needed many a times. The simple to follow steps.
1) Download tomcat and unpack it in 2 different directories. Lets say /sfw/tomcat1 and /sfw/tomcat2 or if it is windows c:\sfw\tomcat1 and c:\sfw\tomcat2.
2) Edit the conf/server.xml file in both the tomcat installations and make sure the ports are different. For example if tomcat1 has the following config snippet -
<Server port="8005" shutdown="SHUTDOWN">
.....................
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
....................
<Connector port="8009" protocol="AJP/1.3" redirectport="8443">
Then tomcat2 should have something like
<Server port="8006" shutdown="SHUTDOWN">
....................
<Connector port="8081" protocol="HTTP/1.1" connectiontimeout="20000" redirectport="8444">
....................
<Connector port="8010" protocol="AJP/1.3" redirectport="8444">
Thats it for the basic config.
1) Download tomcat and unpack it in 2 different directories. Lets say /sfw/tomcat1 and /sfw/tomcat2 or if it is windows c:\sfw\tomcat1 and c:\sfw\tomcat2.
2) Edit the conf/server.xml file in both the tomcat installations and make sure the ports are different. For example if tomcat1 has the following config snippet -
<Server port="8005" shutdown="SHUTDOWN">
.....................
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
....................
<Connector port="8009" protocol="AJP/1.3" redirectport="8443">
Then tomcat2 should have something like
<Server port="8006" shutdown="SHUTDOWN">
....................
<Connector port="8081" protocol="HTTP/1.1" connectiontimeout="20000" redirectport="8444">
....................
<Connector port="8010" protocol="AJP/1.3" redirectport="8444">
Thats it for the basic config.
Thursday, March 19, 2009
SUN - up for sale
This is inevitable that SUN is looking for selling itself as consecutively 4 quarters they reported loss. I wish I could buy SUN ;) It has huge assets used and unused and lots of stuffs that another company may take decades and billions to recreate. IBM is preying on SUN. I am calling it preying as IBM has that bad boy attitude to acquire and kill the way they did with Rational likes. As a SUN admirer I do not wish to see IBM buying SUN. My solely personal opinion is - CISCO should buy it. I am a CISCO employee incidentally but this is my solely personal opinion. What benefits CISCO may get out this buy out and what we SUN admirers may get.
1) CISCO is foraying into server market - if CISCO buys SUN, CISCO can use SUN's mature server ranges along with legendary Solaris. Data center will be sexier than ever with all CISCO+SUN products.
2) Virtualization - CISCO needs it and working on it but in my opinion CISCO will take decade to reach what SUN has already reached if at all it reaches there.
3) Software development legacy - SUN has sound software development legacy and CISCO can not afford to sale only hardware so this can be inherited from SUN.
4) Intellectual Properties - do I need to say anything here!!
5) Love - through Solaris and then mighty Java, SUN acquired huge number of lovers worldwide. CISCO may get that love by buying SUN.
6) Benefit we (SUN admirers) get - IBM would kill SUN's philosophy if it buys it. CISCO I believe will not, rather will allow SUN to grow on its own and leverage the SUN's deeds and we will still get the SUN flavors undistorted.
7) CISCO has strong sales force with very good connections to governments and big industries worldwide. This would play a pivotal role to lift SUN sales up fueling its growth and continuing the R&D thats at the heart of SUN philosophy.
If it has to be sold out SUN should consider selling it to CISCO. If at all they can not afford selling it to Chiradip ;)
1) CISCO is foraying into server market - if CISCO buys SUN, CISCO can use SUN's mature server ranges along with legendary Solaris. Data center will be sexier than ever with all CISCO+SUN products.
2) Virtualization - CISCO needs it and working on it but in my opinion CISCO will take decade to reach what SUN has already reached if at all it reaches there.
3) Software development legacy - SUN has sound software development legacy and CISCO can not afford to sale only hardware so this can be inherited from SUN.
4) Intellectual Properties - do I need to say anything here!!
5) Love - through Solaris and then mighty Java, SUN acquired huge number of lovers worldwide. CISCO may get that love by buying SUN.
6) Benefit we (SUN admirers) get - IBM would kill SUN's philosophy if it buys it. CISCO I believe will not, rather will allow SUN to grow on its own and leverage the SUN's deeds and we will still get the SUN flavors undistorted.
7) CISCO has strong sales force with very good connections to governments and big industries worldwide. This would play a pivotal role to lift SUN sales up fueling its growth and continuing the R&D thats at the heart of SUN philosophy.
If it has to be sold out SUN should consider selling it to CISCO. If at all they can not afford selling it to Chiradip ;)
Tuesday, March 17, 2009
Soft hardware ;)
Software products are getting complex everyday in terms of setting up, configuration, operation and maintenance. What if working on software gives us the feel of working like a well packaged hardware like iPod?
What is my definition of soft hardware?
1) Of the shelf usable.
2) Works with my laptop or desktop or server just upon connecting them.
3) No installation of software and no configuration for general use.
Examples
1) An integrated development environment with 1 or 2 test servers integrated with it (think of websphere integration developer - if you are aware). It is pain to install manage and work daily mainly due to the configuration. Now if that can be obtained in a USB stick and you just have to connect it with your computer and you are all set to go, you can do whatever you want. Not working! just remove the media and start afresh.
2) Pre configured webserver on a card
What is my definition of soft hardware?
1) Of the shelf usable.
2) Works with my laptop or desktop or server just upon connecting them.
3) No installation of software and no configuration for general use.
Examples
1) An integrated development environment with 1 or 2 test servers integrated with it (think of websphere integration developer - if you are aware). It is pain to install manage and work daily mainly due to the configuration. Now if that can be obtained in a USB stick and you just have to connect it with your computer and you are all set to go, you can do whatever you want. Not working! just remove the media and start afresh.
2) Pre configured webserver on a card
Subscribe to:
Posts (Atom)