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.