Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

This is what you get when you try to test code that relies on Java EE APIs. You can compile the code because the javaee;javaee-api;5 dependency has stubs for the methods. Unfortunately, it does not contain implementations for the methods which causes your tests to blow-up. You get around this by including the implementation BEFORE tha javaee5 dependency.

Glassfish 2

<dependency>
     <groupId>net.java.dev.glassfish</groupId>
     <artifactId>glassfish-persistence-api</artifactId>
     <version>b32g</version>
     <scope>test</scope>
</dependency>

Glassfish 3

<dependency>
     <groupId>org.glassfish.extras</groupId>
     <artifactId>glassfish-embedded-all</artifactId>
     <version>3.0.1</version>
     <scope>test</scope>
</dependency>

April 9th, 2012

Posted In: Java, JUnit, Maven

Leave a Comment

We added Springframework to a legacy application. We encountered places where we needed access to Springframework beans by non-Spring beans. The Spring context was loaded by:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

The appeared to work fine, but what we did not realize until we looked at the logging was that the Spring context was refreshed every time this was called. This is a no-no when you have over 1000 classes under packages to be scanned.

The correct way is by:

ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

This worked for the Servlet API objects. We had some POJOs that did not have access to the Servlet API. We were already performing special processing via a filter to add various objects to the current context by using ThreadLocal.

April 4th, 2012

Posted In: Java, Spring

Tags: ,

Leave a Comment

Normally, I have been able to generate a Soap client and be on my way. This particular time I had to perform some business logic on the client. One of the first things that I encountered was the nuisance of debugging classes without a toString() implementation. It’s no biggie, but it does waste time when you have to open a class to inspect it’s contents. When it has a toString() implementation, your editor will generally use this to display the class. Making one less step that I have to perform.

The business logic I had to implement involved splitting a list of items into two groups, according to the value of one of the properties. In addition, if the object was already in the list, but had a different address, I had to add the current address to the existing object instead of adding it again. Great. Now I need to be able to identify an object in a list. I found that JAXB-2 Basics added implementations of equals() and hashcode() methods.

Just when I thought I was done, I had to add the serializable interface because the web application is marked as distributable. This wasn’t provided by JAXB2-Basics and I had to use an XML binding to add that.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.appriss.jxportal</groupId>
        <artifactId>jxportal</artifactId>
        <version>47.0-SNAPSHOT</version>
    </parent>

    <artifactId>tlo-client</artifactId>
    <name>tlo-client</name>

    <properties>
        <cxf.version>2.5.2</cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <!--
            Spring dependencies - needed to force a newer version than CXF used
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.6.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                        <configuration>
                            <sourceRoot>${basedir}/target/generated-sources/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <!--note: Started getting errors by having this hit their URL directly-->
                                    <wsdl>https://webservice.tlo.com/TLOWebService.asmx?wsdl</wsdl>
                                    <!--<wsdl>${basedir}/src/main/resources/TLOWebService-20120306.wsdl</wsdl>-->
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/resources/binding.xjb</bindingFile>
                                    </bindingFiles>
                                    <extraargs>
                                        <extraarg>-xjc-XtoString</extraarg>
                                        <extraarg>-xjc-Xequals</extraarg>
                                        <extraarg>-xjc-XhashCode</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

To use JAXB2-Basics, I added the module dependency starting at line 52. I also added a dependency for the cxf-codegen-plugin at line 93. After that, I had to add the at line 83.

  • -xjc-XtoString – triggers the generation of the toString() method
  • -xjc-Xequals – triggers the generation of the equals() method
  • -xjc-XhashCode – triggers the generation of the hashcode() method

The jaxb binding took a bit more work, but was fairly easy also. I specified the binding file on line 80. ${basedir} is a maven variable that is set to the directory of the module. The binding file is fairly straight forward. I specified a global binding that said serializable. I also specified the optional uid. If the uid is specified, then the value will be specified in the classes. NOTE: that the same uid value will be specified for all classes.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:bindings>
        <jaxb:globalBindings>
            <jaxb:serializable uid="1"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

April 3rd, 2012

Posted In: Java, Maven, xml

Tags: , ,

Leave a Comment

I have a JSP tag file named collapsiblePanel with the following attribute declaration for a JSP fragment.

<%@ attribute name="content" required="true" fragment="true"
              description="Passed as fragment, renders content1 inside of panel." %>

I am using the tag around some output to create a collapsed panel.

                <tags:collapsiblePanel id="principalsSection"
                                       heading="Principals (${fn:length(business.principals)} Found)">
                    <jsp:attribute name="content">
                        <c:if test="${not empty business.principals}">
                            <div class="jx-container">
                                <table class="jx-table">
                                    <thead>
                                        <tr>
                                            <th>Name</th>
                                            <th>Title(s)</th>
                                            <th>Address</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <c:forEach var="principal" items="${business.principals}" varStatus="status">
                                            <tr class="${jxp:rowClass(status)}">
                                                <td>
                                                    ${principal.name.firstName} ${principal.name.middleName} ${principal.name.lastName}
                                                </td>
                                                <td>${principal.title}</td>
                                                <td>${jxp:tloAddress(principal.address)}</td>
                                            </tr>
                                        </c:forEach>
                                    </tbody>
                                </table>
                            </div>
                        </c:if>
                    </jsp:attribute>
                </tags:collapsiblePanel>

The collapsible panel adds an arrow before the heading and makes the arrow and the headging a clickable area that will trigger the uncollapse. I did not want to add the arrow or the clickable area if there wasn’t anything to uncollapse. I tried to test for this by saying

<c:if test="${empty content}">
     ...
</c:if>

This is supposed to work, but in Glassfish 2.1.1 content contains the reference to the content fragment. I worked around this by:

<jsp:invoke var="content" fragment="content"/>
<c:if test="${empty content}">
     ...
</c:if>

This did the trick. I evaluated the JSP fragment and placed the result in a variable also named content. I was able to then act on it like a normal attribute.

April 3rd, 2012

Posted In: Java, JSP

One Comment

LinkedIn Auto Publish Powered By : XYZScripts.com