IT Bits and Pieces

Deploying Adobe Flex into Google AppEngine

Posted in Adobe Flex, Google AppEngine, Java by jpd6 on April 18, 2009

In the right hands, Adobe Flex is a powerful tool that can create very powerful user interfaces. There are lots of examples that can be found here. However, Flex isn’t perfect and does have its faults in particular with accessibility which can also be said of Java Applets and JavaFX. JSPs and Servlets don’t have the same limitations, but the effort to build a powerful UI in Flex is considerably quicker. I’ve found that Java is great at ‘back-end’ development and not so good at ‘front-end’ whereas Flex is the opposite, very good for ‘front-end’ development. Both technologies complement each other. That led me to think,  AppEngine can host ‘static’ files; this combination should work in AppEngine? The answer is yes!

If you want to try this, I’ve provided some small test code that uses the ‘guestbook’ demo project that comes with AppEngine.

The screenshot below shows what the ‘guestbook’ demo looks like.

And deploying a test Flex application with the Java project, we get the screenshot below.

First, build a new ‘Flex Project’ using the following code. The key configuration within this code is the ‘HTTPService’ that will call a Java servlet which in turn returns the XML of the guestbook postings.

<?xml version="1.0" encoding="UTF-8" ?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:jw="components.*" applicationComplete="guestbook.send();">
<mx:Script>
import mx.rpc.Fault;
import mx.rpc.events.*;
import mx.controls.Alert;

public function faultHandler(event:FaultEvent):void
{  Alert.show(event.fault.faultString, "Error");  }

public function resultHandler(event:ResultEvent):void
{  }
</mx:Script>

<mx:HTTPService id="guestbook" url="/greetings"	method="GET" resultFormat="e4x"
  fault="faultHandler(event);" result="resultHandler(event);" />

<mx:Panel id="panel" title="Guest Book">
  <mx:DataGrid id="dataGrid" dataProvider="{guestbook.lastResult.post}" editable="false">
     <mx:columns>
       <mx:DataGridColumn id="nicknameColumn" width="100" dataField="nickname" headerText="Nickname" />
       <mx:DataGridColumn id="contentColumn" width="200" dataField="content" headerText="Content" />
       <mx:DataGridColumn id="dateColumn" width="100" dataField="date" headerText="Date" />
      </mx:columns>
     </mx:DataGrid>
</mx:Panel>
</mx:Application>

Within the ‘Guest Book’ project, create a new servlet ‘GreetingXMLServlet.java’.

Paste the following code that creates the XML file to be returned to the Flex application. This servlet queries the object datastore for all the guestbook postings.

package guestbook;
import java.util.List;
import javax.jdo.PersistenceManager;
import guestbook.Greeting;
import guestbook.PMF;
import java.io.IOException;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class GreetingsXMLServlet extends javax.servlet.http.HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.setContentType("text/xml");
PersistenceManager pm = PMF.get().getPersistenceManager();
String query = "select from " + Greeting.class.getName() + " order by date desc range 0,5";
List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute();
if (!greetings.isEmpty())
{
resp.getWriter().println("<greetings>");
for (Greeting g : greetings)
{
resp.getWriter().println("
<post>");
if (g.getAuthor() == null)
resp.getWriter().println("<nickname>Anonymous</nickname>");
else
resp.getWriter().println("<nickname>" + g.getAuthor().getNickname() + "</nickname>");
resp.getWriter().println("<content>" + g.getContent() + "</content>");
resp.getWriter().println("<date>" + g.getDate() + "</date>");
resp.getWriter().println("</post>");
}
resp.getWriter().println("</greetings>");
}
pm.close();
}
} 

Paste the following into ‘war/WEB-INF/lib/web.xml’ to configure the servlet.

<servlet>
<servlet-name>greetings</servlet-name>
<servlet-class>guestbook.GreetingsXMLServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>greetings</servlet-name>
<url-pattern>/greetings</url-pattern>
</servlet-mapping>

Finally, import the ‘Flex’ release files into the ‘war’ folder of the ‘Guest Book’. If you have both projects in Eclipse, drag and drop it!

The Files are now within the ‘Guest Book’ project, and deploy into AppEngine!

Google Java AppEngine environment from one line of code

Posted in Cloud, Google AppEngine, Java by jpd6 on April 14, 2009

When working on web projects, I always find it useful to capture the ‘environment’ of where the application is being hosted and document it. I’ve found this particularly applicable for .NET projects rather than Java, especially when the application suddenly breaks with no warning! This is more often than not due to a service pack install, security fixes or some software install.

What version information can be gathered from the AppEngine? There’s a handy function getProperties() that will return all the system properties available to a Java app. The list that is available from the AppEngine JVM is restricted as outlined in the ‘Environment’ section of this page. So…. running the following line of code… System.getProperties().list(resp.getWriter());….what do we get?

– listing properties –
java.vendor=Sun Microsystems Inc.

java.specification.version=1.6

line.separator=

java.class.version=50.0

java.util.logging.config.file=WEB-INF/logging.properties

java.specification.name=Java Platform API Specification

java.vendor.url=http://java.sun.com/

java.vm.version=1.6.0_13

os.name=Linux

java.version=1.6.0_13

java.vm.specification.version=1.0

user.dir=/base/data/home/apps/versionapp/1.3327…

java.vm.specification.name=Java Virtual Machine Specification
java.specification.vendor=Sun Microsystems Inc.

java.vm.vendor=Sun Microsystems Inc.

file.separator=/

path.separator=:

java.vm.name=Java HotSpot(TM) Client VM

java.vm.specification.vendor=Sun Microsystems Inc.

file.encoding=ANSI_X3.4-1968

No real surprises, running Java version 6 – update 13 on Linux. Admittedly, this doesn’t tell the full story as the ‘readme’ file included with the App Engine SDK reveals more.

Google App Engine SDK for Java
Copyright (c) Google, Inc. 2009. All rights reserved.
Visit Google Code (http://code.google.com/appengine/).

This product includes software developed by:
- The Apache Software Foundation (http://www.apache.org/).
- The servlet specification classes: servlet-api-2.5.jar, jsp-api-2.1.jar, el-api-2.1.jar
- Apache Ant (http://ant.apache.org/)
- Jakarta Commons Logging (http://commons.apache.org/logging/)
- Jakarta Commons HttpClient (http://jakarta.apache.org/commons/httpclient/)
- Jakarta Commons Codec (http://commons.apache.org/codec/)
- Jakarta Commons EL (http://commons.apache.org/el/)
- Jakarta Jasper (http://tomcat.apache.org/svn.html)
- Jakarta Taglibs (http://jakarta.apache.org/taglibs/)
- The Apache Geronimo Project (http://geronimo.apache.org/)
- javax/mail with modifications
- javax/activation
- geronimo-servlet_2.5_spec-1.2.jar, geronimo-jsp_2.1_spec-1.0.1.jar, geronimo-el_1.0_spec-1.0.1.jar
- Mort Bay Consulting (http://www.mortbay.org)
- Jetty web server

We can see that Jetty web server and the Geronimo project are used.

Before anybody complains, that technically more than one line of code was used as there is no declaration function for the servlet… here’s the full one line!

package com.test; public class versionServlet extends javax.servlet.http.HttpServlet { public void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws java.io.IOException { System.getProperties().list(resp.getWriter()); } }


Google AppEngine: Measuring Java and Python performance

Posted in Cloud, Google, Google AppEngine, Java, Python by jpd6 on April 13, 2009

Google has recently made available the hosting of Java applications on the Google AppEngine environment which is fantastic! It did get me thinking what would the performance of Java and Python be like when hosted in the Google environment? This is difficult to answer as one of the appealing aspects of AppEngine is that the developer does not need to concern themselves with the hardware environment. This also means that we don’t know the precise details as to how the applications are clustered or given allocation of CPU. However, one thing that is appealing about the AppEngine is that it is a scalable environment that can grow depending upon the application requirements. So… I thought that I’ll write a very primitive Java and Python script to run on a local machine and on AppEngine and see what happens.

Disclaimer!! – As with any metric testing, I need to state a disclaimer that a lot of factors can heavily influence the timing, such as other applications running locally, network performance, Java optimization, garbage collection, class loading, caching etc. etc. the list can go on and on. There isn’t an easy way that I’m aware of to get precise and fair timings across all environments. In particular as it’s a hosted environment Google could decide next week to upgrade the hardware thus giving a very different set of results!

I decided to create a bare minimum app that calls math functions that are available in Java and Python. Upload this app to AppEngine without any optimizations to the code or changing any of the default settings in AppEngine. I wanted the code in both apps to be as similar as possible, but another disclaimer Python and Java can’t be compared fully as they both have differences in their behavior. In particular when using the Math functions in Python it is dependent upon the OS that is hosting it. I.e. running ‘print math.pow(10,6)’ generates an error in the Google Python environment.

I decided to run three tests using Square Root, Power and Log. Run it 10 times and note the output. The local environment that I ran the tests is a single core Intel Pentium M 2Ghz, Win XP Service Pack 3 with 1Gb RAM.

What happened? (more…)

Word Clouding Gmail Junk

Posted in Cloud, GMail, Java by jpd6 on April 12, 2009

I couldn’t resist it… I had to know what ‘word clouding’ my Gmail junk folder would look like….

I can sleep safely at night as I can see ‘unsubscribe’ is very popular. Maybe it’s time for me to start reading the junk and clicking ‘unsubscribe’ and buying watches from my junk mail folder.

For those who have the morbid curiosity to do the same for their own mailbox, Gmail currently only provides one documented interface for extracting emails and that’s via POP3/IMAP4. However, an alternative and quicker approach if you use ‘offline email’ is to read the local Gmail Gears sqlite database. Gears uses the database to store the offline data, thus it’s a case of querying the local database with a Sql statement and writing to a text file. The text file I pass through to the word-cloud generator.

The location of the database can vary depending upon the browser you are using and whether you are going through http or https. Look here if you want to find the specific location of the database. The code I used to extract the mails is quite short as below.

import java.sql.*;
import java.io.*;

public class ReadGMail
{
  public static void main(String[] args) throws Exception
  {
    Class.forName("org.sqlite.JDBC");
    Connection conn = DriverManager.getConnection("gmail.com-GoogleMail#database");

    FileWriter fstream = new FileWriter("gmail.txt");
    BufferedWriter out = new BufferedWriter(fstream);
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("SELECT c0Subject, c1Body  FROM MessagesFT_content;");
    while (rs.next())
    {
      out.write(rs.getString("c0Subject") + "\n");
      out.write(rs.getString("c1Body").replaceAll("\\<.*?>","") + "\n\n");
    }
    out.close();
    rs.close();
    conn.close();
  }
}

Cloud Generating a Lotus Notes Mailbox

Posted in Cloud, Java, Lotus Notes, Wordle by jpd6 on April 12, 2009

“Wordle” is a very nice website that allows you to create “word clouds” from text as shown below.

More examples can be found within the gallery. The code to generate the cloud images is available from the ‘Word-Cloud Generator’ project part of IBM alphaWorks. However, what I really wanted to do is to generate a ‘cloud’ from a Lotus Notes mailbox. As the tool only requires a text file, it’s just a case of exporting the subject and body of the emails into a text file. (Initially, I was doing it with DXL and applying a style sheet as it was faster, but DXL crashes too often for my liking!)

(more…)

Office Communicator 2007 SDK Hello World

Posted in .NET, C#, OCS 2007 SDK, Office Communicator by jpd6 on April 11, 2009

When looking at an API, I almost always look at the code samples within the SDK first… and then read the help if I don’t understand it! I downloaded the Office Communicator 2007 SDK the other day and to my surprise there are no samples but only header files. There may be sample files somewhere but it’s not obvious. There is plenty of code within the help documentation, but it would have been nice to have included at least a ‘hello world’ project. Anyway, I’ve attached one below for anybody who just wants the bare minimum ‘hello world’ for the OC 2007 SDK. The code is copied mostly from within the OCSDK help documentation, with some minor modifications.

 (more...)

Confusing Dialog Boxes

Posted in Dialog Box, J2EE, Java by jpd6 on April 10, 2009

Hmmm, what am I meant to do now…..

For some strange reason the ‘Next’ button wouldn’t appear until I forced a screen refresh. (I.e. Alt-Tab)

Gmail S/MIME Encryption

Posted in Email, Encryption, GMail, S/MIME by jpd6 on April 10, 2009

If like me, you use Gmail and send encrypted emails, you will notice at some point that the Gmail interface doesn’t provide support for S/MIME encryption. This doesn’t mean that you can’t send and receive encrypted emails using Gmail; you need to use a different tool to encrypt and read the emails. I won’t go into detail how S/MIME works; there are lots of other websites that provide the details.

In a very simplified nutshell, the key thing to remember is that S/MIME involves sending an ‘encrypted attachment’ called ‘smime.p7m’. You can send this attachment over SMTP and receive it via POP3/IMAP4. When receiving the email, as long as your email client can recognize that there is an attachment ‘smime.p7m’, it will open and decrypt it automatically. (Assuming you have the right certificates installed!)

For instance, Microsoft Outlook has recognized that I have received an encrypted email via Gmail, shown below. I sent this encrypted email from one Gmail account via SMTP and received it in a different Gmail account via IMAP4. (more…)

Follow

Get every new post delivered to your Inbox.