[java] archive category

Goodbye XML

I’m leaving you XML. I’m just not into you anymore. I don’t think I ever was…I just used you. Sorry.

Why? You’re just too damn hard to read and you give me a headache. I always have to check if you’re well-formed and, frankly, most of the time, you’re not.

It’s not because you have only one root. No, it’s not. You’re always so damn verbose…and you use tabs everywhere we go and who picks those up? I do. That’s who.

Well, it stops today.

Yes, there is someone else.

  • Share/Bookmark

Running Cassandra 0.5.0 on Mac OS X Leopard 10.5.8

This is a short post showing you how to install the superdooper Key-Value Store Cassandra version 0.5.0 on a Macbook running OS X Leopard 10.5.8 (yeah I know, I should upgrade to Snow Leopard sometime). Cassandra 0.5 promises to be peppier than the 0.4 version and at this point it hasn’t been released yet, but I’m impatient, so I’m setting up a ‘one-node cluster’ for fun.

Cassandra 0.5.0 was released on 25 Jan 2010.

You will need to ensure that you are running Java 1.6. Since Java 1.5 is the default on Mac OS X, you need to set up a couple of environment variables. I just put the following into my .bash_profile file using vim.

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
export PATH=$JAVA_HOME/bin:$PATH

Now source the file (if you haven’t already done that) and examine your set-up.

Trinity:~ kelvin$ source .bash_profile
Trinity:~ kelvin$ env | grep JAVA_HOME
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
Trinity:~ kelvin$ java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-9M3125)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)
Trinity:~ kelvin$ xcodebuild -version
Xcode 3.1.2
Component versions: DevToolsCore-1148.0; DevToolsSupport-1102.0
BuildVersion: 9M2621

Yeah, my xcode is a bit old too. So, once that is done, download Cassandra. I am going to use the tag in the Subversion repository for the 0.5.0 release.

Trinity:code kelvin$ svn co https://svn.apache.org/repos/asf/incubator/cassandra/tags/cassandra-0.5.0/ cassandra-0.5.0

...svn key gibberish redacted...

A    cassandra-0.5.0/NOTICE.txt
 cassandra-0.5.0/src/java/org/apache/cassandra/io/StreamRequestVerbHandler.java
...checkout redacted...
A    cassandra-0.5.0/build.xml
 U   cassandra-0.5.0
Checked out revision 902040.
Trinity:code kelvin$ cd cassandra-0.5.0
Trinity:cassandra-0.5.0 kelvin$ pwd
/Users/kelvin/code/cassandra-0.5.0
Trinity:cassandra-0.5.0 kelvin$

Cassandra expects some folders to be available, so we will need to set them up (per the instructions in the README file). First of all, check the file conf/storage-conf.xml and make sure that these folders exist before you do anything else.


  <CommitLogDirectory>/var/lib/cassandra/commitlog</CommitLogDirectory>
  <DataFileDirectories>
      <DataFileDirectory>/var/lib/cassandra/data</DataFileDirectory>
  </DataFileDirectories>
  <CalloutLocation>/var/lib/cassandra/callouts</CalloutLocation>
  <StagingFileDirectory>/var/lib/cassandra/staging</StagingFileDirectory>

If they do not exist, you will have to make them (careful with the whoami command in backticks):

Trinity:cassandra-0.5.0 kelvin$ sudo mkdir -p /var/log/cassandra
Password:
Trinity:cassandra-0.5.0 kelvin$ sudo chown -R `whoami` /var/log/cassandra
Trinity:cassandra-0.5.0 kelvin$ sudo mkdir -p /var/lib/cassandra
Trinity:cassandra-0.5.0 kelvin$ sudo chown -R `whoami` /var/lib/cassandra

For logging, Cassandra expects that a system.log file is available. Check the conf/log4j.properties file to check where it expects a log file. It should be similar to this:

log4j.appender.R.File=/var/log/cassandra/system.log

When you start Cassandra for the first time it will start the log file.

I used ‘ant‘ to build Cassandra. Make sure that you are in the root of the Cassandra directory. I have a lot of crap in my Java classpath, so I will temporarily exclude it.

Trinity:cassandra-0.5.0 kelvin$ ant -noclasspath
Buildfile: build.xml

build-subprojects:

init:
    [mkdir] Created dir: /Users/kelvin/code/cassandra-0.5.0/build/classes
    [mkdir] Created dir: /Users/kelvin/code/cassandra-0.5.0/build/test/classes
    [mkdir] Created dir: /Users/kelvin/code/cassandra-0.5.0/src/gen-java

check-gen-cli-grammar:

gen-cli-grammar:
     [echo] Building Grammar /Users/kelvin/code/cassandra-0.5.0/src/java/org/apache/cassandra/cli/Cli.g  ....

build-project:
     [echo] apache-cassandra-incubating: /Users/kelvin/code/cassandra-0.5.0/build.xml
    [javac] Compiling 247 source files to /Users/kelvin/code/cassandra-0.5.0/build/classes
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

build:

BUILD SUCCESSFUL
Total time: 13 seconds
Trinity:cassandra-0.5.0 kelvin$

Now the moment of truth!

Trinity:cassandra-0.5.0 kelvin$ bin/cassandra -f
Listening for transport dt_socket at address: 8888
INFO - Saved Token not found. Using 25907362644306974147256376871662350143
INFO - Starting up server gossip
INFO - Cassandra starting up...

Okay, Cassandra is now waiting for us to do something, so we can open up another Terminal window (Cmd+N) and try the test described in the README file:

Trinity:cassandra-0.5.0 kelvin$ bin/cassandra-cli --host localhost --port 9160
Connected to localhost/9160
Welcome to cassandra CLI.

Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra> set Keyspace1.Standard1['jsmith']['first'] = 'John'
Value inserted.
cassandra> set Keyspace1.Standard1['jsmith']['last'] = 'Smith'
Value inserted.
cassandra> set Keyspace1.Standard1['jsmith']['age'] = '42'
Value inserted.
cassandra> get Keyspace1.Standard1['jsmith']
=> (column=last, value=Smith, timestamp=1264159272089)
=> (column=first, value=John, timestamp=1264159260000)
=> (column=age, value=42, timestamp=1264159282593)
Returned 3 results.
cassandra>

Okay, we now have stored and retrieved some data. In the next installment I’ll try to get Thrift running!

  • Share/Bookmark

Tags: , , , , , ,

Running Shindig on your Mac

If you run a social network, you really owe it to your users to investigate OpenSocial as a way of allowing your users to add third-party functionality to their profiles. Shindig (available in a PHP and a Java version) allows you to easily add this capability.

Mac OS X Leopard has Java 1.6 and Maven 2.0.6 installed. I had some build issues with the older Maven so I upgraded to 2.0.9. You also need to ensure that your JAVA_HOME variable is set. You can confirm this by typing the following commands in a terminal:

Trinity:shindig kelvin$ echo $JAVA_HOME
/Library/Java/Home
Trinity:shindig kelvin$ mvn -v
Maven version: 2.0.9
Java version: 1.6.0_07
OS name: "mac os x" version: "10.5.5" arch: "x86_64" Family: "mac"

Then download the code from the main repository:

Trinity:code kelvin$ mkdir shindig
Trinity:code kelvin$ svn co http://svn.apache.org/repos/asf/incubator/shindig/trunk/ ./shindig
A    shindig/NOTICE
A    shindig/java
A    shindig/java/social-api
A    shindig/java/social-api/src
A    shindig/java/social-api/src/test

...la dee da...

A    shindig/features/mocks/env.js
A    shindig/features/mocks/xhr.js
 U   shindig
Checked out revision 707938.

Okay, Shindig has now been downloaded. Let’s build it. You can try building it using ‘mvn clean install’ first and if you get any errors, try turning off the tests. Sometimes the tests can get out of sync with the code. I was able to build it without any problems.

Trinity:shindig kelvin$ mvn clean install
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO]   Apache Shindig Project
[INFO]   Apache Shindig Project Parent
[INFO]   Apache Shindig Common Code
[INFO]   Apache Shindig Gadget Renderer
[INFO]   Apache Shindig Social API
[INFO]   Apache Shindig Features
[INFO]   Apache Shindig Web App
[INFO] ------------------------------------------------------------------------
[INFO] Building Apache Shindig Project
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory /Users/kelvin/code/shindig/target
[INFO] [site:attach-descriptor]

...etc etc etc...

[INFO] [install:install]
[INFO] Installing /Users/kelvin/code/shindig/java/server/target/shindig-server-1-SNAPSHOT.war to /Users/kelvin/.m2/repository/org/apache/shindig/shindig-server/1-SNAPSHOT/shindig-server-1-SNAPSHOT.war
[INFO] Installing /Users/kelvin/code/shindig/java/server/target/shindig-server-1-SNAPSHOT-sources.jar to /Users/kelvin/.m2/repository/org/apache/shindig/shindig-server/1-SNAPSHOT/shindig-server-1-SNAPSHOT-sources.jar
[INFO]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] Apache Shindig Project ................................ SUCCESS [6.196s]
[INFO] Apache Shindig Project Parent ......................... SUCCESS [0.531s]
[INFO] Apache Shindig Common Code ............................ SUCCESS [9.470s]
[INFO] Apache Shindig Gadget Renderer ........................ SUCCESS [25.571s]
[INFO] Apache Shindig Social API ............................. SUCCESS [11.381s]
[INFO] Apache Shindig Features ............................... SUCCESS [7.117s]
[INFO] Apache Shindig Web App ................................ SUCCESS [13.737s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 14 seconds
[INFO] Finished at: Sun Nov 09 10:43:56 PST 2008
[INFO] Final Memory: 51M/80M
[INFO] ------------------------------------------------------------------------

or if you want to skip the tests, try this…

Trinity:shindig kelvin$ mvn clean install -Dmaven.test.skip=true

Woot. Now let’s start it up. By default, the server will run on localhost port 8080.

Trinity:shindig kelvin$ mvn -Prun
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'jetty'.
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.1/maven-antrun-plugin-1.1.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-plugin-parent/2.0.1/maven-plugin-parent-2.0.1.pom
7K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.1/maven-antrun-plugin-1.1.jar
11K downloaded
[INFO] ------------------------------------------------------------------------
[INFO] Building Apache Shindig Project
[INFO]    task-segment: [jetty:run-war]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing jetty:run-war

...more more more...

[INFO] Starting jetty 6.1.9 ...
2008-11-09 10:47:45.790::INFO:  jetty-6.1.9
2008-11-09 10:47:45.812::INFO:  Extract jar:file:/Users/kelvin/code/shindig/java/server/target/shindig-server-1-SNAPSHOT.war!/ to /Users/kelvin/code/shindig/target/work/webapp
2008-11-09 10:47:47.372::INFO:  No Transaction manager found - if your webapp requires one, please configure one.
Nov 9, 2008 10:47:48 AM org.apache.shindig.common.JsonContainerConfig loadContainers
INFO: Loading resources from: containers/default/container.js
Nov 9, 2008 10:47:48 AM org.apache.shindig.common.JsonContainerConfig loadResources
INFO: Reading container config: containers/default/container.js
2008-11-09 10:47:48.313::INFO:  Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

See if you can render anything by opening a web browser and putting the following into the address bar.

http://localhost:8080/gadgets/ifr?url=http://www.labpixies.com/campaigns/todo/todo.xml

If all went well, you should see the To Do list demo.

The To Do Shindig App

The To Do Shindig App

  • Share/Bookmark

Tags: , ,

Upgrade your Maven on OS X Leopard

As of Mac OS X 10.5.5, Java is now up to 1.6 and Maven comes pre-installed at version 2.0.6. We’re going to upgrade Maven to the latest version 2.0.9. Check your versions!

Trinity:~ kelvin$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode)
Trinity:~ kelvin$ which mvn
/usr/bin/mvn
Trinity:~ kelvin$ /usr/bin/mvn -v
Maven version: 2.0.6
Trinity:~ kelvin$
Trinity:~ kelvin$ 

Visit the Maven web site and download the latest version (currently 2.0.9). It downloads to the Download folder, so you will have to move it. I installed Maven to /usr/local/apache-maven

Trinity:~ kelvin$ sudo sh
Password:
sh-3.2# cd /usr/local
sh-3.2# wget http://gulus.USherbrooke.ca/pub/appl/apache/maven/binaries/apache-maven-2.0.9-bin.zip
--2008-11-09 01:34:39--  http://gulus.usherbrooke.ca/pub/appl/apache/maven/binaries/apache-maven-2.0.9-bin.zip
Resolving gulus.usherbrooke.ca... 206.167.141.10
Connecting to gulus.usherbrooke.ca|206.167.141.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2025041 (1.9M) [application/zip]
Saving to: `apache-maven-2.0.9-bin.zip'

100%[======================================================================================>] 2,025,041    456K/s   in 5.1s    

2008-11-09 01:34:45 (389 KB/s) - `apache-maven-2.0.9-bin.zip' saved [2025041/2025041]

sh-3.2# md5 apache-maven-2.0.9-bin.zip
MD5 (apache-maven-2.0.9-bin.zip) = 5e92d1c5b0a6580127059e28fe2f2b45
sh-3.2# unzip apache-maven-2.0.9-bin.zip
Archive:  apache-maven-2.0.9-bin.zip
   creating: apache-maven-2.0.9/
   creating: apache-maven-2.0.9/conf/
  inflating: apache-maven-2.0.9/LICENSE.txt
  inflating: apache-maven-2.0.9/NOTICE.txt
  inflating: apache-maven-2.0.9/README.txt
   creating: apache-maven-2.0.9/bin/
  inflating: apache-maven-2.0.9/bin/m2.bat
  inflating: apache-maven-2.0.9/bin/m2.conf
  inflating: apache-maven-2.0.9/bin/mvn.bat
  inflating: apache-maven-2.0.9/bin/mvnDebug.bat
  inflating: apache-maven-2.0.9/bin/m2
  inflating: apache-maven-2.0.9/bin/mvn
  inflating: apache-maven-2.0.9/bin/mvnDebug
  inflating: apache-maven-2.0.9/conf/settings.xml
   creating: apache-maven-2.0.9/lib/
  inflating: apache-maven-2.0.9/lib/maven-2.0.9-uber.jar
   creating: apache-maven-2.0.9/boot/
  inflating: apache-maven-2.0.9/boot/classworlds-1.1.jar
sh-3.2# exit
Trinity:~ kelvin$

The Maven installation instructions call for the creation of the M2 environment variables, so we need to do that. I put them into the .bash_profile file which is in your home directory.

export M2_HOME=/usr/local/apache-maven-2.0.9
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Put these somewhere near the top of your .bash_profile file so that the shell will find the mvn 2.0.9 version before it finds the old 2.0.6 version at /usr/bin/mvn.

Trinity:~ kelvin$ source .bash_profile
Trinity:~ kelvin$ mvn -v
Maven version: 2.0.9
Java version: 1.6.0_07
OS name: "mac os x" version: "10.5.5" arch: "x86_64" Family: "mac"
Trinity:~ kelvin$ echo $M2_HOME
/usr/local/apache-maven-2.0.9
Trinity:~ kelvin$ which mvn
/usr/local/apache-maven-2.0.9/bin/mvn
Trinity:~ kelvin$ 
  • Share/Bookmark

Tags:

jEdit on Vista

I use jEdit to do most of my coding. I had some issues using the “Open with…” feature on Vista Ultimate. For some reason the right-click menu would not launch jEdit 4.3pre12 on Java 1.6.0_03.

I got it working by editing the registry and editing the bat file. For some reason the path to javaw.exe was incorrect. If you fix these paths, everything works as expected.

Fixing the registry

  • Open the registry editor using “regedt32″ in the Run dialog (click “the Windows key”-R)
  • Go to: Computer\HKEY_CLASSES_ROOT\*\shell\Open with jEdit\Command
  • Change the key Data: “C:\Program Files (x86)\Java\jre1.6.0_03\bin\javaw.exe” -Xms64M -Xmx192M -jar “C:\Program Files (x86)\jEdit\jedit.jar” -reuseview “%1″
  • Click “OK”

Fixing the bat file

  • Open Windows Explorer and go to: C:\Program Files (x86)\jEdit
  • Edit the jEdit.bat file
  • Modify this line: start “jEdit startup” “C:\Program Files (x86)\Java\jre1.6.0_03\bin\javaw.exe” -Xms64M -Xmx192M -jar “C:\Program Files (x86)\jEdit\jedit.jar” -reuseview %*
  • Share/Bookmark

ElvisJoke

public class ElvisJoke {
	public static void main(String[] args) {
		ElvisJoke haha = new ElvisJoke();
		TNode theBuilding = haha.new TNode();
		TNode elvis = haha.new TNode(theBuilding,null,"Elvis");
		if (elvis.hasLeft(theBuilding)){
			System.out.println("Thank you very much!");
		}
	}
	private class TNode {
		TNode left;
		TNode right;
		String data;
		TNode(){
			this(null,null,"No Data");
		}
		TNode(TNode left, TNode right, String data){
			this.left = left;
			this.right = right;
			this.data = data;
		}
		boolean hasLeft(TNode node){
			return (left!=null);
		}
		boolean hasRight(TNode node){
			return (right!=null);
		}
		String getData() {
			return this.data;
		}
	}
}
  • Share/Bookmark

Morse code for Java

I was rummaging through some old files and I found a Morse Code object I wrote when I was playing around with binary trees. I thought that this might be useful to someone, so here it is licensed under the MIT license.

  • Share/Bookmark