Wednesday, October 21, 2009

JDBC3 Connection and Statement Pooling

JDBC3 Connection and Statement Pooling Documents
http://www.mchange.com/projects/c3p0/#installation

Tuesday, May 26, 2009

Redirect all traffic from port 80 to port 443 https in Apache2

Force an SSL connection and redirect all traffic from port 80 (HTTP) to port 443 (HTTPS), use this instead:
(assuming: Apache2 Listen on 80 and 8080)


RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

RewriteCond %{SERVER_PORT} ^8080$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

RewriteLog "logs/rewrite.log"
RewriteLogLevel 2

Wednesday, May 6, 2009

Struts2 Security Code Action

package com.ozview.struts2.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
*
* SecurityImageCodeAction.
*
*/
public class SecurityImageCodeAction extends BaseAction
{
/** The image font. * */
private static Font imageFont = new Font("Arial Bold", Font.BOLD, 24);

/** The InputStream imageStream. * */
protected InputStream imageStream;

/** The logger log. * */
private final Logger log = Logger.getLogger(this.getClass().getName());

/**
* Generate the security image code.
*
* @return SUCCESS if there is no error.
*/
public String genSecurityImageCode()
{
try
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
String randomCode = generateImageCode(output);
// store the random number in session.
storeInSession(SECURITY_CODE, randomCode);
this.imageStream = new ByteArrayInputStream(output.toByteArray());
output.close();
} catch (Exception e)
{
removeFromSession(SECURITY_CODE);
log.error("generating the security code error");
return ERROR;
}
return SUCCESS;
}

/**
* Generate the Buffered image.
* @param output The ByteArrayOutputStream output.
* @return a SecurityImageCode which contians a random characters and an image.
*/
protected String generateImageCode(ByteArrayOutputStream output)
{
//image size
int width = 150, height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
Random random = new Random();
//g.setColor(getRandColor(200, 250));
//set the background color
g.setColor(new Color(230,241,241));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(imageFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}

String sRand = "";
for (int i = 0; i < 8; i++)
{
String tmp = getRandomChars();
sRand += tmp;
// g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 +
//random.nextInt(110)));
g.setColor(new Color(220, 220, 220));
g.drawString(tmp, 17 * i + 9, 22);
}
g.dispose();
try
{
ImageIO.write(image, "jpg", output);
} catch (IOException e)
{
log.error("Generating the security image code error, " + e.getMessage());
}
return sRand;
}

/**
* Get random color.
*
* @param fc
* a int fc.
* @param bc
* a int bc.
* @return a Random color.
*/
protected Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
* Get a random characters.
*
* @return a random characters.
*/
protected String getRandomChars()
{
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}

/**
* Set the InputSteam imageStream.
*
* @param imageStream
* The InputSteam imageStream to be set.
*/
public void setImageStream(InputStream imageStream)
{
this.imageStream = imageStream;
}

/**
* Get the InputSteam imageStream.
*
* @return An InputSteam imageStream.
*/
public InputStream getImageStream()
{
return imageStream;
}
}

Add the following lines into struts.xml file:



Using the Security Code in the JSP file:

How to ceate a Security Code

package com.ozview;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class Test
{
/** The image font. * */
private static Font imageFont = new Font("Arial Bold", Font.BOLD, 24);



protected String generateImageCode(ByteArrayOutputStream output)
{
int width = 150, height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
Random random = new Random();
//g.setColor(getRandColor(200, 250));
g.setColor(new Color(230, 241, 241));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(imageFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}

String sRand = "";
for (int i = 0; i < 8; i++)
{
String tmp = getRandomChars();
sRand += tmp;
//g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.setColor(new Color(0, 0, 255));
g.drawString(tmp, 17 * i + 9, 22);
}
g.dispose();
try
{
File imagefile = new File("securitycode.jpg");
ImageIO.write(image, "jpg", imagefile);
} catch (IOException e)
{
System.err.print("Generating the security image code error, " + e.getMessage());
}
return sRand;
}

/**
* Get random color.
*
* @param fc
* a int fc.
* @param bc
* a int bc.
* @return a Random color.
*/
protected Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
* Get a random characters.
*
* @return a random characters.
*/
protected String getRandomChars()
{
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}

public static void main(String [] args) {
Test test = new Test();
ByteArrayOutputStream output = new ByteArrayOutputStream();
String randomCode = test.generateImageCode(output);

System.out.println(randomCode);


}

}

Saturday, April 18, 2009

UsingMod_proxy With Apache2 and Tomcat

1. yum install apache2
2. Setup Apache to use mod_proxy (AJP)
Make sure that at least following modules are loaded (uncomment this in httpd.conf)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
Add those lines in /etc/httpd/conf/httpd.conf :
<Proxy balancer://mycluster>
Order deny,allow
Allow from all
BalancerMember ajp://localhost:8009/yourapp
</Proxy>

<VirtualHost *:80>
ServerAdmin admin@yourhost.com
ServerName www.yourhost.com
ServerAlias yourhost.com

ProxyPass /yourapp balancer://mycluster
ProxyPassReverse /yourapp balancer://mycluster/

ErrorLog logs/yourhost_error_log
CustomLog logs/yourhost_access_log combined

</VirtualHost>
3. The yourapp application will be deployed in tomcat, and make sure the tomcat 8009 connector is opened.

4. In a browser, type: http://www.yourhost.com/yourapp, it will go to yourapp that deployed in the tomcat, you don't need to type the port number.

Reference: usintMod_ProxyWithJBoss

Wednesday, April 15, 2009

Installing Apache Tomcat 5 as a service on Linux5(CentOS5)

1. First, create the shell script file below and name it tomcat under /etc/init.d/

#!/bin/sh
#
# /etc/init.d/tomcat
#
# chkconfig: 345 84 16
#
# This is the init script for starting up the
# Jakarta Tomcat server
#
# description: Starts and stops the Tomcat daemon.
#

tomcat=/usr/local/tomcat
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
export JAVA_HOME=/usr/local/java
RETVAL=0

start() {
echo -n $"Starting Tomcat service: "
sh $startup
RETVAL=$?
echo
}

stop() {
echo -n $"Stopping Tomcat service: "
sh $shutdown
RETVAL=$?
echo
}

restart() {
stop
start
}

status() {
SHUTDOWN_PORT=`netstat -vatn |grep LISTEN | grep 8080 |wc -l`
if [ $SHUTDOWN_PORT -eq 0 ]; then
echo "Tomcat stopped"
else
echo "Tomcat is running!"
fi

}

# Handle the different input options
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac

exit 0
2. Change the Permissions:

chmod u+x /etc/init.d/tomcat

3. You can test the script by running this command
cd /etc/init.d
./tomcat status
which will return the Tomcat process info if it is running; otherwise, it will return blank

4. Add tomcat as a service by running the following:
chkconfig --add tomcat

5. Now the tomcat is a service, you can issue these commands from anywhere:

service tomcat start
service tomcat stop
service tomcat restart
service tomcat status
6. Check whether the tomcat will start at boot:
chkconfig --list tomcat

Wednesday, April 8, 2009

How to Install eclipse and Add it to Desktop


1. Download the eclipse-jee-ganymede-SR2-linux-gtk.tar.gz from eclipse.org

2. Extract the eclipse to /opt directory

[root@www opt]#
tar xvfz eclipse-jee-ganymede-SR2-linux-gtk.tar.gz

3. Change the ownship to root:
[root@www opt]# chown -R root:root eclipse
[root@www opt]#chmod -R +r eclipse
4.Then create an eclipse executable in your path
[root@www opt]# touch /usr/bin/eclipse
[root@www opt]# chmod 755 /usr/bin/eclipse

[root@www opt]# vim /usr/bin/eclipse
with this contents:
#!/bin/sh

export ECLIPSE_HOME="/opt/eclipse"

$ECLIPSE_HOME/eclipse $*
5.Then create a gnome menu item, download one of eclipse icon from eclipse website, and put it into the /op/eclipse directory as eclipse.png.
[root@www opt]# vim /usr/share/applications/eclipse.desktop
with the following contents:
[Desktop Entry]
Encoding=UTF-8
Name=Eclipse
Comment=Eclipse IDE
Exec=eclipse
Icon=/opt/eclipse/eclipse.png
Terminal=false
Type=Application
Categories=GNOME;Application;Development;

StartupNotify=true


6. Configuration
You now have a working eclipse, run this command first to initialize the settings:
/opt/eclipse/eclipse -clean

Then from here on you can run from the menu item applications/programming/eclipse