Archive for category java

Simple JSON-RPC updated to 1.0.0

Simple JSON-RPC has been updated to 1.0.0. This new version includes a brand new event messaging system making it possible for classes to react to events generated by the Simple JSON-RPC framework.

Through simple event messaging, your RPC classes are now able to react to and even interact with servlet configuration, context, request, and response objects. To use this facility your class needs to extend the new JSONRPCEventListener interface and implement the messageReceived method, accepting one argument of the JSONRPCMessageEvent type.

Here is an example of how a class could implement a Simple JSON-RPC event listener:

public void messageReceived(JSONRPCMessageEvent me) {
	switch(me.message().getCode()) {
		case JSONRPCMessage.INIT:
			config = me.message().getServletConfig();
		break;
		case JSONRPCMessage.BEFOREREQUEST:
			request = me.message().getRequest();
			session = request.getSession(true);
		break;
		case JSONRPCMessage.BEFORERESPONSE:
			response = me.message().getHttpResponse();
			response.setContentType("text/html");
		break;
		case JSONRPCMessage.AFTERRESPONSE:
			lastresponse = me.message().getRPCResponse();
		break;
	}
}

This new feature should also make it easier to implement solutions that use sessions. This should also make it possible for classes to react to the output of other classes, including errors.

Tags: , , , ,

HBase JSON-RPC Bridge updated, allows scanning

The HBase JSON-RPC bridge has been updated to allow scanning on non-key columns. Now, passing a filter parameter creates a scanner using a RegexStringComparator. Results are returned in 100 block chunks until null is returned, in which case the ResultScanner is closed

Here is an example query that is now possible:

http://localhost:8080/hbasebridge/rpc?debug=true&data={"method":"lookup","params":{"table":"hbasetable","filter":"searchstring"}}

Tags: , , , ,

Bible Flashcards for Android 2.0

It’s been a while since I released Bible Flashcards version 1.0. Long enough in fact that I started getting emails asking whether I intended on updating past the 1.1.4 release.

Well I am proud to announce version 2.0 which includes a lot of requested features and a lot of bug-fixes and improvements. For this release I decided to take my time and re-factor the way various screens interacted, making them more modular and self-contained which should translate into fewer force closes. I also unleased the monkey on my app which helped me improve my code even more.

So without further ado here are the major features included in this release:

  • Ability to mark cards learned/unlearned
  • Ability to choose a random card
  • Cards now cycle so that if you are on card 1 and you attempt to go backwards, you are taken to the last card. Likewise if you are on the last card and you attempt to go forwards you are taken to the first card.
  • Menus also cycle so that if you hit the back button you will go from the active card view to the lesson chooser to the lesson set chooser and back to the card view.
  • Preferences to control settings such as card text size, whether to display learned cards, and a button to remove all learned cards from the internal database.

Some interface items had to be chopped to make room for these improvements. So if you are wondering where the next/back buttons are, they have been removed in favor of the more intuitive gesture controls. Swipe right for next card and left for previous card. Tapping the card flips it over.

I also want to give a special thanks to everyone who contacted me with words of encouragement and everyone who brought to my attention things that were broken and things that could be improved upon.

This release also includes a new lesson set, RossWords, which is being put together by  Samuel Rogers as he takes Hebrew this year. If anyone else is feeling generous  and would like to help out, feel free to contact me about a lesson set that you could help me improve upon or create as Samuel is doing.

Here are some screen shots from the new version:

And as usual, you can find Bible Flashcards in the Android Marketplace by searching for “Bible Flashcards” or by scanning the barcode below:

Tags: , , ,

Simple JSON-RPC updated to 0.9.7

Simple JSON-RPC has been updated to 0.9.7. This new version includes the ability to enable using full class names rather than simple method names. This makes using multiple classes with the same public method names possible.

To enable this functionality simply add an init-param to your web.xml file like:

<init-param>
	<param-name>use_full_classname</param-name>
	<param-value>true</param-value>
</init-param>

With this feature enabled, you then call methods via their full classname + method name separated by a dot (this nomenclature is for both static as well as non-static methods, the framework handles the particulars in the back-end).

Special thanks to Stephan of Cross Pollinate for suggesting this improvement!

Tags: , , ,

Bible Flashcards for Android 1.0

Bible Flashcards is an Android application based on the data files provided by the Crosswire Bible Society‘s Flashcard application. It contains flashcards for both Greek and Hebrew along with appropriate fonts for proper display.

I’ve also included an additional lesson set titled “greekBasics” which includes flashcards for the Greek alphabet.

Here are some screenshots:

You can find Bible Flashcards in the Android app Market by entering “Bible Flashcards” or by scanning the barcode below:

Additionally, if you are interested in helping out with new lessons or if you would like to provide feedback, please feel free to email me.

Tags: , , , , , , ,

Learning Languages: Java

The first thing you’ll need to consider is the development environment you want to use primarily. This is important as it will have an impact on how you run through tutorials and examples later on.

Environments can be broken down into two broad categories; command-line or a visual IDE. Both have their merits and you’ll eventually need to be familiar with both (especially if you expect to be releasing production code or participating in any well-maintained development environment).

The most common command-line environments are Maven and Ivy. Both come with a somewhat steep learning curve (which, unfortunately is unavoidable) but both are well worth investigating as they are both very common in production environments.

Starting out, however, you’ll most likely find that using a visual IDE will help you get right down to learning and compiling example code fairly quickly.

There are several common IDEs; IntelliJ, NetBeans, and Eclipse are all great ones that I’ve seen used in production. My favorite hands-down is Eclipse, especially since it also has configurations to help you develop in other languages such as PHP (Aptana) and C/C++.

For general basics and a broad overview of Java; I would recommend you take a look at the Java Beginner site.

There’s also several handy video tutorials (mostly using Eclipse) on YouTube such as this one:

Once you get the basics down, I’ve found that working on a full project helps. A great place to start would be to help our with an existing open-source project like JSword.

Tags: , , , ,

Javascript implementation of Java’s String.hashCode() method

Here is a direct replacement for Java’s String.hashCode() method implemented in Javascript.

I wrote this function to fulfill a requirement at work. Apparently, the back-end engineers thought hashCode() was a standard function. One of the hurdles for this project was not only figuring out how to translate the mathematical formula used in Java to generate hashCode()’s but also how to force Javascript to use 32bit integer math (no small feat).

Fortunately, I discovered that Java supports bitwise operators which are constrained to 32bit integer math.

So here’s the resulting String prototype in Javascript. With this prototype you can simply call .hashCode() on any string, ie. “some string”.hashCode(), and receive a numerical hash code (more specifically, a Java equivalent) such as 1395333309.

String.prototype.hashCode = function(){
	var hash = 0;
	if (this.length == 0) return code;
	for (i = 0; i < this.length; i++) {
		char = this.charCodeAt(i);
		hash = 31*hash+char;
		hash = hash & hash; // Convert to 32bit integer
	}
	return hash;
}

Tags: , ,

Native Sword libraries for Android

I’ve spend quite a bit of time recently figuring out the best approach for incorporating some form of Sword libraries1 into my Android application. After an unsuccessful attempt to get the pure Java implementation, JSword, to work2 I decided to see if I could, instead, use the Android Native Development Kit and wrap the C/C++ library in a Java Native Interface.

After doing some digging on the web I found out that Troy over at Crosswire had already begun a project named “bishop” whose aim was to provide a Java native interface to the sword library. Just what I was looking for!

While the process of building the library and corresponding java source files is more than what I want to get into here (though I may later on as I hopefully help contribute to the maturity of the project) I wanted to share my findings with anyone else who, like me, is interested in bringing more open-source Bible applications to the Android platform.

So without further ado; Here is a jar file and corresponding JNI library you can use in your own Android project to harness the power of Sword in your own Bible-related apps.

  1. Sword is an excellent suite of libraries for accessing a large array of Bibles and Bible-related modules stored in an open format. []
  2. The fault here was not with the JSword project per-se, the fault really lies with the limited Java environment provided by the Dalvik JVM. []

Tags: , , ,

Simple HBase query bridge

I’ve recently released a simple json-rpc query bridge (using our own simple json-rpc framework) for HBase at http://code.google.com/p/hbasebridge/

You can use this bridge to query HBase for either the current record or the last few versions of a record.

To see the methods

http://localhost:8080/hbasebridge/rpc?debug=true

Which returns a list of usable RPC methods:

{
  "jsonrpc": "2.0",
  "result": {"method": [
    {
      "class": "com.werxltd.hbasebridge.HBaseInfo",
      "name": "listtables",
      "params": [],
      "returns": "org.json.JSONObject",
      "static": false
    },
    {
      "class": "com.werxltd.hbasebridge.HadoopInfo",
      "name": "clusterstatus",
      "params": [],
      "returns": "org.json.JSONObject",
      "static": false
    },
    {
      "class": "com.werxltd.hbasebridge.HadoopInfo",
      "name": "jobstatus",
      "params": [],
      "returns": "org.json.JSONObject",
      "static": false
    },
    {
      "class": "com.werxltd.jsonrpc.RPC",
      "name": "listrpcmethods",
      "params": [],
      "returns": "org.json.JSONObject",
      "static": false
    },
    {
      "class": "com.werxltd.hbasebridge.TableLookup",
      "name": "lookup",
      "params": ["org.json.JSONObject"],
      "returns": "org.json.JSONObject",
      "static": false
    }
  ]}
}

To list tables:

http://localhost:8080/hbasebridge/rpc?debug=true&method=listtables

Which returns:

{
  "jsonrpc": "2.0",
  "result": {"tables": [
    "mytable"
  ]}
}

To get the current status of the cluster:

http://localhost:8080/hbasebridge/rpc?debug=true&method=clusterstatus

Which returns:

{
  "jsonrpc": "2.0",
  "result": {
    "activetrackernames": [
      "trackernode1:localhost/127.0.0.1:33455",
      "trackernode2:localhost/127.0.0.1:54616",
    ],
    "blacklistedtrackernames": [],
    "blacklistedtrackers": 0,
    "jobqueues": {"queues": [{
      "jobs": [
        {
          "cleanuptasks": [{"state": ""}],
          "complete": false,
          "filename": "hdfs://hadoophdfsnode:9000/data/hadoop/mapred/system/job_201003191557_0442/job.xml",
          "jobpriority": "normal",
          "mapprogress": 1,
          "name": "My mapreduce job",
          "reduceprogress": 0.9819000363349915,
          "runstate": "running",
          "schedulinginfo": "NA",
          "setupprogress": 1,
          "starttime": 1269024863960,
          "username": "hadoop-admin"
        }
      ],
      "name": "default"
    }]},
    "jobtrackerstate": "running",
    "maptasks": 1,
    "maxmaptasks": 116,
    "maxmemory": 2079719424,
    "maxreducetasks": 58,
    "reducetasks": 16,
    "tasktrackers": 34,
    "ttyexpiryinterval": 600000,
    "usedmemory": 969170944
  }
}

Key/Value Query:

http://localhost:8080/hbase_tape/rpc?debug=true&data={"method":"lookup","params":{"table":"tablename","keys":["mykey"]}}

Results:

{
  "jsonrpc": "2.0",
  "result": {"rows": [{"mykey": {
    "family:col": "myvalue"
  }}]}
}

Key/Value query with versions:

http://localhost:8080/hbase_tape/rpc?debug=true&data={"method":"lookup","params":{"table":"tablename","keys":["mykey"],versions:2}}

Results:

{
  "jsonrpc": "2.0",
  "result": {"rows": [{"mykey": {
    "family:col": [{
      "value": "myval",
      "version": 123456789
    }],
    "family:col": [{
      "value": "myoldval",
      "version": 123456788
    }]
  }}]}
}

The code should also provide a handy reference for anyone who wants to learn how to query HBase and scrape Result objects for values without knowing family or column names in advance.

Tags: , , , , , , , ,

Simple JSON-RPC updated to 0.9.5

The simple JSON-RPC package has been updated to 0.9.5 It has undergone some extensive refactoring and now includes documentation, and an example project. The source to this package is also available here.

For more information (and for future updates), visit the new project page here.

If you are interested in using, contributing to, or reporting bugs for this project, contact us!

Tags: , , ,