Tuesday, July 29, 2008

Access the target object from a Hibernate CGLIB proxy

Sometimes in Hibernate when you have Lazy Loaded lists of objects you will end up interacting with a proxied instance of the object rather than the object itself, even if you access members of the object so that you know it's been loaded from the database.

This situation can be tricky because your object walks like a duck and talks like a duck...but it's a proxy. You want the duck! You can do something like the following to lure it out:

if (asset instanceof org.hibernate.proxy.HibernateProxy) {
    BankAccount ss = (BankAccount) ((HibernateProxy)asset).getHibernateLazyInitializer().getImplementation();
    String desc = asset.getDescriptionTxt();
}

Friday, July 25, 2008

Pass an encoded message resource in a redirect URL

Often you need to display a message/error on a JSP page but you want to redirect to the page so that it can be refreshed without reposting the form.  If you simply set redirect="true" in your struts-config.xml action forward you end up losing the request-scoped message when struts issues the redirect.

So what you need to do is 1) construct the message from a resource file, 2) encode the message, 3) issue a redirect and 4) display the message in the target JSP.

Access the MessageResources.properties bundle

org.apache.struts.util.MessageResources mr = getResources(request);
String message = mr.getMessage("the.message.key", optionalInterpolationArgument);

URL encode the message and issue a redirect

message = "message=" + java.net.URLEncoder.encode(message, "UTF-8");
response.sendRedirect(request.getContextPath() + "/targetAction.do?" + message);

Display the message

Note the use of <c:out /> here. If you just write out the message without doing any HTML escaping on it you open yourself up to an HTML-injection attack.
<c:if test="${!empty param.message}">
  <c:out value="${param.message}" />
</c:if>

Lookup message resources in a JSP file

Sometimes it may be better to just pass the message key itself and have the target JSP lookup the message and display it. The following snippet shows you how to lookup a message key:
<bean:message key="simple.message.key"/>
<bean:message key="interpolated.message.key" arg0="argument.value"/>

Tuesday, July 22, 2008

Installing MySQL and Apache as Windows services

To install Apache as a service

From the Apache bin directory (usually C:\Program Files\Apache Group\Apache2\bin):
C:\Program Files\Apache Group\Apache2\bin>Apache.exe -k install -n "This is the service name"
Other options are
-k uninstall to uninstall the service,
-k start,
-k stop and
-k restart to control the apache server.

To install MySQL as a service

From the MySQL bin directory (usually C:\Program Files\MySQL\MySQL Server 5.0\bin):
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqld --install "This is the service name" --defaults-file=C:/custom/path/to/my.ini
The example above sets the --defaults-file option which allows you to specify a non-standard options file location. The server will read options from the [mysqld] group.

To control the server you can use mysqladmin --port=3306 -u root shutdown to shutdown the server. 3306 is the default port. I put in the --port option as an example for when you need to specify a non-standard port. mysqld --remove removes the service. There are many other useful commands, to see them use the --help option.

Shut down Windows XP from the command line

You can use following built-in utility to shutdown Windows and then the machine itself:
C:\WINDOWS\SYSTEM32\TSSHUTDN.EXE 0 /POWERDOWN /DELAY:secs

To reboot use the following option:

C:\WINDOWS\SYSTEM32\TSSHUTDN.EXE 0 /REBOOT /DELAY:secs

To see a list of command options type tsshutdn /?

DELAY is in seconds.

  • 30 minutes = 1800 seconds
  • 1 hour = 3600 seconds
  • 2 hours = 7200 seconds
  • 3 hours = 10800 seconds