Thursday, October 22, 2009

Running your script at system startup on linux

Hi,

If you want to run your script at system startup e.g if you want to start jboss on system startup and stop on system shut down then create script and put it in "/etc/init.d" directory.

If you are using Debian then use 'update-rc.d' utility to install and remove System-V style init script links. Other distributions (such as Red Hat) use 'chkconfig'. You can see these utilities the you can locate files and check them.

If name of the script is say jboss then use following command

update-rc.d jboss defaults

If defaults is used, update-rc.d will make links to start the service in runlevels 2345, and stop the service in runlevels 016.

To start the script jboss in runlevels 0123 and stop in 456, run (as root):

update-rc.d jboss start 0123 stop 456

Read more: http://wiki.linuxquestions.org/wiki/Update-rc.d#ixzz0UjiBfsGM


For other distribution like red hat you can use chkconfig

Following tag needs to be added to your script so that it can be added to chkconfig
#!/bin/sh
#
# chkconfig: 2345 90 60

then you can add script by using command

chkconfig --add jboss

you can see all scripts configured by command

chkconfig --list

Regards,
Anand

Wednesday, October 14, 2009

Opening ports to outside world on linux machine

Hi,

If you want to open ports of linux machine to outside world through command line (It can be done through UI too) then follow the below steps:

Goto /etc/init.d/iptables

Add entry like: (this is for port 6667)

iptables -I INPUT -p tcp -m tcp --dport 6667 -j ACCEPT

Then restart iptables so that changes made gets effective

/etc/init.d/iptables restart

Thanks,
Anand

Running multiple jboss 5.0 instance on same machine

Hi,

I wanted to setup production and dev environment on the same machine. As both are using different database instances and I was deploying EJB application, so I couldn't use the same server and deploy two applicatione with different context roots as EJB jndi names were conflicting.

I copied jboss server dir structure and given some other name. I changed properties as needed.

I run new instance of jboss using following command which will save you from port conflicts.

run -Djboss.service.binding.set=ports-01
The above command will use port 8180 (http) and 8543 (https).

There is another way which is bit difficult i.e. change ports in various xml files.


Thank,
Anand

Thursday, October 8, 2009

Installing Verisign SSL certificate to your site

Here is example to create Certificate signing request and install the SSL certificate received from Verisign using openssl tool. (Specific to jboss, may or may not work for other servers. Visit http://www.verisign.com for further information )

1. Generate RSA private key

openssl genrsa -des3 -out domainname.key 1024

You will be promted for passphrase.

2. Create CSR (Cerificate signing request)

openssl req -new -key domainname.key -out domainname.csr

When creating a CSR you must enter information to be displayed on the certificate. E.g. Common name, Organization, Organization Unit etc

3. Send this CSR to get SSL Certificate from Verisign.

4. When you receive SSL Certificate from Verisign, you need to install it.

- Save the certificate in file name it "YourVeriSignSSLCert.crt"

- Get the intermediate CA certificate from: http://www.verisign.com/support/verisign-intermediate-ca/secure-site-intermediate/index.html and save it in a file, name it "YourIntermediateCertificate.cer"

- Command to create PKCS12 keystore

openssl pkcs12 -export -in YourVeriSignSSLCert.crt -inkey domainname.key -out mycert.p12 -name tomcat -CAfile YourIntermediateCertificate.cer -caname root -chain

5. Configure the server conf file with keystoreFile="c:\PATH TO mycert.p12" keystorePass="PASSWORD HERE" keystoreType="PKCS12"

6. Restart the server. You can check your installation status at: https://knowledge.verisign.com/support/ssl-certificates-support/index?page=certchecker

7. Once certificate installed properly you need to add Verisign seal to you site. Refer to https://www.verisign.com/ssl/secured-seal/index.html


In case if you face any issues in the installion, you can call or chat with verisign support team.

For other server installation instruction please refer to https://www.verisign.com/support/ssl-certificates-support/install-ssl-certificate.html

Thanks,
Anand

Friday, August 28, 2009

Enabling https support

Enabling https support for restlet server and client:

1) Create your keys and certificate. I tried both 'keytool' and IBM's
KeyMan to do this (KeyMan is easier, but more work to obtain, as you need
to register, etc...). Using keytool from a command prompt you need to
enter two commands - the first one creates the keystore file with the
keys, the second certifies it (self-certification, ok for testing). The
most important thing is that the name of the machine you will be using the
certificate on matches what you specify (in the example below, my machine
is called 'serverX'):

keytool -genkey -dname "CN=serverX, OU=IT, O=JPC, C=GB" -alias serverX
-keypass password -keystore serverX.cer -storepass password -keyalg "RSA"
-storetype "PKCS12" -provider sun.security.provider.Sun

keytool -selfcert -alias serverX -keystore serverX.cer -storepass password
-storetype "PKCS12"

The keystore file has now been created and self-certified: in this example
it is called 'serverX.cer' and was saved in the current directory. There
are two passwords: one for the keys and one to access the keystore. I set
them both to 'password' for testing. The name of the keystore file
('serverX.cer') is not important, I just used that for consistency.

2) To prevent warnings in a browser, add the keystore to the 'Trusted Root
Certification Authorities' on your computer. In Windows XP, I just used
Internet Options (via IE7 or Control Panel - Internet Options). On the
'Content' tab, click 'Certificates', then go to 'Trusted Root
Certification Authorities' tab, click 'Import...' and follow the steps to
import your keystore file (in my example, 'serverX.cer'). It will give
warnings about not being verified, which is ok for testing (but it must be
properly signed for production).

3) In order for Java security to recognise the certificate, it needs to be
added to \lib\security\cacerts, which is the Java certificates file.
This is important when you use a Restlet client to connect to the server
via HTTPS (but it did not seem to be needed by my browser - it needed the
IE options update described in point 2). On my system, 'cacerts' is
"C:\Program Files\Java\jre6\lib\security\cacerts". I had some trouble
adding my 'serverX' certificate to it, but the following keytool commands
work if you know the password for cacerts ('changeit' is the default I
believe):

keytool -export -alias serverX -file serverX.jks -storetype "PKCS12"
-keystore serverX.cer -keypass password
keytool -import -alias serverX -file serverX.jks -noprompt -trustcacerts
-keystore "C:\Program Files\Java\jre6\lib\security\cacerts"
(when prompted for password give ‘chageit’)

The first command exports the certificate from PKCS12 format into X.509
(JKS) format, which is what cacerts needs. In my case, I had to use
KeyMan to set the password for the 'cacerts' file (I set it back to the
default of 'changeit'), so when I ran 'keytool -import ...' I could enter
the correct password. There may be a better/easier way to do this.

4) In your Java Restlet server program, in addition to the standard
Restlet jar files, you also need jar files for HTTPS. The only HTTPS
connector I could get to work correctly was 'Simple', which uses these jar
files:

lib/com.noelios.restlet.ext.simple_3.1.jar
lib/org.simpleframework_3.1/org.simpleframework.jar
lib/com.noelios.restlet.ext.ssl.jar
lib/org.jsslutils_0.5/org.jsslutils.jar

(Grizzly compiled and ran, but gave inconsistent results - appeared to be
missing requests; Jetty threw an error saying it couldn't register
'AjpServerHelper').

5) Your Restlet server code should then look something like this:

package com.jpc.samples;

import org.restlet.Component;
import org.restlet.Server;
import org.restlet.data.Parameter;
import org.restlet.data.Protocol;
import org.restlet.util.Series;

public class SampleServer {

public static void main(String[] args) throws Exception {
// Create a new Component.
Component component = new Component();

// Add a new HTTPS server listening on port 8183
Server server = component.getServers().add(Protocol.HTTPS, 8183);

Series parameters = server.getContext().getParameters();
parameters.add("sslContextFactory",
"com.noelios.restlet.ext.ssl.PkixSslContextFactory");

//use com.noelios.restlet.util.DefaultSslContextFactory instead of PkixSslContextFactory
parameters.add("keystorePath", "serverX.cer");
parameters.add("keystorePassword", "password");
parameters.add("keyPassword", "password");
parameters.add("keystoreType", "PKCS12");

// Attach the sample application.
component.getDefaultHost().attach("", new SampleApplication());

// Start the component.
component.start();
}
}

The HTTP examples all show
'component.getContext().getParameters().add(...) but this doesn't seem to
work for any HTTPS connectors. Using the
server.getContext().getParameters().add(...) does work but this doesn't
seem to be clearly documented anywhere.

If everything works, you should get a console message like this when you
start the server:

21-Nov-2008 00:08:44 com.noelios.restlet.ext.simple.SimpleServerHelper
start
INFO: Starting the Simple server

6) You can test it using a browser, going to https://serverX:8183/restlet>

7) If you want to write a Restlet client program you need to add the
following jar file (which has an HTTPS client connector):

com.noelios.restlet.ext.net.jar

8) Your Restlet client code then looks something like this:

package com.jpc.samples;

import java.io.IOException;

import org.restlet.Client;
import org.restlet.data.Form;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;
import org.restlet.data.Response;
import org.restlet.resource.Representation;

public class SampleClient {

public static void main(String[] args) throws IOException {

// Define our Restlet HTTP client.
Client client = new Client(Protocol.HTTPS);

// The URI of the resource "list of items".
Reference samplesUri = new Reference("https://serverX:8183/sample");

// Create 9 new items
for (int i = 1; i < 10; i++)
{
Sample sample = new Sample(Integer.toString(i), "sample " + i, "this
is sample " + i + ".");
Reference sampleUri = createSample(sample, client, samplesUri);
if (sampleUri != null) {
// Prints the representation of the newly created resource.
get(client, sampleUri);
}
}

// Prints the list of registered items.
get(client, samplesUri);
}

Thursday, July 9, 2009

Converting JPG image to BMP image in Java

Hello,
Here is utility to convert JPG image to Bitmap image:

BufferedImage loadImg = ImageIO.read(new File("D:\\testimages\\test.jpg");
ImageIO.write(loadImg, "bmp", new File("D:\\testimages\\test.bmp"));

Problem with above conversion is, bitmap generated will always have 24bpp (bits per pixel).
I needed 1bpp bitmap image. Unfortunately google didn't help.

Here is what I tried,

BufferedImage loadImg = ImageIO.read(new File("D:\\testimages\\test.jpg"); //image to be converted to bmp

BufferedImage img = new BufferedImage(loadImg.getWidth(), loadImg
.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
//create new image with type as TYPE_BYTE_BINARY for 1bpp, there are other options too for other bpp like 8, 24 etc
for (int y = 0; y < loadImg.getHeight(); ++y)
for (int x = 0; x < loadImg.getWidth(); ++x)
img.setRGB(x, y, loadImg.getRGB(x, y)); //copy RGB data from original image to new image
ImageIO.write(img, "bmp", new File("D:\\testimages\\test.bmp")); // now convert the above image to bmp

The above code gave me 1 bpp bitmap image.

Now image can be of any size, how to compress image to desired size ?
Here is answer:

//proved xfactor and y factor i.e width and heigth compression factor
AffineTransform tx = new AffineTransform();
tx.scale(xfact, yfact);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);

You can calculate xfactor and yfactor from the desired width -height and actual width - height of the image.

More on image manipulation in java can be found at http://www.javalobby.org/articles/ultimate-image/

Thanks,
Anand

Tuesday, April 28, 2009

Using Websphere Dynamic Object Cache Feature

Hello,

Web sphere has provided very nice feature to cache your objects. Suppose you have some static data in database which does not gets updated frequently, but you need to access that data frequently. You can fetch data once in object and you can cache this object in webspheres DynamicObjectCache, so when required it can fetched from cache reducing expensive db calls.

Lets see how we can configure this cache in Websphere v6.1:

1) Login to admin console
2) Go to Resources -> Cache instances -> Object cache instance
3) Create new cache instance by provinding cache name, jndi name, cache size etc

Using object cache instance in your code:

InitialContext ic = new InitialContext();
DistributedMap dmap = (DistributedMap) ic.lookup("jndiName");

For DistributedMap you need "was-runtime-6.1.0.jar"

Now you can use this map to store and get your objects.
dmap.put("key", object);
dmap.get("key"); ...

Default size of this maps is 2000. You can set desired size from admin console.

More info on object cache setting can be found at :
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/udyn_cacheinstancescollection.html

Detailed article on the object cache can be found at:
http://www.entrepreneur.com/tradejournals/article/117988840_5.html

Thanks,
Anand