IntelliJ Live Template Tip – Autowired Dependencies

Do you type these lines a lot?

@Autowired
private MyAwesomeService myAwesomeService;

Then try adding a Live Template to your IntelliJ settings that helps you reduce the number of keystrokes. Go to your Settings (Cmd+, on Mac or Ctrl+Alt+S on Windows) and find the Live Templates panel. On the right, select the ‘other’-group and click on the plus sign to add a new template.

Give the template a name (I chose auto and a description, then make sure it has a context of Java Declarations and the window looks somewhat like this:

live_template

Click ‘Edit Variables’ and make sure the TYPE variable has the expression set to expectedType() (this will open IntelliJ’s type completion box when the template activates). Then set the expression for VAR_NAME to suggesteVariableName() (this whill cause IntelliJ to suggest a name of the variable when you select the type).

template_vars

Now click Ok and close the Settings dialog. Go to your class file and at the place of an instance variable declaration, start typing auto and wait for the popup to show. Press Enter, type the name of your type and press Enter again, you should have a beautiful declaration.

–JH

Spring Context Mix-Ups

While trying to add caching to a bean in my client’s project, I ran into some strange behaviour. The project already used caching on several components and used the @Cacheable annotation with an EhCache backing store. I added a cache definition to the ehcache.xml file and put the @Cacheable annotation on the method, recompiled, tested… No caching.

Update 2015-01-26: I added a little trick to mitigate this situation if you run into it. See below for the fix.

Confused, I cleaned, recompiled again, restarted, tested. Still no caching. My first instinct was: maybe caching is improperly configured and the other beans also have no active caching, even though we think they do. So I started debugging, to see if the calls to the other beans did use caching. To my surprise, they did. Refreshing the page did not cause those methods to be called again, but mine were.

Some further debugging showed that the SpringCacheAnnotationParser did process my bean and was able to see the @Cacheable annotation. However, when debugging the method that was supposed to be cached, I noticed there were no references to any CacheInterceptor instances in the stack trace. Even though during startup my bean was replaced by a generated proxy.

This raised a suspicion that maybe I was looking at two instances of my bean. A breakpoint on my bean’s constructor revealed that indeed, it was called twice. The cause became pretty obvious when I examined the Spring configuration files:

applicationContext.xml

<beans ...>
  ...
  <context:component-scan base-package="com.our.app"/>
  ...
</beans>
servlet-context.xml

<beans ...>
  ...
  <context:component-scan base-package="com.our.app"/>
  ...
</beans>

Aha! It appears our Spring context is loading all classes annotated with @Component, @Service, etc. twice! Once on the startup of the application and again when the first request comes in for the dispatcher servlet. Now, the reason that caching worked on some, but not all beans, lied in the fact that caching was configured in the applicationContext.xml file, which applied caching wrappers to all beans in that context. There was no caching configured in servlet-context.xml, causing beans loaded there to not have caching wrappers.

Our Java EE Servlet Filters were loaded from the application-context.xml, which contained caching. Those were fine. But any requests coming from the dispatcher servlet would reach beans in the servlet-context.xml context, which did not have caching.

The solution was quite a lot of work, since we had to re-evaluate all annotated classes and decide whether they should live in the application context, or the dispatcher servlet context. The rule of thumb we used here was:

Classes annotated with @Component and @Service will be loaded in the application context. Classes annotated with @Controller and its derivatives will be loaded in the dispatcher servlet context.

I realize there may be more subtle cases where this rule does not fully apply, but it’s a good starting point. Generally you want any potentially shared beans in the application context and anything specifically tied to the dispatcher servlet in its context. At least take this advice to heart:

Never, ever use component-scan on the same package tree from different contexts

Update 2015-01-26: If you have this same problem and are unable to move your controllers to a separate package tree, you can use a little configuration option of Spring’s component scan. You can use include and exclude filters to filter the @Controller annotations out of the application context and only include them in the servlet context. The snippet below filters out the controllers, you should use this in you application context:

<beans ...>
  ...
  <context:component-scan base-package="com.our.app">
    <:context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
  ...
</beans>

The same with annotations:

@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})
@Configuration
public class MyConfiguration {}

Then reverse the filter on the servlet-config and you have separated your stereotypes. Note that I believe this is more of a hack than a sound application architecture, but in legacy applications you may not have much of a choice…

–JH

PS: This post talks about @Cacheable, but the same behaviour might be triggered by things like @Transaction and other bean post processing features in a context.

Returning Groovy’s GStringImpl in a Jersey endpoint

I started playing around with some technologies to try and increase my productivity when building RESTful APIs. The current setup I am using has Groovy, Spring 4, Jackson 2 and Jersey 2. The project is built by Gradle and runs on the Google AppEngine. A post describing the way this project was setup will follow shortly. I just wanted to share a little piece of code with the world first.

The above setup works nicely for JSON objects and HTML and things like that. I just ran into a little snag when I tried to get a little test running:

class ApiInfo {
    int count = 1;

    @GET
    hello() {
        "Hello world ${count}"
    }
}

This resulted in an Error 500, with the following problem as its cause:

SEVERE: MessageBodyWriter not found for media type=text/plain, type=class org.codehaus.groovy.runtime.GStringImpl, genericType=class java.lang.Object

At first it looks strange that a ‘normal’ String cannot be serialized to text/plain, until you realize you’re working with Groovy and Java’s String is final. So we apparently need a MessageBodyWriter to support this:

import groovy.json.internal.Charsets
import org.codehaus.groovy.runtime.GStringImpl

import javax.ws.rs.WebApplicationException
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.ext.MessageBodyWriter
import javax.ws.rs.ext.Provider
import java.lang.annotation.Annotation
import java.lang.reflect.Type

/**
 * Adds support for Groovy's GStringImpl as a Jersey output.
 * LICENSE: Do-whatever-you-want-but-attribution-would-be-appreciated.
 *
 * @author J.H. Kuperus (www.jhkuperus.nl)
 */
@Provider
class GStringImplMessageBodyWriter implements MessageBodyWriter< GStringImpl > {

    @Override
    boolean isWriteable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        GStringImpl.isAssignableFrom(type) && mediaType == MediaType.TEXT_PLAIN_TYPE;
    }

    @Override
    long getSize(GStringImpl gString, Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        gString.length()
    }

    @Override
    void writeTo(GStringImpl gString, Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap< String, Object > httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
        entityStream.write(gString.getBytes(Charset.forName('UTF-8')))
    }
}

Feel free to use this piece of code, send me remarks or correct any mistake(s) 😉

–JH

Is Spring Boot rebooting Spring’s Ease of Use?

Spring started out as a ‘small’ Dependency Injection container, which gave the Java world a whole new direction. Especially on the area of Java Enterprise, it offered innovations which allowed for faster and easier development of new Java applications.

In the last few years, Spring has grown enormously as a framework. The basics are still relatively simple, but due to the large number of different extensions it can be hard to start a new project. The people at Spring are familiar with this situation and are trying to remedy it with its newest addition: Spring Boot.

Continue reading

Using Apache CXF to connect to Microsoft Dynamics

A few weeks back a friend of mine approached me with a request. His client was building an application which was supposed to connect to their Microsoft Dynamics CRM. However, they were unable to get any decent response from the CRM Server and needed a little help. My friend and I took up the task of digging into this and solved it, using the Apache CXF framework (which the client was using). In this post, we will explain how to do this. (If you’re just looking for the solution, jump straight down)

Update 17-06-2014: Marnix Klooster used the stuff from this post and elaborated on it over at stackoverflow. Could be a good next step if the stuff below doesn’t help you.

WSDL Analysis & Authentication

The strange thing was that the web service seemed to not respond at all, to any sort of SOAP request, whether faulty or correct. This appears to be Microsoft’s way of saying ‘Hey, you need to authenticate before you can ask me anything’. Not really standard, but I guess we have to live with it.

The documentation of these services leave some things to wish for, so we were forced to delve through the WSDLs and use SoapUI to throw a lot of requests at the services. Eventually the STS authentication service replied with a valid security token. But, Apache CXF seemed to be unable to handle this response. Analysis of the response showed that Microsoft returns an encrypted SAML token, instead of a publicly readable one. Apache CXF is only just able to handle this (see issue CXF-4357), so it takes a snapshot build to authenticate which has been fixed in the 2.6.2 release.

The solution

The solution itself is actually surprisingly simple. The most important part is having the most recent versions of the CXF libraries. The XML-fragment below shows the dependencies required for a Maven project. If you don’t use maven, download these libraries from Apache.

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>2.6.2</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>2.6.2</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-ws-security</artifactId>
    <version>2.6.2</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-ws-policy</artifactId>
    <version>2.6.2</version>
</dependency>

Most of the ‘magic’ happens in the Spring application context. In the following fragment, we glue together a JAX-WS client for the SOAP service and an STSClient which will take care of obtaining the SAML tokens. The context also includes a policy object telling the CXF framework that Microsoft’s extensions on the WS-Security policy can be ignored. Without this little piece, CXF will reject the configuration, because it cannot parse those policies. Let’s have a look:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:jaxws="http://cxf.apache.org/jaxws"
  xmlns:cxf="http://cxf.apache.org/core"
  xmlns:policy="http://cxf.apache.org/policy"
  xmlns:security="http://cxf.apache.org/configuration/security"
  xmlns:http="http://cxf.apache.org/transports/http/configuration"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <jaxws:client name="{http://schemas.microsoft.com/xrm/2011/Contracts}CustomBinding_IOrganizationService" createdFromAPI="true">
    <jaxws:properties>
      <entry key="ws-security.sts.client"  value-ref="crm.sts-client" />
    </jaxws:properties>
  </jaxws:client>
  
  <bean name="crm.sts-client"  class="org.apache.cxf.ws.security.trust.STSClient">
    <constructor-arg ref="cxf"/>
    <property name="wsdlLocation" value="https://adfs.example.com/adfs/services/trust/mex"/>
    <property name="serviceName"  value="{http://schemas.microsoft.com/ws/2008/06/identity/securitytokenservice}SecurityTokenService"/>
    <property name="endpointName"  value="{http://schemas.microsoft.com/ws/2008/06/identity/securitytokenservice}UserNameWSTrustBinding_IWSTrust13Async"/>
    <property name="properties">
      <map>
        <entry key="ws-security.username" value="user1"/>
        <entry key="ws-security.callback-handler" value="net.quies.demo.sts.HardcodedPassword"/>
      </map>
    </property>
  </bean>
  
  <!-- Skip Microsoft extensions -->
  <bean class="org.apache.cxf.ws.policy.IgnorablePolicyInterceptorProvider">
    <constructor-arg>
      <list>
        <bean class="javax.xml.namespace.QName">
          <constructor-arg value="http://schemas.microsoft.com/xrm/2011/Contracts/Services"/>
          <constructor-arg value="AuthenticationPolicy"/>
        </bean>
      </list>
    </constructor-arg>
  </bean>
  
  <cxf:bus>
    <cxf:features>
    <!-- Enables policy support: -->
    <policy:policies/>
    <!-- Enables logging of SOAP messages. -->
    <cxf:logging/>
    </cxf:features>
  </cxf:bus>

</beans>

Note that the crm.sts-client bean contains the key information about the actual services invoked. The WSDL location, service name, endpoint and the username and password. These are all settings you should adjust to your particular environment. Note that it also references a class called HardcodedPassword, which is a very simple and insecure implementation of a CallbackHandler, providing CXF with the password:

public class HardcodedPassword implements CallbackHandler {
  @Override
  public void handle(Callback[] callbacks)
      throws IOException, UnsupportedCallbackException {
    for (Callback c : callbacks) {
      if (c instanceof WSPasswordCallback) {
        WSPasswordCallback passwordCallback = (WSPasswordCallback) c;
        passwordCallback.setPassword("passw0rd");
        continue;
      }
      throw new UnsupportedCallbackException(c);
    }
  }
}

Bringing it all together

With these beans setup, you’re ready to use the XRMServices in your code. Below is a little unit test to demonstrate how you can now create a CXF bus from the Spring context, create a service from it and call the port:

@Test
public void authenticate() throws Exception {
  URL wsdlLocation = new URL("https://xrmservices.exmple.com/XRMServices/2011/Organization.svc?wsdl");
  URL springLocation = new URL("classpath:applicationContext.xml");
  
  SpringBusFactory factory = new SpringBusFactory();
  Bus bus = factory.createBus(springLocation);
  BusFactory.setDefaultBus(bus);
  
  OrganizationService service = new OrganizationService(wsdlLocation);
  IOrganizationService port = service.getPort(IOrganizationService.class);
  
  Entity entity = new Entity();
  port.create(entity);
}

And that’s it. Calling the OrganizationService will now cause the CXF framework to first call the Token service to authenticate itself, obtain a token and use that as a SAML assertion in the resulting SOAP call. Heck, any service you now create from the Bus and that requires SAML authentication, will use the same setup transparently.

Happy coding!

–JH

Special thanks go out to Pascal de Kloe, from Quies.net.

GWT: Dynamically Changing CSS Styles

Are you building a Google Web Toolkit application? Has someone asked you to personalize colors or images for their account? If so, you may have tried to look for things like dynamic CSS in GWT as well. While Googling these terms, I could not find what I needed. After some more Googling and reading some not so helpful posts, I found a project titled Advanced GWT Components.

This project has a set of widgets which support switching themes at runtime. That was exactly what I was looking for, but I had no need for the widgets. So I picked the project apart and adapted the way it changes CSS on runtime to fit our application. In this post I will take you through the pieces of code we used to complete this. Note: this is going to be quite a long and code heavy post ^_^

Continue reading

The Road to an Agile Project – Visualize!

Hello and welcome back! In this series of posts I am trying to give other teams of developers out there some path to embracing agile methodologies, such as Scrum. Previously, in my Prequel post, I described how you could start doing regular standup meetings in any type of project to increase your teams awareness about each member’s work.

Assuming your team has now incorporated standup meetings, preferably on a daily basis, it is time to take the next step. Agile methodologies generally rely on a rather simple notice: Visualized Management! Now don’t get scared, the word ‘Management’ here does not mean you need another manager. When done right, this visualization can eliminate the need for a manager. Now doesn’t that sound sweet? :)

Continue reading

The Road to an Agile Project – The Prequel

Well hullo there! It has been quite some time since my last post. A lot has happened recently, some of which prevented me from posting regularly. Let’s just blame the current economic conditions for that.

Since my last post, we have pretty much finished our JavaFX project. I was at the NLJUG’s JFall where I hosted a JavaFX workshop all afternoon and I have been to Devoxx in Antwerp where I met Richard Bair and Jasper Potts from the Oracle JavaFX team.

I have also joined Stephen Chin in the maintenance of the Visage language. This is the continuation of the JavaFX Script language as an open source project (see Google Code).

Now shut it, you mentioned something about agile…

Continue reading

JavaFX’s largest annoyance

JavaFX is great for building user interfaces. There is however one large annoyance that is probably considered a feature. JavaFX swallows NullPointerExceptions, among others.

NPE’s are meant to signal development errors. In other words, if you see an NPE, you can call your developer and tell him he screwed up. Some developers even need them to be reminded of the pieces of an application they were working on.

Continue reading

JavaFX : Centering Controls

Across the blogosphere, I found a post on how to center controls by using binding. The proposed method certainly works, but there is a catch.

When you are doing your layout by binding the layoutX and layoutY properties of your controls, you may experience a degradation of performance while resizing or scrolling. This will probably not be noticeable with only a handful of controls/shapes on screen. If however you start drawing hundreds of shapes/controls with this method, you will certainly see the slowdown. Let’s see why…

Continue reading