|
|
|||||||||||||||||||||||||||||||||||||||||||||
Jim Driscoll's Blog
Mojarra's JSF 2.0 EDR1 shipsPosted by driscoll on June 26, 2008 at 02:05 PM | Permalink | Comments (0)The Mojarra Project is proud to announce the release of the JSF 2.0 EDR1 implementation. "EDR" stands for Early Draft Review, so this is a very early snapshot of what we're doing with the new version of JSF. It should be of interest to anyone who'd like to see where JSF is headed in the months ahead. You can download the new API, along with the implementation, from the Mojarra project download area. For the folks using the Glassfish Application Server, the new API will be available via the Glassfish Update Center later today... but remember that this is very early code, so don't update your production machines just yet. Release notes for the EDR1 release of Mojarra are also available. The actual JSF 2.0 EDR1 specification is available from the JCP site. For those who'd prefer a more example-laden description of some of the new JSF 2.0 features, please check out Ryan Lubke's blog - he's been blogging about the new 2.0 features since February - skip back a bit in his blog to Part 1, and start reading from there.
As always, we'd love to hear your feedback! The whole purpose of this release is to get feedback both on the API, as well as on the implementation of it, so feel free to leave comments either here, on our forum, or on the mailing lists - or you could even just file a bug, either on the implementation or the spec.
Writing a (bit complex) Glassfish Update ModulePosted by driscoll on June 24, 2008 at 12:55 PM | Permalink | Comments (0)There have already been a few blogs on creating Glassfish addon modules and update center modules. In particular, the one by Manveen and Alexis are pretty good, and clear. Please read them first, if you're looking to create an update center module. That said, they both only cover a small amount of the information you need. I was able to put together the rest of the information required by looking at an existing example for Roller, as well as reading some of the API docs.
First, let's go over the actual structure of the program - there's really three different parts: The addon module (which has two parts, the installer and the configurator), and the update center module itself. The addon module is the way to actually modify Glassfish, while the update center module is just a delivery mechanism for the addon module. In fact, you can run the addon module without the update center by issuing the command The addon module is divided into two parts - the installer, and the configurator. The installer runs when the addon is first run, and it knows about the location of the install directory of the App Server. This is the process that you'd use to actually move all the files around to their proper places. The configurator runs when you next restart the App Server, and it knows about the location of the server instance that it's started under. This is the process that you would use to modify the server's configuration, located in the domain.xml file. So, with all that in mind, take a look at the code that I wrote, and follow along: First, we'll write the installer. This class implements com.sun.appserv.addons.Installer, and there are two methods that matter to us right now - install and uninstall. To install, we're going to take two files (jsf-api.jar, and jsf-impl.jar) and put them into the lib directory of Glassfish, backing up the existing jsf-impl.jar. Uninstall will reverse the process. First, we need to find destination directory - we do this by using the InstallationContext we're passed as part of the method call:
File appserverDir = ic.getInstallationDirectory();
String appServerInstallRoot = appserverDir.getAbsolutePath();
String appServerLibDir = appServerInstallRoot + File.separator + "lib";
And we also need to find the source for these files - which is the jar that we created for the install.
String addOnsDir = appServerInstallRoot + File.separator + "addons";
String installerJar =
addOnsDir + File.separator + INSTALLER_JAR;
Where INSTALLER_JAR is the name of the installer file.
Now, the rest of the program is just standard unjar, copy, and move commands in Java, except for one last thing: we also need to place the configurator jar someplace where the starting App Server instance will find it upon next startup.
String addOnsLibDir = appServerInstallRoot + File.separator+ "lib" + File.separator + "addons";
....
File confJarFrom = new File(tmpWorkDir + File.separator + CONFIGURATOR_JAR);
File confJarTo = new File(addOnsLibDir + File.separator + CONFIGURATOR_JAR);
copyFile(confJarFrom, confJarTo);
Where, once again, CONFIGURATOR_JAR is the name of the jar file that holds the configurator information.
Now we'll talk about the configurator. Don't worry, we'll discuss packaging next. The configurator implements com.sun.appserv.addons.Configurator, and once again, there are two methods we need to be concerned about: configure and unconfigure. Our configure method does only one thing: it loads the domain.xml configuration file into a DOM tree, modifies the part that we need to change, and then writes it back out. Unconfigure will reverse this process. First, we need to find the domain.xml file for the running instance:
String domainRoot = cc.getDomainDirectory().getAbsolutePath();
String domainFilePath = domainRoot
+ File.separator + "config"
+ File.separator + "domain.xml";
File domain = new File(domainFilePath);
Then we simply modify the xml and rewrite it in a fairly standard manner. Note that since we're going to be changing the classpath of the server, a addtional restart is going to be required, for a total of two restarts (one to run the configurator, and one more to change the classpath.)
Now, let's talk packaging, because that's the entirety of what's left to do. Manveen's blog covers this, but it's worth going over again. The configurator jar will have two parts - a file, called services/com.sun.appserv.addons.Configurator which contains the name of the file implementing Configurator as it's content, and the class files, including the named file. Also, the configurator jar file should be named something like addonname_configurator_versionstring.jar by convention. The installer jar is similar in structure - a file, named services/com.sun.appserv.addons.Installer containing the name of the file implementing the Installer interface. The installer jar will also have the files to install, such as jsf-impl.jar in our case, as well as the configurator jar file mentioned above. This installer jar is the file that you can use with the asadmin install-addon command. Lastly, there's the Update Center module jar. The format of this jar is based on the Netbeans module format. Pretty simple stuff - there's two top level directories - info and module. Under module, we have the installer jar file. Under info, we place a small info.xml file, which is basically just a stub. Ours looks like this:
<module codenamebase="jsf2.0">
<description>JavaServer Faces 2.0 EDR1 Release</description>
</module>
Lastly, if you want to put this into the official Glassfish Update Center, you'll need an additional xml file - for information on this, check out Manveen's Update Center Testing blog. So, that's it - how to write an update center module that installs a new library, and adds it to the classpath of the app server in domain.xml. Hope this helps you. UPDATE: There's a little confusion about how uninstalling works, so it's probably worth a quick note: GF v2 doesn't support a programmatic unconfigure (or for that matter, uninstall) via the updatetool - when you want to uninstall an installed module, you need to first manually edit a configuration file called domain-registry in your domain's config directory (the same place as the domain.xml file). You'll find two properties there - change them to "false". My file looks like this: #Wed Jun 25 13:17:28 PDT 2008 jsf2.0_configurator_01_00_00.configured=true jsf2.0_configurator_01_00_00.enabled=trueOnce you've changed these files, bounce the server. That will run the Configurator's unconfigure method of the configurator class. Then, uninstall the package either via the updatetool or via asadmin uninstall-addon addonname - that will then run the uninstall method of the installer class.
Comet TicTacToePosted by driscoll on May 07, 2008 at 06:45 PM | Permalink | Comments (0)Here's the Comet TicTacToe that I went over in my BOF on Comet on Wednesday night. It's pretty simple (though not as simple as my first example, or even the somewhat improved version) - just 200 lines of Java code (including the game logic), 50 lines of JavaScript (embedded in an HTML page), 50 lines of HTML, and a 75 line CSS file. Simple stuff, but if you're looking to write your own Comet app, this might help get you started. You can find the full Netbeans project here.
Essentially, the program logic is contained in only two files: ttt1.html, and CometServlet.java. Check 'em out. After my initial example two posts ago, these should be pretty self explanatory. If you have any questions, by all means ask in the comments below.
Solving the Comet timeout problemPosted by driscoll on May 05, 2008 at 02:07 PM | Permalink | Comments (0)In my previous blog, I mentioned that I didn't like the hack of reloading the iframe via the post action - it's hacky, and it's not hard to imagine it messing things up in a more complex program. Turns out the answer is both easy and blindingly obvious once you think of it: the iframe onload event. And while we're add it, we'll add a onerror event too. In my previous program, I had had a hidden iframe, and on every update, I would reset the source for the iframe using the location property. We'll still do that, but add a new function:
<iframe name="hidden" src="CometCount" frameborder="0" height="0" width="100%"
onload="restartPoll()" onerror="restartPoll()" ></iframe>
Note the onload and onerror events - whenever the server closes the connection, these will be called. And here's the function that's called:
var retries = 0;
function restartPoll() {
if (retries++ > 10) {
alert("The connection has errored out too many times");
} else {
hidden.location = url;
}
}
Also, I've added a retry limit in there - it wouldn't do to have the client go into a fatal spin just because the server is down. Now when the server closes the connection (from a timeout, or an error), the client will continue to function, automatically calling back into the server. Not a solution you'll want for every situation, but useful enough, especially for our small example. Lastly, there was a bug in my previous version under IE - sorry about that. It turns out that if you send a POST via IE, you need to have a content body, or IE gets fussy. The fix is to change the line
xhReq.send(null);
to
xhReq.send("null");
I've uploaded new versions of the files index.html and CometCount.java, so you can see the complete code in context.
Dead Simple Comet Example on Glassfish v3 / GrizzlyPosted by driscoll on May 01, 2008 at 03:02 PM | Permalink | Comments (1)I was looking at a recent blog by Shing Wai Chan and going through the Comet example, when I noticed that the example wasn't working correctly. Although he updated his example to get around that problem, I was still a bit unsatisfied, and decided to sit down, using his basic example, and see if I could make it even simpler. I've whittled it down to about 100 lines, and only 2 files, and I thought I'd go over it here. The full example (both files) are index.html and CometCount.java. So this will be a little long, but if you're tired of reading my rambling, just look at the files, and the code should speak for itself. First, about the app: It's a simple counter, which is updated every time you hit a button on the page. Pretty basic, except - every other web browser viewing that page will have the counter updated as well, through the magic of Comet. About setting it up: make sure that the url mapping points to /CometCount, the value is hardcoded in a few places. Also, to compile you'll need access to the Grizzly Comet APIs - you can either get them from Grizzly, or Glassfish v3 tp2. You'll need to also add the jar in the modules directory named grizzly-optionals to your classpath in order to build, along with the standard Servlet API. You'll also need to update the domain.xml of the v3 instance to add the property cometSupport=true, as you see below:
<http-listener acceptor-threads="1" address="0.0.0.0"
blocking-enabled="false" default-virtual-server="server"
enabled="true" family="inet" id="http-listener-1" port="8080"
security-enabled="false" server-name="" xpowered-by="true">
<property name="cometSupport" value="true"/>
</http-listener>
Now on to the description of the program flow. On startup, the servet is initialized, and registers itself with with the Comet Engine (make sure the servlet is installed with a url CometCount, or it won't work). We set a timeout of 2 minutes.
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = config.getServletContext();
contextPath = context.getContextPath() + "/MyComet";
CometEngine engine = CometEngine.getEngine();
CometContext cometContext = engine.register(contextPath);
cometContext.setExpirationDelay(120 * 1000);
}
Then on the first load of index.html in the browser, the hidden iframe makes a call to the doGet of the servlet - this call suspends, awaiting further action. It does this by attaching the response to a handler (of type CounterHandler), and attaching the handler to the servlet's CometContext. This also gives rise to the first bug of the program - there's no display of initial result.
So this:
<iframe name="hidden" src="CometCount" frameborder="0" height="0" width="100%"></iframe>
calls this:
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
CounterHandler handler = new CounterHandler();
handler.attach(res);
CometEngine engine = CometEngine.getEngine();
CometContext context = engine.getCometContext(contextPath);
context.addCometHandler(handler);
}
Next, someone, somewhere, who's also using the program, hits the button marked "click". This calls the onclick method, postMe(). postMe sends an empty POST to the servlet, triggering the doPost method.
So this:
<input type="button" onclick="postMe();" value="Click">
Calls this:
var url = "CometCount";
function postMe() {
function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("Sorry, you're not running a supported browser - XMLHttpRequest not supported");
return null;
};
var xhReq = new createXMLHttpRequest();
xhReq.open("POST", url, false);
xhReq.send(null);
hidden.location = url;
};
The doPost method increments the counter, and then sends a notify event to the servlet's CometHandler.
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
counter.incrementAndGet();
CometEngine engine = CometEngine.getEngine();
CometContext> context = engine.getCometContext(contextPath);
context.notify(null);
}
This event now wakes up that initial GET request, and sends a bit of javascript down the line. Lastly, we call resumeCometHandler, which marks the current event as completed, removing it from the active queue.
public void onEvent(CometEvent event) throws IOException {
if (CometEvent.NOTIFY == event.getType()) {
int count = counter.get();
PrintWriter writer = response.getWriter();
writer.write("<script type='text/javascript'>parent.updateCount('" + count + "')</script>\n");
writer.flush();
event.getCometContext().resumeCometHandler(this);
}
}
Back at the client, that javascript that got sent down gets put into the hidden iFrame, and then executed. This calls the updateCount function, which updates the counter with the new value. Then, we set the iframes' location object, which reconnects with GET to the servlet, and we're ready for our next request.
function updateCount(c) {
document.getElementById('count').innerHTML = c;
hidden.location = url;
}
That is, unless we time out. If we time out (remember, we set the timeout to 2 minutes, not "infinite"), the iframe goes dead, the GET polling loop is broken, and we never again update the counter on the webpage, though the user's increasing frustrated button pushing on the client will happily update counter on the server. So, to get around this, we add a single line at the end of our postMe function, updating the hidden iframe's location again. This is the one really hacky part of the program, and I'd love to know a better way to do it. Also, it gives rise to the second bug of our program, related to the first - if the connection dies, you need to click the button twice to see an update of the counter on the client. So - ta da! We have a bare bones, long polling comet application in two files and about 100 lines. Again, here's the programs Download file index.html Download file CometCount.java
Jeanfrancois Arcand and I have a BOF on Wednesday night (May 7th, 2008) at JavaOne. If you're at JavaOne and are just getting started with Comet, come on by.
|
June 2008
Search this blog:CategoriesCommunity: GlassfishCommunity: Java Enterprise J2EE JavaOne Linux Open Source Web Applications Archives
June 2008 Recent EntriesWriting a (bit complex) Glassfish Update Module | ||||||||||||||||||||||||||||||||||||||||||||
|
|