Tip: ie7 301 redirect with hash in URL

Posted in web on March 20th, 2010 by lucas – Be the first to comment

Our main public website has been around for a few years now.  As we’ve moved from static pages to an asp framework to ultimately a CMS of our own creation, our URLs have been mucked up.  We’ve always ended them with a unique id, but the middle could change in order to help add in some SEO keywords for the page.  Our custom delivery system can detect when the innards are incorrect but the id is correct, and will issue a 301 redirect to the right URL in order to keep google from crawling all permutations of a particular page’s URL.

So, now we have a page with a few dynamic javascript tabs, and we want to use a hash in the location to tell the page to display a particular tab when it’s loaded.

http://example.com/seo/friendly/111111#tab2

http://example.com/seo/friendly/111111#tab3

However, the seo/friendly part wasn’t current, so the server was issuing a 301 redirect to the proper page, and ie7 drops the location hash upon redirect.  I was able to solve this by getting the URL right and not having to 301 redirect, but I’ve seen a few sites that people had success with things like this:

http://example.com/seo/friendly/111111?#tab3

http://example.com/seo/friendly/111111?&#tab3

Yet another ie7 annoyance.

Gradle intro

Posted in build and deploy, java on March 16th, 2010 by lucas – Be the first to comment

A better way to build

Gradle is a flexible build system written in groovy.  It is feature-rich and configurable, and allows for freeform groovy code inside the script.  It takes much of the nice parts of ant, strips out the XML, and adds some good dependency management features.

I’ve recently moved to using Gradle for most new projects at work.  Previously, we would Right click -> export as WAR (myeclipse).  While that was easy and quick, a little configuration upfront would make the deployment process even easier (with a CI tool like Hudson).  Further, gradle buys us a lot in terms of customization and dependency management.

Advantages

  • Able to invoke via command line.  Advantage over myeclipse export being that it can be automated with ease.
  • We can configure to fit our environment and IDE.  We can modify build process on a project to project basis, taking into consideration dev and staging environments, need for testing, and the like.
  • Ease of managing dependencies.  We are able to use a combination of Maven and the file system to automate the process of downloading and adding jars to build path, and to exported war.

Caveats

  • Gradle is new.  The user guide is extensive in parts, and shallow in others.  There is no ability to google a stacktrace if you get stuck, as not many are using it yet.  The examples are helpful.
  • Necessitates telling svn to ignore certain files.  Specifically, the local .classpath and the /build/ folder.
  • Understanding which properties files are used where, when projects have multiple.

To Install:

Download then install to a location like Program Files\gradle.  To use from the command line, make sure JAVA_HOME (or have the java binaries in your path somewhere), and add GRADLE_HOME to point to the location of the top level of your gradle install.

(from gradle site)

Environment variables in Windows XP

You can install as an external tool in myeclipse, or just invoke it from the command line.

To use:

Gradle user guide (HTML, one page per chapter)

Gradle’s philosophy is convention over configuration. Throughout the core and plugins, gradle assumes a certain convention in regards to file locations, steps to execute, etc.  You can override these if necessary, but if you don’t need to, this makes configuration very straightforward and easy to get up and running.

A minimal configuration to build a non-web java project:

usePlugin 'java'

Invoking gradle build in the same working directory as the build script will build the java, run any tests, and create a jar.  It assumes no dependencies, source files are under src/main/java, and test files are under src/test/java.  To add dependencies, it looks like this:

build.gradle
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}

This will tell gradle that your code depends on these java libraries, and will fetch them from the main Maven repository.

More about the java plugin

The .war plugin functions much the same way.  You get a set of configurations by default, which may be overridden.

war {
fileSet(dir: file('src/rootContent')) // adds a file-set to the root of the archive
webInf(dir: file('src/additionalWebInf')) // adds a file-set to the WEB-INF dir.
additionalLibs(dir: file('additionalLibs')) // adds a file-set to the WEB-INF/lib dir.
libConfigurations('moreLibs') // adds a configuration to the WEB-INF/lib dir.
webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
}

That is enough to build a web project!

More about the war plugin

A First Foray Into Xen VPS with Prgmr

Posted in servers on March 1st, 2010 by lucas – Be the first to comment

Xen

Backstory

My problems began when I stumbled upon the fact that I could no longer use my old desktop as a development server.  Behemoth cable companies being what they are, I realized that mine was deviously blocking port 80, but only outside of a ~50 mile radius of my home.  That Thanksgiving week with the family a few hundred miles away was a bummer.

I needed some hosting.  And I needed something that I could play with, outside of the confines of even what the more configurable hosting services had to offer.  I needed tomcat and an apache tomcat connector.  I needed svn.  I needed cron and some executable .jars.  I needed.. xen.

Linode or Slicehost seemed to be the name of the game.  They looked comparable.  Expensive, but with a variety of features.  On the other hand, I always wanted the geek cred of setting up a server without help.  I went with pgrmr.com.

Now, I’m a programmer by trade.  I like the command line, but I spend my days in Eclipse.  Would I be able to handle prgmr?  After I saw the prices, I knew I had to try.

Enter Prgmr

I signed up for the 512mb/80gb transfer for $12/mo.  Upside: cheap, cheap, cheap.  Downside: expect to do everything yourself.  This includes DNS and mail.  Fortunately, I still have a dreamhost account for some freelance and such (including this blog).  I kept my DNS on dreamhost and pointed domains I wanted over to my prgmr.com vps.  I used google apps and their gmail for a mailserver.  This was fairly easy, but I may do a post on it in the future.

After I received the welcome email, I was able to select the distro I desired (ubuntu) and gave him a private key and I was on my way.

Setting up users

First things first, what you’re given to log into is the xen hypervisor.  It’s a launchpad to your vps as root, essentially, with other options like restart the box.  Once you’re in as root, set up some users, then get out.  If you set up a few admin accounts so you can ssh into your box and use bash just like any other ubuntu machine.  The Xen Hypervisor isn’t really conducive to getting work done (has some issues).

In Ubuntu, it’s like this

adduser joe

The adduser command prompts you for the password and such.

There is a similar command, useradd.  I learned the hard way: useradd does not create .bash_profile and .bashrc for that user.  Hence, all the nice bash colors and autocomplete and such do not happen.  If you find yourself in the same predicament, the default for these files exist in /etc/skel.  I was able to copy them over to my new user’s home directory successfully.

Setting up Apache and Tomcat

From this point, you can essentially follow any of the FAQs out there to set up your web servers.  As for myself, I needed Apache httpd, mysql, and tomcat.  I ended up installing apache httpd from the ubuntu repositories.  For Tomcat, I downloaded the binaries.  I had problems with the repository version before, and I’m used to having all the server files in the same place.

Apache httpd

sudo apt-get install apache2;

Server config files:

/etc/apache2/

Logs:

/var/logs/apache2/

Default public document root:

/var/www/

Tomcat

I followed the instructions from here.

Starting and stopping the servers:

sudo /etc/init.d/tomcat [start,stop,restart]
sudo /etc/init.d/apache2 [start,stop,restart]

For a server connector, I chose mod_proxy_ajp.  I’ve used mod_proxy and mod_jk before, so I tried something new.  I actually think it’s my favorite now.  The only thing I don’t like is that sometimes if Tomcat goes down, Apache2 needs to be kicked to get the connection up and going again.  More to come on server connectors in a future post.

Mysql

Mysql is the easiest of the bunch.

sudo apt-get install mysql-server

During the installation it will prompt you for a password for the root user.  It’s very important to remember this one.  When you’re done, you can log in with

mysql -u root -p

and providing the password at the prompt.  You’ll get a fully interactive mysql shell.

If you’re new to using the command line tool like I was, here are some commands to get you started:

Display all tables:

show databases;

Create a database:

create database the_database;

Switch to using the database you created:

use the_database;

From this point you could execute sql in the console window, like so:

create table users;

Display all tables and some info about them:

show tables;

Display all columns in the table and their type:

describe users;

Cred Established

All in all, it went very well, and I now consider myself a card carrying sysadmin.  Well, any sysadmin worth their salt will still probably despise my lack of blowfish t-shirts and perl skillz, but oh well.  I heartily recommend prgmr if you want to save a few bucks on hosting.  As far as uptime, I cannot say for sure.  I’ve never had any problems though.

Addendum

Is a 512mb “slice” good enough for the memory hog that is tomcat?  I think so.  Tomcat does end creep up from 30% of virtual memory to about 45-50%, but as I only use the box for my tomcat server, it’s no biggie.  I’ve yet to do any memory profiling, but I’ve had tomcat up and running for a good month without a crash.  I may do another blog on memory in the future.