Author: janb

  • i-jetty 3.1 Released

    Release 3.1 of i-jetty for Android is now available from the Android Market and the i-jetty download page.
    This release updates the embedded Jetty to jetty-7.6.0.RC4, although the majority of the changes have been to the Console, which is a webapp that allows you to interact with your Android device from a remote browser.
    Higlights include:

    • pagination of large data sets such as Contacts and Media thumbnails (images, videos)
    • re-implementation of generated content as json & ajax REST
    • ability to cause the device to ring (helpful for finding it around the house!)
    • ability to show current location of the Android device on Google maps , or track its location on the map as the device moves.

    Here’s a screenshot showing tracking my phone as it moves from the Sydney Opera House to Fort Denison on Sydney Harbour:

    Tracking phone via i-jetty console webapp
    Tracking phone via i-jetty console webapp

    Enjoy.

  • mvn jetty:run-forked

    Being able to run the jetty maven plugin on your webapp – but in a freshly forked jvm – is a feature that has been requested for a loooong time. With jetty-7.5.2 release, this feature has been implemented, and it even works on your unassembled webapp.

    How to Run


    mvn jetty:run-forked

    That will kick off a Jetty instance in a brand new jvm and deploy your unassemabled webapp to it. The forked Jetty will keep on running until either:

    • you execute a mvn jetty:stop (in another terminal window)
    • you <cntrl-c> the plugin

    The plugin will keep on executing until either:

    • you stop it with a <cntrl-c>
    • the forked jvm terminates

    NOTE: I’m interested in obtaining feedback about the lifecycles of the plugin and the forked Jetty. Is the lifecycle linkage that I’ve implemented the way you want to use it? Do you want the forked jvm to continue on, even if the plugin exits? Please post your input to the Jetty list at jetty-users@eclipse.org.

    How to Configure

    You need a few different configuration parameters from the usual jetty:run ones. Let’s look at an example:

         <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>7.5.2.v20111006</version>
            <configuration>
              <stopPort>8087</stopPort>
              <stopKey>foo</stopKey>
              <jettyXml>src/main/config/jetty.xml</jetty.xml>
              <contextXml>src/main/config/context.xml</jetty.xml>
              <contextPath>/foo</contextPath>
              <tmpDirectory>${project.build.directory}/tmp</tmpDirectory>
              <jvmArgs>-verbose:gc -Xmx80m</jvmArgs>
            </configuration>
          </plugin>
    

    You need to specify the stopKey and stopPort so that you can control the forked Jetty using the handy maven goal mvn jetty:stop.
    You can use the jettyXml parameter to specify a comma separated list of jetty xml configuration files that you can use to configure the container. There’s nothing special about these config files, they’re just normal jetty configuration files. You can also use this parameter with the jetty:run goal too.
    The contextXml parameter specifies the location of a webapp context xml configuration file. Again, this is a normal jetty context xml configuration file. You can also use this with the jetty:run goal too, either in conjunction with, or instead of, the <webAppConfig> parameter (which configures the webapp right there in the pom). As the jetty:run-forked goal does NOT support the <webAppConfig> element, you MUST use contextXml if you need to configure the webapp.
    The contextPath parameter specifies the context path at which to deploy the webapp. You can use this as a simple shortcut instead of the contextXml parameter if you have no other configuration that you need to do for the webapp. Or, you can specify both this AND the contextXml parameter, in which case the contextPath takes precedence over the context path inside the context xml file.
    tmpDirectory is the location of a temporary working directory for the webapp. You can configure it either here, or in a contextXml file. If specified in both places, the tmpDirectory takes precedence.
    With the jvmArgs parameter, you can specify an arbitrary list of args that will be passed as-is to the newly forked jvm.
    There’s also the same parameters as the mvn jetty:run goal:

    • skip – if true the execution of the plugin is skipped
    • useTestScope – if true, jars of <scope>test</scope> and the test classes are placed on the webapp’s classpath inside the forked jvm
    • useProvidedScope – if true, jars of <scope>provided</scope> are placed on the container’s classpath inside the forked jvm
    • classesDirectory – the location of the classes for the webapp
    • testClassesDirectory – the location of the test classes
    • webAppSourceDirectory – the location of the static resources for the webapp

    Also, just like the mvn jetty:run case, if you have dependencies that are <type>war</type> , then their resources will be overlaid onto the webapp when it is deployed in the new jvm.

  • GWT and JNDI

    Many folks want to use some features beyond the bare servlet basics with GWT, such as JNDI lookups. It’s not hard to set up, but there are a couple of steps to it so here’s a detailed guide.
    Since GWT switched to using Jetty for its hosted mode (also known as development mode) back at GWT 1.6, lots of people have been asking how to use features such as JNDI lookups in their webapps.  Several people have posted helpful instructions, perhaps the best of them being from Nicolas Wetzel in this thread on Google Groups, and from Henning on his blog (in German).
    In this blog post, we’ll put all these instructions together in the one place, and give you a couple of projects you can download to get you started faster. You might want to skip down to the downloadable projects.

    Customizing the GWT Launcher

    The first step is to customize the JettyLauncher provided by GWT.  Unfortunately, at the time of writing (GWT2.3.0) you cannot customize by extension due to the use of final inner classes and private constructors. Therefore, you will need to copy and paste the entire class in order to make the necessary and trivial modifications to enable JNDI.
    You can find the source of the JettyLauncher.java class inside the gwt-dev.jar in your local installation of the GWT SDK.  Here’s a link to the jar from Maven Central Repository for convenience: gwt-dev-2.3.0.jar.  Unjar it, and copy the com/google/gwt/dev/shell/jetty/JettyLauncher.java class to a new location and name.
    Edit your new class and paste in this declaration:

    public static final String[] DEFAULT_CONFIG_CLASSES =
    {
        "org.mortbay.jetty.webapp.WebInfConfiguration",    //init webapp structure
        "org.mortbay.jetty.plus.webapp.EnvConfiguration",  //process jetty-env
        "org.mortbay.jetty.plus.webapp.Configuration",     //process web.xml
        "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",//process jetty-web.xml
    };

    This declaration tells Jetty to setup JNDI for your web app and process the various xml files concerned.  Nearly done now. All you need to do is now apply these Configuration classes to the WebAppContext that represents your web app. Find the line that creates the WebAppContext:

    WebAppContext wac = createWebAppContext(logger, appRootDir);

    Now, add this line straight afterwards:

    wac.setConfigurationClasses(DEFAULT_CONFIG_CLASSES);

    Build your new class and you’re done. To save you some time, here’s a small project with the class modifications already done for you (variants for Ant and Maven):

    Modifying your Web App

    Step 1

    Add the extra jetty jars that implement JDNI lookups to your web app’s WEB-INF/lib directory. Here’s the links to version 6.1.26 of these jars – these have been tested against GWT 2.3.0 and will work, even though GWT is using a much older version of jetty (6.1.11?):

    Step 2

    Now you can create a WEB-INF/jetty-env.xml file to define the resources that you want to link into your web.xml file, and lookup at runtime with JNDI.
    That’s it, you’re good to go with runtime JNDI lookups in GWT hosted mode. Your webapp should also be able to run without modification when deployed into standalone Jetty. If you deploy to a different container (huh?!), then you’ll need to define the JNDI resources appropriately for that container (but you can leave WEB-INF/jetty-env.xml in place and it will be ignored).

    If You’re Not Sure How To Define JNDI Resources For Jetty…

    The Jetty 6 Wiki contains instructions on how to do his, but here’s a short example that defines a MySQL datasource:
    In WEB-INF/jetty-env.xml:

    <New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>jdbc/DSTest</Arg>
        <Arg>
         <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
           <Set name="Url">jdbc:mysql://localhost:3306/databasename</Set>
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
         </New>
        </Arg>
    </New>

    Now link this into your web app with a corresponding entry in your WEB-INF/web.xml:

    <resource-ref>
        <description>My DataSource Reference</description>
        <res-ref-name>jdbc/DSTest</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    Of course, you will also need to copy any jars required by your resources – in this case the MySQL jar – into your WEB-INF/lib.
    You can then lookup the JNDI resource inside your servlet, filter etc:

    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/DSTest");
    

    An Example WebApp

    An example usually helps, so I’ve put together a silly, tiny webapp that does a JNDI lookup. It is based on the standard GWT “Hello World” webapp that is generated by default by the GWT webAppCreator script. This webapp does an RPC call to a servlet to get a message incorporating the name entered by the user. I’ve simply modified the message that is returned to also include an extra sentence obtained by doing a java:com/env lookup.
    Here’s my WEB-INF/jetty-env.xml:

    <Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
      <!-- An example EnvEntry that acts like it was defined in web.xml as an env-entry -->
      <New class="org.mortbay.jetty.plus.naming.EnvEntry">
        <Arg>msg</Arg>
        <Arg type="java.lang.String">A bird in the hand is worth 2 in the bush </Arg>
        <Arg type="boolean">true</Arg>
      </New>
    

    This defines the equivalent of an <env-entry> outside of web.xml. In fact, the boolean argument set to “true” means that it would override the value of an <env-entry> of the same name inside WEB-INF/web.xml. This is actually most useful when used in a Jetty context xml file for the webapp instead of WEB-INF/jetty-env.xml, as it would allow you to define a default value inside WEB-INF/web.xml and then customize for each deployment in the context xml file (which is external to the webapp). For this example, we could have just as well defined the <env-entry> in WEB-INF/web.xml instead, but I wanted to show you a WEB-INF/jetty-env.xml file so you have an example of where to define your resources.
    Here’s the extra code that does the lookup inside of GreetingServletImpl.java:

      private String lookupMessage (String user) {
        try {
            InitialContext ic = new InitialContext();
            String message = (String)ic.lookup("java:comp/env/msg");
            return message +" "+user;
        } catch (Exception e) {
            return e.getMessage();
        }
      }
    

    Running the built project in hosted mode and hitting the url http://127.0.0.1:8888/HelloJNDI.html?gwt.codesvr=127.0.0.1:9997 I see:
    Screen shot of webapp in action.
    Here’s an Ant project for this trivial webapp: HelloJNDI

    1. edit the build.xml file to change the property gwt.sdk to where you have the GWT SDK locally installed.
    2. build and run it in hosted mode with: ant devmode
    3. follow the hosted mode instructions to cut and paste the url into your browser

    Resource Listing

  • Google Just Doesn't Understand Community

    We’ve said it before (Bad Robot!), but after the Android 2.0/Nexus One developments, it really bears repeating: Google either do not understand or do not care about community once their immediate corporate goals have been met.

    In the Bad Robot! blog, Greg commented on the disparity between Google’s talk of Android’s openness and their provision of early candidates of the cupcake release only to selected customers with NDAs. Google needed to get Android into the hands of developers to create the rich library of apps that would make Open Handset Alliance’s handsets attractive to customers and compete with the iPhone. Yet, by holding back on the releases, they left a lot of these developers in limbo, not knowing what the future held and being unable to plan how to allocate scarce resources or whether their work would even have to be thrown away. Bear in mind that most mobile developers are small companies or individuals, and so are greatly effected by this kind of treatment. Why screw with the very people you need for your success?

    Eventually the cupcake release was made public and everyone picked themselves up, dusted off the software and got on with it. I, for one, was hoping that Google had learnt a lesson from the understandably angry reaction on the android developer lists. However, the launch of the Nexus One handset and Android 2.0 shows that Google have not. In fact, they probably weren’t even paying attention the first time around.

    Other commentators have remarked upon the numerous ways in which Google’s release of their own-brand handset screws over their partners in the Open Handset Alliance and others in the industry, but what has been missed from most media commentary is the callous disregard Google have shown (again) for their developer “community” with Android 2.0. The fact is that the 2.0 release was announced on 27th October 2009, but yet it is still not available on the ADP1 development handset, and indeed may never be.

    About a year ago developers were encouraged to pay $US500 for the handset in order to try out their apps on a real phone. In conjunction with HTC, the producer of the handset, Android updates for the ADP1 were made available for web download. Yet, despite repeated pleas from the community such as here and here, no release of 2.0 is forthcoming. Indeed, Google has not even had the courtesy to make an official announcement on the future of the ADP1.

    So here we developers are again – cold shouldered by the corporate giant and yet expected to provide apps to lure customers to buy their phone. Google’s behaviour is also rather contemptuous of customers – some developers are receiving feedback that their widgets don’t work properly under 2.0, and yet are unable to recreate and fix the problem because there is no access to 2.0 on a dev handset.

    So whilst I heartily agree with the rhetoric of Android and the Open Handset Alliance, I like the Android UI, API and linux/java-like platform, and I enjoy developing i-jetty, the realization of the rhetoric falls far, far short of what we should expect of a company like Google. Or …. is it exactly what we should expect of a company like Google?

    This judge awards you nul points, Google.

  • Jetty, JPL and the Mars Rovers

    In what has to be one of the coolest ever uses of Jetty, JPL are using Jetty for missions operations planning on the Mars rovers!!!

    Khawaja Shams from the JPL Operations Planning Software team dropped by the Webtide booth at EclipseCon and just blew us all away with this news. So now whenever I check on the rovers’ progress, I get a warm and fuzzy glow knowing that Jetty is helping this extraordinary mission succeed.

    It seems to me that the longevity and success of the rover project is due to smart engineering based on well designed components – and that’s something that we continually strive for with Jetty. Our recent move to the Eclipse Foundation has already spurred us on to critically examine every jetty module, excise the cruft, streamline a few things and generally enhance our componentization for jetty-7.

    To paraphrase Buzz Lightyear, for Jetty, its “to Mars … and Beyond!!!!”.

  • i-jetty Release 2.0

    Release 2.0 of Jetty for Android (i-jetty) is now available in src and binary form from
    i-jetty downloads.
    This release is the first to also be available (for free) on the Android Marketplace.
    To download from the Marketplace, you’ll need to have an Android handset. You’ll find i-jetty under the Applications category in the Communication subcategory.
    If you’re using the emulator from the SDK, then you should download the i-jetty-2.0-final-signed.apk
    bundle from i-jetty downloads.
    In this release, we support Android sdk 1.1_r1 and jetty-6.1.16. We’ve moved to using a combination of json, javascript and xhr for rendering the Console webapp rather than generating html. We’ve also improved the contacts management part of the Console webapp, allowing you to upload an image for a contact, as well as edit all their phone numbers, addresses etc. A new feature in this release is the ability to view your images and video and listen to the music stored on your phone via your desktop browser. We’ll be adding more functionality to the new media serving feature next release around.
    Speaking of the next release, I’ll be doing some testing of the newly available SDK 1.5 preview to establish whether or not it fixes some of the outstanding android issues that have been bugging i-jetty, like the Bundle resource problem and the dalvik cache problem which have prevented dynamically downloaded webapps from running on a real handset. I hope to have some info on these two issues in the next couple of days.

  • Fujitsu – Android Everywhere ?!

    Interesting development – Fujitsu announced recently that they will be offering a service to deploy Android onto all different types of hardware.

    This potentially opens up interesting opportunities for smart software on a whole range of devices, not just mobile handsets. As Jetty is already running on Android, we could see Jetty on all kinds of devices! Of course, Jetty has already been ported to run under J2ME-based devices in the past, but Android provides a more familiar development environment for users accustomed to J2SE. With Jetty onboard, devices could be dynamically updated with new functionality over HTTP simply by downloading a new war file (well, a war file with classes compiled for the Android dalvik vm of course).

    The Jetty-on-Android project – called i-jetty – already provides this dynamic download feature, along with a couple of webapps as demonstrations.

    Although, to make this really fly, Google would need to make some modifications to their classloading security mechanism to really support servlet-style classloader hierarchies. This feature has been requested many times on the android groups, such as here and here. Lets hope this feature moves up the priority list soon!

  • Release 1.1 of i-jetty for Google Android

    To coincide with the first availability of the Android handsets, we’ve released version 1.1 of i-jetty. This release has a lot of improvements in it and some rather cool new features.
    One of the improvements we’ve made is to add the ability to configure Jetty settings. Currently, you can decide whether or not to use the NIO connectors (the default is not to, as testing shows that there are some issues with the dalvik NIO lib), which port to start i-jetty on, and set the password to protect access to the i-jetty console webapp.
    We’ve also dispensed with the need to setup the sdcard before installing the i-jetty application bundle. Instead, i-jetty will automatically setup the sdcard with it’s directory structure, and also automatically install the console webapp on first startup.
    Speaking of the console webapp, we’ve also managed to squeeze it a bit of development on it so that now you can create, delete and update all your Contacts either from the console or the in-built Android application.
    But I think the coolest new feature is the ability to download and install webapps from remote http servers on the fly. All you need to do is enter the url of the remote webapp and the context path at which you’d like it to be deployed. i-jetty will fetch it, unpack it, make a context xml file for it and configure the context path for it. You can manually edit the context xml file later if you want to add other setup. We’ve made the 2 example webapps that previously you had to build yourself and copy onto the sdcard downloadable from http://code.google.com/p/i-jetty/downloads/list. For now, after downloading a new webapp, you need to stop and restart jetty to get it deployed. That’s because we disabled the hot deployer as we felt that continually running a thread to check for new webapps would eat the battery too much.
    To implement the download feature, we ported the Jetty Http Asynchronous Client onto Android. The Jetty client gives you a nice and extensible way to conduct http conversations with servers. As it’s name suggests, it operates asynchronously, so you get called back when your http response is ready, or you can get intermediate callbacks as various parts of the response come in, such as the headers, or the content bytes etc. It supports proxies, authentication and ssl, although we’ve yet to instrument those for Android.
    I also just wanted to mention in case you missed it that one of the Jetty guys, David Yu, has also been doing some cool work on Android, and has recently ported Jetty’s Bayeux client in java onto the phone as well. In case you didn’t realize it, you can do Bayeux either from javascript using one of the libs from the Dojo Foundation’s Cometd project, or from Java using Jetty. David’s wrapped up the client in a demonstration webapp – the famous chat room example! So, you can design your webapps using the highly convenient Bayeux protocol (which is based on a message passing paradigm, so the apis look pretty familiar) and rest easy in the knowledge that it’s going to be accessible both by browser clients (either desktop or on a mobile device) via javascript, and now also even via Android applications!
    Finally, if you’re looking for info on how to go about Android-izing a webapp, check out the src of i-jetty, or download the src bundle. We’ve build the webapps using Maven and called out to the Android tools at right places in the build cycle to ensure the servlet classes get built for the dalvik compiler into the appropriate zip file and stored in WEB-INF/lib. Eventually it would be nice to tidy this into a plugin.

  • Jetty and Cloud Computing

    Cloud computing is understandably a hot topic. It refers to the ability to deploy your application on infrastructure that you do not necessarily own nor manage yourself and that can be easily scaled up to handle demand and provide resiliency to failure. The hardware infrastructure can be composed of clusters of many cheap machines or it may be high-end hardware which is virtualized into many nodes. In either scenario, the cost of ownership is dramatically less than in the traditional model.
    Additionally, your application executes in an environment where the services it consumes can be transparently provided for you and scale along with the application. Think services like databases, messaging and servlet session clustering for example.
    Software as a service is an increasingly popular paradigm for webapps so let’s look at how Jetty is used in the cloud and some of the types of technologies involved.

    Cloud Platform: Morph

    Lets look briefly at a cloud platform solution for scalable webapps. Morph allows you to upload your war file and have it automatically deployed to as many virtual nodes as you need. Morph handles the load balancing for you and allows you to add or subtract virtual servers elastically as your load dictates. Moreover, your webapp immediately has access to some of the most commonly-needed resources like relational databases, mail servers and soon also a JMS service! These services are provisioned, configured, backed-up and monitored by Morph 24×7. Better still, Morph ensures high-availability of your webapp by configuring a fail-over pair as a matter of course. And at the heart of this great service, what do we find? Yes, that’s right – Jetty! Jetty is the servlet container into which webapps are deployed and has been especially configured for a cloud-hosted environment.

    Cloud Technology: Terracotta

    Terracotta provides a shared memory model. It is mostly unobtrusive in code, generally just requiring good synchronization boundaries around the objects to be shared to enable it to efficiently disseminate updates amongst nodes. You can use Terracotta to implement cloud-type facilities for your webapp when running in Jetty. In fact, Jetty can already make use of Terracotta as its distributed sessions mechanism. We’ve recently been collaborating with the Terracotta guys to really hone the performance of the Jetty/Terracotta session clustering and we’re getting some very pleasing results, which will be the subject of another blog.

    Cloud Infrastructure: Hadoop

    Hadoop is an open-source implementation of the MapReduce algorithm for breaking computational problems into smaller blocks that can be distributed over a cluster so that they may execute in parallel. Hadoop uses Jetty in two ways: to help distribute the jobs amongst the nodes, and also to monitor and report job execution status. FYI, Hadoop recently broke the terabyte sorting record – well done guys!

    Cloud Infrastructure: Gigaspaces

    Gigaspaces provides a space-based infrastructure to scale-out applications. The space’s job is to ensure that data can be made available in the most efficient way possible to whichever node requires it. There are different options for configuring the space including partitioning based on characteristics of the data, or data persistence via an RDBMS. A number of different API facades onto the space (JMS, JDBC, Map and Space) are provided, so you can pick the semantic appropriate to your application. Jetty itself uses the Space API in the implementation of the Jetty/Gigaspaces clustered sessions module1.
    However, the space can be used for more than just distributing data, and can also be used to scale applications themselves, more akin to grid computing. In this scenario, nodes in the space (or grid) called processing units execute application logic and can be added on demand to handle load. Webtide has been collaborating with the Gigaspaces guys and we’ve put a Jetty instance into each and every processing unit. This means that a webapp can be instantly scaled simply by deploying it to more processing units in the grid.


    1.
    Session clustering refers to the ability of more than one node to access the servlet session established between a client and the servlet container. In a non-clustered environment, the servlet session exists only in the memory of the servlet container on the node hosting the container. Thus, if that process or node fails, that user’s session and concomitant data is lost. With clustered sessions, another node can take over for the failed one, accessing the established session and allowing the user to continue using the site. So, in a cloud environment, where the physical nodes hosting your webapp may not be permanently allocated to you and change over time, or when nodes fail and are replaced by others, your site can continue to be available to all your users.

  • i-jetty 1.0 release

    Ta daaaaa! Drum roll please.

    We are pleased to announce that the first release of i-jetty is available. i-jetty is a port of Jetty to the Google Android phone. This release works with the most recent release – SDK 1.0r1 – of the Android platform.

    Go to http://code.google.com/p/i-jetty/ and follow the links to download it now.

    i-jetty not only puts a Jetty webserver on your Android phone, but it also comes with some sample webapps that you can access from the phone AND from your desktop browser – indeed any browser that is on a network accessible to the phone.

    The sample webapps are:

    hello
    A simple webapp with a static page leading to a HelloWorld servlet.
    chat
    A chatroom webapp that uses Cometd to do an Ajax chatroom.
    console
    A webapp that allows you to access and manipulate the on-phone information such as Contacts, Settings, Call logs etc via a browser. No more sync’ing of on-phone data with your pc in order to get that person’s email address any more – email them direct from your desktop browser!

    As you might have read on Greg’s blog, it hasn’t been easy developing for Android due to the restricted access to timely bug fixes, sketchy documentation and of course the lack of access to the source code.

    However, on the positive side, the dalvik vm does support many of the java libraries used by Jetty, and Jetty itself of course has a long history of excellent embeddability on all kinds of devices.

    Oh, and we’re pining for one of those oh-so-cool Android handsets so if someone was so kind as to send one our way, we’d be disgustingly grateful 🙂