Sunday Feb 17, 2008

So how can you tell what server is being used to run a website? Here is a simple Java program that can help one figure out the kind of server that serves a particular website.

package whataretheyrunning;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author octav
 */
public class SnoopURL {

    String URLString = null;

    public SnoopURL(String URLString) {
        this.URLString = URLString;
    }

    public void snoop() {
        try {
            // build URL
            URL url = new URL(URLString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET";);
            conn.setDoOutput(true);

            // start talking
            conn.connect();

            // int length = conn.getContentLength();
            // System.out.println(" **** the length of the content is " + length + " for URL " + URLString);

            System.out.println(" **** Server type is:  " + conn.getHeaderField("Server";) + "\n\n";);

            // get more information
            Map map = conn.getHeaderFields();
            for (int i = 0; i < map.size(); i++) {
                System.out.println(" **** " + conn.getHeaderField(i));
            }

            // cleanup
            conn.disconnect();
        } catch (MalformedURLException ex) {
            Logger.getLogger(SnoopURL.class.getName()).log(Level.SEVERE, null, ex);
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace();
        } finally {
            // do whatever cleanup ...
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

The output of this program is something like this:

**** Server type is: Sun-Java-System-Web-Server/6.1

**** HTTP/1.1 200 OK
**** Sun-Java-System-Web-Server/6.1
**** Mon, 18 Feb 2008 04:46:48 GMT
**** text/html;charset=UTF-8
**** policyref="http://www.sun.com/p3p/Sun_P3P_Policy.xml", CP="CAO DSP COR CUR ADMa DEVa TAIa PSAa PSDa CONi TELi OUR SAMi PUBi IND PHY ONL PUR COM NAV INT DEM CNT STA POL PRE GOV"
**** Servlet/2.4,JSP/2.0
**** chunked
**** Starload=star-fep5; Path=/
 

Now let's look at some popular websites.

Sun's www.sun.com is using the old Web Enterprise server, version 6.1 (the same is true for java.sun.com). The site the publishes this blog: blogs.sun.com is powered by Sun-Java-System-Web-Server/7.0 (a newer version of the same web server). Google seems to use their own version: gws. Yahoo does not tell you (returns null). Microsoft eats their own dog food: Microsoft-IIS/7.0. Nice. IBM runs something called: IBM_HTTP_Server (I bet is Apache's web server).



 

Tuesday Dec 18, 2007

One of the thoughts that I had the other day was related to design patterns in JavaFX Script. Patterns are quite popular with developers. The are books on this topic and lots of web resources. The question is how could write, for instance a Singleton, in JavaFX Script.
 
In Java it would look like this:

public class OnlyOne {

    private static OnlyOne singleton = null;

    private OnlyOne() {

    }

    public static synchronized OnlyOne getInstance() {
        if (singleton == null) {
            singleton = new OnlyOne();
        }

        return singleton;
    }
}

/**
* Test program to prove we've coded the singleton correctly
*/
public class Driver {

    public static void main(String[] args) {
        // one would try this OnlyOne one = new OnlyOne();
        // and it won't work due to the private constructor
       
        // get an instance
        OnlyOne one = OnlyOne.getInstance();
        System.out.println("Myfirst attempt: " + one.toString());
       
        // get another instance to see if they are the same
        OnlyOne two = OnlyOne.getInstance();
        System.out.println("My second attempt: " + two.toString());
    }
}

The result of running the Driver program is: 

Myfirst attempt: onlyone.OnlyOne@9b49e6
My second attempt: onlyone.OnlyOne@9b49e6

As expected, any time one is trying to get a handle to the OnlyOne object, the same instance is returned.

Wednesday Nov 14, 2007

The old proverb: "a picture is worth a thousand words", has an interesting mapping in the world of programming. On the Java platform one has a few facilities that enable a developer to get a stack trace that can point to the cause of a UI freeze, slow down in an application performance, etc. A thread dump can save a developer a lot of time in troubleshooting tough problems. In the old days one could start the application and a terminal and call CTRL + Break on the Windows platform or kill -QUIT pid on the Unix platform. Of course if one has access to the sources you can force the application to show you the call stack by doing something "un-natural" such as Thread.getCurrentThread().dumpStack(). Keep in mind that this will crash you application, so don't do it on production code. This wiki entry has more details on how to, gently :-), get a Thread dump.

In a recent conversation with Tor I learned some more details. For instance, on the Windows platform, using Java 6 one could employ the jps -ln command to get the process id of Java application and then use the jstack command on that process id (jstack pid) to get a thread dump. Nice documentation can be found here.

If you really in the mood to experiment, take a look at the Visual VM project that was just launched on java.net. You'll find a cool tool, based on the NetBeans platform, that helps monitor and profile Java applications. It only runs on top on Java 6, however it can work on applications based on JDK 1.4.2 or later.
 

Tuesday Aug 14, 2007

The answer is simple. Just subscribe to a blog's RSS (Really Simple Syndication, basically a computer-readable summary of the content of a web page) feed. Here is how and what should be on your radar:

There are several ways to subscribe and read feeds. I found this article quite useful. On the other hand I use Apple mail to read the RSS feeds.

Thursday May 31, 2007

Everyone (especially in the US) loves a winner and at every Java One you have one product, technology or event that makes the biggest splash. 2007 is shaping up as the year of Java FX. In a nutshell: "JavaFX comprises a comprehensive set of runtime environments, widgets, development tools, and scripting environments At the 2007 JavaOne Conference, Sun introduced two products in the JavaFX family: JavaFX Script and JavaFX Mobile." You owe to yourself to kick the tires on Java FX Script, so here are some pointers to some resources on the web.

As far as I am concerned, this year's conference was one of the better ones, that I've attended (since '96). I have not been so excited since 2003 when we previewed Java Studio Creator. One of the cool things about the conference is the social networking aspect. You end up having a beer with entrepreneurs that will become the next Jeff Bezos, renowned technologists and lot's of other folks with who you collaborate during the year, but they always at the end of the phone line or just a name in the CC list.
 

Thursday May 03, 2007

The NetBeans platform and IDE has evolved tremendously in the last couple of years. Version 6 is around the corner and the Sun will be releasing a preview at NetBeans Day / Java One. So what's on tap? Support for native and dynamic languages (Ruby, JavaScript), Java Studio Creator's ease of use available for desktop applications, integrated profiler, rapid visual web application development, UML, REST W/S, lots of productivity features, etc. The best way to get more info is by attending NetBeans Day (details below).

May 7th, Moscone 

NetBeans Day

Highlights

  • NetBeans Day Opening Session: Jonathan Schwartz and Rich Green
  • Lunch with the Java Posse
  • NetBeans 6.0 IDE
  • Partner Showcase
    • CollabNet, YASU Technologies
  • Swing GUI Building With Matisse: Chapter II
  • NetBeansMobility
  • JRuby: Understanding the Fuss
  • Partner Showcase
    • Intland, USDA, Xoetrope
  • Closing Session: James Gosling

The first 400 attendees will receive a free copy of Rich Client Programming: Plugging into the NetBeans Platform. We will also be giving out 1 GB USB flash drives, loaded with the latest NetBeans 6.0 Milestone. Throughout the day, we will be giving out special edition NB 6.0 T-shirts and NetBeans cubes. Pay attention, you may have to earn your T-shirt by asking or answering a question.

Friday Apr 20, 2007

The good news:

  • Java 6 is now available on Ubuntu 7.04 (Feisty Fawn) from the multiverse repositories
  • A couple of simple commands will install Java 6 on your system:
    1. Get and install Java: sudo apt-get install sun-java6-jdk
    2. Make Java 6 default: sudo update-alternatives --config java
    3. Edit sudo vi /etc/jvm the first entry should point to your JDK /usr/lib/jvm/java-6-sun
    4. Optionally you can install the jre and firefox plugin: sudo aptitude install sun-java6-jre sun-java6-plugin

Of course, you'll be better off following the documentation available on the Ubuntu website.

The not so good news:

  • Java 6 is still not default on Ubuntu 7.04 (after installing the OS you get 1.4.2)
  • You can install Java 6 from the Applications | Add/Remove, however you have to fish for it and "Show" All Available Applications when you search for it

In addition to Java, Glassfish v1 and NetBeans 5.5 are also available from the multiverse repositories. IMHO this software stack makes Ubuntu very competitive vis-a-vis RedHat / JBoss. I'll blog some other time on this topic. 

Monday Apr 09, 2007

Here is an implementation of the popular "Lines" Russian game written by Alexei Mokeev in F3. Business logic and cool graphics in less than 350 lines of code. Take a look at the snippet below to see how one leverages Java the language, and of course this runs on the JVM.

 

import f3.ui.*;
import f3.ui.canvas.*;
import java.lang.System;
import java.lang.Math;

 ...

operation Lines.moveBallTo (to:LinesCell) {
    var path:LinesCell* = [to];
    while(to.marker <> 0) {
       to = getMinimumAround(to);
       insert to as first into path;
    } //OK. We have Path

    activeCell =-1;
    var frm:LinesCell = path[0];
    to = path[sizeof path -1];

    var l:Integer = sizeof path;

    if (l == 2) {
        to.ballColor = frm.ballColor;
        frm.busy = false;
        to.busy = true;
        changed = true;
        dropBalls();
        return;
    }

    // Clearing FROM
    to.ballColor = frm.ballColor;
    frm.busy = false;

    for (cell in path[indexof . <l-1]) (dur (l-2)*100 linear) {
        cell.marker = -10; //Field in movement
    }

    for (cell in path[indexof . < l]) (dur (l-2)*200 linear) {
        cell.marker = -1; //Stop markup
        if ((cell.x == to.x) and (cell.y == to.y)) { //Ok. We at the end
            to.busy = true;
                   
            for (c in fld[n|n.marker == -10]) { c.marker = -1;}
            changed = true;
            dropBalls();
        }
    }

}
 

Take a look at the screen shot of the game running on my Mac.

 

Friday Feb 16, 2007

Not long ago I went to Monrovia (East of LA) to meet the Sun folks working on SOA toolability and runtimes. I gave a demo of Java Studio Creator and also learned about their work on developing a new technology. Like any presentation it's the demo that makes or breaks the deal and I was blown away by what Chris Oliver has shown that day.

The technology is called F3 - "form follows function" (the name will likely change), a declarative Java scripting language, which among other features, enables easy binding of UI element to data. Here is a link to his blog and a demo of F3 in action. F3 takes advantage of Java 2D and Swing and enables developers to rapidly develop "icandi" applications. This technology does a better job than anything else I've seen in exposing the power of Java for developing GUI applications.

There is a lot of interest around this project and perhaps if it becomes open source soon, the community will help the technology evolve to a point where is has critical mass and wide industry adoption. Having support for something like F3 in an IDE, such as NetBeans would also help. I suspect that this will happen shortly :-) Stay tuned.
 

Sunday Nov 05, 2006

I did not come up with that, it was Leonardo Da Vinci, however we've been trying to apply just that in the tools organization here at Sun. The voice of the customer was clear: they did not like changing tools in process of building Java and native applications using Sun's tools.

A good friend put this is in a nustshell: "The answer is NetBeans", what is your question? We just released NetBeans 5.5 and a series of Packs that complement the functionality already part of the base product. Most of the functionality of Java Studio Creator can now be found in something called the NetBeans Visual Web Pack, enterprise tools, formerly found in Sun Java Studio Enterprise are now bundled in the NetBeans Enterprise Pack. Moreover native development is now possible in Netbeans via the C/C++ Pack (this is wild since it also works on my PowerBook G4 Mac using the native compiler).     

People that know me could tell you that I am a little skeptical so I tried to install myself the whole tools stack and I wanted to see if I can do some real application development using all this functionality, now available in one tool.

Project scope:

  1. Develop a simple Web Service
  2. Rapidly create a web application visually using the packaged JSF components and consume the Web Service developed in step one
  3. Reverse engineer the web application (UML is now available on the NetBeans beta update center) so I can learn (or remind myself :-)) about the framework that Java Studio Creator uses to manage the application life cycle

Let's get to work.

Creating a Web Service (W/S) is part of the basic NetBeans functionality and can be done a couple of ways. I chose to create a web application project to which I could add a W/S. If you are interested in the details take a look at this tutorial (which documents W/S creation for NetBeans 5.0, but is relevant for the 5.5 release). The IDE creates the W/S (class & deployment descriptors, etc) and all you have to do is add one or more interfaces. You can deploy and test the W/S within a minutes. Pretty sweet. The only complain is that I wish I had a W/S project type rather than having to create a generic web project.

Next step involved, creating a web application that can consume my W/S. I chose a Visual Web Application project which, to my surprise was just as simple as using Java Studio Creator. You have all these (JSF extensions) widgets in the palette to choose from and you can just drag and drop them onto the page / form. I used just a couple of components: a button and a static text. The idea was to call the W/S in the action handler of the button and set the text of the static text component to whatever the W/S returns. The only complication was that I had create the W/S client within the project. In Creator all I had to do is locate a W/S via its WSDL and from that point on I could just drag and drop it on the form or a component. In this iteration of the Visual Web Pack you have to manually (right mouse click on the "Web Pages" node in the project and select "New | Web Service Client") create the W/S client. Once the client is created you can expand the tree "Web Service References" node and look for the method / interface and drag and drop it in the page (the Java view of the form / page). This action will insert a code clip:


        try { // Call Web Service Operation
            wsconsumption.OTWebServiceService service = new wsconsumption.OTWebServiceService();
            wsconsumption.OTWebService port = service.getOTWebServicePort();
            // TODO process result here
            java.lang.String result = port.getName();
            System.out.println("Result = "+result);
           

            // this is my code that uses the W/S to set the text
            staticText1.setText(port.getName());
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

My last step involved creating a UML reverse engineering project which has as target the visual web application project. This is a very simple operation, just follow the wizards. In the project, expand the model node, look for the relevant Java package, expand it and select it together with all its files, and use the context menu to generate a diagram (usually class is the most relevant). Now I can look at the various artifacts of the visual web project, understand the dependencies, etc.

In case I have not said this already, all of this is possible using NetBeans 5.5 and its supporting cast of Packs. 

Tuesday Sep 05, 2006

I've been in my new job for three month. The "bits" and I growing farther apart. In an attempt to re-establish myself as a coder I decided to write a simple Netbeans plugin. Here are my findings. [Read More]

This blog copyright 2008 by octav