Thursday, July 26, 2007

SVN/ Subversion Tips and traps

SVN could be tricky and waste a hell lot of time.

Case Issue :
Normally we run SVN server in linux. Our dev env will be in windows NT. Windows doest differentiate lower and upper case file names, while linux does. So when we try to do a SVN update, it gives wierd errors like Lock not released, while the problem is with Case.
This takes hours to sort out.


Merge branch to trunk
Often in a multi developer environment, it is required to maintain seperate branches so that the development is
not dependent on another developer. The developer takes a branch , works on his module and once done and tested,
merges it back to trunk.

Using tortoise svn, this can be achieved as follows:
1. right click on the root folder of your svn dump. Click merge .
2. Choose the revision from to for the branch (use show log to aid in choosing revision).
3. Make a diff. Do a dry run. Finally hit merge when satisfied.
4. Now the branch on ur local disk has the merged content. This can be commited to the trunk normally using commit.



More to come here.

Thursday, July 19, 2007

apache commons configurator usage

commons configurator usage:
Download the below jars from apache site:
commons-collections-3.2.jar  application/octet-stream
commons-configuration-1.4.jar  application/octet-stream
commons-lang-2.3.jar  application/octet-stream

Usage of org.apache.commons.configuration class for dynamic property file reading is below:


 static PropertiesConfiguration config = null;
public void init(FilterConfig filterConfig) throws ServletException
{

try
{
 config = new PropertiesConfiguration(filterConfig.getInitParameter("ROBOT_FILE"));
 FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();
 reloadingStrategy.setRefreshDelay(1000 * 60 * 5);// 5 mins

 config.setReloadingStrategy(reloadingStrategy);
List patternsList = (List) config.getProperty("patterns");

} catch (ConfigurationException e)
{
 log.error(e);
}

}

By default the char seperator is "," and the list is automatically created.
Cool isnt it.


commons digester usage:

QuizResultsTO result = null; try { digester.addObjectCreate("quiz", "com.biomedcentral.business.imagelibrary.model.quiz.QuizResultsTO"); digester.addCallMethod("quiz/title", "setQuizTitle", 0); result = (QuizResultsTO) digester.parse(file); } catch (Exception e) { logger.error("Quiz configuration file not parseable", e); throw new TechnicalException(e); } return result.getQuizTitle();

setting datasource lookup from tomcat JNDI using spring

1. -JNDI configuration is as below: 1. Shutdown liferay/tomcat 2. Modify applicationContext-hibernate.xml as below : Replace - <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/dmpPubPool" /> <property name="resourceRef" value="true" /> </bean> Instead of - <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url"><value>${hibernate.connection.url}</value></property> <property name="driverClassName"><value>${hibernate.connection.driver_class}</value></property> <property name="username"><value>${hibernate.connection.username}</value></property> <property name="password"><value>${hibernate.connection.password}</value></property> </bean> Modify the JNDI path jdbc/dmpPubPool suitable for CMS. 3. Add the context.xml(attached) into /META-INF/ folder of CMS webapp . Modify the JNDI path jdbc/dmpPubPool suitable for CMS. This file has <Context reloadable="true"> <ResourceLink name="jdbc/dmpPubPool" type="javax.sql.DataSource" global="jdbc/dmpPubPool"/> <ResourceLink name="jdbc/adServerPool" type="javax.sql.DataSource" global="jdbc/adServerPool"/> </Context> 4. Update the server.xml of your liferay available under <liferay>/conf as below <GlobalNamingResources> <Resource name="jdbc/dmpPubPool" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@172.20.41.40:1521:xyz" removeAbandoned="true" maxActive="75" maxIdle="30" maxWait="-1" username="****" password="***" validationQuery="select 1 from dual" /> …..> Copy ojdbc.jar available under cms/web-inf/lib into <LRay>\common\lib\ext Modify the url and other settings as per your application. 5. Remove all the org.springframework.jdbc.datasource.DriverManagerDataSource and use javax.sql.DataSource; 6. Deploy CMS app and Startup liferay 7. Test if the db connection is fine. The ant script might not copy the META-INF/context.xml file. Modify the ant script for CMS with <target name="build" depends="compile"> <war> ….. <!--Copy the META-inf library files --> <zipfileset dir="${srcdir}/webapp/META-INF" prefix="META-INF/" /> …. ……

Saturday, July 14, 2007

External Javascript from Java Servlets

Copied from  http://myappsecurity.blogspot.com/2007/01 Like to thank anurag for the content.
/breaking-same-origin-barrier-of.html
External Javascript from Java Servlets

One of the lesser known sides of external JavaScript is the ability to reference a server side program(CGI, PHP or Servlets) instead of the familiar .js file. It is kinda interesting since a client side script interacting with a server side program is not considered safe and is usually not allowed from within the browser but apparently a script can be dynamically generated and loaded, if referenced in src attribute of the script tag, while the html page is being loaded. Using the src attribute of the script tag, we can call an external javascript, we can also call a server side program to dynamically generate a javascript. For example

script type="text/javascript" src="myservlet"

Where "myservlet" is the server side program and could be an absolute path like “http://www.myserver.com/myservlet” or a relative path like “myservlet” instead of the usual .js file. Interestingly you can even pass parameters to the servlet through the URL string. For example

script type="text/javascript" src="http://attacker.com/myservlet?name=myname"

Now the servlet can be invoked and process parameters and return the result back. There is a limitation however. It can only return Javascript code. You also have to set the content type as “application/x-javascript”. Just think of it as returning javascript code instead of html code. But there is a workaround to this limitation. Just like while returning html, if you had Javascript code, you would encapsulate in script tag, here, if you have to return html code, you can always return document .body .innerHTML = ‘html code’ or document .write(‘html code’). So your typical servlet would look like


public class myservlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType(“application/x-javascript”);
PrintWriter out = response.getWriter();
String name = request.getParameter(“name”);
out.println(“document.body.innerHTML = ‘Welcome “ + name + “’;”);
out.close();
}
}


A JavaScript header is sent at the very beginning to inform the browser that it is receiving a JavaScript file. The final output of the servlet needs to be a valid Javascript file and must conform to Javascript syntax, servlet outputs a valid javascript code which replaces the content of the html page and displays “Welcome anurag”.
The other limitation, however, is that it cannot have an interactive session with the server side program. While loading the page, when the browser comes across the script tag, it goes to the URL mentioned in the src attribute and validates the incoming data as a valid javascript and executes it. The script tag is only executed once and after the entire page is loaded, it cannot call the server side program again.

Wednesday, July 11, 2007

Image Verification Utility for Form submission(Captcha)

Got from http://blog.jeffhaynie.us/image-verification-utility-for-form-submission.html
Thanks for the idea author.

Pasting the same below :
Image Verification Utility for Form submission
June 22nd, 2006 · No Comments

image verify screenshot Ever wanted one of those cool HTML form submission pages where you can make sure that only humans are submitting form data, and not machines (like spam bots)? Well, no you can have one. I created a simple Java utility class for generating the dynamic image in PNG format and then a simple servlet and web page for testing. You can download the source and demo here. Good luck!
Utility class

1: /**
2:  * Copyright (c) 2006 by Jeff Haynie
3:  *

4:  * Licensed under the Apache License, Version 2.0 (the “License”);
5:  * you may not use this file except in compliance with the License.
6:  *
7:  * You may obtain a copy of the License at
8:  * http://www.apache.org/licenses/LICENSE-2.0

9:  *
10:  * Unless required by applicable law or agreed to in writing,
11:  * software distributed under the License is distributed on an “AS IS” BASIS,
12:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13:  * See the License for the specific language governing permissions and

14:  * limitations under the License.
15: */
16: package us.jeffhaynie.image;

17:
18: import java.awt.Color;
19: import java.awt.Font;

20: import java.awt.Graphics2D;
21: import java.awt.geom.AffineTransform;

22: import java.awt.image.BufferedImage;
23: import java.io.IOException;

24: import java.io.OutputStream;
25: import java.util.Random;

26: import java.util.UUID;
27:
28: import javax.imageio.ImageIO;

29:
30: /**
31:  * ImageVerification is a simple utility class for
32:  * creating an image verification PNG file that will
33:  * allow you to make sure that only a human can read

34:  * the alphanumeric values and enter them into a text
35:  * field during verification. 

36: * 37: * Make sure that when you can getVerificationCode 38: * you don’t encode the value in the URL or inside the 39: * HTML form - otherwise, this whole excerise is pointless 40: * (dummy!). 41: * 42: * @author Jeff Haynie 43: * @copyright Copyright (c) by Jeff Haynie. All Rights Reserved. 44: */ 45: public class ImageVerification 46: { 47: private String value; 48: 49: public ImageVerification (OutputStream out) throws IOException 50: { 51: this(50,120,out); 52: } 53: public ImageVerification (int height, int width, OutputStream out) throws IOException 54: { 55: BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 56: Random rand=new Random(System.currentTimeMillis()); 57: Graphics2D g = bimage.createGraphics(); 58: 59: // create a random color 60: Color color = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); 61: 62: // the the background to the random color to fill the 63: // background and make it darker 64: g.setColor(color.darker()); 65: g.fillRect(0, 0, width, height); 66: 67: // set the font 68: g.setFont(new Font(“arial”,Font.BOLD,36)); 69: 70: // generate a random value 71: this.value = UUID.randomUUID().toString().replace(“-”,“”).substring(0,5); 72: 73: int w = (g.getFontMetrics()).stringWidth(value); 74: int d = (g.getFontMetrics()).getDescent(); 75: int a = (g.getFontMetrics()).getMaxAscent(); 76: 77: int x = 0, y =0; 78: 79: // randomly set the color and draw some straight lines through it 80: for (int i = 0; i < x="0;" y="0;" i =" 0;" x =" width/2" y =" height/2" fontat =" new" xp =" x-2;" c="0;c 109: { 110: // apply a random radian either left or right (left is half since it’s too far back) 111: int rotate = rand.nextInt(20); 112: fontAT.rotate(rand.nextBoolean() ? Math.toRadians(rotate) : -Math.toRadians(rotate/2)); 113: Font fx = new Font(“arial”, Font.BOLD, 36).deriveFont(fontAT); 114: g.setFont(fx); 115: String ch = String.valueOf(value.charAt(c)); 116: int ht = rand.nextInt(3); 117: // draw the string and move the y either up or down slightly 118: g.drawString(ch, xp, y + (rand.nextBoolean()?-ht:ht)); 119: // move our pointer 120: xp+=g.getFontMetrics().stringWidth(ch) + 2; 121: } 122: // write out the PNG file 123: ImageIO.write(bimage, “png”, out); 124: 125: // make sure your clean up the graphics object 126: g.dispose(); 127: } 128: /** 129: * return the value to check for when the user enters it in. Make sure you 130: * store this off in the session or something like a database and NOT in the 131: * form of the webpage since the whole point of this exercise is to ensure that 132: * only humans and not machines are entering the data. 133: * 134: * @return 135: */ 136: public String getVerificationValue () 137: { 138: return this.value; 139: } 140: }

Tuesday, July 10, 2007

PMD integration -For better quality

To run the PMD on the entire source code, I used the below task:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    
       
       
           rulesets/favorites.xml
           rulesets/logging-java.xml
           basic
           
           
               
           
       
   
 



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
One need's to add the pmd jar's( pmd-4.0rc1 ) to the class path of ant as well to run the report on the complete source.

Source forge link : pmd.sourceforge.net

Thursday, July 5, 2007

Javascript trouble shooting tools:

Javascript trouble shooting tools:

www.getFireBug.com à It is a powerful debugging tool for javascript. It is convenient just like eclipse debug. More details are in the website.

JSEclipse à Javascript editor plugin for eclipse . Helps in autocompletion, find the error in javascript. To use, extract the attached file, copy contents of plugins folder into ..\eclipse\plugins and features into ..\eclipse\features . Restart eclipse.

Simple usage details :

Right click on the JSP code enclosing the tag.

Note that this plugin marks the error in red. But it doesn’t show the actual cause of error(a limitation).

More details http://www.interaktonline.com/Products/Eclipse/JSEclipse/Overview/

Tuesday, July 3, 2007

Banging with cruiseControl

Cruise control,
I’ve done a cruise control setup . Subversion and cruisecontrol are integrated. My cruise control picks up the repository files from the subversion and builds the war file automatically for every 10(configurable) mins. Please follow the same steps. Use the binary mode file rather than using exe installation.
1) download cruisecontrol-bin-2.6.2.zip from http://sourceforge.net/project/showfiles.php?group_id=23523&package_id=16338&release_id=502915
2) set CVS_RSH=http://test.com/repos/Tree/TESTCMS – at cruisecontrol.bat
3) modify the project name in config.xml as TESTCMS
4) create TESTCMS header folder cruisecontrol-bin/projects/TESTCMS
5) Map the TESTCMS header with repository.(it will pull automatically all other files during ant build)
6) Set the interval in the config.xml
7) place the build.xml under TESTCMS
8) run the cruisecontrol-bin-2.6.2 >> cruisecontrol.bat
I am using the same build script where you are using for CMS. But I’ve modified only root directory as TESTCMS from cms
In addition to these, need to add the below in config.xml under tag:
Install the SVN.exe if not installed.
Change the project name in config.xml tag.
Create a folder with the project name in projects folder of CC
Deploy the webapp of CC in tomcat and control the Cruise.
A sample config is below:
<cruisecontrol>
<project name="TESTCMS">
<!-- Load environment variables -->
<property environment="env" toupper="true"/>
<!-- Commonly used directories -->
<property name="reportdir" value="${env.CCDIR}/report"/>
<!-- Defaults for email -->
<property name="buildmaster.email" value="test@test.com"/>
<property name="buildmaster.name" value="Buildmaster"/>
<listeners>
<currentbuildstatuslistener file="logs/${project.name}/status.txt"/>
</listeners>
<bootstrappers>
<svnbootstrapper localWorkingCopy="projects/${project.name}" />
</bootstrappers>
<modificationset quietperiod="30">
<svn localWorkingCopy="projects/${project.name}"/>
</modificationset>
<schedule interval="7200">
<ant anthome="apache-ant-1.6.5" buildfile="projects/${project.name}/build.xml"/>
</schedule>
<log>
<merge dir="projects/${project.name}/target/test-results"/>
</log>
<publishers>
<onsuccess>
<artifactspublisher dest="artifacts/${project.name}" file="D:\TESTRelease\sandeep\cruisecontrol-bin-2.6.2\projects\TESTCMS\pmd_report.html"/>
<artifactspublisher dest="artifacts/${project.name}" file="D:\TESTRelease\sandeep\cruisecontrol-bin-2.6.2\projects\cms\build\war\TESTCMS.war"/>
</onsuccess>
<htmlemail>
<always address="test@test.com"/>
<!-- <failure address="tommy@test.com"/> -->
</htmlemail>
</publishers>
<plugin name="svn" classname="net.sourceforge.cruisecontrol.sourcecontrols.SVN"/>
<plugin name="svnbootstrapper" classname="net.sourceforge.cruisecontrol.bootstrappers.SVNBootstrapper"/>
<plugin name="htmlemail"
buildresultsurl="http://172.16.152.126:7070/cruisecontrol/buildresults/${project.name}"
mailhost="IP adress of mail host"
returnaddress="${buildmaster.email}"
returnname="${buildmaster.name}"
subjectprefix="[BUILD ${project.name}]"
/>
</project>
</cruisecontrol>
Tips and traps:
cruisecontrol-bin-2.6.2. integrating with SVN may be tricky.
You need to install SVN client first. Note that the plugin provided by cruise control for
SVN just calls the SVN.exe file using exec call.
Initially the SVN checkout wont work. This is deu to a bug in the SVN plugin by cruise control. The bug is something related to characters in cmd parameter.
Work around is to do a manual checkout using svn.exe co <path> . Then onwards the cruise control rocks.
Another thing to note is to put the below in cruisecontrol.bat
set CVS_RSH=<path to SVN>
There seems to be a bug in this bat file also.Like single quote not compatible with XP..Be aware
One can integrate PMD with SVN just by adding the build task in build.xml. The artifact can be pointed by specifyin the destination