Tuesday, August 25, 2009

Connecting Coldfusion to a Java class.

For a while now I've want to know how to call Java from ColdFusion but didn't know how. After waking up any Java memories I had from making my "hello world" I found out it was really easy to do.

1. user.java
Remember Java is case sensitive unlike ColdFusion.

public class user{
public String getName(){

String name = "joe";
return name;
}
}

2. Drop the user.java file C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\classes directory.
--NOTE I am using localhost mulitserver and this is where I put it.

3. Test.cfm
I create a test.cfm and create an object of user. I can now work with the object.

<cfset user = createObject("java", "user")>
<cfdump var="#user#">

<cfset name = user.getName()/>
<cfdump var="#name#">

Monday, August 10, 2009

PHP and singleton's

So I wanted to have use singletons in php, but couldn't find any good tuts that didn't involve cake or zend. I did quite a bit research and asking around and here is what I came up with. I framed the findings around Coldspring which is a ColdFusion dependency injection tool, which basically means it handles your objects for you.

First I have an xml file where I define where my objects (classes) are. If you are not familiar with MVC, I would first suggest reading up on it. But anyways, below I define some objects paths so I don't have to worry about where they are. As you can see I have an objects controller, service, and gateway.

<?xml version="1.0" encoding="ISO-8859-1"?>
<objects>

<object name="objectsController" type="controller" address="app/controllers/objectsController.php"/>
<object name="objectsService" type="service" address="app/services/objectsService.php"/>
<object name="objectsGateway" type="model" address="app/models/objects/objectsGateway.php"/>

</objects>

The next step is to read in the xml nodes. So below is a function that I feed a path to my xml file (above) and it returns me the nodes in PHP.

public function loadSimpleXML($xmlPath){
if (file_exists($xmlPath)) {
$xml = simplexml_load_file($xmlPath);
return $xml;
}else{
exit('Failed to open '.$xmlPath);
}
}

The next step I loop the nodes with the name "object" and do stuff with it.

//create singletons array to hold objects in
$singletons = array();

//get the count of the nodes
$cnt = count($xml->object);

//loop object nodes
for($i = 0; $i < $cnt; $i++) { $object = $xml->object[$i];

//get object name and address
$name = $this->getAttribute($object,"name");
$address = $this->getAttribute($object,"address");

//include the file
$this->loadClass($address);

//create an instances of the object
$pointer = (string)$name[0];
$singletons[$pointer] = new $pointer;

}

Alot of stuff happened in the above function so I will break it down by a few lines. First we get the node by doing:

$object = $xml->object[$i];

After we get the node I call a function to get the attributes of the node. In my case I just want the name and where the object file is at.

$name = $this->getAttribute($object,"name");
$address = $this->getAttribute($object,"address");

public function getAttribute($node,$name){
$attrs = $node->attributes();

return $attrs[$name];
}

Next I have to include the file. In PHP I found out you need to include the file if you want to use it. To do this I created a function that does a require_once(). I know you don't need a function for it but I figure it didn't hurt and this way if I have to do anything before my include or after I have the ability to do it now. In case you don't $this is used to call a function within a object file. Also you notice I have a variable call $GLOBALS["siteroot"]. I actually load up an environment xml file that takes of these properties but to be straight...the variable just contains the site root of my project, so no biggie here.

$this->loadClass($address);

public function loadClass($classPath){
require_once($GLOBALS["siteroot"].$classPath);
}

The next part was the hard part...dynamically creating an instance of the object. I found out that I needed to use a pointer to use "new", which took forever to figure out, because me and pointers don't mix (I just wanna chain stuff together). Anyways here's what I got.

//create an instances of the object
$pointer = (string)$name[0];

//append object to an array objects so they can be used later.
$singletons[$pointer] = new $pointer;

Later I store the $singleton's array in the $GLOBAL scope because it's the only scope that I know is some what persistant...but it's not. I don't think PHP has a persistant scope without creating a session. If any one has any ideas on where I can put this $singletons array so I don't to have read it from the xml everytime (if you caught the globals...I am techinically not fully using the singleton's pattern because I have no scope to put it in) I would greatly appreciate it, but I have found this works pretty slick for now.

Sunday, August 9, 2009

Robot Coders

In most trades people do what they are told, it gets done, and life goes on. Development requires you think into the future to prevent issues, which may bring a developer to challenging the validity of the task. Challenging a task could potentially bring up new ideas or issues. I am not saying challenge every thing that is assign to you, but do a quick check against the current state of the app. A quick check could create a new module or even a product. It could also bring up trickle down issues that may occur from bringing in a new change or feature.

From what I've seen so far,in the 2 years of coding, is that some developers are mindless coders. They are like robots whom take input and return results. While management loves this, development hates it. The problem with coding this way is you get crap code that only serves a one-off purpose from either a quick decision or sales promise and harms the purpose of a feature or even the app. I just think things should be thought through a little more before they are coded up, and it is the responsibility of the coder , as a last resort, to take care of this. I am not asking for a design meeting, but 5 to 10 minutes of quick thinking or maybe a peer's opinion may bring something important up that will save time and money in the long run.

A few things I do before coding something up are:
1. What parts will be affected?
2. Will this change the current purpose of the code in a way that is not desired or maybe interpreted incorrectly?
3. How much code do I need to change?
4. Do the database relationships support what I am going to do?
5. Do I have the time to make the changes without throwing some spaghetti code together?
6. Will my changes effect/prevent someone else's ability to work?
7. Do I need a hand with the design or amount of this task?
8. Are the changes worth my time right now or are there bigger fish to fry first?

These are just a few things that go through my mind before I begin a task. I think the robot coders should become human again, by learning to think outside the box or for themselves and not by an outside entity. What goes though your guy's mind before you begin coding?