Externalize JSF project stage

Java ServerFaces (JSF) has a configuration option that should be set to Development in development and Production in production:


<context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
</context-param>

Unfortunately it is defined in web.xml, so the binary file installed in development must differ from the binary file in production. Find and replace at build time works, but it is cumbersome and prone to errors.

The ideal solution would be to use system properties, but JSF does not support that. However, JSF can use JNDI lookups! Why they would support something as complex as JNDI in place of something as simple and straightforward as system properties beats me, but there you are.

To use a JNDI lookup, add the following to web.xml instead of the context parameter:


<resource-ref>
  <res-ref-name>jsf/ProjectStage</res-ref-name>
  <res-type>java.lang.String</res-type>
  <lookup-name>JsfProjectStage</lookup-name>
</resource-ref>

The lookup name can be defined in the application server. If it is missing JSF will warn, but defaults to production mode.

Categories: Java

Chocolatey brings package management to Windows

Finally there is a package manager like apt-get or yum for Windows! Chocolatey can install and upgrade applications with simple commands, very similar to the Linux equivalents. For example, install the native Windows docker client with choco install docker and keep it up to date with choco upgrade docker. Highly recommended.

Categories: Windows

Workaround for WAS NoClassDefFoundError with JAX-WS

If you have to use IBM’s JDK with WebSphere Application Server and like to unit test web services outside of the container, you may encounter:


java.lang.NoClassDefFoundError: com.ibm.ffdc.Manager
  at com.ibm.ws.ffdc.FFDCFilter.processException(FFDCFilter.java:82)
  at com.ibm.ws.webservices.engine.components.logger.LogFactory$2.run(LogFactory.java:159)
  at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:63)
  at com.ibm.ws.webservices.engine.components.logger.LogFactory.createLogFactory(LogFactory.java:141)
  at com.ibm.ws.webservices.engine.components.logger.LogFactory.(LogFactory.java:98)
  at java.lang.J9VMInternals.initializeImpl(Native Method)
  at java.lang.J9VMInternals.initialize(J9VMInternals.java:205)
  at com.ibm.ws.webservices.engine.soap.MessageFactoryImpl.(MessageFactoryImpl.java:103)

What is that? A quick search indicates that the server administration client jar is missing from the classpath, but why would I need that? I just want to publish a simple JAX-WS web service.

Fortunately there is a workaround. Set a system property:


  System.setProperty("javax.xml.soap.MetaFactory",
    "com.sun.xml.internal.messaging.saaj.soap.SAAJMetaFactoryImpl");

This forces the use of Sun’s web service stack and the problem is gone. When the code runs in the container the classes are available and in this way we can get the unit tests to work.

Categories: Java

Basic authentication with JAX-WS in Java SE

With JAX-WS web services it is common to use preemptive basic authentication. It is also common to test the services stand-alone using the built-in support in Java SE. Unfortunately that makes it hard to test the authentication and no principal will be available on the server side. What to do?

It turns out that it is fairly easy to enable authentication after all. I found this example from 2007. In short the endpoint is published using Sun’s built-in web server.

This publishes the web service ExampleWebService on a random port with a hard-coded user and password:


HttpServer webServer = HttpServer.create(new InetSocketAddress(0), 1);
webServer.start();
HttpContext context = webServer.createContext("/");
context.setAuthenticator(new BasicAuthenticator("ExampleRealm") {
  public boolean checkCredentials(String username, String password) {
    return "testuser".equals(username) && "testpassword".equals(password);
  }
});
Endpoint endpoint = Endpoint.create(new ExampleWebService());
endpoint.publish(context);

Don’t forget to stop the web server after the test. Sun’s web server may not always be available in production, but it is most likely there in the unit test environment.

Categories: Java

Java EE two-factor authentication with Yubikey

Tonight I’m giving a presentation at JavaForum in Gothenburg on two-factor authentication in Java with Yubikey. The talk will introduce multi-factor authentication in general and Yubikey in particular; then it shows how to add Yubikey support to several types of Java web applications. It starts out with a simple servlet filter and proceeds with JAAS modules, a JASPIC (JSR-196) module and Apache Shiro.

More details can be found here. The code is available on Github.

Categories: Java

Zoom in and out in Eclipse

Do you use Eclipse for presentations? In that case I bet you want to zoom in and out in order to handle different resolutions. That feature is not built in. I found a good answer at StackExchange.

In short, select "Help/Install New Software" and add http://eclipse-fonts.googlecode.com/svn/trunk/FontsUpdate/ as a site to "Work with". Pick the FontsFeature and install it. There should be two toolbar buttons for zooming in and out and the keyboard shortcuts Ctrl+- and Ctrl+= can be used as well, unless Eclipse has reserved them for something else.

Categories: Java

JMeter with Graphite

JMeter, probably the most popular open-source tool for load testing, can now report test results to Graphite! The Graphite listener is not part of the latest release (2.12), but it is included in the nightly build and it works quite well. It can report summary statistics for the entire test run or statistics for specific samplers with successes and failure counts, percentiles and so on. Highly recommended, in particular if the system under test also reports statistics to Graphite!

Categories: Performance
Follow

Get every new post delivered to your Inbox.