Tomcat: Require authentication but no roles

Sometimes you might want to protect some parts of your Java web application from anonymous access, but not impose any authorization constraints: Every authenticated user should be automatically authorized, no matter what roles they have or don’t have.

Tomcat supports this by setting allRolesMode=”authOnly” on the Realm definition, usually in META-INF/context.xml, in combination with <security-constraint> entries in WEB-INF/web.xml that declare an <auth-constraint> with <role-name>*</role-name>.

JRuby on Rails 3.2

I currently have to work on an application using JRuby on Rails 3.2. My background is 12+ years of Java centric web development using Eclipse or IntelliJ.

There are a few things that I love about working with Java in Eclipse or IntelliJ that I miss when working with Rails:

  • Java’s backwards compatibility
  • Java’s cross-platform consistency
  • That Java has a static type system and is a compiled language
  • The relatively rigid syntax of Java that prevents idiosyncratic DSLs
  • Being able to control mutability, extensibility and visibility at compile-time (private, final)
  • Java’s distinction of interfaces vs implementation classes
  • Powerful auto-completion and Refactoring in the IDE
  • Powerful code inspections and quickfixes in the IDE
  • IDE and compiler feedback about mistakes and design flaws in my code
  • The powerful and rich ORM features of JPA and Hibernate

Dynamic language fans might cringe at some of the points listed above. Feel free to provide constructive feedback or well-reasoned criticism.

There are however a couple of things that I like about Ruby on Rails 3.2:

  • Dependency management using gems and bundler
  • A very standardized project layout (directory structure and conventions)
  • Code generation (as a convenient way to quickly create sample code)
  • Out-of the box integration and support for JQuery, CoffeeScript, SASS
  • Cool gems like colorbox-rails for turning a regular view into a modal dialog
  • The asset pipeline

I am still in the early phase of my Rails experience and I will try to post more about it as things evolve.

Project Jigsaw notes and links

Goals
– Help fix problems with the classpath
– Allow modularization of JDK/JRE
– Enable using JSE subsets
– Improve performance (startup time, download time, etc.)

OSGi was considered too complex and not well-suited for JDK modularization
But: Project Penrose tries to make sure that OSGi can be implemented on top of Jigsaw.

Project is in Phase 1 (exploration, prototyping)
Phase 2 (reference implementation) in very early stages

Project website
Big picture of the Design

A lot of work to do
Was recently deferred from Java 8 to Java 9

Challenges:
– Dynamic modularization
– JEE containers
– Needs a reflective API
– Fundamental changes, require a lot of QA (testing, etc.)

But Java 8 might already introduce a simplified way of using JSE subset:
“JSE Profiles”

Module descriptor: module-info.java
http://openjdk.java.net/projects/jigsaw/doc/lang-vm.html#jigsaw-1.1

Modules are versioned
Only one version of a module can exist

Repositories: HTTP server or local files

Packager to generate deb, rpm packages etc. from Java modules
translate the module metadata to package metadata

JDK modularization
http://openjdk.java.net/projects/jigsaw/doc/jdk-modularization.html

Backwards compat:
Classpath-mode (legacy), Module mode

But: No more dependecies on internal classes (e.g. sun.misc.*) will be possible
No more rt.jar, tools.jar

A font chooser combobox for Swing or AWT components

Useful if you want to let the user choose the font to use for certain components of a Swing UI:

package net.doepner.ui.text;

import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class FontChooser extends JComboBox<Font> {

	public FontChooser(final Component... components) {

		final Font[] fonts = GraphicsEnvironment
				.getLocalGraphicsEnvironment()
				.getAllFonts();

		Arrays.sort(fonts, new Comparator<Font>() {
			@Override
			public int compare(Font f1, Font f2) {
				return f1.getName().compareTo(f2.getName());
			}
		});

		for (Font font : fonts) {
			if (font.canDisplayUpTo(font.getName()) == -1) {
				addItem(font);
			}
		}

		addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				final Font font = (Font) e.getItem();
				for (Component comp : components) {
					setFontPreserveSize(comp, font);
				}
			}
		});
		
		setRenderer(new FontCellRenderer());
	}
	
	private static class FontCellRenderer 
			implements ListCellRenderer<Font> {
		
		protected DefaultListCellRenderer renderer = 
				new DefaultListCellRenderer();
		
		public Component getListCellRendererComponent(
				JList<? extends Font> list, Font font, int index, 
				boolean isSelected, boolean cellHasFocus) {
			
			final Component result = renderer.getListCellRendererComponent(
					list, font.getName(), index, isSelected, cellHasFocus);
			
			setFontPreserveSize(result, font);
			return result;
		}
	}

	private static void setFontPreserveSize(final Component comp, Font font) {
		final float size = comp.getFont().getSize();
		comp.setFont(font.deriveFont(size));
	}
}

Java DSL for object-oriented type-safe CSS styling

I am looking for something like this Java-CSS-Library, to implement server-generated CSS stylesheets backed by a type-safe object-oriented model of CSS classes and rule sets.

This is part of my ongoing quest for the perfect “pure Java” web development framework that would allow me to

  • Focus on my strongest area of expertise: Elegant Java code
  • Use refactorings and other advanced Java tooling in Eclipse or IntelliJ Community Edition
  • Ignore the HTML/HTTP vs JVM objects impedance mismatch as much as possible

Sharing IntelliJ project configuration (.idea, *.iml) in version control

Me and my team do exactly what Christof Schablins describes in this blog post and what the Jetbrains folks recommend here: We use IntelliJ IDEA and share *.iml files (modules) and most files in .idea directory (project config) in the Version Control System (in our case CVS), including shared ant configs, shared run configs for Tomcat 6 and JUnit tests, shared project specific code inspection profile, shared data source definitions, etc.

Overall it works great. But we noticed a few things:

  • Needed an IntelliJ Path variable TOMCAT_HOME because devs have Tomcat installed in different places
  • Dialog “Do you want to add workspace.xml to CVS” keeps popping up (despite an entry in .cvsignore file within the .idea directory)
  • If devs have different sets of IntelliJ plugins enabled, each plugin adds its default code inspection rules to the shared profile (even if the project does not use them, e.g. Ruby stuff in a pure Java project). So developers have to be cautious not to check in those changes (or accept the bloat they cause)

So my question is: Have you come across these or similar issues and did you solve them?

Turn org.w3c.dom.NodeList into Iterable

I recently posted some reusable code that facilitates reverse iteration over a list in Java.

Here is another potentially useful Java method related to iteration, from my buddy Jordan Armstrong:

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
  /**
   * @param n An XML node list
   * @return A newly created Iterable for the given node list, allowing 
   *         iteration over the nodes in a for-each loop. The iteration 
   *         behavior is undefined if concurrent modification of the node list
   *         occurs.
   */
  public static Iterable<Node> iterable(final NodeList n) {
    return new Iterable<Node>() {

      @Override
      public Iterator<Node> iterator() {

        return new Iterator<Node>() {

          int index = 0;

          @Override
          public boolean hasNext() {
            return index < n.getLength();
          }

          @Override
          public Node next() {
            if (hasNext()) {
              return n.item(index++);
            } else {
              throw new NoSuchElementException();
            }  
          }

          @Override
          public void remove() {
            throw new UnsupportedOperationException();
          }
        };
      }
    };
  }

Union and intersection types in Java

Ceylon has union and intersection types at the heart of its type system. Ironically, there is something in Java resembling these powerful concepts, but only in specialized niche contexts and not as “first class citizens” of the type system:

  1. Something like intersection types can be used in generic type boundary declarations (since Java 1.5)
  2. Something like union types can be declared in try-catch blocks that use the multi-catch feature (since Java 1.7)

Interestingly, the Java language engineers used the & and | operators, just like Ceylon. But I have found no evidence so far that they are considering expanding these concepts to the Java type system in general or even let Java developers use them freely in all type declarations …

Update Oct/2012: At JavaOne, I asked Brian Goetz about this. He said that the Java Language team at Oracle has no intention to add additional support – beyond the niches mentioned above – for Union or Intersection types to Java in the foreseeable future.

Generic Apache Ant build.xml for webapps with JUnit tests

This post is about a build.xml file for Apache Ant that compiles, unit tests and packages a typical web application. The main build artifact is a timestamped WAR file.

Standardized directory layout

The build file assumes that your project uses the following directories:

  • src : Java sources and classpath resources
  • test : JUnit test sources and classpath resources
  • web : Web resources (JSP, HTML, CSS, etc.)
  • web/WEB-INF/lib : jars required by the web application at runtime
  • devlib/provided : jars provided by the web container at runtime
  • devlib/test : jars required for test compilation or execution

Transient build directories

The build will create these transient output directories for each stage of the build process. You should configure the version control of your project to ignore them.

  • build/classes : Compiled Java code and classpath resources (compile)
  • build/test-classes : Compiled JUnit tests (test-compile)
  • build/test-reports : JUnit test reports (test)
  • dist : WAR file (dist)

Run ant clean to delete them after you grabbed the WAR file.

The build.xml file

<project name="PROJECT_NAME" default="dist">

    <property name="classes" location="build/classes"/>
    <property name="test-classes" location="build/test-classes"/>
    <property name="test-reports" location="build/test-reports"/>

    <path id="classpath">
        <fileset dir="devlib/provided">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="web/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <path id="test-classpath">
        <path refid="classpath"/>
        <pathelement location="${classes}"/>
        <pathelement location="${test-classes}"/>
        <fileset dir="devlib/test">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="clean">
        <delete dir="dist"/>
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes}"/>
        <javac destdir="${classes}">
            <classpath refid="classpath"/>
            <src path="src"/>
            <src path="common/src"/>
        </javac>
        <copy todir="${classes}">
            <fileset dir="src" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="test-compile" depends="compile">
        <mkdir dir="${test-classes}"/>
        <javac destdir="${test-classes}">
            <src path="common/test"/>
            <src path="test"/>
            <classpath refid="test-classpath"/>
        </javac>
    </target>

    <target name="test" depends="test-compile">
        <mkdir dir="${test-reports}"/>
        <junit printsummary="yes">
            <classpath refid="test-classpath"/>
            <formatter type="brief" usefile="false"/>
            <formatter type="xml"/>
            <batchtest todir="${test-reports}" failureproperty="failed"
                       errorproperty="failed">
                <fileset dir="${test-classes}" includes="**/*Test.class"/>
            </batchtest>
        </junit>
    </target>

    <tstamp>
        <format property="timestamp" pattern="yyyy-MM-dd_hh-mm"/>
    </tstamp>

    <target name="dist" depends="clean, compile, test">
        <mkdir dir="dist"/>
        <war destfile="dist/${ant.project.name}_${timestamp}.war"
             basedir="web"
             excludes="**/CVS">
            <classes dir="${classes}"/>
        </war>
    </target>

</project>