bye bye gemcutter

Starting a few hours ago, I have been getting this error doing almost anything with the gem command:

ERROR:  While executing gem … (NoMethodError)
    undefined method `request_uri’ for #<URI::Generic:0x3d49c332>

When running “gem update —system -V” I noticed that it failed when talking to gemcutter.org.  And then I tried to go there and sure enough some smily blonde with a backpack seems to have taken over the site.  So I guess it’s gone for good?  At least now I know where to go to get a free credit report or cash advance.

So while “undefined method `request_uri’” is not exactly my first choice for verbiage to describe this kind of problem, just run this:

gem sources -r http://gemcutter.org

and problem solved… although I’ve never had to think about my gem sources before.  Are there any good gem sources I should know about besides the two remaining defaults (rubyforge and github)?

Update: gemcutter.org is now redirecting to rubygems.org so the steps above should no longer be necessary.

using jruby with aptana

I enjoy programming in the Ruby language, and I recently started to make the migration to JRuby.  However, I am rather fond of using an IDE for programming.  I know it’s not trendy; some programmers wear their use of vi as a badge of honor.  I consider it to be archaic.  It’s like being proud you grunt and swing a bone around.  What separates man from apes is our use of tools, and IDEs are very useful tools.  (Of course, there is a danger with IDEs in that the programmer won’t learn what is going on behind the scenes: while I agree that every programmer should know HOW to run, maintain, debug, and execute their programs without an IDE, or from within vi and the command line, I strongly disagree that we should always want to.)

I am also an unabashed fan of Windows.  Yes, it’s not trendy, but I like the customizability of it.  And, for the record, I haven’t had any viruses or trojans and I haven’t seen a blue screen in years.

On Windows, the best Ruby IDE is Aptana Studio.  I’ve enjoyed it for quite some time now, but recently I decided to make a more permanent switch to using JRuby.  Unfortunately, Aptana doesn’t play well with JRuby.  One problem is that it seems to have the Ruby interpreter hardcoded as “ruby.exe”.  Not just “ruby” (I initially tried making a ruby.bat batch file that would redirect calls to my actual jruby installation), but “ruby.exe”. 

So, for a workaround, I created an .exe that spawns jruby.exe (or any other executable) and passes all the same command line arguments on.  So what I do is rename my process launcher as “ruby.exe”, stick that in the PATH so Aptana finds the executable it expects to find, configure my launcher .exe to run jruby.exe, and then spawn the child process.  Aptana is none the wiser and everything works beautifully.  Problem solved.

The code is quite simple.  We simply figure out what directory we are in (so we know where to look for the config file), read it (to figure out what process to launch), fix up the command line arguments (by convention, the first argument should point to the process that is being started, and we don’t want our child process to get a path to our launcher), and then use _spawnv to launch it.  We do have to take care to quote any arguments with spaces in them as well.

int main( int argc, char *argv[] ) {
    // figure out what directory this exe is in
    char path[MAX_PATH];
    string filename(path, GetModuleFileName(NULL, path, MAX_PATH));
    filename = filename.substr(0, filename.find_last_of('\\')+1);
    filename.append("\\predirect.config");
    
    // open the config file
    ifstream configfile(filename.c_str());
    if (configfile.is_open()) {
        // read path to jproc from config file
        string proc;
        getline(configfile, proc);
        configfile.close();

        // call our real proc .exe (or whatever else we 
        const char* new_argv[argc+1];
        for (int i = 0; i < argc; i++) {
            if (i == 0)
                new_argv[0] = quote(proc.c_str());
            else
                new_argv[i] = quote(argv[i]);
        }
        new_argv[argc] = NULL;
        return _spawnv(_P_WAIT, proc.c_str(), new_argv);
    } else {
        cout << "could not find " << filename.c_str();
    }
}

Source code and executable are on Github.

resetting the database in hibernate

When running unit tests, I find it helpful to reset the database (for unit tests that use the database) before each run to ensure we have a clean slate.  There are a number of ways to do this.  One way would be to the Hibernate property hbm2ddl.auto to “create-drop” and force each unit test to use an entirely new SessionFactory.  This isn’t always a viable option, especially if the SessionFactory is constructed as a subset of other initializations (such as when loading a Spring configuration), as it would be too expensive to reload the entire configuration from scratch for each test.

Typically, you would want to load those configurations only once for the entire suite.  Because of that, ‘create-drop’ would only be run before the first test case executes.  Since I want to reset the database after every single test, I wrote some code to do so ‘by hand.’

When using in-memory databases, such as H2 or HSQLDB, this is extremely fast and doesn’t add any significant delay to your execution time.

To pull this off, I take advantage of the DatabaseMetadata class Hibernate comes with, which exposes the DROP and CREATE statements that it uses when executing the “create-drop” behavior the “hbm2ddl.auto” property invokes.

@Autowired
LocalContainerEntityManagerFactoryBean lcemfb;

Ejb3Configuration ejb3 = new Ejb3Configuration();
ejb3.configure(lcemfb.getPersistenceUnitInfo(), lcemfb.getJpaPropertyMap());
Configuration config = ejb3.getHibernateConfiguration();
config.buildMappings();
AuditConfiguration.getFor(config); // necessary if you use Envers/Hibernate Audit
Dialect dialect = Dialect.getDialect(config.getProperties());
DatabaseMetadata metadata;
Connection conn = null;
try {
  conn = getConnection();
  metadata = new DatabaseMetadata(conn, dialect);
  DROP_SCRIPTS = config.generateDropSchemaScript(dialect);
  CREATE_SCRIPTS = config.generateSchemaCreationScript(dialect);
} finally {
  close(conn);
}

Then, for each script in DROP_SCRIPTS:

Statement stmt = conn.createStatement(); 
for (String sql : DROP_SCRIPTS) { 
  stmt.execute(sql); 
}

and

Statement stmt = conn.createStatement(); 
for (String sql : CREATE_SCRIPTS) { 
  stmt.execute(sql); 
} 

Some things to be aware of, however: if you have cacheing enabled, you will encounter errors if Hibernate attempts to load from the cache. Either ensure you don’t have the second level cache enabled for your unit tests, or explicitly clear the cache by hand:

@Autowired
EhCacheManagerFactoryBean cacheManager;

cacheManager.getObject().clearAll();

twitter as a global message bus

Apparently, Twitter is losing money.

I don’t understand why.  It is an advanced message bus that allows asynchronous communication between heterogeneous platforms in a dead simple manner.

One of the systems I work on sits in the middle of a spaghetti nest of queues and topics linking up over a dozen different systems.  We waste so much time doing end to end connectivity tests.  I spent a lot of time writing functionality to archive the messages we receive and send out for debugging purposes, and to allow replay of events in the event that a recoverable or transient error occurs.  Twitter supports archival out of the box.

Why can’t Twitter make a private version of their platform that businesses can use internally as a dead simple enterprise service bus?  Their tool already supports many of the features I use every day, and several that I always needed to write by hand.  The Twitter timeline is pub-sub communication.  DMs are a form of point-to-point communication.  Hash tags can be used as message selectors. If I want to replay an event I received yesterday, it’s archived and I can go back and resend that message.

The main limitations would be a need for privacy, stability (it would be need to be separated from the global chat room Twitter that we use today), and there would need to be a way to hyperlink to private data streams outside the 140 character limit (that can be securely accessed).  A platform like this would greatly simplify a lot of businesses and let us concentrate on what we really want to be doing: business logic, not plumbing.

Or, hell, why stop at a private implementation: why can’t they add a feature to the site we have now that gives you private channels that businesses can use to publish events or information to subscribers (for a fee).  Take a business like ValuValu, which charges $49 a month for an email that tells you the one mutual fund you need to buy.  Add in a follow-for-a-fee feature and many business like that could dispense with email and just use Twitter’s platform instead.

With private channels, you could even use Twitter as a transport for communicating with all kinds of third party APIs.  Let’s say I want some information from FullContact: just DM them the name, and they reply with a hyperlink to some private endpoint with the results.  It couldn’t get much simpler.  But the real advantage is that when I talk to Twilio or any other API, I could do it in the exact same manner.

Twitter should be able to take their technology and turn it into a platform — a global message bus — that acts as a transport for the APIs and message publishing needs for businesses all over the world.  And make money doing so - simply building on their already world class infrastructure.

Instead, they just create things that no one gives a rat’s ass about like featured tweets…

brick and mortar retail is obsolete… it’s time for them to adapt

Instead of brick and mortar stores where you buy things, why don’t we just have places where you go to TRY things (and then order elsewhere). They can charge you an entry fee or make money from additional services (such as unboxing and installation for large items like TVs) or handling returns (drop it off and they box it up and send it wherever.)  I’m sure someone enterprising enough could even work out a deal with Amazon and/or other online-only retailers to help cover costs in return for driving business to their sites.

Sucks for you but if people are using your business as a showroom for online purchases, you may as well learn to deal with it, adapt, and figure out how to make money as a showroom.

Amazon’s recent Price Check promotion has hit a nerve with retailers and unleashed a massive whinefest about how Amazon is destroying brick and mortar businesses.  Whatever.  You know what?  The world changes.  Deal with it.  If you can’t figure out how to adapt to a changing world, your business deserves to perish.

java.lang.NoClassDefFoundError: scala/ScalaObject with sbt

Spent some time trying to figure out why sbt no longer worked for me when I upgraded the sbt-launch jar to the 0.11.0 version.  It would complain about a NoClassDefFoundError on scala/ScalaObject.  0.10.0 and earlier versions of sbt ran perfectly.  The problem wasn’t related to a project configuration file as this error occurred even if I ran sbt in a completely empty directory.

Since I’m knew to scala and sbt, I wasn’t sure what it was doing behind the scenes, so I didn’t know where to look.  I’m sure it’s documented somewhere but the troubleshooting link I found didn’t help. 

I knew sbt managed scala versions behind the scenes, so not finding ScalaObject meant that the download was perhaps corrupt.  So the solution was to figure out where sbt puts these things.  I knew it created a local boot folder for new projects I created, but it turns out sbt also keeps a global boot folder under your home folder in ~/.sbt/boot/ (which was C:/Users/<username>/.sbt on my Windows 7 machine). 

Deleting this global boot folder so sbt recreates it was the key.  If you get a similar error message for a specific project, the local boot folder would probably be the culprit in that case.

the mystery of the xcode 4 upgrade, three20, and a blank screen on startup

Recently, I upgraded to XCode 4, and one of my Objective C programs stopped working properly.  This particular program uses Three20 to manage the application’s views. 

Upon startup, I would get a blank white screen.  However, if I closed the app by double tapping on the iPhone’s home button, and then switched back to the already-running instance, I would then see the Three20-managed screens and the app would behave normally.

Like most puzzlers, the solution turned out to be really simple, though figuring it out was not at all obvious.  It turns out I had left my NIB referencing a window which I never actually used (since Three20 managed loading and switching between my screens), and the app delegate had an outlet connection that pointed to this unused window.  So, apparently, when I converted the app over to use Three20, I hadn’t cleaned up the NIB to remove the unused outlets and resources. 

This didn’t cause problems with XCode 3 and earlier, but XCode 4 is pickier, I guess.  No problem: the references didn’t belong there anyway.  Once I cleaned up the NIB and corresponding @synthesize and IBOutlet references, the programs worked as expected.

cooler javaFX kreations

Oracle recently released JavaFX 2, which is a rich client API that ditches the JavaFX language from JavaFX 1.x in favor of a pure Java library.  And man, is it slick.  It makes it very easy to implement rich, graphical applications or applets.

Inspired by @visualrinse’s Cooler Kreator, I created my own (greatly simplified)  version of it, which I called “Cooler Kreations FX”.  To use it, simply launch it, give it permission to run, type in a tag, and the program will consult Kuler for a random color scheme with the specified tag.  Assuming, of course, that the Kuler API is working at the time (which, lately, hasn’t been very often…).

To create an application is as simple as extending the Application class.

public class CoolerKreations extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        stage.setScene(createScene());
    }
 
    private Scene createScene() {
        ....
    }
}

You can use FXML to create your scenes declaratively, or write pure Java code to create your scenes programatically, or mix and match the two.

// declaratively, via FXML
Group root = FXMLLoader.load(getClass().getResource("my.fxml"));
Scene scene = new Scene(root, Color.BLACK);

// the FXML:
<VBox fx:controller="com.metatrope.controller.Controller" alignment="center" xmlns:fx="http://javafx.com/fxml" style="-fx-spacing: 5;">
   <children>
        <HBox alignment="center">
           <children>
                <Label text="Cooler Kreations" style="-fx-font: 42 Tahoma;" textFill="white" fx:id="title"/>
                <Label text="FX" style="-fx-font-weight: bold; -fx-font-size: 42; -fx-font-style: italic;" textFill="white" fx:id="titlefx"/>
           </children>
        </HBox>
   </children>
</VBox>

// programatically
Group root = new Group();
Text text = new Text();
text.setText("hi");
root.getChildren().add(text);
Scene scene = new Scene(root, Color.BLACK);

There are a lot of controls available. Visually, you can style your controls with CSS and there are plenty of effects you can use, such as drop shadows, reflections, blurs, and more. You can even chain them:

Bloom bloom = new Bloom();
bloom.setThreshold(2.0);

BoxBlur blur = new BoxBlur(4 + randomInt(0, 5), 4 + randomInt(0, 5), 4 + randomInt(0, 6));
blur.setInput(bloom);
 // the output of bloom will be the input for this effect

DropShadow ds = new DropShadow();
ds.setOffsetY(4.0f);
ds.setOffsetX(4.0f);
ds.setColor(Color.CORAL);
ds.setInput(blur);
 // the output of blur will be the input for this effect

// apply bloom AND blur AND a drop shadow to this text
text.setEffect(ds);

JavaFX also makes it dead simple to animate controls. Simply bind a property and identify key points in the timeline, and the system will interpolate all the values in between. For example, here I tell JavaFX to rotate one of my nodes repeatedly:

// loop my node from 0 degrees to 360 degrees, completing one rotation every 15 seconds.
// repeat ad infinitum.
Timeline animation = new Timeline();
animation.getKeyFrames().addAll(
  new KeyFrame(Duration.ZERO, new KeyValue(node.rotateProperty(), 0d)),
  new KeyFrame(Duration.seconds(15.0), new KeyValue(node.rotateProperty(), 360d)));
);
animation.setCycleCount(Animation.INDEFINITE);
animation.play();

This is a huge step forward for rich client development in the Java space and a wonderful replacement for Swing.  On modern JVMs, it even loads quickly in the browser; this wasn’t historically the case with JavaFX 1.x, or for applets in general, but nowadays Java applets start up almost as fast as Flash (and this will improve even more when Java becomes modularized in future versions.) 

The source code can be viewed on my github account.  You can test it out online as well (though only if you use Windows, unfortunately.)  The result could look something like this:

force create hibernate envers audit events

Recently, I had decided to adopt Hibernate Envers to audit changes to several tables on my database.  I decided to attempt to force create the insert events that would have been generated had I been using Envers from the start.  Unfortunately, there wasn’t an obvious way to do this, short of writing my own SQL scripts.  That approach is fine, but I had several tables to migrate, and I prefer to avoid bypassing Hibernate when I can.

This is what I came up with:

Envers works by using Ejb3 listeners to find out when an entity is inserted, updated, deleted, etc. So, the obvious solution was to trigger this listener. The listener is an instance of org.hibernate.ejb.event.EJB3PostInsertEventListener and expects to receive PostInsertEvents.

PostInsertEvent pie = new PostInsertEvent(entity, entity.getId(), state, persister, source);
eventListener.onPostInsert(pie);

Entity is simply the entity that you saved to the database, so of course we already have that. We just need to figure out what Envers expects for the source, state, and the persister

The source is simple.  This turns out to be the Hibernate session itself, which you can obtain from the JPA EntityManager:

EventSource source = (SessionImpl)jpaEntityManager.getDelegate();

The persister is an instance of an EntityPersister, which just captures the mapping and persistence logic for the given entity.  Every entity has one, and this can be obtained from the EventSource.

The state is an object array containing the values of each property that we are persisting.  It can be obtained from the EntityPersister.

Object[] state = persister.getPropertyValuesToInsert( entity, null, source ); 

So, basically, all of that information could be obtained with just the entity and the session.  I’m not sure why they make us pass in all these additional fields when you can get them all from just the EventSource and the entity itself, but whatever.  Once we have all that information, we assemble the PostInsertEvent and pass it to an instance of the AuditEventListener that will do the work.  We don’t even need to hook into the one that Hibernate creates (from your configuration files), we can just create it “by hand”.

The complete code is this:

// instantiate the event listener
AuditEventListener eventListener = new AuditEventListener();
eventListener.initialize(HibConversation.getHibernateConfiguration());
EventSource source = (SessionImpl)jpaEntityManager.getDelegate(); EntityPersister persister = source.getEntityPersister( null, entity );

// create an insertion event for each entity you want to audit
Object[] state = persister.getPropertyValuesToInsert( entity, null, source ); PostInsertEvent pie = new PostInsertEvent(entity, shippingRate.getId(), state, persister, source);
eventListener.onPostInsert(pie);

overriding rails validations metaprogramatically

I had a need to modify the validations for a model in a Rails 2.3.5 app from a plugin.  I did not want to directly modify the source code; in my case, I was writing my code as a plugin for an existing Rails application so I could upgrade the base code without worrying about losing my changes, or dealing with the hassle of reapplying my patches.  There are several really great Rails frameworks out there that are applications in their own right, such as Redmine and Spree, and writing my customizations as plugins or extensions to them is simply a more flexible solution than modifying the applications directly.

I was modifying a User class, extending the login length to 255 characters. We were using emails as the login, and where I work they can get pretty long because they are usually a concatenation of one’s first and last names. And some of my co-workers have obscenely long names. Directly modifying User would be trivial — you would just increase the value associated with the maximum attribute for the validates_length_of validation we are changing:

validates_length_of :login, :maximum => 255

But, as I mentioned, I did not want to change the original code. So, we need to get at the list of validation callbacks. Luckily, that’s easy: there’s a @validate_callbacks instance method available to us. So, we just need to iterate over it and find the callback that we don’t want.

Each entry in @validate_callbacks is an instance of ActiveSupport::Callbacks::Callback. This class contains an accessor called method which returns the Proc that is executed to run the validation. Unfortunately, we want to get at the values being sent to that proc. We know that the validates_length_of method takes in an attrs parameter, so we just eval that against the callback method’s binding to find the list of attributes that this proc will validate.

Because we know we only passed one attribute in (instead of a chain of them), we just check the first value, and double check that the maximum option was set to 30 (which is the original value). This also allows us to easily filter out the other validations on the login field that we aren’t interested in, such as validates_format_of.  The finished product looks something like this:

require_dependency 'user'
require 'dispatcher'
module UserPatch
def self.included(base)
  base.class_eval do
    @validate_callbacks.reject! { |c| true if Proc === c.method &&
      eval("attrs", c.method.binding).first == :login && c.options[:maximum] == 30 rescue false }
    validates_length_of :login, :maximum => 255 # new value
end end Dispatcher.to_prepare do unless User.included_modules.include?(UserPatch) User.send(:include, UserPatch) end end