Friday, April 20, 2007

The Power of Phobos and The Platform

I wanted to talk about the power and platform that Phobos offers and how easy it is to use. Let's take an example of wanting to accomplish a simple task like reaching out across the Net in a script and retrieving a web page and sending it back to the browser.

The first thing we have to do is figure out a way to reach out across the Net. If we were programming in say asp, we would need to use a com object or dll control as vbscript does not have the power to do that. Phobos does have a library function to accomplish this task, but lets assume that it didn't. What do we do? Hhhhmmmm . . . well every class in Java is available to us, or any Java class that is installed into your Phobos installation for that matter. So in order to solve our problem, I did a quick Google search for a Java example with the terms "Get URL Java" and landed on the following URL: http://www.devdaily.com/java/edu/pj/pj010011/index.shtml

Now this example will give us a step by step procedure for doing what we want to do in Java, but we are writing in Javascript. All we have to do is rewrite the example in Javascript. It is quite easy. Here is the code for a Phobos script to do the same thing:

********************************************************************
//Set status and content type
response.setStatus(200);
response.setContentType("text/html");
// Get writer
writer = response.getWriter();
// Create the URL object
var u = java.net.URL("http://192.168.0.76/");
// Open an input stream from the url.
var is = u.openStream(); // throws an IOException
// Convert the InputStream to a buffered DataInputStream. //
// Buffering the stream makes the reading faster; the //
// readLine() method of the DataInputStream makes the reading //
// easier.
var dis = java.io.DataInputStream(java.io.BufferedInputStream(is));
// Now just read each record of the input stream, and print it out.
while ((s = dis.readLine()) != null) {
writer.println(s);
}
//Close the InputStream
is.close();
// Flush to the browser
writer.flush();
*************************************************************************

See how easy that is! Using Java classes just like they are native to Javascript. Now, we should do some error trapping and other checking, but the code above illustrates how incredibly easy it is to use Java classes.

Another great benefit of using Phobos and Java for that matter is the Java Virtual Machine. It is so powerful, you sometimes forget what it is doing for you. For example, I developed a stub application on a 32 bit workstation and tested and ran it. I then deployed it to a 64 bit dual processor Windows server running the Sun Glassfish Application Server and of course it ran flawlessly. I also deployed to a Linux server running Glassfish and it ran the same. Portability and power available to every developer!

Monday, April 16, 2007

Phobos and the Java platform . . .

Phobos has been getting more coverage lately and hopefully will continue to receive more in the near future. I believe Sun is on the right track to make a really good, lightweight, scripting stack for the average developer.

I think one of the biggest hindrances for Sun in years past was their lack of vision for reaching out to every business or developer. I think that has changed the last few years and they are on the right track on converting developers and businesses to their software. I have been a fan of Sun's for years even though I have used very little of their software. The biggest problem being ease of use and simplicity. Everyone knows that many, many huge enterprise applications run on Solaris and Java. However, for small business, it is a different ball game and it is hard to utilize all that power and stability. Java was originally written as a replacement for C. While Java was easier than C, a VB, VFP, Perl, or PHP developer wasn't really interested in learning C or Java. Yes, Java was more powerful, but the other languages were simple enough for the masses to crank out simple to somewhat complex applications. Java developers touted for years how great Java was, but you didn't see developers who were comfortable using higher level languages migrating down to Java.

This brings us to Java's opportunity which is now. Microsoft has discontinued VB6, VFP, and Access and is pushing their developers to migrate down to VB.NET or C#. Many developers would rather not do this. They are looking for alternatives. With tools like Phobos and Jmaki for the web, the landscape is looking better for those developers. I am looking forward to Netbeans 6 and the automatic databinding for the fat client, but I think the killer app would be to be able to use the Netbeans IDE tools for fat client, but be able to write your code in Javascript or a higher level language. When the masses can crank out simple applications to meet their needs, you will see migrations of developers who are looking to be able to develop cross platform apps.

I have seen some comments about Phobos from Java developers who feel we do not need another Java based web framework. I think Java developers should reach out to those who are not using Java and strive to encourage their movement to the Java platform. The Java community needs to show that it is reaching out to all developers. It has been a great stride for other languages to be ported to the Java platform.

Linux has achieved ease of use for the masses, but the lack of easy developer tools for the average developer to write applications to run on Linux has inhibited its growth. I think a Java foundation with high level languages can fulfill that role. Solaris is an outstanding operating system, but until it can be loaded on generic hardware as easy as Linux or Windows, it is difficult for people to use.

If you are currently using a Microsoft platform, you should give Phobos and the Java platform a try. Join the mailing lists and try writing a simple application. The Sun engineers answer questions and are a great help.

Next week I will discuss the similarities script developers on a Microsoft platform will find on the Phobos platform.

Monday, April 9, 2007

Phobos Ajax Site Layout

Here is an example of a simple Ajax layout. This example will show how simple it is to do Ajax with Phobos and how little code it requires. Also, it should be noted, how clear the separation of logic and presentation is by following the Phobos architecture. In order to follow the logic, you should be familiar with the Phobos architecture of controllers and views. Let's get started.

We have a main controller which is commented below. Here is the code.

// Main controller for the application.
// define a controller module called "main"

library.common.define(controller, "main", function() {
// controller class (constructor)
this.Main=function() {
// insert instance initialization code here
// "show" action method

this.show = function() {
//Use the model global variable to pass information to the view.
model = {};
// render the /application/view/main.ejs view
library.view.render({view: "home.ejs",
layout: "main.ejs"});
}
//navigate function: routes and renders the view requested
//This method is used for Ajax call to update the center section of the layout.
this.navigate = function()
{
//Get the parameter for which view to render and send back
var strUrl = String(request.getParameter("url"));
//render the requested view back to the client
library.view.render({view:strUrl});
}
};
});

That's it! That is all the controller code we need for an Ajax layout.

Our layout is a two column layout with a header, but you can do the same thing with any layout. We have one core file which makes up the structure of our layout. This is main.ejs. This is our main view. Here is the code for it.

<html>
<head><link rel="stylesheet" href=<%= library.view.quoteUrl("/jmaki-standard.css") %> type="text/css"></link>

<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var req;
var isIE;
var list;
var entryField;

// This function will take care of the cal backs to the server
// You will notice that once it gets the data back, it sets the innerHtml for the main content
// section of the layout.

function initRequest(url) {
var list = document.getElementById("list");
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
}

req.onreadystatechange = processRequest;
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(null);

function processRequest () {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById("maincontent").innerHTML = req.responseText;
}
}
}

}

//This function will be called by every hyperlink with a parameter of which view we want rendered.

//After it assembles the URL, it will call the Request function to get the view from the server.

function
submitData(strUrl) {
var url = <%= library.view.quoteUrl("/main/navigate") %> + "?url=" + strUrl;
initRequest(url);
}
</script>
</head>
<body>
<div class="outerBorder">
<div class="header">
<div class="banner">Application Name</div>
<div class="subheader">
<div>
<a
href="javascript:submitData('feedback.ejs')">Feedback</a> |
<a href="javascript:submitData('sitemap.ejs')">Site
Map</a> |
<a
href="javascript:submitData('home.ejs')">Home</a>
</div>
</div> <!-- sub-header -->
</div> <!-- header -->
<div class="main">
<div class="leftSidebar">
Sidebar Content Here
</div> <!-- leftSidebar -->
<div class="content" style="height:400px" id="maincontent">
<% library.view.layoutContent(); %>
</div> <!-- content -->
</div> <!-- main -->
</div> <!-- outerborder -->
</body>
</html>

Notice how each of our hyperlinks call the submitData function with the name of the view they link to. They do not need a path to a file, just the view name. After the functions complete, The main content section will display the view without refreshing the page.

All that is left is the three files which the above links reference. Here they are.

The home.ejs file contains: <p>Hello! This is the home page</p>
The feedback.ejs contains: <p>Hello from the feedback page</p>;
The sitemap.ejs contains: <p>Hello from the sitemap page</p>

With this foundation, we can build any number of pages and load them without redisplaying the whole page. We might should add a few more things like a progress bar and a custom back button.

I hope to move to a web server and web pages soon so that the formatting for the code will be much more readable, but you get the basic idea.

Happy coding!

Thursday, April 5, 2007

Phobos Appeal And Killer Apps

I haven't had time to work with Phobos this week, but I am hoping to design an Ajax layout for a simple forum soon.
In my mind's wandering this week I was thinking about how Phobos could appeal to and be adopted on a large scale. A few things came to mind. It must appeal to the average scripter by providing easy data access to open source databases. It must be supported by web hosts as the average scripter is not using a dedicated server. It also must be marketed as a scripting platform that has ease of use as well as stability and speed.
At this point, I think if a developer takes the time to explore Phobos, they will see that it is a very conducive scripting platform. You can essentially plug in any scripting language and modify the platform to your needs. The database access is easy, but it does require the NetBeans IDE to avoid writing Java code.
Support by web hosts should be easy. Java web hosting is much more common than it was five years ago and is just as cheap as any other hosting. A web host could even market a solution as a javascript server platform. They could bundle in a few specific Java objects and deploy a basic Phobos web app. The customer could script their solution right inside Phobos without ever needing to deploy a web app or know what it is. This should also cure the reasoning of some that Java in a shared environment is not as stable as PHP. As long as Phobos doesn't hiccup, errors in code shouldn't matter.
The killer app for Phobos as a platform would be a Visual IDE. The JMaki project is really exciting from the fact that you can create your own components using simple text tools such as html, css and then of course javascript. This combined with the simplicity of Phobos is very powerful. The killer app would be if Netbeans had a visual layout screen similar to the NetBeans Visual WebPack or Visual Studio, but instead of custom controls written for an IDE, you could use Jmaki controls. Then the average web developer could create their own components for visual layout as well as script their pages.
After this was done the next killer app would be to be able to wrap Phobos up into a single window application with the embedded browser engine. Like the Gecko engine from FireFox. You would be able to write a desktop application with a scripting language using html widgets and of course it would be Internet enabled as it could make calls to servers or databases. You would also have a built in database engine for desktop apps using Derby. Now with one stack, you can write desktop apps, web apps, or Ajax apps with a scripting language, and these apps would be cross platform.
Phobos presents lots of possibilities.