Tuesday Sep 08, 2009

Profiling an ANT process

I had an opportunity recently to take a look at what was causing a significant increase in the time required for automated builds. The build system is based on Ant so what I wanted to see was a list of the top targets and sub tasks, so I could easily find the performance hotspots. I was hoping to use a performance flag that would generate this information but I didn't see anything readily accessible that would give me the information I needed. 

After looking around the ant documentation, I found the BuildListener interface which provides notifications of build events (target started, target completed ...). What I wanted to do was to implement this interface to capture the starting and ending of tasks and targets so that I could generate an ordered list of the targets consuming the most time. I created a TimingBuildListener class that does the following:

  • keep a list of the x targets that take the most time. Each target maintains the list of tasks (and their times) so that decomposition within a target is possible.
  • manage the target lists according to the current stack of running targets.
  • provide thread safety to work with the parallel task. There isn't as much documentation on how events are generated so this code was written empirically based on how Ant 1.7.1 is implemented and works for the intended project, so you may have different results. 

Once I had this code, I got this working on a simple build file (adding a build listener only required adding the fully qualified classname of the listener using the -listener command line switch as well as the lib switch to specify the appropriate classpath for the class) and progressed to profiling the full build.

The profiling information identified a problem with the way we were compressing css and html files which was taking more than 50% of the build time (that includes unit tests), so we now get continuous build results in half the time :)

Friday Nov 14, 2008

How to pass a parameter into an XSLT stylesheet

First step is to set the parameter in Java. Here is some sample code:

TransformerFactory tf = TransformerFactory.newInstance();
File transform = new File("myTransform.xsl");
Transformer t = tf.newTransformer(new StreamSource(transform)); 
t.setParameter("aParam", "value");
t.transform(new StreamSource(new File("input.xml")), new StreamResult(new File("output.xml")));⁞

Above is the basic code to get a stylesheet working, so performance could be improved by compiling the stylesheet using TransformerFactory.newTemplates. The Transformer.setParameter call is what actually passes the parameter into the stylesheet.

 The next step is to start using the parameter in the stylesheet. The parameter is treated just like an param element so this can be accessed by adding a param element as a child of the stylesheet element.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">

   <xsl:param name="aParam"/>
...

Now this can used within the stylesheet just like a parameter. 

Saturday Aug 25, 2007

NetBeans 6 Web Service Designer

NetBeans has introduced a Web Service designer, as you may have seen in Geertjan's blog. In addition to the ability to graphically view and modify operations for implementation first web services (WSDL is generated based on the JAXWS annotations), the web service designer provides the ability to work incrementally starting from a WSDL document. This represents a significant enhancement to the development experience as the WSDL document can become a first class source artifact, while the developer experience remains the same as the Web service designer provides the same implementation
 view. The add operation feature has also been extended to support the direct addition of XML schema. Direct addition of schema elements will perform an on demand generation of JAXB to provide a Java binding.


 

 Having the WSDL generated as part of deployment presents makes it difficult for composite application assembly tools, such as  the NetBeans CASA editor, to take advantage of the abstract WSDL binding capability of the openESB runtime. A composite application provides code composition based on well defined WSDL based interfaces. Since all the modules are deployed together and each WSDL interface is exposed on the service bus, developers can rely on bus endpoints instead of the concrete endpoints exposed in WSDL, thus achieving loose coupling as only the abstract WSDL is needed. The WSDL document can also be modified using the NetBeans WSDL editor to enhance the generated information such as adding documentation.

You will also notice the ability to see sample input and output messages. This is useful when working from Java types to see  how changes to the structure of Java classes will affect the XML messages.

This new feature is worth a look, you can find this in NetBeans 6.  




Tuesday Aug 21, 2007

Unit Testing HTTP Calls

I have been working with RESTful web services and had the need to verify the HTTP messages I was sending were appropriate. Since I am doing this work using Java SE 6, I was able to take advantage of the httpserver package. This package provides a lightweight HTTP server and is used for things like publishing web services and displaying memory graphs based on jmap.

The code to take advantage of this is easy. As you can see below, my strategy was to get an arbitrary open port on the local machine, bind a test handler to a context where the message is directed, and then verify what was sent matches what was expected.

 InetSocketAddress address = new InetSocketAddress(0); // get free port on local machine
HttpServer server =HttpServer.create(address, 1); // create a server on the arbitrary port
int portNumber = server.getAddress().getPort(); // get the port where this is bound for the invocation
       
MockHttpHandler handler = new MockHttpHandler(); // create a mock handler to capture the HTTP input
try {
    server.start(); // start the server
    server.createContext("/test",handler); // create a context with the mock handler
 ...
        } finally {
            server.stop(0); // stop the server after the test is complete
        }

Saturday Jun 16, 2007

Workaround to get more information for the JPA exception: During synchronization a new object was found through a relationship that was not marked cascade PERSIST.

I was getting a bit frustrated dealing with the JPA exception During synchronization a new object was found through a relationship that was not marked cascade PERSIST. The issue is that the message doesn't display the object in question. I wanted to grab the code and fix this issue, so here is what I did

  • download the persistence source from https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html. I am working with V1 so I used that version.
  • I copied the class I wanted to modify to my unit test area using the same package structure as in the persistence jar file. I am using NetBeans for unit testing so I adjusted the unit test classpath to put the test classpath ahead of the toplink essentials jar file which effectively replaces the class.
  • Looks like this is just a bug where the IllegalStateException thrown in RepeatableUnitOfWork line 115 (as we all know and love) is passing the object, so all that needs to be done is to fix the error message. I found ExceptionLocalizationResource where the error messages are stored and added a parameter to the new_object_found_during_comment message. I changed it to: "During synchronization a new object was found through a relationship that was not marked cascade PERSIST. {0}." This will provide the output of the toString method for the object where EntityManager.persist was not invoked. Since the toString is in the user space, this allows me to add any additional information to help figure out where to add CascadeType or call persist.

After doing this, additional information on which class (and the toString) is now contained within the exception message.

The glassfish tracking issue has been filed as https://glassfish.dev.java.net/issues/show_bug.cgi?id=3254 



Thursday May 10, 2007

Interesting Class in J2SE 6

I was looking at the Javadoc for Java SE 6 recently and I came across the SchemaLoaderFactory javadoc. The javadoc mentions this class was not supposed to be part of Java SE. 

Friday May 04, 2007

NetBeans REST support

Given that Java One begins next week, I wanted to mention some really interesting technology which is available in  NetBeans 6.0 (milestone 9 build). This preview release provides the ability to generate REST services based on a set of JPA entity beans, which you can also generate using NetBeans. The REST services are annotated using the annotations proposed in JSR-311 (so this will change by the time the JSR is finalized). JSR 311 removes the need for service developers to use the Servlet or JAX-WS and instead provides a runtime and a set of annotations to handle dispatching of requests to your REST enabled resources. This feature also requires installation of the Sun Web Developer Pack which contains Phobos (JavaScript application server), JMaki, and of course the RESTful web services supported.

The wizard determines the set of entity beans available in a project and allows a set of beans to be selected and exposed as resources. In addition to generating the rest resource code, the wizard generates a converter layer which provides the ability to marshal and unmarshal to and from the wire protocol. Currently, the converters are using custom XML to serialize data; however, JSON and ATOM publishing protocol are also being considered. Finally, the converter layer translates object references (such as JPA references) into URI references. All the code generated by NetBeans is freely editable (no guarded blocks) and only relies on the JSR-311 annotations.


Accompanying the wizard is the ability to generate a test client to exercise the generated resources. The test client supports interaction with resource collections as well as individual resources through GET, PUT, POST, and DELETE (depending on the options available via the resources). The test client is generated locall and uses XHR for communicating with the resources, which should mimic most common architectures.

This is best to see in action, check out the video

Tuesday Apr 24, 2007

JavaScript documentation generator

I came across the JSDoc project recently which extends Javadoc like concepts into Javascript. The documentation and the procedure for using this are straightforward if you are used to javadoc. 

Sunday Apr 08, 2007

Blueprints article about using JPA in a web module

I came across this article about using JPA in a web module. Using JPA in a web only module is more difficult than using JPA in EJB because of the differences in the threading models. Using a request scoped managed bean makes things a bit easier as the injection model is similar to EJB. 

Saturday Feb 24, 2007

Nice summary of monitoring features in J2SE

There is an interesting blog entry by Mandy Chung on the monitoring capabilities of J2SE. Worth a look to get an overview and references to other blogs describing how to use these cool features. If you are running on windows, you might want to get the pid from the task manager to use jstack. The processes tab has a column (by default hidden) for the pid. Turn the column by selecting the processes tab,then under the view menu option use the select columns action.

Sunday Feb 11, 2007

Difference between extends and super In generic collections

The guidelines I have been using to differentiate when to use extends v. super are the following:

  • Use extends if you need to read from the collection (i.e. List<? extends A>;). This will ensure that the collection itself contains items which extends A. This is read-only because there is no way to determine the exact type to add to the collection (the parameter could be List<B> and there would be no way of ensure the type safety of an addition).
  • Use super  if you want to write to the collection (i.e. List<? super A>;). In this case, the collection can support addition of A types as we know the specified type of the collection is a super class of A. Therefore, A typed items can always be added to the collection.


Here is some code to look at:

import java.util.List;

public class GenericTest {
   
    private void useExtends(List<? extends SomeInterface> l) {
        for (SomeInterface i:l) {
           
        }
       
        // below doesn't compile because the exact type of list isn't known
        l.add(new SomeInterface(){});
    }
   
    private void useSuper(List<? super SomeInterface> l) {
        // the for loop below doesn't compile as the List is one of the super
        // type of SomeInterface but unknown as to which one
        // Object could be used in the for loop
        for (SomeInterface i:l) {
           
        }
       
        l.add(new SomeInterface(){});
    }
   
    interface SomeInterface {}
   
}
 



About

cwebster

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today