Java


The key here is using Location (instead of Directory) for your Deny directive (otherwise your JkUnmount’d calls will not be blocked)

  SetEnvIfNoCase User-Agent "Firefox" bad_bot

  <Location "/">
    Deny from env=bad_bot
  </Location>

Amusing use of the ternary from our codebase..

log.debug( ( randomize ? "R" : "Not r" ) + "andomizing content" );

:)

Facebook development team has open-sourced their light-weight, cross-language development platform, Thrift.

I played around it with it for a while and it looks interesting enough, but I don’t have an immediate need for it.

Check it out if you have a cross-language environment Thrift currently supports C++, Java, Ruby, Python and PHP - a list that should satisfy most everyone.

A POSIX-compliant *NIX system is a requirement, but I’d be curious if it’s possible to get it up n’ running inside of Cygwin (as a development-only exercise, of course).

I haven’t been using “traditional” singletons for a couple of years now (Spring craze and all), so the Initialization On Demand Holder idiom, which allows for lazy instantiation of singletons, has escaped me until now.

private static class LazySomethingHolder {
    public static Something something = new Something();
}

public static Something getInstance() {
    return LazySomethingHolder.something;
}

Here’s the explanation, courtesy of Bill Pugh, Brian Goetz and friends.

Found via TheServerSide.

I was writing a multi-line batch script with multiple call-outs to Ant and I was confused when it kept exiting after the first call-out.

I’m proud to be rusty at batch scripting, so I’m not ashamed to share the (obvious) fix…

To run a batch program from within the current batch program, use the call command followed by the name of the batch program you wish to run. After the second program is finished, it will return to the command which follows the call command.

If you invoke a batch file without using the call command (from within a batch file that is), control passes over to the new batch program and does not return.

Every so often, I want to document my JSPs w/o having the comments leak into the public HTML source.

Luckily, Sun provided aa straight-forward way to get this done using JSP Comments

<%@ page language=”java” %>
<html>
<head><title>A Comment Test</title></head>
<body>
<h2>A Test of Comments</h2>
<%– This comment will not be included in the response –%>
</body>
</html>

If you would like Spring to do the heavy-lifting for your Factory class, you can simply inject ApplicationContext into your factory and delegate your create() methods to Spring’s getBean() methods.

public class YourServiceFactory implements ApplicationContextAware {
    private ApplicationContext context;

    public void setApplicationContext(ApplicationContext context) {
        this.context= context;
    }

    public YourService createYourService() {
        return (YourService ) context.getBean(serviceName);
    }
}

Here’s a simple recipe I’ve used over and over (wherever auto-wiring constructors was inappropriate). Validating at container startup time is certainly better than relying on NPEs at runtime.

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

public class FooService implements InitializingBean {
    private Dependency dependency;

    public void afterPropertiesSet() throws IllegalArgumentException {
        Assert.notNull(dependency, "Dependency cannot be null");
    }

    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }
}
  1. Ctrl+Alt+O - clean up shit they import
  2. Ctrl+Alt+L - format their shit so you can read it
  3. Shift+F6 - fix their shitty naming convention
  4. Ctrl+Alt+V - break up all the shit they squeezed onto one line
  5. Ctrl+Alt+M - break up their shitty method into two smaller shitty methods
  6. Ctrl+Alt+T - surround their shit with a try/catch
  7. Alt+Insert - add hashCode() to the shit they forgot to add it to
  8. Alt+Enter - ask IntelliJ to help you clean up their shit
  9. F6 - move their shit to a class you don’t have to deal with
  10. Alt+Delete - remove their shit

As I’m working on an API that returns matrices of solutions in its responses, I’m finding Wikipedia’s definitions for some of the concepts I’m trying to express quite handy.
Methods for ordering & storing multidimensional arrays fall into the bucket, as I’ve struggled to figure out how to express a matrix in JDK 1.5 (1-D Array? 2-D Array? List of Lists? Map?).

It turns out that there are two fundamental ways to store multidimensional matrices in linear memory - Row-major & Column-major. They’re not that different from each other (in fact, they’re transpositions of each other), but it’s nice to attach a proper name to the concept.

1  2  3
4  5  6

In row-major storage, a multidimensional array in linear memory is accessed such that rows are stored one after the other. It is the approach used by the C programming language as well as many other languages, with the notable exception of Fortran.

1  2  3  4  5  6

Column-major order is a similar method of flattening arrays onto linear memory, but the columns are listed in sequence. The programming language Fortran uses column-major ordering.

1  4  2  5  3  6

Over the last couple of months I’ve spent a bit of time studying Scala, a multi-paradigm (function + oo) programming language designed to inter-operate with both .NET and Java.

One of the (many) neat features of the language is its inclusion of the trait abstract data type. Scala traits are similar to Java interfaces in that they are used to define method signatures for classes in an inheritance hierarchy. In contrast to interfaces, however, they can also include definitions of some of their methods.

trait Similarity {
def isSimilar(x: Any): Boolean
def isNotSimilar(x: Any): Boolean = !isSimilar(x)
}

Traits are not unique to Scala, they have also been tried out in Squeak, Perl and C#.

While studying Lisp documentation on S-expressions and their use of prefix notation, I accidentaly picked up the proper name for the notation I have used all these years (in imperative language land) - infix notation. As always, Wikipedia definition comes in handy…

Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on (e.g. 2 + 2). It is not as simple to parse by computer as prefix notation ( e.g. + 2 2 ) or postfix notation ( e.g. 2 2 + ), but many programming languages use it to take advantage of its familiarity.

In infix notation, unlike in prefix or postfix notations, parentheses surrounding groups of operands and operators are necessary to indicate the intended order in which operations are to be performed. In the absence of parentheses, certain precedence rules determine the order of operations.

Next Page »