Generating Java Getter and Setters with Regular Expressions and IntelliJ IDEA

This is mostly a story about the power of a good UI, again demonstrated by IntelliJ IDEA (Version 9, Ultimate).

I have a buddy that says, “If you have a problem, and you want to solve it with regular expressions, then you have two problems.”

I’ve done a fair bit of regular expressions in my day.  My first contracting gig way back in 1991 (I was 24) was using regex to transform specifications in Word documents into something parseable by a system that would store the data in a database that could then be searched by a desktop app.  I’ve never been an expert at regex, at least not since I left that gig, but with a cheatsheet I know what I am doing, mostly.  Still, it often takes me more time than it’s worth.  Enter IntelliJ IDEA.

I had a generated interface for what amounts to an Entity with hundreds of getters (or the interfaces of them).  I needed to create a value object from it.  Of course, I’ll eventually use code generation, but I’m need to move quickly right now, so I figured I’d take a stab at using RegEx.  I started using IDEA and was surprised to see that as I typed my RegEx, it caught many of my mistakes by showing red underlines (like Word misspelling) and gray backgrounds for warnings.  Within 5 minutes, I had it working.  That would have taken me 20 minutes easily with a decent likelihood of being a complete waste of time if I couldn’t figure out my mistakes through eyeball inspection.

Here’s the search that matches “Type getFoo();” or “boolean isFoo();” (with support for generics):

([ \t]+)([a-zA-Z.0-9<>]+)[ ]+(get|is)([^(]*)\(\);

And the replacement expression to make:
Type Foo;
Foo getFoo(return Foo;):
void setFoo(Foo Foo) {this.Foo=Foo);

\t$2 $4;\n\tpublic $2 $3$4(){return $4;}\n\tpublic void set$4($2 $4){this.$4 = $4;}

Enjoy.

Flex Building error “unable to export SWC oem” (Flexmojos)

I’ve been working on perfecting a build environment for Flex4+Flash Builder 4+JBoss+Maven that works for IDEA & Eclipse (FB), hot deploys to JBoss, and plays well in continuous integration (Hudson).  I was following a Maven anti-pattern that’s easy to fall into – make your maven aggregator projects your parent projects.  This is a bad idea since projects that the aggregator builds may have common settings with other children, but not with all other children (swc’s vs. swf’s in particular).  So I did the right thing and created a tree of parent pom’s that my projects use as parents, leaving the aggregator as just an aggregator.

This post is just to capture the solution to an error that I came across.  One of my swc’s was generated this error during compilation:

"unable to export SWC oem"

Googling showed:

- lots of people ran into this when mixing different versions of the flex compiler.

- lots of people ran into it when dealing with resources and fonts

The fix was to specify the mergeResourceBundle option in the flexmojo's config:
<plugin>
  <groupId>org.sonatype.flexmojos</groupid>
  <artifactId>flexmojos-maven-plugin<artifactId>
  <version>${flexmojos.version}</version>
  <configuration>
    <mergeResourceBundle>true</mergeResourceBundle>
  </configuration>
</plugin>

Installing Postgresql for Mac for Django

http://www.postgresqlformac.com/

That was easy, but after doing it, I realized there was an official install. I would have uninstalled it, but the promised uninstalled script n the README was non existent. :-( (And just deleting one dir would be insufficient.) It looks like all the other postgresql installs, so I think the db is OK, but I wish I had the tools that come with the official install, I’ll figure that out later.

I then followed this to get psycopg2 installed to prep Postgresql for use with DJango
http://stubblog.wordpress.com/2009/06/07/installing-psycopg2-on-osx/.

A snap.

Creating an Executable Apache Pivot App using Maven

Apache Pivot is a very interesting new RIA platform.  You can think of it as a competitor to Flex, Silverlight, and JavaFX.  Unlike all three of those languages, it uses Java and XML – two generally familiar languages, to create applications.  It’s structured similar to Flex – it has an XML file that describes an application’s layout and allows enough functionality to create simple and not so simple applications with just XML with embedded scripts.  Unlike Flex, the “code behind” language is straight up Java.  It’s deployment model is just like an applet’s, so it’s ubiquitous.  When you dig deeper, there are lots of little niceties – things that are simple and make sense.  It’s nice to work with.

This post describes how to set up a maven project to build and deploy an Apache Pivot application.  Once Pivot is in the maven central repo (it’s not quite out of incubator as of this writing), then the result of this post can be made into an archetype and contributed back.  The result of this post is is this GitHub repository commit.  (Git is very nifty – the fact that the entire set of files is available from one Git commit id is, well, wicked, as we say in Massachusetts.)

We’ll steal the code from the Pivot StockTracker tutorial, since it will be extended with a graph and used as an example of how and why to use the EventBus in UIs.  The examples will be posted on the EventBus.org blog soon.   I’ll add a blog entry here when the eventbus.org blog post is available.

Start by using the maven quickstart to create a blank Java project from an archetype:

mvn archetype:create -DgroupId=org.eventbus.tutorial -DartifactId=stocktracker
cd stocktracker
mvn install
 

To populate the example, we can pull the StockTracker sources to the our package in the src/main/java directory and delete the files we don’t need:

mkdir src/main/java/org/eventbus/tutorials/pivot/stocktracker
cd src/main/java/org/eventbus/tutorials/pivot/stocktracker
svn co http://svn.apache.org/repos/asf/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/ .
rm -rf .svn
rm App.java
rm ../../../../../../../test/java/org/eventbus/tutorial/AppTest.java
 

According to Maven conventions, only the java code belongs in this directory, so let’s move the other files to the resource directory so that they can easily be picked up from the jar at runtime.  A better practice may be to make image, json, and wtkx directory, or maybe make a main/wtkx directory for the latter, but let’s keep it simple.  The community may come up with better standards for Pivot development with Maven.

cd ../../../../../..
mkdir resources
mv java/org/eventbus/tutorial/*.json resources/org/eventbus/tutorials/pivot/stocktracker
mv java/org/eventbus/tutorial/*.png resources/org/eventbus/tutorials/pivot/stocktracker
mv java/org/eventbus/tutorial/*.wtkx resources/org/eventbus/tutorials/pivot/stocktracker
 

Change all the packages in the Java files to the new package.

find . -name *.java -print | xargs sed -i -e 's/org.apache.pivot.tutorials.stocktracker/org.eventbus.tutorials.pivot.stocktracker/g'
find . -name *.wtkx -print | xargs sed -i -e 's/org.apache.pivot.tutorials.stocktracker/org.eventbus.tutorials.pivot.stocktracker/g'

(This is why I keep such detailed blogs.  It took me while to figure out that the -e is required on Mac, but not Linux, and I’m sure I’ll forget again, but I’ll remember that I had to do this before.)

 

The maven default build uses Java 1.3 source, even though Java 1.5 is about to be obsoleted as of this writing, so we’ll have to tell maven to get with the new decade and use  Java 6  (I think Pivot will work for Java 5 too) by adding this before <dependencies>:

<build>
 <sourceDirectory>src</sourceDirectory>
 <testSourceDirectory>test/src</testSourceDirectory>
 <plugins>
   <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
       <source>1.6</source>
       <target>1.6</target>
     </configuration>
   </plugin>
 </plugins>
</build>
 

Pivot is not in the central maven repo yet, so we have to install it locally, which means building it first.  The Pivot team is rock solid, pulling and building the project is easy and reliable.  See the BUILD file in the svn trunk for details, but on a Mac using Java 6, it’s like this:

cd ~
mkdir pivot
svn co http://svn.apache.org/repos/asf/incubator/pivot/trunk/ .
export CLASSPATH=~/dev/maven-ant-tasks-2.1.0.jar:~/dev/junit/junit-4.8.1.jar:/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/plugin.jar:/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/javaws.jar
ant maven-install
 

This builds all the pivot jars using ant and installs them to your local maven repo.

Now go back to our project and add the pivot dependencies to pom.xml:

<dependencies>
 <dependency>
    <groupId>org.apache.pivot</groupId>
    <artifactId>pivot-core</artifactId>
    <version>1.4</version>
 </dependency>
 <dependency>
    <groupId>org.apache.pivot</groupId>
    <artifactId>pivot-wtk</artifactId>
    <version>1.4</version>
 </dependency>
 <dependency>
    <groupId>org.apache.pivot</groupId>
    <artifactId>pivot-web</artifactId>
    <version>1.4</version>
 </dependency>
 <dependency>
    <groupId>org.apache.pivot</groupId>
    <artifactId>pivot-wtk-terra</artifactId>
    <version>1.4</version>
 </dependency>
 <dependency>
    <groupId>org.apache.pivot</groupId>
    <artifactId>pivot-charts</artifactId>
    <version>1.4</version>
 </dependency>
 <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
</dependencies>

You might not need charts, but I will for the EventBus stuff coming soon.

 

Now

mvn install

should lead to a successful build.

 

It would be real nice to generate an executable jar file that has the entire classpath in it so that launching the app is a simple java -jar command.  To do this add the following as a plugin after the compiler plugin configuration shown above:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
   <execution>
     <goals>
       <goal>attached</goal>
     </goals>
     <phase>package</phase>
     <configuration>
       <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
       </descriptorRefs>
       <archive>
         <manifest>
           <mainClass>org.eventbus.tutorials.pivot.stocktracker.StockTracker</mainClass>
         </manifest>
       </archive>
     </configuration>
   </execution>
 </executions>
</plugin>
 

Now run build again and launch the app:

mvn clean install
java -jar target/stocktracker-1.0-SNAPSHOT-jar-with-dependencies.jar

Pivot Stock Tracker

Pivot Stock Tracker Built with Maven

Setting up a GitHub repo, Secure Access on Mac

I installed GitHub by first upgrading my mac to Snow Leopard, then installing the free XCode tools for Snow Leopard the Snow Leopard upgrade was not necessary, but I wanted to upgrade anyway and the XCode tools were specific to the OS version.  After the XCode install, typed git in the terminal and I was in business.

Following up on the post before last, there’s nothing interesting at all in setting up security for amazon ec2 and GitHub, since we pointed to a specific key file when ssh’ing into Amazon.  Secure access to GitHub is very well documented. The MacOS makes dealing with pasphrases very easy.  Wow, I’m impressed and am certainly not going back to Windows.

I created a repo using the GitHub web pages for my account, which creates a page for the repo with full instructions on how to use it.  How convenient.

Changing MySQL Password for Alfresco EC2

Well, all kinds of walls are breaking down this week.  I’m using MySQL for the first time.  Most of my DB experience is in Oracle, I’ve used Sybase, but for open source databases, I’m a fan of Postgresql, and Derby is great for Java on the client. I’ve avoided MySQL since I can’t respect a database that didn’t have transactions until version 5 and uses a “root” user.

The official Alfresco EC2 AMI uses MySQL.  There are two users – the root (the MySQL root) and the alfresco user, both are in the mysql database (as opposed to the alfresco database).  To change the default passwords do this:

ubuntu@domU-12-31-39-09-E1-A1:~$ mysqladmin -u root -p'alfresco' password 'newpass'

ubuntu@domU-12-31-39-09-E1-A1:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.1.37-1ubuntu5 (Ubuntu)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> update user set password=PASSWORD(“new password”) where User=’alfresco’;

mysql> show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| alfresco           |
| mysql              |
+——————–+
3 rows in set (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=PASSWORD(“new password”) where User=’alfresco’;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

I’m surprised there were two rows updated, but we’ll close our eyes and cross our fingers.

Lastly, change it in the alfresco config file:

sudo vi  /usr/share/tomcat6/shared/classes/alfresco-global.properties

SSH to EC2 Instance and GitHub on a Mac

Yup, I’m moving to the Mac.  It’s really cool – it has this terminal thingy that you can just type commands into – what an elegant interface!  Simple black and white and it’s real clear that all I need to do is type!  ;-)

A windows user asked me how to log into an Amazon EC2 instance using SSH.  I said, “um, well , you, wait, I was just doing this … um.  I’ll look it up and send you instructions.”  Yes, I had already done it, but was confused because I was actually in the middle of setting up secure access for GitHub as well.   I’m going to host some demos for the EventBus (particularly for Apache Pivot) on GitHub since my project on java.net will forever be stuck in CVS even though new projects can use SVN or Mercurial.

So let’s configure this Mac to talk to a new EC2 instance, then allow it to hook up to GitHub too (in a second post).  That might be mildly interesting since there may be a cat of keys involved – OK, maybe not interesting anyway.

To use ssh on an EC2 instance, ensure that when you create the instance, you also create a security group with port 22 open, since that’s what SSH runs over.   You will also create a keypair.  (You don’t need the Access Key Identifiers that you use for command line tools.  The ssh keys for an instance are different from the Access Key Identifiers and are created when you create (or reuse) a keypair for the instance. )

When you create the keypair for the instance you are prompted to download the key file (pem).  This is the private part of the keypair.  If you lose it, you are out of luck, AFAIK.  By default, on my Mac using Firefox, the download, the private part of the pair, gets downloaded to /Users/mbushe/Downloads/<name-of-key-pair>.pem.  The public part of the keypair is held on the server.  You will be forced by ssh to protect your private key, so run:

chmod 700 /Users/mbushe/Downloads/<name-of-key-pair>.pem

..so that only you can access it on your local box.  I’m going to assume you are connecting as root, but a good image will have you connect as another user, set up special.  (OTOH, sometimes you have to log in as root.  For example, if you are using a Content Management System that opens the CIFS port, you need to log in as root since it’s under 1000.)  The supported Alfresco EC2 image will boot you if you log in as root and ask you to in as ubuntu.

To connect using ssh as root, do the following (I’m giving all my info since I’m trashing this instance and key anyway):

ssh -i /Users/mbushe/Downloads/my-key-pair-name.pem root@ec2-67-202-0-107.compute-1.amazonaws.com

You will get this:The authenticity of host ‘ec2-67-202-0-107.compute-1.amazonaws.com (67.202.0.107)’ can’t be established.
RSA key fingerprint is 24:e6:b6:62:fd:f3:54:c0:5f:86:06:97:8d:b7:d4:4f.
Are you sure you want to continue connecting (yes/no)?

At this point, you are connecting with something out there in the world that has a public key with a certain fingerprint that’s displayed.  Some nafarious teenager in your neighbor could be sitting on your wireless network and be running a DNS server for you that points you to his machine instead of yours, or someone scarier anywhere between you and what you are connecting to.  My neighbors likely haven’t hacked into my amazon box and stolen the public key though.  Can you trust everyone on Amazon’s staff though?  If I wanted to cause trouble, that’s the job I’d be applying for. This is why Bruce Schneier consults on human hacks now.  But to limit the risk, you should connect to your AWS console, click on the instance (not the keypairs), and show the system log.  Likely somewhere near the bottom, you’ll see the keys for the fingerprints:

ec2: #############################################################
ec2: -----BEGIN SSH HOST KEY FINGERPRINTS-----
ec2: 2048 66:27:32:9f:05:f7:84:97:53:f4:53:46:67:36:5f:6d /etc/ssh/ssh_host_rsa_key.pub (RSA)
ec2: 1024 00:84:16:fd:4e:5f:7d:8d:07:20:40:1a:e3:36:0b:94 /etc/ssh/ssh_host_dsa_key.pub (DSA)
ec2: -----END SSH HOST KEY FINGERPRINTS-----
ec2: #############################################################

Look at the RSA fingerprint from the system log and see if it matches the output of ssh.  If it does, type yes, get warm fuzzy feelings and continue on.  If it doesn’t type no and trash your instance!  Hopefully you set up your instance via a script you can run again on a new instance.  Isn’t virtualization great?

First post, 4th blog

I’m starting this blog as my techie blog.  I’ve had other techie blogs mostly with drivel that is now woefully out of date on tech sites that change, get bought, etc.  I figure a tech-neutral setting allows me to write about my wide range of tech interests.

I also have a blog that is just starting now on the domain for my open source project: www.eventbus.org.  It’s a Blog/Wiki actually, or as Martin Fowler calls it, a Bliki.  I’m still in the middle of my first blog post on Apache Pivot and the EventBus, it’s already quite long.

I now will use michaelbushe.blogspot.com as my personal blog.  I never posted techie stuff there anyway.

Follow

Get every new post delivered to your Inbox.