Short source code examples

Here's an example of how to do a 301 Redirect with Nginx:

rewrite /blog/Content/2/8/581 /blog/post/java/dos-batch-files-compile-run-java-program-create-jar-file permanent;

As you can see, Nginx uses the keyword permanent instead of 301.

I just did a few hundred of these on this website. I put them in a separate "redirects.conf" file, and included that file into my main nginx.conf file like this:

Just a quick note today that MongoDB supports both null and undefined data types, and you can set them as shown here:

{ "a" : null }

{ "a" : undefined }

MongoDB also supports a boolean data type, which you can define like this:

{ "a" : boolean }


According to the URL shown, here's the difference between save and insert in MongoDB:

For save, if you provide _id, it will update. If you don't, it will insert.

There is additional documentation at the bottom of that thread:

From this, we can see that save is a wrapper for update and insert. Functionally, save and insert are very similar, especially if no _id value is passed. However, if an _id key is passed, save() will update the document, while insert() will throw a duplicate key error.

To remove all documents in a MongoDB collection (from the command line) use the remove function, and give it an empty set of parentheses, like this:

// remove all documents in the "stickynotes" collection

You can do the same thing from your Java, Scala, Ruby, Python, PHP, etc. code, just created an empty MongoDbObject and supply it as an argument to remove(). (Of course you'll want to test this somewhere besides your production systems.)

In the excellent book, Beginning Scala, there is an error in the an example named Sum that leads to an error message that begins, "missing arguments for method collect".

As a quick note, there are probably better ways to do this, but the only way I've found so far to see the number of open MongoDB database connections is to look at it from the Unix/Linux command line, using the lsof command:

I’m very short on time today, but here's the source code for a Java SwingWorker with ProgressBar example from Sun:

How to set a timeout on an Apache HttpClient:

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout", new Integer(10*1000));

I like to make my code easier to read, so I use the "10*1000" there to make it easier to read that I want a ten-second timeout.

I used the following code to convert an array of Scala objects on the server into a JSON string that I could return back to the client. So it's going from a MongoDB collection to an array of Scala objects through Gson to become a JSON string.

I accidentally created some MongoDB collections within my database named "stickynotes" that I didn't want. All I wanted was a collection named notes, but I was fooling around and accidentally created some other collections. Here's how I deleted those collections using the mongo command line. (My commands are shown in bold.)

Here's an example of how I deserialize an array of objects encoded in JSON, in this case using Scalatra and Scala:

I was able to get Gson to work with Scala and Scalatra very easily. Here's a sample Scalatra POST method where I receive a JSON string and convert it to a StickyNote case class object:

When working on a Scalatra project, this is how I start the Jetty server using SBT:

# (1) start sbt
$ sbt

# (2) start the jetty server at the sbt command line
> container:start

There is supposed to be a way to get this to be a "continuous compile", so you don't always have to stop and start Jetty, but I haven't been able to get that to work properly.

This is a simple way to center a JFrame in Java:


Scala doesn't have any special email handling facilities, so I just use my normal Java approach to send mail.

Here's the source code for my Scala 'send email' class. It's basically just a port of my previous Java 'send email' class, and requires the Java Mail API and Activation Framework jar files:

I took a couple of hours to write some Scala scripts to email me some Twitter information a couple of times a day, and while creating those scripts I created a simple Linux shell script to compile my Scala classes, while also including my library dependencies. Here's the Linux shell script I created and named

How to get the current working directory in a Scala application:

There may be other ways to do this, but I just use the Java current directory approach:

def getCurrentDirectory = new "." ).getCanonicalPath

I wrote a naive servlet application, where I'm printing to System.out and System.err, and when I use Jetty, I'm not seeing any of this output. According to the URL I've linked to, this should work with the following syntax. Here's what they say, and the Jetty startup command they recommend:

You can use etc/jetty-logging.xml to take all System.out and System.err output (from any source) and route it to a rolling log file. To do so, include etc/jetty-logging.xml on Jetty startup.

java -jar start.jar etc/jetty-logging.xml

How to create a JButton without a border:


I just tested this on a new Java Swing app, and it works as advertised, removing the JButton border.


In Java you often use ".class" on a class or getClass on an object to do some things, usually some sort of reflection work. The rough equivalent of that in Scala is the classOf operator, as shown in this code: