Posts

Showing posts from 2011

How to Ignore Files or Directories in Subversion

Image
We are facing a common problem when trying to commit some changes to the SVN. That is each time we have to ignore some JAR files, because those JAR files are user run time specific and if they are committed, it would not work on other local runtimes. Here is a pretty simple solution for this problem. We can tell subversion to ignore directories or specific files as follows. Ignore directories or specific files.    svn propedit svn:ignore ./some_path It will ignore all files in the specified directory. Here only wildcard indication is supported, and regular expressions are not supported. Example wildcard supprt:     svn propedit svn:ignore * Example ingnore class files with wildcard:   svn propedit svn:ignore *.class If you are using Eclipse SubEclispe plugin, Right Click file or folder --> Team --> Then click on svn:ignore

How to debug using Maven Jetty

Image
Debugging is a better approach to find out bugs in our programs. Here is a way to debug a maven project, and I am going to run my web application in the Jetty server. First you need to add the following environmental variables on your Windows/Linux environment. MAVEN_OPTS = -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y Now go to your maven web project location through command line and start the jetter server -> mvn jetty:run Now in eclipse debug configuration, add a remote Java application instance on port 4000. Then Add some debug points to your codes  and start the remote app instance on debug mode. You can see the steps from following screen shots. In this manner you can debug your java web apps and find out the bugs. With this way you can debug your java web app. if you are using maven latest version( Maven 2.0.8 or later) run the mvnDebug command instead of mvn and attach a debugger on port

Google Cloud SQL, Hibernate JPA Support

With my last post about Google Cloud SQL(Google App Engine) with Spring Hibernate, many people had encountered the same issue about HIbenrate JPA. If you are developing Hibernate JPA based applications,there is some issues with Hibernate JPA support which you need to be awared. When you are running the application you may get an error like this: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode; This is because Hibernate used JPA 2.0 specification. But Google App Engine only support JPA 1.0 specification. I also got the same issue, and therefore I used Spring Hibernate support (instead of Hibernate JPA). "The App Engine Java SDK includes an implementation of JPA 1.0 for the App Engine datastore" Google App Engine JPA : http://code.google.com/appengine/docs/java/datastore/jpa/ Love to hear from you! feedback(comments) are always welcome

Google Cloud SQL with Struts, Spring and Hibernate

With Google App engine SDK 1.5.5, Google have introduced Google Cloud SQL for GAE (Currently we can't connect any other external application with Google Cloud SQL. This can be only with Google App Engine Apps). It's faster than connecting with Amazon RDS and other Cloud based Data solutions. No need to worry about load balancing as well. Cloud SQL supports 5 QPS read/write rate, so as long as you don't go over that, you can use cloud SQL. If you are going over this read/write rate limit, datastore might become a much more effective approach to you.If you are storing large blobs data(images, attchments and sound files, it's better storing them in the datastore or blobstore. When Google App Engine became populer in late 2009 many people asked about Hibernate support. But with their datasore people were unable to develop Hibernate support applications with GAE. But with Google Cloud SQL developers can use Hibernate supported applications as well.

Sample Application with Google Cloud SQL

Three months ago I was able to participate in one of the Google App Engine based task management project.  However, in that project, I had to face some critical issues in searching as Google Datastore did not support many search options including LIKE search. It is known that searching is one of the essential parts of the enterprise applications. However, Google introduced Cloud SQL last week and they approved my "Google Cloud SQL" request today (they took 5 days to approve it.:D ). Once they approved, I decided to develop a sample application with Cloud SQL. Gogole Cloud SQL database engine is MySql Version 5.1.59 and database size must be no larger than 10GB. Here I have added option to Test LIKE search. If you need more clarification, feel free to contact me. Test Application http://securezilla.appspot. com/ PS: Added Source Code Sample Project : http://code.google.com/p/cloudsql/      Backup Link:  https://github.com/cnapagoda/cloudsql New Post : Google Cloud

Introduction to OSGi(Java Modular)

Image
OSGi Alliance is the governing body of this stranded and it was started at 1999. their initial goal was create open stranded for network devices. Based on this idea this specification introduced for Java also. Eclipse was first in Java. they introduced OSGi based Eclipse IDE at 2004 June. OSGi is way to define dynamic module in java. There are main three OSGi container implemented for Java,such as Apache Felix , Eclipse Equinox and Knopflefish . Why OSGi? Because OSGi provide ability to divided application in to multiple module and those module easy to manage with other dependencies. other than that is very easy to install, update,stop and delete module without stop engine(Ex: Tomcat web application container). We can have multiple version of implementation with effecting to other references. There are main 3 layers in web based Java framework(Presentation , Business layer and  DAO layer). There we can divide it into three OSGi based module. then we can very easily fix

Advanced Java questions -4

Image
A program which will accept three sentences (one sentence per line) and print the words having Initial Caps within the sentences. Below is an example. Here is an example. If the below three sentences are given to the program as input, This is a Program Coding test of Initial Caps the program Will Test You Then, the output would look like: This Program Coding Initial Caps Will Test You Solution: Pattern p = Pattern.compile("^[A-Z]");

Advanced Java questions -3

Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The program should display the day on which June 28, 1999 fell, and in this case the output will be MONDAY. The output should be displayed in uppercase letters. Suppose the following INPUT sequence is given to the program: 1999-5 1998-6 Then the output should be: MONDAY TUESDAY Solution: import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.Scanner; public class Solution { /** * @param args */ public static void main(String[] args) { boolean condition = false; do { Scanner scanner = new Scanner(System.in); String value = scanner.nextLine(); condition = value.equalsIgnoreCase("exit"); if(!condition && value.contains("-")){ calculate(value); } System.out.println("Count is: " + condition); } while (!condition); } private static void calculate(String value) { final String[] inpu

Advanced java questions -2

Write a program that prints the numbers between 258 and 393 (both inclusive) which do not end with 5. The program should print the output so as to have one value per line. The output would therefore follow the below format: value1 value2 value3 . .. . so on Solution: public class Solution2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int start = 258; int end = 393; for (int i = start; i < end; i++) { if(i%10 !=5){ System.out.println(i); } } } }

Advanced Java questions

A program which will accept a single pair of strings separated by a comma; the program should calculate the sum of ASCII values of the characters of each string. The program should then subtract the sum of the ASCII values of the second string from the sum of the ASCII values of the first string. Suppose the following input is given to the program: 123ABC,456DEF Then the sum of the ASCII values of the characters in '123ABC' is 348 and in '456DEF' it is 366. The Difference between these numbers is 348 – 366 = -18 The corresponding output to be printed by the program is: -18 Solution: import java.util.Scanner; public class Solution { /** * @param args */ public static void main(String[] args) { boolean condition = false; do { Scanner scanner = new Scanner(System.in); String value = scanner.nextLine(); condition = value.equalsIgnoreCase("exit"); if(!condition && value.contains(",")){ calculate(value);

Google App Engine DataNucleus “does not seem to have been enhanced” Issue

Image
I got DataNucleus exception while developing a Google App Engine based Web Application for one of my office clients. Here is the error message. javax.jdo.JDOUserException: Persistent class "Class com.xxxxx.xxxxx.domain.XXXXXUser does not seem to have been enhanced. You may want to rerun the enhancer and check for errors in the output." has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class. at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:375) at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:252) NestedThrowablesStackTrace: Persistent class "Class com.xxxxx.xxxxx.domain.XXXXXUser does not seem to have been enhanced. You may want to rerun the enhancer and check for errors in the output." has no table in the database, but the operation requires it.Please check the specification of the MetaData for this class. org.datanucleus.store.excep

How to parse toString date value to Date object

I have some issue with toString date value parse to Date object. here is my solution for that. try { Date cutterntTime = new Date(); String dateStringValue = cutterntTime.toString(); //String dateStringValue = Thu May 05 11:26:49 IST 2011; DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Date date = (Date)formatter.parse(dateStringValue); System.out.println("Date Value is : " + date); } catch (ParseException e){ System.out.println("Exception :"+e); }

How do I get dates between two dates?

In the following example code I am going to get  dates between two dates. First, I need to convert the date value into milliseconds and increase startup date value from one date. One date in milliseconds equlas to 24 * 60 * 60 * 1000L import java.util.*; public class DateUtil { static final long ONE_DAY = 24 * 60 * 60 * 1000L; public static void main(String[] args) { getDatesBetween("03/23/2011","03/28/2011"); } public static void getDatesBetween(String startDate,String endDate) { long from=Date.parse(startDate); // long value of from date long to=Date.parse(endDate); // long value of to date int x=0; while(from <= to) { x=x+1; System.out.println ("Dates : "+new Date(from)); from += ONE_DAY; } System.out.println ("No of Dates :"+ x); } }

Java Hex Color Code Generator

Last week I got an ideas on a random color generator in Java. One of my friends asked me to implement this logic. Here is my example hexadecimal color code generate logic. public Map hexCodeGenerator(int colorCount){ int maxColorValue = 16777215; // this is decimal value of the "FFFFFF" int devidedvalue = maxColorValue/colorCount; int countValue = 0; HashMap hexColorMap = new HashMap(); for(int a=0; a < colorCount && maxColorValue >= countValue ; a++){ if(a != 0){ countValue+=devidedvalue; hexColorMap.put(a,Integer.toHexString( 0x10000 | countValue).substring(1).toUpperCase()); }else{ hexColorMap.put(a,Integer.toHexString( 0x10000 | countValue).substring(1).toUpperCase()); } } return hexColorMap; } Or You can use Math.random also. Here is example using Math.random public static Map randomCodeGenerator(int colorCount){ HashMap hexColorMap = new HashMap(); for(int a=0;a < colorCount; a++){ String c

Java HTTPS Client

Here is example for a Java HTTPS client import java.net.URL; import java.io.*; import javax.net.ssl.HttpsURLConnection; public class HttpsClient{ public static void main(String[] args) throws Exception{ String httpsURL = "https://encrypted.google.com/"; URL myurl = new URL(httpsURL); HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); InputStream ins = con.getInputStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader in = new BufferedReader(isr); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } }