December 2006 - Posts
Here is my version of Hello World. I call this, Pluggable HelloWorld! :)
Greeting.java, which all Classes must implement to be a plug-in.
1 2 3 4 5 6 | package ph.devpinoy.helloworld;
public interface Greeting { public String sayGreeting(); } |
HelloWorld.java
1 2 3 4 5 6 7 8 9 | package ph.devpinoy.helloworld;
public class HelloWorld implements Greeting { public String sayGreeting() { return "Hello World!"; } } |
Now we want to be able to make our app pluggable even when the class
files are already compiled(i.e. we won't be able to make any changes).
We need a properties file. It's just a plain text file with a key and
value specified in it.
greetings.properties
greeting-class=ph.devpinoy.helloworld.HelloWorld
Now for our main class. It doesn't have to extend Greeting. We just
need to load our class dynamically through the ClassLoader and locate
the class with the help of the Properties file.
GreetingDriver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package ph.devpinoy.helloworld;
import java.io.InputStream; import java.io.IOException;
import java.util.Properties;
public class GreetingDriver implements Runnable { public static final String propertyFileName = "ph/devpinoy/helloworld/greetings.properties"; public static final String className = "greeting-class"; public static void main(String[] args) { GreetingDriver gDriver = new GreetingDriver(); Thread t = new Thread(gDriver); t.run(); }
public void go() { System.out.println("Loading property file: " + propertyFileName ); System.out.println("Loading class from property: " + className ); InputStream propertyFileStream = ClassLoader.getSystemClassLoader().getResourceAsStream(propertyFileName); Properties properties = new Properties(); try { properties.load(propertyFileStream); Class customGreetingClass = Class.forName( properties.getProperty(className) ); Greeting greeting = (Greeting)customGreetingClass.newInstance(); System.out.println( greeting.sayGreeting() ); } catch(IOException ioex) { ioex.printStackTrace(); } catch(ClassNotFoundException cnfex) { cnfex.printStackTrace(); } catch(InstantiationException iex) { iex.printStackTrace(); } catch(IllegalAccessException iaex) { iaex.printStackTrace(); } } public void run() { go(); } } |
It doesn't have to support multithreading. I just implemented the
interface because I thought it would be nice to be able to print it and
see the output change without having to close the app. But then, I
realized it will take a lot of tweaking and I got lazy! :)
So how does it become pluggable? Remember our Greeting interface? Let's try to make another one of those...
MerryChristmas.java
1 2 3 4 5 6 7 8 9 | package ph.devpinoy.helloworld;
public class MerryChristmas implements Greeting { public String sayGreeting() { return "Merry Christmas!"; } } |
and let's change the value in greetings.properties file from
greeting-class=ph.devpinoy.helloworld.HelloWorld
to
greeting-class=ph.devpinoy.helloworld.MerryChristmas
Ooops! No need to compile everything. Just the MerryChristmas class. See the magic! :)
The code is by no means copy-pasted. I made it on my own. But like many others, I did use some resources and they deserve some credit. It was a really fun coding experience for me. :)
Resources:
http://mindprod.com/jgloss/classforname.htmlhttp://site.red-illusions.dk/2006/11/04/simple-dynamic-loadingunloading-of-code/http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.htmlhttp://www.theserverside.com/tt/articles/article.tss?l=IOCBeginners
I was instructed to convert a PHP script that executes a shell command(mysqldump command) to Java. There were various resource available and executing the mysqldump command was a peice of cake. However, I wasn't getting the output that I expect. The output continously says that the mysqldump command has failed and would show an error
table ">" not found
hmmm... let's try to look at my command(this is just a sample)
mysqldump -uUser -pPassword mydatabase mytable > mydumpfile.sql
it works ok with the command line... But not with Java. I searched google and found numerous answers.
The most obvious way perhaps is not to use the redirect operator(>) and just create a File within the Java program and write the InputStream you get from class Process's getInputStream() into the a FileWriter object. But a more likely solution was the one I found where you just change the ">" to -r. I'm not sure what the problem really was but it seems that there were localization issues and any non-english machine would probably get this wrong if used inside Java. I use a properties file so changing the command was easy(and flexible). The command now looks like this
mysqldump -uUser -pPassword mydatabase mytable -r mydumpfile.sql
hope this helps! :)
P.S. The redirect operator is not a MySQL only command. It lives natively on Windows or other operating systems as well.
Ok, here's something that I learned from my supposedly team lead. Unfortunately, it seems that she won't be staying long here in our company. Anyway, here it goes.
I'm assuming you already have Eclipse and a class file setup. Now type in this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class CleanUpTest { int field1; String field2; public int getField1() { return field1; } public void setField1(int field1) { this.field1 = field1; } public String getField2() { return field2; } public void setField2(String field2) { this.field2 = field2; }
} |
That, or something similar. Just don't mess with the curly braces first or you'll defeat the purpose of this tutorial.
You should a setting similar to mine... It doesn't matter how many packages you have, just have at least 1 class file with code written in it.

Now click on window, and then preferences

The preferences window should appear. Collapse the Java menu, code style and then clik on formatter.

Click on new button. A small window similar to the screen below should appear. Type-in MyFormatter for the name and just choose Eclipse[build-in] on the drop-down menu. Click ok.

You should be brought to the Edit Profile window. Here, you can define how you want to format your code. Try to play with it. For the sake of this example, just click on Braces tab and change all "Next "line". Click ok until you're back into your main workspace.

Right click anywhere in your code. Click on source, format and....

tadaaaaaaaaa!

Now I arranged my code according to my preference! Pretty neat eh? This is also available in other IDE's. I just wanted to share this because for about 11 months of using eclipse, I didn't know this until now!
My friend, Adonis(his nick here in devpinoy) mentioned to me this PHP framework called CodeIgniter. It kind of resemble the Struts framework and I have to say that I really like it! It's neat and is easy to use. I haven't really tried it yet. I just watched the video right here...
http://www.codeigniter.com/videos/ci_intro.movBut it should be enough to get you the feel of how easy it is.