Java multiple dispatch / multimethod libraries

Multiple dispatch (aka multimethods) is a method dispatch feature of some languages whereby the method that is dispatched to is based on the runtime type of the method arguments, not just the runtime type of the class being called (single dispatch polymorphism), and compile-time type of the method arguments.

Java doesn’t support multiple dispatch at a language level, but fortunately, it can be implemented in a library.

A simple test program to show Java behavior:

public class Mapper {

	static abstract class Content {}

	static class Link extends Content {}

	static class Message extends Content {}

	void map(Content c) {
		System.out.println("shouldn't get here!");
	}

	void map(Link l) {
		System.out.println("It's a Link");
	}

	void map(Message m) {
		System.out.println("It's a message");
	}

	public static void main(String[] args) {
		Mapper mapper = new Mapper();
		Content c1 = new Link();
		Content c2 = new Message();
		mapper.map(c1);
		mapper.map(c2);
	}
}

The output for the above test program is:

shouldn't get here!
shouldn't get here!

Java is dispatching to the overloaded map method based on the compile-time type of c1 and c2, which is the Content class. The above example is a bit contrived, but it shows that when working with base class references, for instance if you are working with a List of Content objects, Java won’t dispatch based on the runtime type.

I set out looking for a library that would implement multiple dispatch. I was a bit surprised the Google Guava library didn’t implement it. This seems to be right up their alley. What I did find was the following implementation: jmultimethod

Nice and simple, multiple dispatch using annotations and reflection.

Using jmultimethod, our test program becomes:

package org.sculptor.mapprovisions.domain;

import jmultimethod.Multi;
import jmultimethod.Multimethod;

public class Mapper {

	// Object responsible for doing the dispatching
    protected static Multimethod mapMM =
        new Multimethod("map", Mapper.class);

	static abstract class Content {}

	static class Link extends Content {}

	static class Message extends Content {}

    @Multi("map")
	public void map(Content c) {
		System.out.println("shouldn't get here!");
	}

    @Multi("map")
	public void map(Link l) {
		System.out.println("It's a Link");
	}

    @Multi("map")
	public void map(Message m) {
		System.out.println("It's a Message");
	}

	public static void main(String[] args) {
		Mapper mapper = new Mapper();
		Content c1 = new Link();
		Content c2 = new Message();

		// Invoke the multiple-dispatch method "map" on mapper, passing c1 as an argument
		mapMM.invoke(mapper, c1);

		// Invoke the multiple-dispatch method "map" on mapper, passing c2 as an argument
		mapMM.invoke(mapper, c2);
	}
}

The new output is:

It's a Link
It's a Message

I’d still prefer to have it directly supported by Java, but it’s good to have this solution when needed.

Posted in Java | Leave a comment

Web and mobile UI wireframes

People think visually. A picture is worth a thousand words as the saying goes.

We’ve recently been using Balsamiq to put together wireframes we use to work out user experience concepts, and to work with stakeholders. Balsamiq is an Adobe AIR-based desktop application that can be used to quickly put together web and mobile UI wireframes. It’s also relatively easy to make clickable demos in Balsamiq by stringing together screen wireframes.

What I probably like most about Balsamiq, is the wireframes have a rough and unfinished look to them. They get the point across, but purposefully look informal so people focus on the general concepts and information presented on the UI, and not the color or exact positioning of buttons.

If you’re an OmniGraffle user, there’s some stencils that are also a good option, among them Konigi, and Yahoo Design Stencils (also available for Visio and in other formats).

Posted in General | Leave a comment

Customizing Sculptor to generate Groovy classes

I’ve been using the Sculptor DSL/tool to build Java EE applications based on DDD and EDA principles. This has worked out great, and Sculptor allows me to express the intent at a higher level and more succinctly than I could with straight Java or another GPL.

When it comes to expressing the behavior of your domain, that’s done outside of the Sculptor DSL, using Java. The Sculptor DSL is largely about the solution structure; the structure and relationship between classes, and what standard behaviors they support, such as CRUD repository methods. If you need to implement complex logic for your credit score calculator class, Sculptor is used to define the interface into your method, but it’s up to you to implement the logic for that method in Java. After a while, I found this was significantly slowing me down. Java is so verbose compared to languages such as Groovy or Scala, in particular when it comes to working with object graphs and functional programming.

Fortunately, Sculptor and the XText tools it is based on support customizing each step of the generation process. This post outlines what I did to customize Sculptor to allow me to implement my domain and service classes in Groovy. This only applies to the concrete implementation classes you would customize when using the ‘gap’ generation option. The base classes that are regenerated each time are still Java, as there was no benefit to using a different language for them.

The following changes should be made to the business tier project.

Maven POM updates

First, the Groovy libraries and a Groovy compiler need to be added to the Maven build Traditionally this was done using the GMaven plugin, but I came across this post that describes using the eclipse-groovy plugin to jointly compile Java and Groovy together in a single pass. Much better.

To use this, modify the pom.xml to add the springsource maven dependency and plugin repositories:

	<repositories>
		<repository>
			<id>springsource</id>
			<url>http://maven.springframework.org/snapshot</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>springsource</id>
			<url>http://maven.springframework.org/snapshot</url>
		</pluginRepository>
	</pluginRepositories>

Then update the maven-compiler plugin in the build/plugins section to use the groovy-ecilpse-compiler:

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.1</version>
				<configuration>
					<encoding>ISO-8859-1</encoding>
					<source>1.6</source>
					<target>1.6</target>
					<compilerId>groovy-eclipse-compiler</compilerId>
					<verbose>true</verbose>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.codehaus.groovy</groupId>
						<artifactId>groovy-eclipse-compiler</artifactId>
						<version>0.0.1-SNAPSHOT</version>
					</dependency>
				</dependencies>
			</plugin>

Finally, add groovy to your dependencies:

		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-all</artifactId>
			<version>1.7.5</version>
			<type>jar</type>
		</dependency>

Xpand templates

The next step is to create Xpand templates that will generate Groovy class files instead of Java. I’m using the Sculptor ‘hint’ feature to control whether a class will get generated as Java or Groovy. If a Service or Domain class is annotated with hint=”gapImpl=groovy”, Groovy will be generated, otherwise it’ll fall back to the default of Java.

The code below defines an aspect for the generation of the DomainObject and Service sub-classes. Whenever these templates are invoked within Sculptor, the aspect below will be evaluated, deciding whether to generate Groovy code or to proceed with the normal Sculptor templates.

Add to src/main/resources/templates/SpecialCases.xpt:

«AROUND *::domainObjectSubclass FOR DomainObject»
        «IF getHint("gapImpl") == "groovy"»
                «EXPAND Groovy::Groovy»
        «ELSE»
            «targetDef.proceed()»
        «ENDIF»
«ENDAROUND»

«AROUND *::serviceImplSubclass FOR Service»
        «IF getHint("gapImpl") == "groovy"»
                «EXPAND Groovy::GroovyService»
        «ELSE»
            «targetDef.proceed()»
        «ENDIF»
«ENDAROUND»

Next, define the templates that will generate the actual Groovy code. Since Groovy syntax is practically a superset of Java, I was able to copy the code from the existing Sculptor templates (DomainObject.xpt and Service.xpt). The one key difference is where the code gets generated to – a new helper function groovyFileName returns a .groovy file name.

Create src/main/resources/templates/Groovy.xpt:

«REM»
Templates to generate Sculptor implementation classes as Groovy
«ENDREM»

«IMPORT sculptormetamodel»
«EXTENSION extensions::helper»
«EXTENSION extensions::groovyhelper»
«EXTENSION extensions::properties»

«DEFINE Groovy FOR DomainObject»
«FILE groovyFileName(getDomainPackage() + "." + name) TO_SRC»
«javaHeader()»
package «getDomainPackage()»;

«IF formatJavaDoc() == "" -»
/**
 * Data transfer object for «name». Properties and associations are
 * implemented in the generated base class {@link «getDomainPackage()».«name»Base}.
 */
«ELSE -»
«formatJavaDoc()»
«ENDIF -»
«EXPAND templates::DomainObject::domainObjectSubclassAnnotations»
public «getAbstractLitteral()»class «name» extends «name»Base {
        «EXPAND templates::DomainObject::serialVersionUID»
    «IF getLimitedConstructorParameters().isEmpty || getMinimumConstructorParameters().isEmpty»public«ELSE»protected«ENDIF» «name»() {
    }

    «EXPAND templates::DomainObject::propertyConstructorSubclass»
    «EXPAND templates::DomainObject::limitedConstructor»
    «EXPAND templates::DomainObject::minimumConstructor»

}
«ENDFILE»
«ENDDEFINE»

«DEFINE GroovyService FOR Service»
	«FILE groovyFileName(getServiceimplPackage() + "." + name + "Impl") TO_SRC»
«javaHeader()»
package «getServiceimplPackage()»;

/**
 * Implementation of «name».
 */
«IF isSpringAnnotationToBeGenerated()»
@org.springframework.stereotype.Service("«name.toFirstLower()»")
«ENDIF»
«IF webService»
«EXPAND webServiceAnnotations»
«ENDIF»
public class «name»Impl extends «name»ImplBase {

    public «name»Impl() {
    }
	«EXPAND templates::Service::otherDependencies»

    «EXPAND templates::Service::implMethod FOREACH operations.select(op | op.delegate == null && op.serviceDelegate == null)
    	.reject(e|e.hasHint("EventSourcingScaffold")) »
}
	«ENDFILE»
«ENDDEFINE»

Xtend helper function

The last step is to define the helper function used above to return the groovy file name.

Create src/main/resources/extensions/groovyhelper.ext:

import sculptormetamodel;

String groovyFileName(String name) :
    name.replaceAll("\\.", "/") + ".groovy";

Model file

Finally, to use this, add the hint to whatever Service or Domain classes you need it for in your model file:

		Service MyService {
			gap
			hint="gapImpl=groovy"
			...
		}
Posted in MDSD | Tagged , | Leave a comment

Fornax Sculptor DSL tool: Switching an existing app to MongoDB

Fornax Sculptor is a DSL, Eclipse-based editor, and set of code generators used to build Java/JEE applications.  You define your model and other parts of your application in the Sculptor textual DSL, and the Sculptor model transformations and code generators generate Java code, Spring configuration, Hibernate mappings, and UI code.  Of course, Sculptor only takes you so far, which is where you supply your own code via extension points and composition/delegation, but Sculptor goes a long way to minimize repetitive code, and keeping you working closer to the actual problem domain.  You can also customize and extend Sculptor via the excellent Eclipse MDSD tools (Xtext, MWE, Xpand, Xtend, Check) that Sculptor is based on.

I’ve been building a JEE application using Sculptor, based on JSF2, Hibernate, MySQL, and Spring.  The latest release of Sculptor added support for MongoDB and Events.  I’ve been looking to eventually migrate this application to a distributed & schema-less database, and more of an Event-Driven architecture, so I decided to try converting it.  All in all it went very smoothly.  Following are my notes on switching over a Sculptor application from Hibernate to MongoDB.

Updating the Maven POM and Sculptor configuration

First, remove mysql, hibernate, spring-jdbc and related dependencies from the business tier project pom, then add the following dependency:

<dependency>
    <groupId>org.fornax.cartridges</groupId>
    <artifactId>fornax-cartridges-sculptor-mongodb</artifactId>
    <version>${sculptor.version}</version>
</dependency>

Then modify sculptor-generator.properties to switch the persistence tier from Hibernate to MongoDB.  Comment out:

#db.product=mysql

and add:

nosql.provider=mongoDb

Run a build:

mvn clean install

Fix domain concrete classes

If you have domain classes with gap classes, as I had, you’ll have some compile errors due to JPA-isms that are in the domain concrete classes. Since these concrete domain classes are generated only once, they don’t get overwritten on the next build and still have the JPA-specific code in them. Edit each concrete domain class that has a gap class, and remove the JPA-isms:

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "LINK")
public class Link extends LinkBase {

The domain base classes don’t have to be modified, because they’re regenerated each build.

Fix references to entity unique IDs in your custom code

Another difference with the MongoDB persistence layer is that the entity unique IDs are Strings rather than Longs. Any place in your hand-written code that uses entity IDs, you’ll need to make this change.

Fix service and repository unit tests

When using the JPA-based persistence tier, the service tests  are based on dbunit, which won’t work with MongoDB.  I did a quick search and didn’t find a dbunit-equivalent for MongoDB.  The Sculptor MongoDB test samples use a technique of setting up test data using Java code and the repository classes:


package my.application.serviceapi;

...

/**
 * Spring based test with MongoDB.
 */
@RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =  {
    "classpath:applicationContext-test.xml"}
)
public class LinkServiceTest
    extends AbstractDependencyInjectionSpringContextTests
    implements LinkServiceTestBase {
    @Autowired
    private DbManager dbManager;
    @Autowired
    private LinkService linkService;

    @Before
    public void initTestData() {
        // Set up your test fixtures here:
        Link link1 = new Link("http://some.com", "Some title");
        linkService.save(link1);
    }

    @Before
    public void initDbManagerThreadInstance() throws Exception {
        // to be able to do lazy loading of associations inside test class
        DbManager.setThreadInstance(dbManager);
    }

    @After
    public void dropDatabase() {
        Set<String> names = dbManager.getDB().getCollectionNames();
        for (String each : names) {
            if (!each.startsWith("system")) {
                dbManager.getDB().getCollection(each).drop();
            }
        }

        // dbManager.getDB().dropDatabase();
    }

    private int countRowsInDBCollection(String name) {
        return (int) dbManager.getDBCollection(name).getCount();
    }

    @Test
    public void testFindById() throws Exception {
        // Your test code and assertions here...
    }

}

Simply set up your test fixtures using your service/repository classes or the MongoDB DbManager within the initTestData() method.

No support for findByQuery repository method

When building, I got errors such as:

.../LinkAccessFactoryImpl.java:[21,66] cannot find symbol
symbol  : class MongoDbFindByQueryAccessImpl
location: package org.fornax.cartridges.sculptor.framework.accessimpl.mongodb

This is odd since LinkAccessFactoryImpl is a generated class. The cause of this error was that I have findByQuery as one of my scaffolding operations that is generated for every repository. But this type of method isn’t supported by the MongoDb cartridge. In the future it’d be nice if there was a model validation that checked for this and generated an error before generating code. To fix this, remove findByQuery from the list of default scaffolding operations in scupltor-generator.properties, if you have it there at all:

#scaffold.operations=findById,findAll,save,delete,findByQuery,findByKey,findByCondition
scaffold.operations=findById,findAll,save,delete,findByKey,findByCondition

Fix bidirectional associations between aggregate roots

I had a bidirectional association between domain aggregate roots that caused problems. I had something along the lines of:

    Entity A {
    aggregateRoot 

    - Set<@A> children <-> parent 

    - @A parent nullable <-> children
    }

This resulted in an endless loop in the generated data access code, because it was following both the parent and children associations when mapping the objects to a MongoDB document.

In hindsight, I think this should not be supported within Sculptor when using the MongoDB cartridge. It may make sense for relational databases, but since MongoDB doesn’t support transactions across collections (ditto for most other NoSQL databases), it cannot be guaranteed that both sides of the association will be updated as a unit of work. This also makes distributing your database across many nodes much more complicated. I changed my model to make this a unidirectional association, problem solved.

An interesting point is that with MongoDB and similar databases, the association reference is stored on the “one” side of the association, rather than the “many” side as with relational databases. The document at the “one” side of the association contains a list of IDs of the “many” instances.

Tip: To see how Sculptor maps entities to MongoDB documents, see the “mapper” package within your app.

Add DbManagerFilter to web.xml

Lastly, I had to add the DbManagerFilter servlet filter to the web.xml file. This filter registers a DbManager instance in a thread local so that it may be used to lazy-load associations as needed within your JSF managed bean/Facelets code.


  <filter>
    <filter-name>dbManagerFilter</filter-name>
    <filter-class>org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.DbManagerFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>dbManagerFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Overall the switch to MongoDB went very smoothly, and there were very few changes I had to make, largely thanks to Sculptor. The most significant change was switching the unit tests from using dbunit to using Java test fixtures.

Posted in MDSD | Leave a comment

ActiveJDBC: an implementation of the Active Record pattern for Java

Igor Polevoy, who I’ve had the pleasure of working with on a couple projects, has just posted his ActiveJDBC project to Google code.  If you work on Java applications that access relational databases, you owe it to yourself to check it out.

ActiveJDBC implements the Active Record pattern, initially documented by Martin Fowler in his book Patterns of Enterprise Application Architecture and popularized by Ruby on Rails.

What’s interesting about ActiveJDBC is how little baggage it brings with it, and how quickly one can get started using it.  It’s pure Java, requires few other libraries (no Hibernate, JPA, etc), and requires no configuration (no mapping, etc).  All metadata is inferred from the database schema.

Some examples of ActiveJDBC in action:

Persistent Book class definition:

import activejdbc.Model;

public class Book extends Model {}

Adding a new Book:

Book b = new Book();
b.set("isbn", "0321127420")
  .set("title", "Patterns of Enterprise Application Architecture")
  .set("author", "Martin Fowler")
  .set("publisher", "Addison-Wesley Professional");
b.saveIt();

Paging through Addison-Wesley books in sorted order

List<Book> books = Book.where("publisher = ?", ""Addison-Wesley Professional");
  .offset(11)
  .limit(10)
  .orderBy("author asc");

It looks like this framework strikes a good balance between a simple framework that works as you expect, and providing an elegant DSL for database access.  This is particularly impressive given Java as a language doesn’t lend itself to implementing DSLs.

One thing I was curious about but didn’t see an example of, was working with associations, which is listed on the features page.

For more info, see the Getting Started Guide on Google Code, Igor’s blog, and the Productive Edge Java blog.

Posted in Java | 1 Comment

Using Scala for something I’d normally using a “scripting” language for

From time to time, some repetitive task comes up that I can do quicker by writing a script to do it than to do it manually. Especially if it’s something I may be needing to do again in the future.

Usually I’d turn to a “scripting” language like Python, Groovy, or back in the day, Perl for this type of thing.

Today such a need came up, and I decided to try tackling it with Scala since it has many of the features that make the above dynamic languages good for this:

  • first-class support for map and list data structures
  • an interactive shell
  • minimal overhead to write a program, compile, and run it
  • support for functional programming
  • good regular expression support

The problem:
A small performance test program ran a large number of tests, and measured the elapsed time for each execution. The program output a line like “request completed in 10451 msecs” for each test. I needed to parse the output, collect the elapsed time measurements, and get some basic statistics on them; simple average, minimum, and maximum.

I used a Scala 2.8 snapshot, and fleshed out the code using the Scala interactive shell. First, define a value with the raw output to be processed:

scala> val rawData = """request completed in 10288 msecs
     | request completed in 10321 msecs
     | request completed in 10347 msecs
     | request completed in 10451 msecs
     | request completed in 10953 msecs
     | request completed in 11122 msecs
... hundreds of lines ...
     | request completed in 11672 msecs"""

The above uses Scala’s support for multi-line string literals.

The next thing I needed to do was parse the above output, using a regular expression to extract just the milliseconds. There’s several ways to create a regular expression in Scala. This is the one I like:

val ReqCompletedRE = """\s*request completed in (\d+) msecs"""r

There’s a bit of magic in how the above string literal actually ends up becoming a regular expression. There’s an implicit conversion in the Scala Predef object which turns a Java String into a RichString. RichString provides a ‘r’ method that returns a regular expression object. The members of the Predef object are automatically imported into every Scala module, so the Scala compiler will attempt to apply any conversions it finds in Predef when trying to resolve the ‘r’ method. So the above expression is creating a RichString from a String via an implicit conversion, then calling the ‘r’ method on it, which returns the regular expression.

To apply the regular expression to a line of the output and to extract the milliseconds, we can use an expression like:

scala> val ReqCompletedRE(msecs) = " request completed in 10451 msecs"
msecs: String = 10451

msecs gets bound to the first group in the regular expression (the part that matches (\d+)). This takes place via the Scala extractors feature – the scala regular expression class defines an extractor which extracts the grouping results.

The next step is to iterate over the lines of the output, extract the milliseconds, and turn the results into a list.

scala> val msecsVals = rawData.lines.map { line => val ReqCompletedRE(msecs) = line; Integer.parseInt(msecs);} toList
res11: List[Int] = List(10288, 10321, 10347, 10451, 10953, 11122, ..., 11672)

The above code is using the RichString lines, Iterator.map method, along with Scala closures.

Finally, to get the simple statistics:

scala> (msecsVals.sum / msecsVals.length, msecsVals.min, msecsVals.max)
res21: (Int, Int, Int) = (10736,10288,11672)

Putting the whole script together:

val ReqCompletedRE = """\s*request completed in (\d+) msecs"""r
val msecsVals = rawData.lines.map { line =&amp;amp;gt; val ReqCompletedRE(msecs) = line; Integer.parseInt(msecs);} toList
(msecsVals.sum / msecsVals.length, msecsVals.min, msecsVals.max)
Posted in Scala | 6 Comments

New change management tool: Redmine

I was starting a new project, needed a change management tool, and wasn’t really satisfied with the tools available last time I looked, so I took a look at what was currently available.  One option I came across that hadn’t been on my radar before was Redmine.

Redmine has many of the features I like from Trac, such as the Roadmap view, version control integration, an Activity view, and built in Wiki.  It also has a bunch of plugins, one of which looks like it’ll generate burndown charts, which I’m planning to use.

It looked like Redmine was fairly well supported on Windows (box I have available) so I decided to try it out.  Redmine is based on Ruby on Rails, so the first step to installation is to get Ruby and Rails installed.

I started off trying to install Ruby and Rails under cygwin, but ran into problems with building mysql from source (no cygwin port for this!).

So instead I went down the route of installing everything outside of cygwin.  Here’s a summary of the steps:

  • Install the Windows Ruby binary, along with Gem package system.
  • Install the Rails 2.1.2 gem, which Redmine 0.8.X depends on
  • Download and unpack the Redmine distribution
  • Setup the database (running on MySQL in my case) and edit database.yml per instructions.
  • I had to install older MySQL client library due to some Rails/MySQL library incompatibility (described here)
  • Create the database schema for Redmine via:
    rake redmine:load_default_data RAILS_ENV=&amp;quot;production&amp;quot;
  • Populate a default configuration via:
    rake redmine:load_default_data RAILS_ENV=&amp;quot;production&amp;quot;
  • You can then run Redmine via:
    ruby script/server webrick -e production
Posted in Software Development, Tools | Leave a comment

Scala, Maven and IntelliJ

I’m evaluating Scala for a project I’ve recently started.  The project requires processing a large number of data structures.  Between Scala’s good support for parallelism and functional programming, it seems like a good fit.  I’ve heard Scala brings many of the development efficiencies and expressiveness of dynamic languages, while being statically typed.  It does this through a number of features, including smart type inferencing, great functional programming support, traits, and a bunch of other tricks I’m in the process of learning.

We’ve seen several dynamically typed languages gain widespread adoption in the last eight years or so… Python, Ruby, Groovy, and you can go back to Javascript, Perl, PHP, etc..  What’s the last statically typed language that’s gained a wide following?  C#?  I’ve pretty much come to assume that any new language that was highly expressive, concise, and good for DSLs, would be a dynamic language.  Dynamic languages do have their drawbacks..  performance is usually not up to par with statically typed languages (although it rarely makes a difference), and the lack of type information at compile type make it very difficult for IDEs to provide features such as autocomplete, symbol lookup, and early error detection.  So if languages such as Scala manage to deliver a highly development-efficient language while retaining static typing, that’s all the better in my eyes.

Here’s what I did to get a minimal project set up in Maven and IntelliJ IDEA:

Create a shell of a project using a Maven archetype:

mvn org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha-7:create \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \
-DarchetypeVersion=1.1 \
-DremoteRepositories=http://scala-tools.org/repo-releases \
-DgroupId=com.rps -DartifactId=scalatest

This created a project with a single Scala class, test, and a Maven POM containing the minimal dependencies and plugins.

The version of Scala generated by the archetype isn’t the most current. Just update the version in the POM:

<scala.version>2.6.1</scala.version>

to 2.7.6, or whatever is the version you want.

I then imported the project into IntelliJ IDEA using the excellent IDEA Maven integration.  I had to add the Scala nature to the project in IDEA myself, but otherwise, everything is working..  Editing, unit tests, debugging, etc.

The IntelliJ IDEA editor support for autocomplete seems to work pretty well, but it doesn’t seem to do much detection of errors as you’re editing yet.

Posted in Scala | Tagged , | Leave a comment

Maven and skinny wars

Maven is a great build system, but sometimes limitations come up that can drive you crazy.  One such limitation is support for building “skinny war” files.  Granted, I don’t think it’s the most common configuration, but for certain types of applications, it’s the preferred way to package your application.

By default, war files are packaged with all of the libraries they use embedded within them.  This is fine, unless your application uses a large number of libraries, and you have more than one war file within your enterprise application archive (ear file).  In that case, you’re causing the JVM to load the same set of classes multiple times, once for each war, which can use up a significant amount of memory, and bloating the size of your ear file.

In a skinny war configuration, the libraries are packaged at a higher level, within the ear file itself, and the war files are made to reference the libraries packaged within the ear file within their MANIFEST.MF file.  The classes are loaded once, and can be used in multiple web applications bundled in the ear.

Unfortunately, this isn’t directly supported in Maven, although there are workarounds.  As this page states, “The Maven WAR and EAR Plugins do not directly support this mode of operation but we can fake it through some POM and configuration magic”.  The workaround is to list the jars in each war, but tell Maven to exclude it from the WEB-INF/lib and to add references to the jar in the MANIFEST.MF file.

The ear file Maven project then needs to list every library it will package as a dependency.  This means that common libraries will be listed in the ear project, and each war project that uses them, causing quite a bit of bloat in the Maven project files.

This wiki page has a good description of the situation, some alternate solutions, and requirements for a long term solution.

Ideally, I’d like to be able to take the same Maven war project file, and build for standalone deployment (fat war), or deployment within an ear project where the common libraries are included once in the ear file (skinny war), without having to change the Maven war project file.

For this to work, I think the ear project would have to control whether and which war project libraries are pushed up to the ear project.  The ear project file could explicitly list those libraries that should be bundled in the ear, or it could have an option where it calculates the common libraries across the contained war files, and automatically bundles them in the ear file.  As the link above indicates, the ear project would have to have the ability to rewrite the manifest entries of the contained war projects.

Technorati Tags:

Posted in Java, Tools | Tagged | Leave a comment

Public mashup and data feed APIs

I’m doing some research for a possible venture.  A part of this venture would fall into the mashup category – pulling information from other sites and services, and combining it in a unique way.  Following are my initial notes on some interesting APIs and information feeds:

Programmable Web is a great resource for those building mashups – it has a directory of APIs and Mashups that make use of those APIs.  The blog post “Top Twenty Open APIs and Mashup Resources for Web Developers” lists some of the most popular APIs.

Google Transit Feed Specification

  • Provides information on stops, routes, calendar, fares, frequencies, usage policies
  • Unfortunately, only available for a limited set of metro areas

Google Maps AJAX API:

  • AJAX API
  • Display map with road, satellite, hybrid, and terrain, by latitude & longitude
  • Can respond to user events within the map – e.g. clicking, zooming in/out, panning, etc
  • Can manipulate map via JavaScript – adding markers, changing location, zoom, etc
  • Can create various types of overlays on the map
  • Includes google street view

Google AJAX Feed API

  • Include any RSS or ATOM feed on your site, with various presentation options

OpenSocial

  • Common API for multiple social networking sites, including hi5, LinkedIn, MySpace, Netlog, Ning, orkut, and Yahoo.  Unfortunately Facebook is not one of them.
  • JavaScript APIs for client side and REST/RPC APIs for server side
  • Can be used to create apps that are embedded in social networking sites, as well as use user social information (user profile, friend lists, events) from social networks in your site.
  • OAuth is used to allow users to authorize use of their social network information

YouTube APIs

  • Integrate YouTube functionality into your site: video searches, upload videos, etc
  • Integrate video player into your site
  • Allow users to see/manage their favorites on your site
  • RSS/ATOM data feeds from YouTube

YELP APIs

  • REST APIs
  • Retrieve business info and reviews for a geographic region and business category.  This includes the business location, and their categories.
  • Retrieve neighborhood name and info by location
  • Retrieve reviews for a particular business
  • Retrieve business info and pictures
  • It looks like this isn’t a complete directory – only those businesses and features that have been rated are listed

Zillow APIs

  • “Neighborhood and city affordability statistics: Zillow Home Value Index, Zestimate distribution, median single family home and condo values, average tax rates, and percentage of flips.”
  • Demographic data at the city and neighborhood level – local market data, affordability, household income, average age, commute time, etc.
  • Home Valuation: “Search results list, Zestimate® home valuations, home valuation charts, comparable houses, and market trend charts.”
  • Property Details: “Property-level data, including historical sales price and year, taxes, beds/baths, etc.”
  • Lists of counties, cities, ZIP codes, and neighborhoods, as well as latitude and longitude data for these areas so you can put them on a map.
  • “boundaries for nearly 7,000 neighborhoods and 150 cities”  available via Creative Commons license.
  • License restricts site from charging money for Zillow data

Yahoo! Local Web Services

  • REST APIs
  • Provides access to public collections created with Yahoo! Local Collections
  • Can perform local searches based on location, radius, route, categories: returns results with location info, ratings, categories, etc
  • Yahoo Local search web site: http://local.yahoo.com/ – You can see some of the information that’s available

Walkscore API:

  • Get walk score by location
Posted in Software Development, Web | 3 Comments