January 2009 - Posts

I've only used this template engine for a short time but I'm already inloved with it. We use Velocity to generate e-mail templates and I happen to encounter an error when I was trying to configure it progmatically:

            Properties p = new Properties() ;
            p.setProperty("resource.loader","file");
            p.setProperty("file.resource. loader.class", "org.apache.velocity .runtime. resource. loader.FileResou rceLoader");
            p.setProperty("file.resource. loader.path", "C:/opt/templates/");           
            p.setProperty("file.resource. loader.cache", "false");
            p.setProperty("file.resource. loader.modificat ionCheckInterval", "0");

            Velocity.init( p);

            VelocityEngine ve = new VelocityEngine( );
            ve.init();

            Template t = ve.getTemplate("my_template. vm");

            VelocityContext context = new VelocityContext( );
           
            t.merge( context, writer );

I would encounter an error

org.apache.velocity .exception. ResourceNotFound Exception: Unable to find resource 'my_template. vm'

I'm surpirsed it took me about 2 or 3 days to figure out that there are actually 2 ways of using the Velocity engine. Singleton and separate instance model. So the above code could've been fine if I changed the lines

            VelocityEngine ve = new VelocityEngine( );
            ve.init();

            Template t = ve.getTemplate("my_template. vm");


            VelocityContext context = new VelocityContext( );
           
            t.merge( context, writer );

 

with
            Template t = Velocity.getTemplate("my_template. vm");

            VelocityContext context = new VelocityContext( );
           
            t.merge( context, writer );

 

I hope this helps a lot of people. :)

 

 

Posted by lamia | with no comments

I'm not really fond of configuring log4j. I know the basic stuff but then, didn't anticipate to be assigned a task to log into two different loggers. I usually just log in the console and leave the complexities to the guys who setup the application server in production.

My typical, simple log4j.properties

log4j.rootCategory=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n

What the above does is just simply to log in the console whenever it encounters a "debug" statement. Now, what I needed was to log to a different log. I didn't want to share with the console and I want specific things to be written to my other log file.

First off, make sure the log4j.properties is visible in your classpath.

Add the following code in your log4j.properties

log4j.logger.devpinoyLogger=DEBUG, dest1

log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=${application.log.root}/MyOtherLogFile.log

The above listing  gives us a logger named devpinoyLogger. You might be wondering what dest1 is. It's an Appender, named dest1. Yes, you can name it anything you like. One last thing you might be wondering is ${application.log.root}. This is called a System Variable, it's a little different from an environment variable because this is something you either set in your application server or with the -D argument when you invoke a console application.

 

Now, going to the Java code.

 

Typically, you would call a logger like this.

Logger logger = Logger.getLogger(SomeClass.class);

To setup a different logger, just supply the logger name(in our case, devpinoyLogger) as the argument to getLogger.

Logger logger = Logger.getLogger("devpinoyLogger);

 

You can now log separately. I use this to log in two different files.

Logger logger1 = Logger.getLogger(SomeClass.class);

Logger logger2 = Logger.getLogger("devpinoyLogger");

public void testLog(){

    logger1.debug("This is logger 1 logging");

    logger2.debug("This is logger 2 logging");

}

Now, I know I didn't went into details. I expect you to have enough common sense to modify the above properties and code to fit your needs.

HTH. ;)

 

 

 

 

Posted by lamia | with no comments
Filed under:

I'm very sensitive on how I know my variables when I program and the most I'm very careful with is naming boolean variables.

So I was trying to create a new variable inside of Eclipse and I'm not used to naming my boolean vars like isNew, isAllowed or isValid but instead I know them new, allowed or valid but still adhere to the Java naming convention for my methods like setNew() and isNew().

Just today though, I accidentally named one of my boolean variables "isNew". To my surprise, Eclipse was intelligent enough to detect this and named my generated accessor/mutator as setNew() and isNew() instead of setIsNew and isIsNew().

Nice. :)

Posted by lamia | with no comments
Filed under: ,

I was setting up my workspace today and noticed that my wrist was hurting.

Sometimes, we just so forget to get back to our fundamentals and leaves us stressed. We don't always realize that they could make our job easier.

 

So I was creating the directory of the following structure

/dir1/some_other_dir/one_more_level_deep/put_here

I don't know if I was the only one guilty of this... What I did was right click several times and create a new folder for each of the directory listed above. I was about to do it again until I remembered... Wtf?I studied computer 4 years in College and I'm doing this? So I fired up my command prompt did

c:\ mkdir c:/dir1/some_other_dir/one_more_level_deep/put_here  <---paste

Only took a few steps. Whew!

Weird post? Not unless you've been doing the same! Lolz!

Posted by lamia | 6 comment(s)

I just want to spread that word that there is a lightweight framework for running EJB's outside a container. You could use it together with JUnit so unit testing EJB's becomes easier. The current project that I'm working on uses this and though right now. I don't have a concrete example on how this works, I can guarantee you that it does though. It's free and opensource .

http://www.mockejb.org/

Long live the EJB!

Posted by lamia | with no comments
Filed under: ,