I decided to learn a bit about Googles take on dependency injection called Guice today.  After reading the user guide I decided to start writing some simple code.  I figured what better way to do this than to mimic the first few examples of Craig Walls Spring in Action! The first example is just a DI Hello world.


package org.minus9.guice.chapter01.hello;
public interface GreetingService {

void sayGreeting();

}

package org.minus9.guice.chapter01.hello;
public class GreetingServiceImpl implements GreetingService {
private String greeting = "oh hai dint c u thar!!!!";
public void sayGreeting() {

System.out.println(greeting);

}

public String getGreeting() {

return greeting;

}

public void setGreeting(String greeting) {

this.greeting = greeting;

}

}

package org.minus9.guice.chapter01.hello;
import com.google.inject.AbstractModule;
public class HelloModule extends AbstractModule{
@Override

protected void configure() {

bind(GreetingService.class).to(GreetingServiceImpl.class);

}

}

package org.minus9.guice.chapter01.hello;
import com.google.inject.Guice;

import com.google.inject.Injector;
public class HelloApp {
public static void main(String... args) {

Injector injector = Guice.createInjector(new HelloModule());

GreetingService service = injector.getInstance(GreetingService.class);

service.sayGreeting();

}

}

Now the real magic here is in the final snipped of code where you utilize the Injector class and the Static createInjector method of the Guice class. This is the equivelent of Springs Bean Factory for running outside of a Web Container. Now a few things you will notice also is that I am actually setting the Greeting String inside the implementation instead of being injected through XML properties. This felt really weird for me but I suppose it being in XML or Java code really is meaningless. On that same note.. there is no XML here which makes me very happy. The other part of the magic is the HelloModule which is what registers the implementation to the interface. This is the most basic way of doing this and it is very similar to way beans are wired in Springs XML.

If you need to create a self signing ssl cert on IIS you can use SelfSSL.  This tool is part of IIS Resources that can be downloaded here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=56fc92ee-a71a-4c73-b628-ade629c89499&displaylang=en

Then once it is installed just go to SelfSSL.exe which will pop up a command line and run:

selfssl /T /N:CN=something.com /V:9999

The /V value is how many days this certificate is good for.  The default is 7 so just set it high.

Just wanted to write this out since I have done this several times and have to remember how I did it every time.

…written by a Java developer in an airport with access to the first two 20 page tutorials of the Scala Homepage.  This is obviously not the most efficient implementation but I felt it was an interesting perspective on how a Java develper can decently adapt to scala.


object Main extends Application {

val numbers = new Array[int](10)

//Figure out how to create simple Arrays on the fly
numbers(0) = 200;  numbers(1) =12;

numbers(2)=31;  numbers(3)=13;

numbers(4)=32;  numbers(5)=322;

numbers(6)=33;  numbers(7)=354;

numbers(8)=353;  numbers(9)=63;

//Using a while like a Java For loop.. any scala equivelent?
var isSorted : boolean = false

while(!isSorted) {
var i:int = 0

isSorted = true

while (i < numbers.length - 1){
var lower :int =numbers(i)
var higher : int = numbers(i+1)

if (lower > higher){
numbers(i+1) = lower
numbers(i) = higher
isSorted = false
}

i = i + 1
}
}

for(val number:int <- numbers){
print(number + " ")
}

}

Will update this with some commentary when I get a chance.

A colleague of mine wrote up some SQL to do some inserts into a Postgres database and I thought some of the functions were pretty cool:

nextval(table_ids)

This function will pull the next id value from the tables id table

now()

Pulls the current timestamp

lpad(text, int, text)

left pads the text in the first given parameter by the int specified in the second parameter and pads it with the text in the third parameters.  An example is lpad(’50′,4,’0′) this will print out 0050.

generate_series(int or bigint, int or bigint)

This function generates records starting at the first parameter and ending at the last parameter.  For example a generate_series(1,100) will generate all records between 1-100

All of these functions are documented but for me I never really think of a use until I see a very good use case.  In this particular case we had a String field that needed to be itereated that the first 3 letters were a static location code.  As an example we needed to generate 00100001 - 00102000 into a table.  For the insert we used the following cobination of functions:

(’005′ || lpad(” || generate_series(2, 2000), 5, ‘0′)) which did the trick.  Pretty sweet.

Here is a pretty good list of Postgres functions from their own documentation,

The following is  a Spring MVC implementation of Controller that will take a CSV format and serve it up as an excel document.  This is just writing out a simple test so of course getting whatever you need in csv format is up to you.

This is probably very easy for most Java developers but us kids who started using the Spring MVC when we started and get kind of abstracted from the ServletRequest and ServletResponse I wanted to write this down just for reference.


public class TestExportController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
//You must set the content type before outputting!
  response.setContentType("application/msexcel");
  //get the writer from the response
  PrintWriter out = response.getWriter();
  out.print("test");
  out.flush();
  return null;
  }

}

It seems that JodaTime responds differently to the JVM System Properties that are default in Ubuntu 8.04 (At least this is where I found the issue.)

The default system property is SystemV/EST5EDT and on Windows and Solaris I am seeing US/Eastern.

The problem arose when using the following code:

MutableInterval interval = new MutableInterval();

System.out.println(interval.getEnd());

Here is the outputs from each run

US/Eastern:

1969-12-31T19:00:00.000-05:00

SystemV/EST5EDT

1970-01-01T00:00:00.000Z

Both of these Timezones are EST time zones so why is one defaulting to to UTC?  My guess is that JodaTime does not have an entry for SystemV/EST5EDT and is using a default setting.

An easy way to get System properties is the following command:

jinfo -sysprops <pid>

Hope no one else has to wasted 6 hours of their day trying to debug this.

For all you web 2.0 developers we all know that clients can’t have edges corners anymore they want the new hotness of rounded corners.

There is a cool google tool to make rounded corners.  Use the following link:

http://groups-beta.google.com/groups/roundedcorners?c=black&bc=white&w=40&h=40&a=tr

Then replace the parameters with whatever you want.

Paramater:

c= means color of the item you want to give corners this can be represented in english or the hex notation (ie. 222222)

bc= means the background color of the item ( if anyone figured out how to make this transparent drop a comment)

w= is width

h= is height

a= is alignment and takes arguments of bl (bottom left, br (bottom right), tl (top left), tr (top right)

I guess this is only half the battle to making rounded corners but at least we don’t have to deal with mucking with graphics anymore.

My coworker John had already posted about this but I wanted to add it since it is pretty cool.

In ubuntu if you run the following command:

sudo update-alternatives –all

It will start a prompt to set several different versions in ubuntu including which Java version your machine you want to use.

If you want to consolidate all of your logging into one log4j properties file instead of having multiple you can do the following:

Append something like the following to the bottom of your log4j.properties:

log4j.appender.NewLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.NewLog.DatePattern=’.'yyyy-MM-dd
log4j.appender.NewLog.file=logs/new.log
log4j.appender.NewLog.layout=org.apache.log4j.PatternLayout
log4j.appender.NewLog.layout.ConversionPattern=%d{MM-dd@HH:mm:ss}|%5p|%x| %m%n

log4j.appender.NewLogConsole=org.apache.log4j.ConsoleAppender
log4j.appender.NewLogConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.NewLogConsole.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %m%n

log4j.logger.com.newsite=DEBUG, NewLog, NewLogConsole
log4j.additivity.com.newsite=false

This will Add these two appenders and they will not interfere with current logging. (Of course if your other logging is set to a top level package then it will still log so be wary of this. )

The key to all of this is the line:

log4j.additivity.com.newsite=false

It basically says do not add this logging to the main logging.

Of ourse you can also just use two property files and have logging for each of these. In my opinion this is kind of annoying especially if you do not change the name of the log4j.properties file (Also possible.) but if you are not wanting to mess with an existing configuration of log4j then this works great.

If you are using the Hibernation Validation framework on your objects you are able to specify constraints on the getters of your pojos. When trying to persist these objects if any constraint is violated you will receive an IllegalStateException the refers to what object the validation failed on. This is sweet but if you have several fields in your object it does not specify what variable is problematic.

The good news is the InvalidStateException has a method which returns InvalidValue[]. All you have to do is catch the exception and loop through these.

Unfortunatly I was receiving an UnexpectedRollbackExceptiion so I had to write some kind of annoying code:


try {
  //try to do some hibernate stuff
} catch (UnexpectedRollbackException ex) {
  Throwable t = ex.getCause();
  InvalidStateException ise = (InvalidStateException) t.getCause();
  for (InvalidValue value : ise.getInvalidValues()) {
    System.out.println(value.getMessage());
  }
}

This is very very hacky code since I was on a time constraint but hopefully it will show you the basic idea of how to get these values. As I said it is kind of a hassle when you are working on a tight deadline and hibernate suddenly wants to play some games with you to figure out what it is grumbling about.

Next Page »