Oops Null Pointer

Java programming related

Scala and Eclipse Luna – installed but no scala perspective or tooling

I just installed a fresh Eclipse Luna (J2EE) on Linux Mint and then installed the Scale IDE (version 4 for Scala 2.11.1) from the nightly build and after the restart there was no sign of Scala. However the error log was complaining about the Scala components, so they were installed… somewhere…

I restarted Eclipse with -clean and after this (and some tweaks to the Eclipse ini file) it all works fine. I didn’t try the milestone build so I can’t comment if it shares the same issues.

ScalaIDE_EclipseLuna

Gerkin evaluation engine

An workmate from a previous job posted this article discussing the release of a Gerkin (Given-When-Then) evaluation engine. Interesting idea. I figure that JBehave has a built in interpreter and maybe this could be leveraged for that project to switch from JBehave to another testing framework at will.

Wiring Restlet JaxRs application with Spring

When wiring a JaxRs application from Spring I got most of the way using this post. My issue was different as  I already had a bean ref context for the rest of my application that I wanted to get my bean from.

I also wanted to respect the scope of the objects in the Spring configuration files and the example seemed to be always creating new instances when calling beanFactory.createBean(jaxRsClas).

The key was to convert the class name into a string and call beanFactory.getBean(classAsName) and then casting the bean it’s (generic) type.

import org.apache.commons.lang.StringUtils;

/**
 * Uses application content and bean ref context in web.xml to wire each new object from Spring.
 */
public class SpringRestletServlet extends ServerServlet {

  private static final Logger logger = Logger.getLogger(SpringRestletServlet.class);

  /**
   * Override the createApplication method to inject Spring objects
   * @param context - Restlet context
   * @return - application instance
   */
  @Override
  public Application createApplication(Context context) {
    JaxRsApplication app = (JaxRsApplication)super.createApplication(context);

    // set up spring to be the object creator
    app.setObjectFactory(new SpringObjectFactory(getWebApplicationContext().getAutowireCapableBeanFactory()));
    return app;
  }

  private static class SpringObjectFactory implements ObjectFactory {
    private final AutowireCapableBeanFactory beanFactory;

    public SpringObjectFactory(AutowireCapableBeanFactory beanFactory) {
      this.beanFactory = beanFactory;
    }

    @Override
    public T getInstance(Class jaxRsClass) throws InstantiateException {
      logger.debug( "Wiring JAX-RS class [" + jaxRsClass + "] using Spring." );

      String classAsName = StringUtils.uncapitalize(jaxRsClass.getSimpleName());
      return castBeanToType(classAsName);
    }

    @SuppressWarnings("unchecked")
    private T castBeanToType(String classAsName) {
      return (T)beanFactory.getBean(classAsName);
    }
  }

  private WebApplicationContext getWebApplicationContext() {
    return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
  }
}

And here is the related parts if the web.xml file that use the bean ref content that is on the classpath to get beans used by the rest of the application:


<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<context-param>
 <param-name>locatorFactorySelector</param-name>
 <param-value>classpath*:beanRefContext.xml</param-value>
</context-param>

<context-param>
 <param-name>parentContextKey</param-name>
 <param-value>keyInBeanRef</param-value>
</context-param></em>

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Restlet adapter -->
<servlet>
 <servlet-name>RestletServlet</servlet-name>
 <servlet-class>com.company.SpringRestletServlet</servlet-class>
</servlet>

<!-- Catch all requests -->
<servlet-mapping>
 <servlet-name>RestletServlet</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>
<pre>

Is REST over or just re-dressed?

I just read Steve Klabnik’s REST is over article which, while not as dramatic as the title suggests, pitches the admirable idea of renaming “real” REST services to Hypermedia APIs. I like the idea as I have felt and seen reoccurring confusion caused by (mis)understanding what representation state transfer encompasses. Steve’s main point is that the term Hypermedia clearly excludes certain types of REST-ish APIs. It certainly took me a little while to let go of idea of invoking operations and instead focus on changing the state of entities resources.

Steve’s article was also a call to stop arguing and start building. I saw the argument as a symptom of the confusion around focusing on the best way to live up to REST-ful principals. There is clearly a love of getting things done http with (typically) human readable message formats. I’m more inclined to peek at systems via their REST / JSON interfaces to quickly gather information rather than going via a client.

We think we still need a bite sized term for Hypermedia API; I like HAPI. Adding a little developer happiness can’t be argued with, right?

Book review: ExtGWT Rich Internet Application Cookbook

ExtGWT cookbook

Overview:

The ExtGWT Cookbook from Packt Publishing covers the GXT UI framework version 2 (currently at 2.2.6) as well as a quick overview of key features of GWT and some ideas on building robust UI frameworks. While a good knowledge of GWT itself is recommended, this book could be used a basic primer of some of the required concepts.

I think it’s important to bring up that the version of GXT this book covers is now a major revision behind the latest: GXT 3.0.

A lot has changed in GXT 3.0 and thus some of the detail in the Cookbook is not applicable to projects using this version. Some examples are:

  • The GXT data model (BeanModel) is now replaced by POJOs (using GWT AutoBeans)
  • Wigets can now use UIBinder
  • GXT Event listeners are replaced by GWT handlers
  • The flash charts are replaced by a new JavaScript based chart package

So with that in mind, who is this book useful for? The GXT widgets are relatively unchanged and this book covers the set-up and options of nearly all the widgets in detail and is still helpful from this aspect. The book also covers some useful ideas for building robust UI frameworks; it explores the idea of an “action” that different views can share, each which their own set of supported options. For those who have existing investments in applications using GXT 2 or are just starting projects will find this book a great companion – I wish I had this book during the creation of my current application as it would of helped clarify some of the quirks in GXT and replace the rather lack lustre GXT documentation. It also would of added some depth in the architecture, reducing repetition and linking related components.

Layout:

The ExtGWT Cookbook’s layout is code heavy – in a good way. Each recipe follows the format of a description of a feature, a “how do do it” code section and a detailed “how it works” line by line breakdown of the code. In this way you are rarely left wondering what a line of code does and any GXT quirks are normally highlighted and explained.

The progression of the examples builds well through each chapter and in the same way the chapters tend to naturally flow on from one another, so the book can be read linearly, but each chapter is self contained enough that you can jump into a recipe with little reference to other sections.

The main documentation that GXT provides is the API and a set of examples with code. While better than nothing, they often are not enough to cover real life scenarios or they leave important options out. The ExtGWT Cookbook has more examples with a focus on building practical applications, and importantly the examples are detailed and backed by a comprehensive description.

I felt the detail and relevance was typically compelling enough to encourage reading through whole chapters. Nearly ever chapter covered a new aspect or feature that I hadn’t previously been aware of, making reading this book worthwhile.

Chapters:

The first three chapters cover the basic elements of panels, windows, tabs and “click-ware” (buttons, toolbars and menus). The section on tabs shows real world experience in the example which toggles the tab strip based on the number of tabs. The last recipe of chatper 3 “Bind a single action to several click-wares” introduces a useful pattern to relate an underlying action or behaviour to different representations that is elaborated and expanded in Chapter 11 in the recipe “A novel UI with MVP, actions and a bus”.

The next four chapters cover GXT specific layouts, trees and grids with enough thoroughness to provide a good head start in using these components.

Chapter 8 covers the GXT templates which allow the modification of views within components with easy access to model data properties. This was a topic that I found GXT’s own documentation was particularly unhelpful, while this chapter succinctly covered with several handy examples.

The built in charts are well covered in Chapter 9, although few would find this flash based package very useful (especially for mobile devices). The last recipe “Drawing on a canvas” hints at alternatives, but for detailed charts look to GXT 3 or an external library.

The chapters “Drag-and-drop”  (chapter 10) and “Theming” (chapter 12) adequately explain these topics and the appendices provide thoughtful coverage of topics that GWT developers need to know.

I found the “Advanced Tips” chapter (11) the most useful and insightful as it covered important architectural concepts for larger applications (MVP, History, working with persistence frameworks). Having gone through the pain of building similar patterns before they were more commonly discussed for GWT projects, I like the way this chapter helps lay the foundations of testable and robust GWT based software. The working server push recipe was also stimulates thoughts on building more interactive applications.

Summary:

In all the ExtGWT Cookbook is a very detailed and comprehensive overview of GXT 2.x. It inspired me to read the whole thing cover to cover and I only really didn’t learn something from the charts chapter as we have moved to Highcharts (and GXT has moved to a new JavaScript based charting framework in GXT 3).

There are some aspects of GXT’s layout system that were (for me) difficult to deal with that are not covered in this book – in particular deeply nested layouts and using components in resizable windows (say a grid in a portlet in a resizeable window). The GXT documentation avoided complicated layouts and the cookbook’s layouts are typically not too complicated either. This book is aimed at the beginner to intermediate as it lacks some depth in relation to constructing large / complicated GXT based applications, but it still has enough detail to learn some useful tips from.

I’d look forward to a GXT 3 version by the same authors, especially if it covered some more advanced patterns.

Quick guide to transforming XML with XSLT

As a reminder to myself for a quick pattern for transforming XML via XSLT:

Some setup:


public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
public static final String XSL_HEADER = "<xsl:stylesheet version=\"1.0\" " +
    "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
public static final String ID_XFORM = "<xsl:template match=\"@*|node()\"><xsl:copy>" +
    "<xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template>";

public static final String TRANS_ATTR = "<xsl:template match=\"ATTR_PATH@ATTR_NAME\">" +
    "<xsl:attribute name=\"ATTR_NAME\">ATTR_VALUE</xsl:attribute></xsl:template>";
public static final String XSL_END = "</xsl:stylesheet>";
public static final String XFORM_ATTR_XSL = XML_HEADER + XSL_HEADER + ID_XFORM + TRANS_ATTR + XSL_END;

And a usage sample transforming an attribute’s value:

String xsl = XFORM_ATTR_XSL.replace("ATTR_PATH", "");
xsl = xsl.replace("ATTR_NAME", "myAttribute");
xsl = xsl.replace("ATTR_VALUE", "newValue");

ByteArrayInputStream xslInputStream = new ByteArrayInputStream(xsl.getBytes("UTF-8"));
FileInputStream xml = new FileInputStream(new File("myXmlFile");
InputStream transformedXml = null;

try {
    transformedXml = transformXml(xml, xslInputStream);
}
finally {
    if (xml != null) xml.close();
}

and the transform method:

private InputStream transformXml(InputStream xml, InputStream xsl) throws TransformerConfigurationException, TransformerException {

    TransformerFactory factory = new org.apache.xalan.processor.TransformerFactoryImpl();
    Transformer transformer = factory.newTransformer(new StreamSource(xsl));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(xml), new StreamResult(baos));
    return new ByteArrayInputStream(baos.toByteArray());
}

ExtGWT Cookbook Review in progress

I’m in the process of reviewing a GXT book called ExtGWT Rich Internet Application Cookbook.

Hopefully I’ll pick up a few tips on the way. It will be interesting to compare it to a book a did a technical review for around a year ago: Ext GWT 2.0 Beginners Guide.

XStream 1.3.1 to 1.4.3 ReflectionConverter changes

Recently while upgrading to Java 7 I had to upgrade XStream due to (I think) Oracle changing the name of the JVM or reflection providers. My converters that subclass the ReflectionConverter class started to fail. I had been omitting fields and then manually marshalling them in by overriding the marshall and marshallFields methods.

The 1.3.1 version of the RelectionConverter (in AbstractConverter) passed all non transient fields to the marshallField method. But in 1.4.3 it now checks if the fields should be omitted and will not pass omitted fields through to the marshallField method.

While mine was a relatively unusual case I hope this post can help others out stuck on similar issues.

C# Error Serialising IList

Recently I hit an error trying to Serialize a IList (with the actual type being an ArrayList) in C#. The only error I could get was:

System.InvalidOperationException: There is an error in XML document

which was not particularly helpful. As far as I can tell the System.Xml.Serialization.XmlSerializer can’t serialise IList as the root node, but has no issues if the IList is a child node.

I found one workaround here that uses a IXmlSerializable wrapper for IList.

(I tried to use a z in serialise when it refers to a method, but couldn’t bring myself to break with Australian English for other uses!)

EasyMock Generic(ish) Collection Matcher

I wanted to check that a collection is basically the same as another without knowing the collection type. I can only really check that the same elements are present, like a basic set.

The code below is based on the code here but with generics where possible (the matches method is not generic in EasyMock 2.5):


private static class CollectionMatcher<T> implements IArgumentMatcher {
    private Collection<T> collection;
    private String notFound = "";

    public CollectionMatcher(Collection<T> collection) { this.collection = collection; }

    public static <T> Collection<T> collectionEq(Collection<T> collection) {
        reportMatcher(new CollectionMatcher<T>(collection));
        return null;
    }

    public void appendTo(StringBuffer buffer) {
        buffer.append(notFound).append(" not found in {");
        String comma = "";
        for (Object o : collection) {
            buffer.append(comma).append(o.toString());
            comma = ",";
        }
        buffer.append("}");
    }

    public boolean matches(Object otherCollection) {
        if (!(otherCollection instanceof Collection)) return false;

        Collection<T> other = (Collection<T>) otherCollection;
        if (other.size() != collection.size()) return false; // Optionally check sizes
        for (T o : other) {
            if (!collection.contains(o)) {
                notFound = o.toString();
                return false;
            }
        }
        return true;
    }
}

Follow

Get every new post delivered to your Inbox.