John Manko

stuff

Archive for the 'Java' Category

Perl Script To Merge Javadoc Into Single HTML For Printing

I forgot Perl (been about 10 years), but this script I wrote does the job.   Released under GPL v2.  Modify and share as you see fit.

#!/usr/bin/perl

use Fcntl qw(:flock :seek);
use WWW::Mechanize;

# Would like to accept the javadoc root path as a parameter.  Right now, need to hard code.
$allClassesFile = "./allclasses-noframe.html";

my $mech = WWW::Mechanize->new();
$mech->get( "file:$allClassesFile" );
my @links = $mech->links();

# This is the new single file
$sitedata="single.html";
open(DAT,">$sitedata") || die("Cannot Open File $sitedata");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_SET);

print DAT "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
print DAT "<HTML><HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">";
print DAT "<TITLE>Single Page</TITLE><LINK REL =\"stylesheet\" TYPE=\"text/css\" HREF=\"stylesheet.css\" TITLE=\"Style\"></HEAD><BODY BGCOLOR=\"white\">";

# Would like to include a table of contents of all classes docs included, with an href="#someclass" linking to the proper section in the page

# TOC
print DAT "<UL>";
for my $link ( @links ) {
    $linkText = $link->text;
    chomp($linkText);
    $linkUrl = $link->url;
    chomp($linkUrl);
    if (($linkText ne '') && ($linkUrl ne '')){
      $linkUrl =~ s/\//\./g;
      $linkUrl =~ s/\.html//;
      print DAT "<LI><a href='#$linkUrl'>$linkUrl</a>";
    }
}
print DAT "</UL>";
print DAT "<hr  /><br/>\n";
print DAT "<div style=\"page-break-before: always;\"></div>\n";

# maybe fix to better handle errors
for my $link ( @links ) {
    $linkText = $link->text;
    chomp($linkText);
    $linkUrl = $link->url;
    chomp($linkUrl);
    if (($linkText ne '') && ($linkUrl ne '')){
      #printf "%s, %s\n", $linkText, $linkUrl;
      open ACF, "<./$linkUrl"  || die("Cannot Open File $linkUrl");
      $infile_contents = do { local $/; <ACF> };
      close ACF;

        # I'm sure there is a better way to split this
      @content = split(/<!-- ======== START OF CLASS DATA ======== -->/, $infile_contents);
      @content = split(/<!-- ========= END OF CLASS DATA ========= -->/, $content[1]);

      print DAT "<a name='$linkUrl'\n";
      print DAT "$content[0]\n";
      print DAT "<div style=\"page-break-before: always;\"></div>\n";
    }
}

print DAT "</BODY></HTML>";
close(DAT);
posted by john in Computers & Technology,Java and have No Comments

Writing a ServletContextListener

A ServletContext listener is useful when you need to perform an action upon application start-up or shutdown, such as initialize application wide variables from a data store.

Let’s look at what Java EE classes we’ll be using for this demonstration:  ServletContextEvent and ServletContextListener

// This is the event class for notifications about changes
// to the servlet context of a web application.

import javax.servlet.ServletContextEvent;

// Implementations of this interface receive notifications
// about  changes to the servlet context of the web
// application they are  part of.  To receive notification
// events, the implementation class  must be configured in
// the deployment descriptor for the web  application.

import javax.servlet.ServletContextListener;

Our first step is to create a class that implements the ServletContextListener interface.  There are only two methods that require implementations:  contextInitialized and contextDestroyed.

From the Javadoc:

void contextInitialized(ServletContextEvent sce)
Notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.
void contextDestroyed(ServletContextEvent sce)
Notification that the servlet context is about to be shut down. All servlets and filters have been destroy()ed before any ServletContextListeners are notified of context destruction.

Below is our listener class. You’ll notice that I’ve included a @PersistenceUnit annotation. Since context listeners are created by the servlet container, all dependency injections occur as requested.


package com.manko.servlet.listener;
public class MyAwesomeContextListener implements ServletContextListener {

    /* Needed in order to get the application values from our data store  */
    @PersistenceUnit
    private EntityManagerFactory emf;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}

Now, let’s add code to contextInitialized. Assuming we have an entity class ApplicationConfiguration

        EntityManager em = null;
	Query query = null;
	Hashtable table = null;

	try {
		// Get configuration entries
		em = emf.createEntityManager();
	        query = em.createNamedQuery("ApplicationConfiguration.findAll");
	        List<ApplicationConfiguration> configs = (List<ApplicationConfiguration>) query.getResultList();

		// Put them in a Hashtable for easy reference
	        table = new Hashtable(configs.size());

	        for (ApplicationConfiguration config:configs){
	            table.put(config.getId(), config.getValue());
	        }

		// Save the Hashtable as a ServerContext variable
	        sce.getServletContext().setAttribute("MyApplicationConfiguration", table);

	} catch (Excception e) {
		// do stuff
	}

        em = null;
        query = null;
        table = null;

Of course, simply coding a context listener is meaningless without informing the container.  There are two ways to accomplish this.

  • If you are running within a version 2.5 servlet container, an xml deployment descriptor is your only option.
  • For version 3.0 containers, you may annotate your class with @WebListener
 

v2.5 (web.xml – Glassfish 2.1 for example):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	 xmlns="http://java.sun.com/xml/ns/javaee"
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>com.manko.servlet.listener.MyAwesomeContextListener</listener-class>
    </listener>
</web-app>

v3.0 (annotations – Glassfish 3.0 for example):

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyAwesomeContextListener implements ServletContextListener

IMPORTANT – This is from the Servlet 3.0 specification (p180):

The web application deployment descriptor contains a metadata-complete attribute on the web-app element. The metadata-complete attribute defines whether the web.xml descriptor is complete, or whether other sources of metadata used by the deployment process should be considered. Metadata may come from the web.xml file, web-fragment.xml files, annotations on class files in WEB-INF/classes, and annotations on classes in jar files in the WEB-INF/lib directory. If metadata-complete is set to “true”, the deployment tool only examines the web.xml file and must ignore annotations such as @WebServlet, @WebFilter, and @WebListener present in the class files of the application, and must also ignore any web-fragment.xml descriptor packaged in a jar file in WEB-INF/lib. If the metadata-complete attribute is not specified or is set to “false”, the deployment tool must examine the class files and web-fragment.xml files for metadata,as previously specified.

That’s it!   We now have a servlet context listener in our application.  Please, do yourself a favor and read the Servlet 3.0 specification.

Tags: ,
posted by john in Computers & Technology,Java and have No Comments

A Case Against JPA

Here are some numbers for JPA generated queries verses a custom formatted one:

Based on the following query:

em.createQuery(" SELECT object(o) FROM INTERNALIPADDRESSBINDING o WHERE o.internalIpAddress.ipAddress = :ip AND o.macAddress.macAddress = :mac AND o.bindTimestamp = :bind ")

JPA generated query based on Entity classes and foreign keys:

SELECT t0.ID,
       t0.BINDTIMESTAMP,
       t0.MACADDRESS_MACADDRESS,
       t0.internalIpAddress_ipAddress
FROM   INTERNALIPADDRESSBINDING t0,
       MACADDRESS t2,
       INTERNALIPADDRESS t1
WHERE  ((
         ((t1.IPADDRESS ='4.4.4.4') AND (t2.MACADDRESS = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ'))
         AND
         (t0.BINDTIMESTAMP = '2009-09-22 15:01:53.0')
        )
        AND
        (
         (t1.IPADDRESS =t0.internalIpAddress_ipAddress)
         AND
         (t2.MACADDRESS = t0.MACADDRESS_MACADDRESS)
        )
       );

 Empty set (20.53 sec)

My query:

SELECT t0.ID,
       t0.BINDTIMESTAMP,
       t0.MACADDRESS_MACADDRESS,
       t0.INTERNALIPADDRESS_IPADDRESS
FROM   INTERNALIPADDRESSBINDING t0
WHERE  t0.INTERNALIPADDRESS_IPADDRESS = '4.4.4.4'
   AND t0.MACADDRESS_MACADDRESS = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ'
   AND t0.BINDTIMESTAMP = '2009-09-22 15:01:53.0';

 Empty set (1.35 sec)
posted by john in Java and have No Comments