Tejus Parikh

I'm a software engineer that writes occasionally about building software, software culture, and tech adjacent hobbies. If you want to get in touch, send me an email at [my_first_name]@tejusparikh.com.

2009 In Review

Posted by Tejus Parikh on January 04, 2010

It’s the first work day of the new year and in order to delay heading out into the frigid cold (it’s about 15 right now), I figure it’s a good time for a 2009 recap.

Read the full post »

Sizzle: A Standalone CSS3 Selector Library

Posted by Tejus Parikh on January 19, 2010

Sometimes a full blown javascript library like JQuery, MooTools, or Dojo is impractical. I just had such a project, but the one feature I could not live without was CSS3 selectors. Walking the DOM manually would have taken hours to implement correctly, especially since each page needed to be treated differently. My first thought was to rip out the JQuery selector functions. That’s when I discovered Sizzle. Sizzle is the CSS3 selector engine written by John Resig that was originally written for JQuery and is now also used in Dojo. Since it’s a standalone library, you can also use it directly for your projects. The first step is simple, you need to include the Sizzle js file in your page. Once you do that, you can call Sizzle just as you would call $ in normal JQuery. Examples:


// get all divs

Sizzle("div")


// Get all links in the paragraphs in the sidebar

Sizzle("#sidebar p a")


// Get all li elements under a ul

Sizzle("ul > li")

Just like JQuery, right? Well no. Unlike JQuery, which returns a JQuery object that can be accessed with JQuery’s easy and cross browser API’s. Sizzle returns a list of elements. If you wanted to set the text of the first link element to “Go To This Page,” then what you’d have to do is the following:

var elements = Sizzle("#sidebar p a");

elements[0].innerHTML = "Go To This Page";

If the necessary manipulations are simple, using Sizzle standalone is the way to go. It’s much smaller than a full blown JS library and it gives you what’s normally the most painful part.

Read the full post »

Dear Eclipse

Posted by Tejus Parikh on January 23, 2010

It’s over, we’re done. No need to come by and collect your stuff. Your workspace is already in the trash. There’s no trace you were even here.

Read the full post »

Splitting Personalities

Posted by Tejus Parikh on February 02, 2010

I’m not a software developer, entrepreneur, or engineer. That’s just what I do. There’s a lot that I’m interested in beyond my work. For the longest time, my online presence has been at ViJedi.net. However, in the era of micro-blogging and increased social interaction on the web, I’ve found myself holding back from posting things that I find interesting. I’m always faced with the fact that half my audience doesn’t really care that I went to park and took some pictures. The other half doesn’t care that Slicehost rocks or are interested in a new Spring MVC trick.

Read the full post »

JBoss Hot-Deploy With Maven

Posted by Tejus Parikh on February 03, 2010

If you’re a web-developer, one of the best features of Eclipse is the JBoss IDE plugin. The mere fact that you don’t have to re-compile/re-deploy the war for every change saves countless hours per build. If you don’t want to use Eclipse, you can replicate this behavior with Maven and run the app out of your working directory. The first step is to navigate to the location of your code (hereon referred to as $CODE_DIR) and execute the maven task:


$ mvn war:inplace

This will create the entire war structure in $CODE_DIR/src/main/webapp. Take a look. You’ll see a classes and lib directory under WEB-INF. This is probably a good time to exclude these directories from version control. I use Git, so I added the following lines to .gitignore at the top level of the project:

src/main/resources/webapp/WEB-INF/classes

src/main/resources/webapp/WEB-INF/lib

Now that you have a complete war you can have it deployed in jboss by creating a symlink. Assuming that $JBOSS_HOME is set, you need the following command:

ln -s $CODE_DIR/src/main/webapp $JBOSS_HOME/server/default/deploy/web.war

Startup JBoss and you can now edit your non-compiled resources, like Javascript, JSPs and CSS at will, without a compile/deploy cycle. If you want similar behavior for changes to code or libraries, there’s just one more step. Running:

mvn compile war:inplace

on its own won’t redeploy the classes. For that, you’ll need to touch the web.xml file. You can do this with the maven exec plugin. This part is Linux/Mac OSX only. If you’re on Windows, well you have bigger problems beyond hot-deploy. Open up $CODE_DIR/pom.xml and add the following lines:

org.codehaus.mojoexec-maven-plugin1.1exectouchsrc/main/webapp/WEB-INF/web.xml
Now running mvn compile war:inplace exec:exec will redeploy the project right from your working directory. I have this configuration mapped to a keystroke in Intellij to make the process seamless. The benefits of this approach is that it will work with any IDE or development environment. It also works with JBoss 4.2 and 5.0 (JBoss tools has had some issues with 5.0 because of the VFS changes). It also works every time. JBoss tools would occasionally not update static files. The downside is that it’s not as seamless nor as fast. It will redeploy the webapp, just as if you had pushed a new war. The time savings are still substantial and I would give this approach a try if you use an environment other than eclipse for your java development.

Read the full post »

Using Java Enums to Return Collections of Other Enums

Posted by Tejus Parikh on February 25, 2010

I really like Java enums as a way to organize and consolidate all the string constants that come with building systems with a lot of settings files. In our case in particular, we have an ETL process that moves data between sources that have no knowledge of each other. All checks to ensure that there are only valid values in each system need to happen in the Java code. Enums are perfect for this, but some of our settings are hierarchical and it’s a little unclear how this would work in code. Our lexicon looks a little like this:


music

 \

    jazz

    pop

    funk

    blues

    rock

|

article

 \

    blog

    feature

    review

|

video

 \

    music

    interview

    comedy

Creating an enum for the top level is straight-forward:

public enum Scheme {

	music, article, video;

}

So now how do we get Scheme.music to return the valid values for it’s subtypes? First we need to create a marker interface:

public interface SchemeClass {}

You can add abstract methods on an enum, so we can create a method that will return an array of SchemeClasses:

public enum Scheme {

	music, article, video;

	

	abstract public SchemeClass[] getClasses();

}

And while we’re at it, lets create the enums for the sub-categories (consolidated here, but in practice I put them in different files):

public enum MusicClasses implments SchemeClasses {

    jazz, pop, funk, blues, rock;

}



public enum ArticlesClasses implements SchemeClasses {

    blog, feature, review;

}



public enum VideoClasses implements SchemeClasses {

    music, interview, comedy;

}

The important thing is that all of these enums implement the SchemeClass interface. Now to get the Scheme enums to return the list of valid subtypes. Now all that’s left is implementing the abstract method for each of the possible values in Scheme.

public enum Scheme {

    music {

        public SchemeClass[] getClasses() {

            return MusicClasses.values();

        }

    },

    article {

        public SchemeClass[] getClasses() {

            return ArticleClasses.values();

        }

    },

    video {

        public SchemeClass[] getClasses() {

            return VideoClasses.values();

        }

    }

    ;

    abstract public SchemeClass[] getClasses();

}

Read the full post »

Manually Set the Terminal Title in OSX

Posted by Tejus Parikh on March 14, 2010

Having the process automatically set the name of the terminal in OSX almost never works for me. A little googling turned up the bash sequence for setting the process name in the terminal.


echo -n -e "\033]0;TerminalName\007"

This command will set the name of the terminal to “TerminalName.” Instead of trying to remember this string, I created a little shellscript:

#!/bin/bash



echo -n -e "\033]0;$1\007"

I called this script setname and put it in /usr/local/bin. Then you can call it with:

setname TerminalName

Read the full post »

Keeping Test Emails out of the Wild In Rails

Posted by Tejus Parikh on June 29, 2010

There are a few cardinal sins for a developer: deleting the production database, deploying code to the wrong machine, and sending out emails to all the mock users. These situations happen because the terminal window on production looks an awful lot like the terminal window on dev. I don’t have solutions to the first two problems, but preventing emails to the test user database is pretty easy in Ruyb on Rails. Two solutions I’ve found are santize_email and mail_safe. These are both ActiveRecord extensions that allow the user to set an override address that will be the recipient of the email. Sanitize_email is the first solution I tried. It’s configuration is straight forward and it can either be installed as a plugin or gem. Personally, I like gems because they can be shared across multiple projects. Once you’ve installed the gem, you need to configure an initializer with the following (from their README):


    # Settings for sanitize_email gem.  These can be overridden in individual config/%env%/environment.rb files.



    require 'sanitize_email'

    ActionMailer::Base.sanitized_recipients = "jtrupiano@gmail.com"

    ActionMailer::Base.sanitized_bcc = nil

    ActionMailer::Base.sanitized_cc = nil



    # optionally, you can configure sanitize_email to to include the "real" email address as the 'user name' of the

    # "sanitized" email (e.g. "real@address.com ")

    ActionMailer::Base.use_actual_email_as_sanitized_user_name = true # defaults to false



    # These are the environments whose outgoing email BCC, CC and recipients fields will be overridden!

    # All environments not listed will be treated as normal.

    ActionMailer::Base.local_environments = %w( development test staging )

You can set the configuration to override the recipients, the cc, and the bcc. One could use sanitize_email to automatically set a bcc for all production emails, along with setting all three to prevent emails from going into the wild. Another neat feature is that it provides all Mailers with the force_sanitize method, which programmatically traps emails through a specific path. Mail_safe is available strictly as a gem. It’s configuration is a little simpler. The following is the basic config from their README:

  if defined?(MailSafe::Config)

    MailSafe::Config.internal_address_definition = /.*@my-domain\.com/i

    MailSafe::Config.replacement_address = 'me@my-domain.com'

  end

It doesn’t allow distinct addresses for bcc, cc, and recipients, preventing it’s use for automatic override addresses. While it lacks that feature, it does allow addresses for white-listed domains to be delivered, as well as allowing users to provide a proc for the options settings. For example:

    MailSafe::Config.internal_address_definition = lambda { |address|

      address =~ /.*@domain1\.com/i ||

      address =~ /.*@domain2\.com/i ||

      address == 'full-address@domain.com'

    }



    # Useful if your mail server allows + dynamic email addresses like gmail.

    MailSafe::Config.replacement_address = lambda { |address| "my-address+#{address.gsub(/[\w\-.]/, '_')}@gmail.com" }

Our choice was to use mail_safe since it did allow white-listed domains to be delivered normally. This fit the feature set of giving us a safety net incase our mock data got pushed to a system with a live mail-server, while still enabling our test users to use the application.

Read the full post »

Creating a Custom XSLT Function in Saxon HE

Posted by Tejus Parikh on July 23, 2010

In our XSTL workflow we make use of a lot of XPATH 2.0 features, such as it’s built-in regex support. Unfortunately, the default Java6 XML parsers only support XPATH 1.0. The library we settled on is Saxon HE, since it was free, supported the features we needed, and could be extended with Java functions. One of my tasks was to convert all relative paths in an XHTML document to absolute paths. The server prefix was set as variable in the stylesheet. The transformer would have to determine if the selected path is a relative url, then work to resolve what the absolute path is based on the root passed into the page. It could be done with an advanced XSTL template, but since we already had the resolution function written in Java, it made more sense to write a Java plugin to Saxon. One of the missing features of Saxon HE is the seamless, reflection based integration of plugins. However, one can use the Extension Function API to achieve the same results. First on the agenda is creating an extension point:


package net.vijedi.saxon.extensions;



import net.sf.saxon.expr.StaticProperty;

import net.sf.saxon.expr.XPathContext;

import net.sf.saxon.functions.ExtensionFunctionCall;

import net.sf.saxon.functions.ExtensionFunctionDefinition;

import net.sf.saxon.om.*;

import net.sf.saxon.trans.XPathException;

import net.sf.saxon.value.SequenceType;

import net.sf.saxon.value.StringValue;



public class AbsolutizeUrl extends ExtensionFunctionDefinition {

    /**

     * The function will need a name you can call

     */

    private static final StructuredQName qName =

            new StructuredQName("", 

                    "http://vijedi.net/", 

                    "absolutizeUrl");



    @Override

    public StructuredQName getFunctionQName() {

        return qName; 

    }

}



The extension point extends ExtensionFunctionDefinition. I went ahead and created a constant that will store the name of the class and the function to return it. You will use this to access the function from inside of your XSLT. Now it’s time to think about the interface of this function. The function can take up to two string parameters, the absolute url base, and an optional url to process. The url to process is optional since it is not a requirement that an <a> will have an href attribute. The function will return either a string or null if the second parameter does not exist. This is how you define this interface in the code.

private final static SequenceType[] argumentTypes = new SequenceType[] {

        SequenceType.SINGLE_STRING,

        SequenceType.OPTIONAL_STRING

};



@Override

public int getMinimumNumberOfArguments() {

    return 1;

}



@Override

public int getMaximumNumberOfArguments() {

    return 2;

}



@Override

public SequenceType[] getArgumentTypes() {

    return argumentTypes;  

}



@Override

public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {

    return SequenceType.makeSequenceType(

            suppliedArgumentTypes[0].getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_ONE);

}

Once the interface is defined, it’s time to define the actual work. The actual call is handled by a class that extends ExtensionFunctionCall. I like to define these as inner classes of the ExtensionFunctionDefinition. The pattern for this class is pretty simple. You need to process the arguments to the function. Saxon will give you wrapped arguments that you will need to unwrap. Then you need to call the actual logic (which should be in a separate class for re-usability) and finally wrap and return the value. Just as crucially, you need to override the function that tells the Saxon parser to use your implementation of ExtensionFunctionCall for this definition.

    @Override

    public ExtensionFunctionCall makeCallExpression() {

        return new AbsolutizeUrlCall(); 

    }



    private static class AbsolutizeUrlCall extends ExtensionFunctionCall {



        @Override

        public SequenceIterator call(SequenceIterator[] arguments, XPathContext xPathContext) throws XPathException {



            StringValue pageUrlSV = (StringValue) arguments[0].next();

            if(null == pageUrlSV) {

                return EmptyIterator.getInstance();

            }



            StringValue hrefUrlSV = null;

            if(arguments.length > 1) {

                hrefUrlSV = (StringValue) arguments[1].next();

                if(null == hrefUrlSV) {

                    return EmptyIterator.getInstance();

                }

            }

            

            String pageUrl = pageUrlSV.getStringValue();

            String hrefUrl = hrefUrlSV.getStringValue();



            // Url transformation magic goes here



            Item item = new StringValue(fullUrl);

            return SingletonIterator.makeIterator(item);  

        }

    }

That completes the definition of the function. You can find the full example code on GitHub. Now that you’ve written an extension, you need to tell Saxon that this function exists. For this, you will need to add the following to wherever you are currently accessing the TransformerFactory.

private TransformerFactory getTransformerFactory() throws net.sf.saxon.trans.XPathException {

    TransformerFactory tFactory = TransformerFactory.newInstance();

    if(tFactory instanceof TransformerFactoryImpl) {

        TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) tFactory;

        net.sf.saxon.Configuration saxonConfig = tFactoryImpl.getConfiguration();

        saxonConfig.registerExtensionFunction(new AbsolutizeUrl());

    }

    return tFactory;

}

This code checks to see whether or not you’re using a Saxon processor, and if so, registers your new function within it. Finally, it’s time to update the stylesheet to use the new function. You’ll need to add the function to the namespace, using the same parameter found in the second argument of the StructuredQName constructor.

Now you can use it like any other function:

Read the full post »

Evaluating Vaadin: A Java Web Application Framework

Posted by Tejus Parikh on August 05, 2010

Vaadin is an interesting RIA platform built by Vaadin, LTD. Vaadin differs from your standard RIA by existing almost exclusively on the server. All application state remains on the server and all events are handled through communication with the server. The display layer is written as a layer above GWT and the whole project is open source and free. This evaluation post is the first in a series of my lessons learned while evaluating this framework. Vaadin is a good fit for all the web-applications that fit in the general bucket of “enterprise support tools.” The reasons why are covered after the jump.

Read the full post »

Ruby Full Text Search Performance: Thinking Sphinx vs Sunspot Solr

Posted by Tejus Parikh on November 05, 2010

Full text search support has come a long way since the early days of Ferret. I’ve been using Ultrasphinx for a few years, and while it runs great, it doesn’t work out of the box with Rails 3. Two search projects that seem to be garnering a lot of support from the community are Thinking Sphinx and Sunspot. Thinking Sphinx is the most logical successor to Ultrasphinx, since both utilize Sphinx as the search server. Sphinx works by reading information out of the database to build the search index. Communication with the Sphinx server occurs by sharing C “objects” over sockets. Sunspot uses Solr, a Java search server built on the Lucene search library. Sunspot communicates with Solr through its REST API, using XML. Although the search engine is written in Java, Sunspot bundles a version of Solr that runs as a standalone server to make deployment just as easy as Thinking Sphinx. Solr is a compelling alternative to Sphinx, since the most scalable Web apps (Facebook, Twitter) use Java behind the UI layer. Solr servers can be clustered and since they manage the index, Sunspot can automatically update the indexes when the model objects change. There’s no need to run a cron job to reindex the data or setup delta indexing like with Thinking Sphinx. However, the impact of XML serialization/deserialization required for communicating with Solr on performance worries me. Processing XML documents is not as fast as unpacking C objects. In order to test this difference, I created a little benchmark to measure the relative impact. The Readme describes the test in more detail along with providing the source and instructions so you can configure it to your needs. To give the test a slightly more realistic scenario, the benchmark was run within my Ubuntu VM while communicating with the search process running on my OSX host. The host box has four cores clocked at 2.66 GHz each. The Ubuntu VM had one core dedicated to it. There was plenty of ram available for both the search engine and the benchmark task processing the search results. Mostly I did this to ensure that Thinking Sphinx wasn’t cheating by using unix sockets for communication. I did 50,000 searches and printed out a timing after every 5000 searches. These are the results:


Runs    Thinking Sphinx       Sunspot

5000              38.49       1611.60

10000             38.54       1648.51

15000             39.06       1614.52

20000             38.86       1583.53

25000             39.78       1613.79

30000             38.83       1595.60

35000             38.34       1571.96

40000             38.06       1631.87

45000             37.57       1603.31

50000             38.23       1634.53

Total            385.80      16109.26

I had expected Thinking Sphinx to be faster, but not 45 times faster. Extrapolating the numbers out, one can run 200,000 searches in the time it takes Solr to run 5,000. This was just a rough test to see the relative difference and is purely based on read performance of a few hundred records. It’s possible that proper tuning could improve performance or frequent re-indexing could degrade Thinking Sphinx’s performance, but it’s hard to see that chasm closing enough for there to be comparable performance when the search index can fit on one machine.

Read the full post »

Macbook Air (13") Review

Posted by Tejus Parikh on December 06, 2010

The question I had to ask myself was is it really worth upgrading from a 1st gen MBA with a 1.8Ghz Core 2 Duo to a brand new one with a 1.86Ghz Core 2 Duo and could I do something better with the money. Since I was already buying lots of gear for the upcoming trip, I put my old one up on CL and wandered over to MacMall. A few days later, I got rid of the old one, had the new one, and had a more manageable number in my bank account.

Read the full post »