Skip to content

Ramblings

Musings of Matt Williams
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for the 'Computers' Category
02Mar10 Rails & JRuby in a Jar
gotchas jruby rails tips
4 Comments

For political reasons, I needed to ship a single jar file.  I didn’t want to have the overhead of a war & an appserver, so I set out to embed my rails app within a single jar file.  I needed to make some changes in order to get it to work.

This assumes Rails 2.3.5 and JRuby 1.4.0.

First, you need to create your rails app.   Freeze rails and any gems required.

Next download jruby-complete.  Once you’ve done so, unzip it.  I’m assuming you’ve unzipped it to complete.

Note: When you repackage the jar file, DO NOT use jar.  Use zip.  This is very important.  If you don’t, you’ll trash your jruby instance.

Copy your rails instance to complete/META-INF/jruby.home. I assume it will be complete/META-INF/jruby.home/rails.

Next create complete/META-INF/jruby.home/bin/server with the following content:


#!/usr/bin/env ruby

RAILS_ROOT = File.join(File.dirname(File.dirname(__FILE__)),'rails')

require File.join(RAILS_ROOT,'config/boot')
require 'commands/server'

The RAILS_ROOT needs to be set in order to have the proper paths within the jar file.

Next, edit complete/META-INF/jruby.home/rails/vendor/rails/railties/lib/initializer.rb In the set_root_path method, edit it so it looks like this:


    def set_root_path!
      raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT)
      raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT)
      # I changed this... mkw21 20100301
      @root_path = RAILS_ROOT
        # Pathname is incompatible with Windows, but Windows doesn't have
        # real symlinks so File.expand_path is safe.
#        if RUBY_PLATFORM =~ /(:?mswin|mingw)/
#          File.expand_path(::RAILS_ROOT)#

        # Otherwise use Pathname#realpath which respects symlinks.
#        else
#          Pathname.new(::RAILS_ROOT).realpath.to_s
#        end

      Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT)
      ::RAILS_ROOT.replace @root_path
    end

This is related to paths within the jar file.

Additionally, you need to change the initialize_logger method. (this might not be needed, see below). Change


          logger = ActiveSupport::BufferedLogger.new(configuration.log_path)

to


          logger = ActiveSupport::BufferedLogger.new("/tmp/transfer.log")

The idea is to change it to something definitely outside the jar. The reason I did this, and didn’t change the config in environment.rb was that the changes in environment.rb were not getting picked up. I’ve since come to the belief that this is due to an issue with the Rack LogTailer detailed at https://rails.lighthouseapp.com/projects/8994/tickets/2350-logtailer-ignores-configlog_path. So complete/META-INF/jruby.home/rails/vendor/rails/railties/lib/rails/rack/log_tailer.rb needs to be edited. Edit the EnvironmentLog assignment to match the file we’d specified. (You may be able to substitute RAILS_DEFAULT_LOGGER but I have not tested that)


      EnvironmentLog = "/tmp/transfer.log"

That’s pretty much it. Zip up your exploded jar from the complete directory: zip -r ../complete.zip *. Then you can run it via java -jar complete.jar -S server.

Enjoy!

01Sep09 Fractal Terrain Generation
Uncategorized programming ruby
1 Comment

The original idea behind the generation was in Computer Rendering of Stochastic Models, in the June 1982 issue of  Communications of the ACM.  I’m basing my code on Paul Mertz’s Generating Random Fractal Terrain.

The gist of what I’m doing is to start with a square, like this (I’ve added a grid to make it easier to see the elements):

Our map

Our map

From here, we determine the corners:

Four corners

Four corners

Each location in the grid has a “height”. In my implementation, I assign it a value from (-1 .. 1). From these four points we find the center point, then average the values of the 4 corners and add to it some random value.

The center point

The center point

This random value, h is in a range of (-1 .. 1). So if the 4 corners all had a value of 0, and our random value was .5, then the center would be .5:

0.0 0.0
0.5
0.0 0.0

From here we determine the midpoints on the sides of the square:

Midpoints

Midpoints

Notice that where we once had one (1) square, we now have 4 smaller squares.  From here, we do each square in turn in the same fashion.  However, this time the range of h has been halved — the range can be tweaked.  If you use the formula 2^(-n), to make the terrain smoother, use a n closer to 1.  Likewise, the smaller n is, the more random the terrain.

The smaller squares are each processed, yielding smaller squares.

Finally, you have a grid of values, which range from -1 < n < 1.  From this terrain, you could then assign ranges, such as n <= -.5  for water, -.5 < n <= -.3 for swamps, -.1 < n <= .2 for plains, snd so forth.  From that you could then have something like this (tiles are  from Wesnoth):

Sample Terrain

Sample Terrain

If anyone’s interested, I can post sample code.

Enhanced by Zemanta
25Aug09 Using jnp as a JBoss heartbeat
Computers jboss jruby
0 Comments

jnp is a JBoss protocol which exposes jndi.  It is, by default, bound to port 1099.  I’d been using that port as a heartbeat, but “cheating” — I would open a socket and then close it immediately.  However, this caused problems.  jnp is chatty.  And it got upset at my not letting it say ‘hi’ before I dropped the connection.   So, here’s a code snippet (jruby, the Java should be an exercise for the student) which allows you to actually do an intelligent check.

  def check_port(server)
    @logger.debug "check_port: #{server.fqdn}(#{server.host}:#{server.port})"
    begin

      env = java.util.Properties.new();
      env.set_property("java.naming.factory.initial",
                       "org.jnp.interfaces.NamingContextFactory");
      env.set_property("java.naming.provider.url",
                       "#{server.host}:#{server.port}");
      ctx = javax.naming.InitialContext.new(env);

      # if the server is not running, we'll get an error here because
      # it will timeout.
      ctx.list("")
      ctx.close
      @logger.debug "check_port: #{server.fqdn}(#{server.host}:#{server.port}): Succeeded"
    rescue javax.naming.CommunicationException => comm_error
      @logger.debug "check_port: #{server.fqdn}(#{server.host}:#{server.port}): Not Running"
      begin
        ctx.close
      rescue Exception => ignore_me
      end
      return false

    rescue Exception => e2
      @logger.info "check_port: #{server.fqdn}(#{server.host}:#{server.port}): FAILED: #{e2.to_s}"
      return false
    end
    return true
  end
Enhanced by Zemanta
29Jul09 JBoss port confusion
gotchas jboss
0 Comments

We ran into a case where JBoss was unable to come up; it gave the following (partial) exception:

java.rmi.server.ExportException: Port already in use: 1098; nested exception is:
java.net.BindException: Cannot assign requested address

After poking around with netstat and lsof, we couldn’t find anything that was binding to the port.  I’d made the assumption that it was bound, totally missing the next line.  As it turns out, we were attempting to bind to a vip which, although it existed in DNS, was not defined on the host on which the error occurred.  The “cannot assign requested address” was the clue.

Enhanced by Zemanta
14May09 SSL Joys
gotchas howto tips
0 Comments

The nice thing about standards is that there are so many of them to choose from. Andrew S. Tanenbaum

I’ve been working with a Apache proxy to force SSL and https.  Well, I haven’t had any control over the certificates.  And they can come in so many versions, especially given that Microsoft wants to do things its own way and the Apache web server instance is sitting on a windows virtual instance.

I’ve gotten the private key in two different formats, .pfx and .pvk.

.pfx can be converted to the pem file format which Apache is expecting via the following:

openssl pkcs12 -nodes -in infile.pfx -out out.pem

You then need to edit it, stripping out everything but the private key.  Once that’s accomplished you’re good to go.

.pvk, however, isn’t supported in openssl until version 1.0.  Joy!

Enhanced by Zemanta
05Feb09 Web Based Portable mysql tool suite
glassfish jruby mysql php tools utilities web
0 Comments

Are you limited in what software you can use at work?  This article details how to have a web based tool suite for mysql.  It currently has the following tools:

  • AjaxMyTop — a php implementation of mytop (think top for mysql) which runs in a browser.
  • rbdb — a phpmyadmin work-alike in progress.  It’s the result of the 2008 Rails Rumble contest.

So you’ve noticed that both a php and a ruby application are included.  Pretty spiffy, eh?

The magick partly lies in the container — I’m using GlassFish v3 prelude.  Another piece is Quercus, a Java implementation of PHP 5. JRuby is used for ruby.
Continue reading ‘Web Based Portable mysql tool suite’

23Jan09 Prawn’d by Scruffy, Gruff
jruby ruby
2 Comments

I’ve been putting together an automated report for my team, and decided to give prawn and scruffy a try.  I ended up using Gruff, but here are some of the lessons along the way:

  • Scruffy has issues with bar charts and values from 1-3 (actual values were [1,3,1]).  I was able to get around this by setting a minimum value of 0 and a maximum of (datapoints.max + 1)
  • Scruffy failed, miserably, when I just had one column.
  • Prawn has issues, potentially, with png’s generated by gruff (It’s actually a rmagick issue).  The easy way to get around this is to generate a jpg.
  • I’d seen indications that you could use to_blob to generate the jpg, and yes, you can, but it won’t
    1. Write the file
    2. If you attempt multiple generation types in sequence, it creates multiple copies of text in the resultant graph, and they don’t always line up.

Right now I’m working on packaging with rawr. Once that’s done, I’ll publish my results.

14Jan09 SSL and Java Keystores
gotchas tips
0 Comments

If you happen to change a machine’s name, be sure to delete the previous definition from the keystore.  Otherwise it will complain mightily and, indeed, fail when you attempt to add in the pem file.

17Dec08 JBoss and Sesame Street’s Count
jboss tips
0 Comments

One, One database record!
Two, Two database record!
Muahahaha!

By default, when using datasources with JBoss, it does a count to validate a connection, both on creation and when it is requested from the connection pool.  It looks something like:

select count(*) from x

Now, this can take take a while when working with tables containing 1.8 million rows and growing.  That said, the datasource config file has two properties which can be set:

  • new-connection-sql
  • check-valid-connection-sql

These allow you to specify something like

count 1 from x

which runs a good bit faster.

25Nov08 Upgrading rubygems on Ubuntu
ruby tips
2 Comments

Maybe it’s me.  Maybe it’s a feature of ubuntu, but on two separate systems, when I went to upgrade rubygems to the latest via

gem update –system

it didn’t work for me.  So, I had to go the more extreme route of:

gem install rubygems-update
updatee_rubygems

This is mostly for my own reference, it’s not the first time I’ve had to do so…..

 
Browse Archives »
  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (16)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (4)
  • jruby (6)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (6)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (9)
  • tools (2)
  • Uncategorized (9)
  • utilities (3)
  • web (5)
  • web design (3)
 

Latest

  • Rails & JRuby in a Jar
  • Fractal Terrain Generation
  • Quick thought on programming and distractions
  • Using jnp as a JBoss heartbeat
  • z-index and events
  • JBoss port confusion
  • SSL Joys
  • After long silence
  • New Ruby Blog
  • Javascript with CSS Sprites Animation

Flickr

layout_newm3headerTerrain Testa

Blogroll

  • Development Blog
  • Documentation
  • Plugins
  • Suggest Ideas
  • Support Forum
  • Themes
  • WordPress Planet

Search

Browse by Category

  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (16)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (4)
  • jruby (6)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (6)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (9)
  • tools (2)
  • Uncategorized (9)
  • utilities (3)
  • web (5)
  • web design (3)

Browse by Tag

  • 1.2.6
  • 2.1
  • administrivia
  • autotest
  • books
  • controller
  • css
  • feed-normalizer
  • feeds
  • gotchas
  • idiocy
  • irb
  • Java
  • javascript
  • jboss
  • jruby
  • just enough programming
  • mini sagas
  • open-uri
  • philosophy
  • php
  • pragmatism
  • programming
  • quotations
  • rails
  • rants
  • reading
  • restful_authentication
  • rspec
  • rss
  • ruby
  • rubygems
  • scriptaculous
  • setup
  • simplicity
  • sprites
  • statemachine
  • tips
  • treetop
  • utilities
  • web
  • web design
  • websense
  • yaml
  • zentest

Browse by Month

  • March 2010 (1)
  • September 2009 (1)
  • August 2009 (2)
  • July 2009 (2)
  • May 2009 (1)
  • April 2009 (1)
  • February 2009 (4)
  • January 2009 (2)
  • December 2008 (2)
  • November 2008 (5)
  • October 2008 (3)
  • September 2008 (12)
  • August 2008 (28)
 
 
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed