Friday, April 17, 2009

Glassfish quickies

Here are two quickies for Glassfish.

Quickie 1: How to create a new domain.
asadmin create-domain --adminport 4949 --savemasterpassword true mydomain
where --adminport is the port number for the administration GUI
and --savemasterpassword tells GlassFish to save the master password. It will make it easier then to create a service.

See Also

To see the full syntax and options of the command, type asadmin create-domain --help at the command line.

Quickie 2: Setting GlassFish as a Windows Service
When running GlassFish on a Windows box one has to install it as a service so GlassFish keeps running once you log off.
Here how you do it:
1. Download GlassfishSvc.jar
2. Copy your file to your GlassFish installation and cd to this folder
3. Run the following command:
C:\Program Files\Sun\GlassfishV2>java -jar GlassfishSvc.jar -i -n ServiceName -m DomainName
where -i is to install, -u to uninstall.

See Also
More info here

Thursday, April 16, 2009

Adding users to SVN and setting SVN with Apache

A quickie... to add users in SVN running under Apache, this is the syntax for Windows:

htpasswd D:\Repositories\svn-auth-file jack

You will be prompted as follows:
New password: *******
Re-type new password: *******
Adding password for user jack

Of course when setting up SVN with Apache the svn-auth-file is created.

One can set SVN with Apache as follows:
Copy the the following modules in Apache modules folder:
mod_dav_svn.so, mod_authz_svn.so

Modify the httpd.conf, by adding the following:

# Subversion modules
LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so
<location>
DAV svn
SVNPath "C:/Repository/repositoryname"
AuthType Basic
AuthName "Subversion Casino repository"
AuthUserFile "C:/Repository/svn-auth-file"

Require valid-user
<location>

Sunday, April 12, 2009

Simple Castor unmarshalling example

Castor is great framework to provide Java to XML binding and vice versa. In this little post I am going to show how to unmarshall (marshall-out) an XML file to Java objects.

My sample XML is the following snippet:

Its the Google sitemap.xml. I would like to iterate over the urls in this file to check that there are no errors (no 404s, 500s) in this URL list.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url><loc>http://www.mysite.com/</loc><lastmod>2009-04-09T18:33:21+00:00</lastmod><changefreq>daily</changefreq><priority>1.00</priority></url>
<url><loc>http://www.mysite.com/register.jsp</loc><lastmod>2009-04-09T18:33:18+00:00</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
</urlset>


These are the steps to get Java objects in your code representing the above XML.

1. Download Castor from here

2. Create an XSD file for the above XML (there are online schema generator like HIT software
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="changefreq">
<xs:complexType mixed="true" />
</xs:element>

<xs:element name="lastmod">
<xs:complexType mixed="true" />
</xs:element>

<xs:element name="loc">
<xs:complexType mixed="true" />
</xs:element>

<xs:element name="priority">
<xs:complexType mixed="true" />
</xs:element>

<xs:element name="url">
<xs:complexType>
<xs:sequence>
<xs:element ref="loc" />
<xs:element ref="lastmod" />
<xs:element ref="changefreq" />
<xs:element ref="priority" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="urlset">
<xs:complexType>
<xs:sequence>
<xs:element ref="url" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="xsi:schemaLocation" type="xs:string" />
</xs:complexType>
</xs:element>

</xs:schema>


3. Generate Java objects from the schema using an ANT task

4. Write a simple test case to test the code generation:


public void testCastorMarshallingOut() throws FileNotFoundException, MarshalException, ValidationException {
FileReader reader = new FileReader(Settings.getInstance("sitemap"));
Urlset urls = Urlset.unmarshal(reader); assertNotNull(urls);
assertEquals(urls.getUrlCount(), 35);

for(int i = 0; i <>
org.castor.sitemap.Url theUrl = urls.getUrl(i);
String currentUrl = theUrl.getLoc().getContent();

System.out.print(currentUrl);
assertNotNull(currentUrl);
}
}


P.S. I had a problem with a required attribute. It seems Castor was not mapping correctly xsi:schemaLocation attribute. In my case I modified the generated XSD, still looking at this issue.

Wednesday, March 25, 2009

Changing GlassFish Java JDK version

Following the previous post, if one wants to update GlassFish JDK's version has to do the following:

Goto the config folder, edit the asenv.bat file and change the following to the desired version:

set AS_JAVA=D:\java\jdk1.6.0_11\jre/..

VisualVM installation problem

VisualVM is a visual tool integrating several commandline JDK tools and lightweight profiling capabilities. Designed for both production and development time use, it further enhances the capability of monitoring and performance analysis for the Java SE platform.
One can download it here

One can use it to monitor glassfish. Here is a good blog how to configure the glassfish plugin.

I had an issue with the installation. The issue was that I was running JDK6.12
Switching to JDK6.11 solved the problem.

Monday, March 9, 2009

Enabling GZIP on GlassFish on v2

I had problems enabling GZIP on GlassFish on V2. For later version there are new settings like
compression = force

But the following settings worked for me at least.
One can configure GZIP from the GUI for the http-listener nodes as follows:






Using FireBug (or Fiddler) one can verify that the request is GZIPped as the following diagram shows.

Sunday, February 15, 2009

GlassFish and Java memory allocation

Last week I was very confused and bewildered when all of a sudden I could no longer find my compiled JSPs.
We are using GlassFish application server and normally one can find the compiled JSPs in the generated folder.
Anyway after the amusement passed I realised that something must have changed and remembered one of my mottos "Remember the solution!"
Basically what happened was that the server Java JDK was upgraded to 1.6
One of the features of 1.6 is memory allocation. Thus JSPs are compiled and stored in memory. The benefits are obvious, performance! That's where the JSPs have gone!
Anyway, GlassFish has a setting, keepGenerated. Prior version 1.6 this setting is set to true. In 1.6 the default is false. If you are using 1.6 and want to debug the compiled JSPs change keepgenerated (found in default-web.xml and sun-web.xml) to false.
Remember the solution!