A Simple Entity Bean with OpenEJB

With a stateless session bean under our belt, a simple entity bean logically follows. This example mirrors the OpenEJB CMP entity bean example provided by the OpenEJB team. The database changes from PostgreSQL to HSqlDB. And the ant build file incorporates a custom OpenEJB configuration, automated entity bean deployment, and both local and remote execution.

Let’s start with the mapping file. The example is self-explanatory: specify the primary key or identity value along with the insertion key generation method, map the three bean member variables to database elements, and declare types for each mapping pair. This example uses the HSqlDB identity for key generation, where the OpenEJB example used a PostgreSQL sequence. Take a look at the Castor DTD and the Castor JDO key generator support for more information on writing a mapping file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0"?>

<mapping>
<class name="org.acme.keyword.KeywordBean" identity="id" key-generator="IDENTITY">
<map-to table="keywords" />
<field name="id" type="integer" direct="true">
<sql name="id" type="integer" />
</field>
<field name="name" type="string" direct="true">
<sql name="name" type="varchar" dirty="check"/>
</field>
<field name="info" type="string" direct="true">
<sql name="description" type="varchar" dirty="check"/>
</field>
</class>
</mapping>

The remaining configuration files mirror the OpenEJB entity bean example. Note that the included configuration files use absolute path names and must be modified for your installation. All the configuration files are located in the ./src/conf/ directory.

There are only slight changes to the ant build file for this example. We explicitly copy the jar file to the deployments directory and execute the deploy method in place. This works around surprising deploy copy method behavior when using a custom bean deployments directory. The new deploy target is shown below. Note that an input file has been specified for the openejb deploy method – the input file provides the query for a Home findByName method.

1
2
3
4
5
6
7
8
9
<target name="deploy" depends="jar,deploy-check" unless="deploy.notNeeded">
<copy file="${BeanJar}" todir="${obj-dir}/beans" />
<exec dir="${OPENEJB}"
executable="${OPENEJB}/openejb.sh"
input="${src-dir}/conf/keyword.deploy"
>

<arg line="deploy -conf ${src-dir}/conf/openejb.xml -a ${obj-dir}/beans/${JarName}"/>
</exec>
</target>

Here is the Home interface definition. The findByPrimaryKey method is automatically implemented while the findByName method requires that we define a query when deploying the bean (as described in the previous paragraph).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package org.acme.keyword;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import javax.ejb.FinderException;
import java.rmi.RemoteException;


public interface KeywordHome extends EJBHome
{

public Keyword create( String name )
throws RemoteException, CreateException;


public Keyword create( String name, String info )
throws RemoteException, CreateException;


public Keyword findByPrimaryKey( Integer id )
throws RemoteException, FinderException;


public Keyword findByName( String name )
throws RemoteException, FinderException;

}

Exercise the entity bean with this simple driver method. It simply searches for a keyword bean named openejb. If the bean exists, then we remove it. And if it does not exist, then we create it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import org.acme.keyword.*;

import java.util.Properties;
import java.rmi.*;
import javax.ejb.*;
import javax.rmi.*;
import javax.naming.*;


public class Driver
{


public static void main( String[] args )
{

try {
InitialContext ctx = new InitialContext( System.getProperties() );
Object obj = ctx.lookup( "KeywordBean" );
KeywordHome home = (KeywordHome)
PortableRemoteObject.narrow( obj,KeywordHome.class );

Keyword keyword;
try {
keyword = home.findByName( "openejb" );
System.out.println( "Remove: " + keyword.name() +
"-" + keyword.info() );
keyword.remove();
}
catch ( ObjectNotFoundException notFound ) {
System.out.println( "Create keyword openejb" );
keyword = home.create( "openejb","an EJB container" );
}
catch ( FinderException fe ) {
fe.printStackTrace();
}
}
catch ( Exception ex ) {
ex.printStackTrace();
}
}

}

Sample output from two local executions. The first execution creates the bean and the second removes it.

1
$ ant local
Buildfile: build.xml

init:

compile:

jar:

deploy-check:

deploy:

local:
     [java] OpenEJB 0.9.2    build: 20030605-0409
     [java] http:/\/openejb.sf.net
     [java] Create keyword openejb



BUILD SUCCESSFUL
Total time: 27 seconds
$ ant local
Buildfile: build.xml

init:

compile:

jar:

deploy-check:

deploy:

local:
     [java] OpenEJB 0.9.2    build: 20030605-0409
     [java] http:/\/openejb.sf.net
     [java] Remove: openejb-an EJB container



BUILD SUCCESSFUL
Total time: 18 seconds

Just for the record, here is my openejb software configuration. And here is the simple OpenEJB Entity Bean.

Disclaimer: I don’t claim to be an expert on OpenEJB. Please send comments and corrections.

What is a Free Market?

The BusinessPundit started me thinking about what makes a free market. He thinks that a free market rewards innovation, aesthetics, safety, and lots of other things. But if it truly rewards innovation, then the first-movers would win more often; and if it truly rewards aesthectics, then we’d all use Mac’s. It seems to me that the strength of a free market is that it doesn’t actually reward anything in particular.

Pursuing that line of thought, a free market is governed by rules that do not dictate the winning strategy. The free-ness of a market is in some sense a measure of the number of winning strategies that can exist. And the wonder of a free market is that many winning strategies are not yet known.

The free market only rewards efficiency when everyone competes on price. If you can successfully compete on different terms, then the free market will reward different traits.

Tying not Bundling

Small Business Trends posts on how the recent European Union – Microsoft anti-trust ruling hurts small business. To summarize: Microsoft product bundling raises the lowest common denominator of business software, making small business owners more effective. Which is true – as far as it goes. However it misses the mark by confusing tying with bundling.

The EU ruled that Microsoft abused its market power … by tying its Windows Media Player with its ubiquitous Windows operating system. People want to buy bundled products. People have to buy tied products. Microsoft already has tremendous legal advantages, do we have to allow them illegal advantages as well?

R-E-S-P-E-C-T

I confess, I didn’t think that St Joseph deserved a 1-seed. Now they’re sitting pretty in the Elite Eight. And with Xavier also in the Elite Eight, the conference tournment waxing by Xavier doesn’t look so bad. A cinderella seed is usually the sentimental favorite this time of year, but St Joe’s is becoming the team I want to win. And earning a ton of respect in the process.

CSI: Connecticut

Cat Scene Investigation: Connecticut - Sam and/or Mark have some issues. We thought that the problem might be caused by the prednisone Mark was taking for his gums, but that treatment ended last week.

Mark has an appointment to see the Vet tomorrow. We’re using a black light to spot new problems, hoping that Feliway will make a difference and cleaning like nobody’s business.

Unintended Consequences

Creation is full of unintended consequences. Radio Userland uses a smart client and a dumb server. Comments and trackbacks are implemented as pop-ups because embedding them into the post would require a smart server. The unintended consequence is that Radio weblogs make poor targets for comment and trackback spam – there is almost no motivation to spam since Google doesn’t follow the javascript driven link.

MoveableType uses a smart server and a dumb client. The smart server embeds comments and trackback pings into the page that they reference. This allows the flow of conversation from comment to comment and blog to blog to be more easily followed. The unintended consequence is that MoveableType weblogs attract comment and trackback spam from the attendant Google Leech.

Using tools with limited scope protects against unintended consequences, while using powerful tools like identity requires more care to protect against unintended consequences. Both comment and trackback spam can be defeated by controlling the flow of google juice. But what unknown gremlins are waiting to emerge from Typekey? Will a Typekey identity be protected against sniffing while using a public wifi hotspot?

That’s my issue with Typekey. It is much too powerful relative to the problem it addresses. Resources and value go hand in hand. I doubt that commenting has the value to command the resources to do Typekey right.

PS. Just for the record, Userland may have forseen the advent of comment spam way back when. I have no knowledge one way or the other.

PPS. Why not initially render comments and trackback pings with disabled links (i.e. something that is not followed by search engines)? And allow the links to be subsequently enabled by the website owner.

Google Leech

n.
One who posts links to high page rank sites in order to direct google juice to their own site.
v.
To divert the flow of google juice by questionable means.

Killing Fleas with Bombs

Sixapart has announced Typekey, a verified identity service. I know that it’s easier to complain than create, but I feel obliged to point out that using an identification system to cure commenting ills is like using explosives to kill fleas.

Identity is one of the most important commodities on the internet. It takes time and hard work to establish who you are and what you represent. And once you establish your identity, it becomes a tool that can be used to promote ideas and issues that you support.

Identity is also one of the most powerful tools on the internet. Identity tokens are used to access email and buy goods online. And verified identity eliminates spam.

My concern about Typekey is that it is massive overkill to cure commenting. The cost of doing identity right far exceeds the value created by fixing weblog comments. I think that it only makes sense as part of a bigger picture and I’m waiting for the other shoe to drop.

Stanford 67 - Alabama 70

Ugh. A promising season goes down in flames. I was watching the Avalanche-Maple Leafs game on TV, the Syracuse-Maryland game live and the Stanford game via the internet. One minute Stanford was up 13 and the next thing I knew they were down three. Once CBS switched to the game, I almost got whiplash twisting my head to watch both Stanford and Maryland try to make it to another weekend.

I have no idea what happened. I was concerned about Alabama when I first saw the bracket. But I never thought that Alabama would go on a 16-0 run in the second half. And I never thought they would go 34-44 from the foul line while Stanford would only go 8-11 from the line. Oh well, thats March Madness.