Sunday, September 27, 2009

Developing in Android without Eclipse

Recently I'm back in java land, more specifically android. The past few months I've exclusively been doing scripting languages using much more primitive editors (textmate, emacs). Going back to java, I'm finding Eclipse is just getting in my way. So I decided to try and develop an android app using textmate and found it incredibly easy. It appears you have everything you need from the command line tools and all the eclipse plugin does is interface with those.

Step 1, create your project

THis couldn't be easier:

android create project --target 2 --name install_test --path . --activity InstallTest --package com.jonandkerry.install

Obviously make sure your android sdk is in your path. NOTE: if you are using SNow leopard you will need to patch your android install. See here


Step 2, build your project

The android create project tool actually builds you a very simple java skeleton project with an ant build.xml. You get several targets:

aidl
android_rules.aidl
android_rules.compile
android_rules.debug
android_rules.debug-sign
android_rules.dex
android_rules.dirs
android_rules.help
android_rules.install
android_rules.no-sign
android_rules.package
android_rules.package-resources
android_rules.release
android_rules.release-package
android_rules.release.check
android_rules.release.nosign
android_rules.resource-src
android_rules.uninstall
android_rules.uninstall.check
android_rules.uninstall.error
compile
debug
debug-sign
dex
dirs
help
install
no-sign
package
package-resources
release
release-package
release.check
release.nosign
resource-src
uninstall
uninstall.check
uninstall.error
Default target: help


obviously you see and "ant compile", "ant install", etc. Its important to note that ant compile will generate the R.java resource just like eclipse does.

Step 3, run

Couldn't be easier.

$ ant install
Buildfile: build.xml
[setup] Project Target: Android 1.6
[setup] API level: 4

dirs:
[echo] Creating output directories if needed...

resource-src:
[echo] Generating R.java / Manifest.java from the resources...
[exec] (skipping hidden file '/Users/jonathan/Development/personal/install_test/res/.DS_Store')
[exec] (skipping hidden file '/Users/jonathan/Development/personal/install_test/res/layout/.DS_Store')

aidl:
[echo] Compiling aidl files into Java classes...

compile:
[javac] Compiling 1 source file to /Users/jonathan/Development/personal/install_test/bin/classes

dex:
[echo] Converting compiled files and external libraries into bin/classes.dex...

package-resources:
[echo] Packaging resources
[aaptexec] Creating full resource package...
[null] (skipping hidden file '/Users/jonathan/Development/personal/install_test/res/.DS_Store')
[null] (skipping hidden file '/Users/jonathan/Development/personal/install_test/res/layout/.DS_Store')

debug-sign:

package:
[apkbuilder] Creating install_test-debug-unaligned.apk and signing it with a debug key...
[apkbuilder] Using keystore: /Users/jonathan/.android/debug.keystore
[apkbuilder] /Users/jonathan/Development/personal/install_test/bin/classes.dex => classes.dex

debug:
[echo] Running zip align on final apk...
[echo] Debug Package: bin/install_test-debug.apk

install:
[echo] Installing bin/install_test-debug.apk onto default emulator...
[exec] 317 KB/s (10728 bytes in 0.032s)
[exec] pkg: /data/local/tmp/install_test-debug.apk
[exec] Success

BUILD SUCCESSFUL
Total time: 5 seconds


And there you go. To be honest, I'd much rather run this stuff from the command line than watch the spinning beach ball in eclipse.

For more things you can do, see the official documentation:


And also, there is a textmate plugin that simply runs a few of these tasks:

Friday, September 25, 2009

Installing packages in Android

I've been playing around with Android quite a bit lately. One really cool thing is you can write a package (package == application) that can install other applications. Obviously this isn't done silently. A UI will be presented to the user asking them if they want to install this package and info about it.

Its basically done by sending an intent.

Intent intent = new Intent(Intent.VIEW);
intent.setDataAndType(Uri.parse("The url to your package"), "application/
vnd.android.package-archive");
startActivity(intent);

And there you go. Obviously of you want to to be notified when its complete you can also do a startActivityWithResponse call. I see this as being very powerful.

Monday, July 27, 2009

Interesting opportunities

On Monday Techcrunch released this story:

Justin.tv Opens Its API For Free, Hopes Live Video Will Explode - http://shar.es/xYpF

I don't have much experience in JustinTV except for the few events I've been to that have used it. From my understanding its like a YouTube but live video rather than uploaded and downloaded. I can't help but see the opportunities in this.

Tuesday, July 21, 2009

Venture Capital Investment Stabilizes, sort of

Venture Capital Dollars Stabilize in Second Quarter at Mid-1990s Levels

The above article shows the first increase in VC deals in a while. While the internet and clean tech are still floundering it appears VCs are all about biotech and medical devices these days. I knew I should have paid more attention in biology.

Tuesday, July 14, 2009

Joining technorati

Yes I'm joining technorati, ignore this

xfkq4bzcgs

Tuesday, January 20, 2009

Apache Camel TLP at Apache

Apache Camel was just upgraded to a top-level project. If you haven't played around with Camel you really should check it out. It abstracts any jms messaging bus and gives you a pretty interface to use cookie-cutter enterprise integration patterns. When looking at some of the patterns alarms go off on how many times I've had to hack them out on my own. Great tool to have in your pocket.

http://camel.apache.org/

Tuesday, December 2, 2008

Cherrypy + Routes with mod_wsgi

If you haven't played around with deploying your python apps using mod_wsgi, I highly recommend it. It is a very stable way to deploy your application that takes advantage of the good parts of the apache2 runtime.

Vanilla Cherrypy apps run great as wsgi applications. However, I did come across a problem when deploying a Cherrypy app with Routes. Running the application using the cherrypy server worked fine, but deploying it using apache2 + mod_wsgi always gave me the following on every request:

503 Service Unavailable

The CherryPy engine has stopped.

After some looking around the mod_wsgi project (which is well documented I must say) I found the answer here: http://code.google.com/p/modwsgi/wiki/IntegrationWithCherryPy

When running a Cherrypy app with the routes dispatcher you need to make sure you call:

cherrypy.engine.start(blocking=False)

After mounting your app. This sets the cherrypy engine state to running. After doing this everything worked great and I now have beautiful restful routes in my mod_wsgi app.

Friday, November 21, 2008

REST with cherrypy

I've been doing web services for a while. And while SOAP definitively has its place, if you control both sites of the web service REST can really make life easier. Cherrypy with Python Routes pretty much makes this trivial. Obviously everyone knows how to route gets, but what about the nasty post and put (REST talk for create and update). NO problem.


d = cherrypy.dispatch.RoutesDispatcher()

d.connect('create_something', '/something', controller=root.something_controller, action='create_something',conditions=dict(method=['POST']))

What this is saying is I want a to route all routes that match /something and are of the method "POST" to the controller root.something_controller and the method "create_something". Thats it. PUT is just as easy.

d.connect('create_something', '/something/:id', controller=root.something_controller, action='update_something',conditions=dict(method=['PUT']))

Same thing as above except the method update_something will get the param id.

Sunday, October 5, 2008

routes with Cherrypy

As I posted earlier, my new favorite web framework is cherrypy. I love its simplicity and the fact that I'm not wrestling with magic to do exactly what I want to do. One bug knock against cherry py is its routing mechansim. By default it uses an object reference routing, so take the following:


class HelloWorld()
def index(self):
return "

Welcome

"

def say_hello(self):
return "

Hello World!

"

class Root(object):

hello = HelloWorld()
app = cherrypy.tree.mount(Root(), config=conf)
cherrypy.quickstart(app)



Notice that our Root object creates a controller as a member var called hello. By default the way we would have a browser call the "say hello" method is by:

http://127.0.0.1:8080/hello/say_hello

This is fine and simple, but as we all know in the world of search engine optimization you have to put as much into your urls as you do your content. This is why things like swimlanes and routes in frameworks such as rails and Servlets are so important. Not to fear becase you can do this in cherrypy also.

Cherrypy pretty much allows you to drop in any request dispatching mechanism you want. Thats the whole point of cherrypy is it completly stays out of your way so you can build the exact webapp you want. If you want to use the python routes package, go a ahead. If you want to write you own, more power to you.

Here is a tutorial on using routes with cherrypy.

Quickly, just by adding the following lines to the code where I start my cherrypy app:
 d = cherrypy.dispatch.RoutesDispatcher()
d.connect('blog', 'helloworld/, controller=Root.hello, action="say_hello")

And I now get routes style power!

Thursday, October 2, 2008

python and sweet cherrypy

I've been doing a lot of python sever-side programming lately, and I really like it. One diamond in the rough I've found is Cherrypy (http://www.cherrypy.com). Cherrypy is a very lightweight and pythonic web framework that does the bare-minimum needed to get you up and running with a model/view controller framework. After that its all up to you and it pretty much stays out of your way.

At its basic cherrypy is really just an application. They do include an http server that runs pretty well, and you can also run your app as a mod_wsgi or mod_python app.

Lets write hello world:

import cherrypy

class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True

cherrypy.quickstart(HelloWorld())

running this piece of code you see:

[02/Oct/2008:06:55:40] ENGINE Listening for SIGHUP.
[02/Oct/2008:06:55:40] ENGINE Listening for SIGTERM.
[02/Oct/2008:06:55:40] ENGINE Listening for SIGUSR1.
[02/Oct/2008:06:55:40] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[02/Oct/2008:06:55:40] ENGINE Started monitor thread '_TimeoutMonitor'.
[02/Oct/2008:06:55:40] ENGINE Started monitor thread 'Autoreloader'.
[02/Oct/2008:06:55:41] ENGINE Serving on 127.0.0.1:8080
[02/Oct/2008:06:55:41] ENGINE Bus STARTED

and going to http://127.0.0.1:8080/ you see "hello world".

Thats pretty much it.




I know I know, a lot of you are saying "Wait, why would I use this over something like JEE and rails that does all this majic for me?" There is no shortage of "out of the box" web frameworks that promise a "build your app in hours" magic


I've been using web frameworks going on 9 years, everything from JEE (Jboss), Ruby on Rails, and even Django. And they are all great. The probem is they are opinionated. To try and remove responsibilities from the developer they make decisions for you, for example:

  1. How they create and commit your db session between requests
  2. How many db sessions you can have.
  3. View rendering technologies
  4. which ORM to use
The list goes on and on. And if you are using one of these frameworks you are enjoing all these "freebies" if you agree to follow the opinion and rules set by the framework. However, the moment you have to go against one of these "pre-decisions" you can find yourself wrestling with the framework. Just try to extend RoR to have multiple db sessions. Or JBoss to use a "share nothing" architecture. I usually find these issues come out when addressing scaling issues specific to your user behavior.

With cherrypy's "minimal" architecture you are free to pretty much do what you want, but you have to do it. You decide how db sessions are opened and closed before and after requests, or how your controllers are stuctured. Its all up to you. And the interesting thing is when you start doing many of the "freebies" your self, you find you you weren't getting as much for free as you thought.

Friday, April 25, 2008

closures and you

So I'm diving into dynamic programming with Groovy and I love it. I consider myself an expert Java & C++ programmer, but I'm definitely having to do some re-learning with dynamic languages. I think its like when you encounter someone who, for some reason or another, really doesn't grasp object oriented methodologies. Its a way of thinking and designing your software that you really just have to understand. I think programming in dynamic languages, while very different, follow the same learning process. And I'm definitely in it.

So lets start with closures, probably the most verbalized features of dynamic languages.


Obviously the most famous is iterating a list. Lets say I want to print out every element in a list... obviously I could do the following in straight java.

Vector list = new Vector();
// add stuff to the list
for ( Object x : list)
System.out.println(x);

In groovy I can do:

def list = ["one","two","three"]
list.each { println ${it}}

The idea is I actually pass the list a segment of "code" to execute on itself. But wait, it gets better.

Lets say I want to create a function that will do something n times... easy in Groovy:

def doSometing(number, Closure c) {
0.upto(number) {c}

}

// now you can call the method like this:
doSomething (4) { println "Say Hello" }

Pretty smooth huh? Obviously the possibilities are endless and, possibly dangerous. When passing a closure to an object, you basically give it permission do do whatever it wants

class Hello {
String name
String important = "DON'T CHANGE"

def Hello (name, Closure c) {
this.name = name

c()
}
}


I can then create a Hello object like this

def h = new Hello("Jonathan" ) { this.important = "BAD"}

And the Hello object's important field will be changed, even if I make it private. Makes it a little difficult to create well-formed and protected libraries.

Thursday, April 17, 2008

grails and the nulls that confuse me

Recently I've started doing a lot of my work in groovy. Being an old fart I love my java, but many of the young guys are pushing me to pick up the dynamic languages of the times. I'm not a fan of Ruby (mostly due to the community, you know who) and while I like Python it just doesn't fit for scalable enterprise systems.

Then I noticed groovy, and I love it. I have all the power of java with the syntactical candy of dynamic languages. And Grails is really awesome (especially the 1.x series). Its like Ruby on Rails, but made by people who really want to use it in production. I can write groovy code, and use hibernate, and run on top of a clustered Jetty with terracotta, how cool is that?

I do have to say, its the little things that get you. A good example is GORM and exception handling. In EJB3 or even straight hibernate, when I save an object that doesn't validate I'll get a java.lang.RuntimeException. Example


Person p = new Person();
p.setName("something Bad);
entityManager.persst(p);

If for some reason p isn't able to be saved by the entity manager a runtime exception will be thrown. Being a seasoned EJB developer I'd use this exception to my advantage and let it trickle out to the container and roll back the managed transaction (I think this is beautiful, but I guess a thing of the past).


Groovy, grails, and even rails does this different. The way you know an entity is saved is by the return value on the entity.save() method. Lets use the example above in grails.


def p = new Person(name:"something bad")
p.save()


if for some reason p is unable to be saved we won't be notified by the above code, you actually have to do it like this:


def p = new Person(name:"something bad")
if ( !p.save()) { p.errors.each(log.error(it))}


What I find fascinating about this example is a statically typed language is actually less code than a dynamic one!

Thursday, January 31, 2008

Jetty & spring are awesome

One reason I love java is it has so many tools out there to solve the problems you need to solve. I needed to write a server-side app that was way under the scope of jBoss, so I downloaded Jetty and used spring.

For those who don't know, Jetty is a super fast and light weight servlet container that uses NIO heavily. For apps that don't need stuff like transactions its perfect. And pair that up with Spring and you have a super fast & lightweight app that still has very good design.

So check out Jetty and Spring.

Saturday, November 17, 2007

Call for Action

I've worked on a few open source projects and I have nothing bad to say about any of them. If there is one shortfall in the open source world its that everyone wants to be the hero. Everyone wants to "start" an open source project. But far less people are willing to join existing projects and take them those last tedious few steps necessary to truly make them useful. These steps usually include bug fixing and documentation. This is why sourceforge has hundreds of thousands of projects that will never be used.

At RubyConf Charles Nutter issued a "Call for Action" asking people to help take things those last steps, both on documentation of the new Ruby runtime AND bug fixing for JRuby. So if you are interesting in doing something new, try and help him out. Its the little things that makes most open source projects great.

Charles Nutter's Call to Action Post

Friday, November 16, 2007

JRuby and Seam

After spending a month or so playing with Ruby and Rails I'm really not happy with Rails.  I build systems that have to scale and Rails has some serious issues when it comes to that.  Its great for simple CRUD apps, but if you need to move outside of that or need to scale its got issues.

I do, however, like JRuby.  I'm also a big fan of jBoss Seam.  It just makes sense to me, use the scalability of EJB3 with state management in web apps.  In JBoss Seam 2.0 you can write your Seam components not only as EJB3, but also in Groovy.  I love EJB3 so that would be my first choice.  But I work with people who really want to use Ruby.  To meet in the middle I've been playing around with integrating JRuby into Seam.  Luckly, Seam is designed well to allow this with some learning curve.  

I'll post more later, but I've got a proof of concept where I can write my view in JSF, and a Seam-managed component in JRuby, which then uses JPA for persistence. 

Wednesday, November 14, 2007

Dreams of Fuel Cells

I live about an hour from the auto-motive capital of the U.S. (well, the former one at least). The big 3 and the UAW are pretty much house-hold names in Michigan. Most people who know me would testify that I'm not a fan of the U.S. auto industry. From union squabbling to companies who produce non-creative, sub quality products and insist that the reason you should buy them is not because they are better, but because its your patriotic duty.

Even though I rarely achieve this, I always strive to be a creative, forward thinking engineer. I believe that sometimes its not always enough to just solve a problem, but solve it in a fantastic way that yourself and others can learn from. I simply don't see this a lot from the big 3, which is why I drive a Honda.

Tonight I saw a commercial for the new Honda hydrogen fuel-cell car, and I was captured. I felt like I was looking at the iPhone of cars. A company whose industry is as old as some governments refuses to subside into the monotonous productions of U.S. big business and big labor competitors.

I need to do more reading to fully understand what this car is, but it looks like it will hit the road in the summer of 2008. While other companies are just trying to stay a float and squabbling about overtime pay for employee uniform changes, Honda is making history.

http://www.honda.com/fuel-cell/

Friday, November 2, 2007

well-defined api

So from being a java/c++ developer for years I've been taught the importance of exposing well-defined save apis. You expose parts of your api that you think is safe for black-box users to incorporate and abstract/protect the rest. This is usually done by the visibility, security, and overriding features of the language.

For instance, if I have a method that I think its unsafe for any class, including sub-classes to call, I'll define it as private.


Ruby seems to think this isn't important. I can do this:

class Base
def aMethod
puts "A private method";
end
private :aMethod
end

class Derived
public :aMethod
end

In one line the sub class "Derived" was able to change the visibility of the method "aMethod". A little scary when trying to define things like data structure libraries and application frameworks that depend on the validity of certain methods.

Monday, October 29, 2007

Ruby on.... jetty?

So I'm playing around with JRuby, and I think its really cool. The java community has really come a long way and created some great tools for doing enterprise deployments, why should the ruby community spend time backtracking and re-inventing the wheel? The JVM has become a proven execution environment and with an open byte-code standard its trivial to compile to it (well not trivial, but do0-able).

So with JRuby you can get the syntactial sugar of ruby with the heavy lifting of such java services and JMS, hibernate, and even servlet containers. Currently Ruby on Rails uses either WeBrick or Mongrel as their web server. Both are written in ruby and, well, slow. If there is one thing the java community has done a good job of the past few years its build fast and reliable servlet containers (just look at tomcat and jetty).

What I'm wondering is how hard would it be to replace mongrel/WeBrick with jetty in a JRuby on Rails app? Just think of the performance gain with Jetty's NIO engine?

sequel to the rescue

No sooner than I pushed the "post" button on my last post did I find sequel. Its a light-weight ORM in Ruby that looks promising. Does all the association fetching and even has 2nd-level cache support.


http://code.google.com/p/ruby-sequel/

Active Record, the sludge in Ruby

So I've been learning Ruby lately, and being a bug ORM guy I decided to checkout the source for active record and see how it works. There are some very serious issues here. Active record uses the dynamic mixin ability of Ruby to do its work, but that same logic kills itself in performance. To explain myself further I we should compare the active record model with JPA.

JPA
Java persistence architecture delegates all ORM work to the persistence manager, an external library that manages db entities. If I want an entity I ask the persistence manager for it, if I want to save, I do it through the persistence manager. When my app starts up the persistence manager looks at all my entities and my schema and creates the necessary object structure to do its ORM magic. The mapping/querying logic is done once and used by everyone.

ActiveRecord
Active record delegates all ORM work to the entity itself. Entities must know how to query/save themselves and if they are transient or persistent. They do this by mixing in methods to to the ruby-defined entities at runtime and then, when an instance of an entity is instantiated, created the necessary links to do its ORM stuff.

The disadvantage of active record is the function of ORM processes is spread throughout however many entities you have. It becomes hard to do things like locking, transactional caching and even transactional enlistment. If I have a 100 entities, each entity has to be concerned with cache invalidation and managing their relationships.


Until the ruby community either fixes this in active record, or comes out with a JPA/Hibernate -like ORM they will never be in the spotlight. If you talk to any ruby-evangelist they always bring up that Twitter uses RoR. And that is true, but if you read any of their posts to scale Twitter had to remove ActiveRecord from their deployment because it was just too slow.

So I think Ruby is cool but I really don't like active record. Anyone interested in porting hibernate to Ruby???