<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" >
  <channel>
    <title>Usability, Web Standards &#38; Design &#124; OneGeek</title>
    <atom:link href="http://www.onegeek.com.au/feed" rel="self" type="application/rss+xml" />
    <link>http://www.onegeek.com.au</link>
    <description>Just another WordPress site</description>
    <lastBuildDate>Wed, 08 Feb 2012 13:06:11 +0000</lastBuildDate>
    <language>en</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <generator>http://wordpress.org/?v=3.0.3</generator>
    <item>
      <title>Automatically generate your JPA Metamodel classes using OpenJPA and Maven</title>
      <link>http://www.onegeek.com.au/articles/programming/java/automatically-generate-your-jpa-metamodel-classes-using-openjpa-and-maven</link>
      <comments>http://www.onegeek.com.au/articles/programming/java/automatically-generate-your-jpa-metamodel-classes-using-openjpa-and-maven#comments</comments>
      <pubDate>Wed, 08 Feb 2012 13:06:11 +0000</pubDate>
      <dc:creator>Matthew Fellows</dc:creator>
      <category><![CDATA[Java]]></category>
      <category><![CDATA[java]]></category>
      <category><![CDATA[jpa]]></category>
      <category><![CDATA[maven]]></category>
      <category><![CDATA[openjpa]]></category>
      <category><![CDATA[persistence]]></category>
      <guid isPermaLink="false">http://www.onegeek.com.au/?p=431</guid>
      <description><![CDATA[A simple pom.xml for automating the generation of JPA Metamodel classes for OpenJPA using Maven]]></description>
      <content:encoded><![CDATA[<p>I found several potential candidate posts on the Internet that I thought would help me with this simple problem. As it turns out, it is simple, but get a few params slightly wrong and you&#8217;re in a mess.</p><p>Here is the plugin section of my pom.xml, it will generate JPA Metamodel classes into the target/generated-sources directory of your build and compile them into the end product.</p><pre class="brush: xml; title: ;">&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;generate-entity-metamodel&lt;/id&gt; &lt;phase&gt;generate-sources&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;compile&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;optimize&gt;true&lt;/optimize&gt; &lt;showDeprecation&gt;true&lt;/showDeprecation&gt; &lt;showWarnings&gt;true&lt;/showWarnings&gt; &lt;proc&gt;only&lt;/proc&gt; &lt;compilerArgument&gt;-Aopenjpa.metamodel=true&lt;/compilerArgument&gt; &lt;generatedSourcesDirectory&gt;${project.build.directory}/generated-sources&lt;/generatedSourcesDirectory&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;execution&gt; &lt;id&gt;compile&lt;/id&gt; &lt;phase&gt;compile&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;compile&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt;</pre><p>If you want to store these classes in your SCM, try the following instead:</p><pre class="brush: xml; title: ;">&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;generate-entity-metamodel&lt;/id&gt; &lt;phase&gt;generate-sources&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt; compile &lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;optimize&gt;true&lt;/optimize&gt; &lt;showDeprecation&gt;true&lt;/showDeprecation&gt; &lt;showWarnings&gt;true&lt;/showWarnings&gt; &lt;proc&gt;only&lt;/proc&gt; &lt;compilerArgument&gt;-Aopenjpa.metamodel=true&lt;/compilerArgument&gt; &lt;generatedSourcesDirectory&gt;${basedir}/src/main/java&lt;/generatedSourcesDirectory&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;execution&gt; &lt;id&gt;compile&lt;/id&gt; &lt;phase&gt;compile&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt; compile &lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;generatedSourcesDirectory&gt;${basedir}/src/main/java&lt;/generatedSourcesDirectory&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt;</pre><p>Hopefully this helps somebody out there.</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/articles/programming/java/automatically-generate-your-jpa-metamodel-classes-using-openjpa-and-maven/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>Lambda &#8211; Hybrid Keyword\Data-Driven Browser Automation Framework for Selenium</title>
      <link>http://www.onegeek.com.au/projects/lambda</link>
      <comments>http://www.onegeek.com.au/projects/lambda#comments</comments>
      <pubDate>Thu, 06 Oct 2011 12:27:11 +0000</pubDate>
      <dc:creator>Matthew Fellows</dc:creator>
      <category><![CDATA[Projects]]></category>
      <guid isPermaLink="false">http://www.onegeek.com.au/?p=421</guid>
      <description><![CDATA[]]></description>
      <content:encoded><![CDATA[]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/projects/lambda/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>On OSGi, Spring and Eclipse Virgo</title>
      <link>http://www.onegeek.com.au/articles/programming/on-osgi-spring-and-eclipse-virgo</link>
      <comments>http://www.onegeek.com.au/articles/programming/on-osgi-spring-and-eclipse-virgo#comments</comments>
      <pubDate>Sat, 18 Jun 2011 07:21:07 +0000</pubDate>
      <dc:creator>Matthew Fellows</dc:creator>
      <category><![CDATA[Programming]]></category>
      <category><![CDATA[eclipse]]></category>
      <category><![CDATA[java]]></category>
      <category><![CDATA[jee]]></category>
      <category><![CDATA[mvc]]></category>
      <category><![CDATA[osgi]]></category>
      <category><![CDATA[spring]]></category>
      <category><![CDATA[virgo]]></category>
      <guid isPermaLink="false">http://www.onegeek.com.au/?p=359</guid>
      <description><![CDATA[Review of OSGi Web Development, using Eclipse Virgo Tomcat Server and the Spring Framework.]]></description>
      <content:encoded><![CDATA[<p>It has been several months since I&#8217;ve started using OSGi to build Web Applications with Spring and Eclipse Virgo, and I thought it would be a good time to review the various learning&#8217;s to date and present my opinions, in particular on OSGi and Virgo.</p><p>It is a work in-progress, but I hope this works out to be a good resource for OSGi \ Virgo programmers and a starting point for some discussion around the below points &#8211; I invite comments&#8230;</p><h2>Eclipse Virgo</h2><p>In summary, Virgo (now Virgo Tomcat Server to differentiate between it&#8217;s sibling Virgo Jetty Server) is neat little Tomcat web server embedded as an OSGi module within Equinox. It uses the OSGi extender pattern to create a bridge between Equinox and Tomcat so that Web artifacts (WARs) inherit the same properties and lifecycle as a regular OSGi bundle &#8211; meaning they can be hot-deployed at runtime &#8211; with little effort from the developer.</p><p>It has some excellent features, including out-of-the-box declarative services support, the ability to specify dependencies on entire bundles or libraries (instead of tediously entering in billions of error prone Import-Package statements &#8211; use with <a href="#evil">caution</a>) using Import-Bundle and Import-Library manifest headers, OSGi configuration artifacts and possibly the most import &#8211; the concept of a &#8216;plan&#8217;; an XML artifact that specifies the atomic installation order of various other OSGi modules, optionally limiting the scope the modules to other modules within the plan. In my view, the &#8216;plan&#8217; artifact coupled with configuration artifacts provide a clean and dramatically simpler deployment packaging process across environments.</p><p>From a Web Application point-of-view, under the hood, it is still Tomcat. Clustering and most configuration is straightforward and is performed in the same manner as you would normally. In fact, your application doesn&#8217;t need to be an OSGi bundle, so if you want to migrate slowly to a complete OSGi system over time, Virgo supports this. As a bonus, the latest 3.0.0.M05 Milestone release contains Tomcat 7 meaning the Servlet 3.0 and EL 2 specifications is also now available and the default.</p><p>In general, the documentation is pretty good (http://www.eclipse.org/virgo/documentation) and in true Spring fashion there is a decent reference application that takes you through a fairly sophisticated Web Application.</p><p>Find out more here: <a href="http://www.eclipse.org/virgo/">http://www.eclipse.org/virgo/</a></p><h3>Community</h3><p>Possibly the only downside is that I feel Virgo has a reasonably small community when compared to the likes of Glassfish, for example. It is probably explained by the fact that OSGi in the Web is still relatively new, and the fact that Virgo is the successor to Spring DM Server &#8211; a gift from SpringSource. I imagine there are many customers still on that platform and the Spring forums still have some value for OSGi issues.</p><p>Again, to be fair, whilst the Glassfish community is large I would say only a small percentage are actually using OSGi.</p><h3>Debugging</h3><p>Virgo has an excellent, yet relatively undocumented feature. Glyn Normington <a title="Diagnosing Uses Violations in Virgo" href="http://underlap.blogspot.com/2011/03/eclipse-virgo-diagnostics-for-uses.html">hints</a> at it, but I don&#8217;t think the post does it enough justice. One of the most difficult problems to solve are <em>uses conflicts</em>. Virgo has some nice features around making them clearer in the log files, but sometimes they are a little cryptic. However, Virgo is configured by default to take a snapshot of the current state of bundles during an installation failure. The snapshot alone is of little use on it&#8217;s own, however if you navigate to the admin console at <a href="http://localhost:8080/admin">http://localhost:8080/admin</a> and choose &#8216;OSGi state&#8217; there is an option to select live or the dump just created by Virgo &#8211; this allows you to see what Virgo sees when it&#8217;s trying to resolve your set of bundles. I won&#8217;t go into detail here on this, as I&#8217;ve dedicated a <a href="/articles/programming/osgi-debugging-in-eclipse-virgo">separate post</a> to debugging in Virgo.</p><h3>Spring integration</h3><p>One of the best features of Virgo for Spring developers, is that it came out of Spring DM server and is therefore &#8216;Spring ready&#8217;. That means, it was designed to work with Spring applications &#8211; Web applications or otherwise. To get a basic Spring MVC application running in Virgo is not a big deal; it pretty much boils down to creating an appropriate OSGi manifest and modifying the context in web.xml to use the ServerOsgiBundleXmlWebApplicationContext context class instead of the default. From here on, the usual dependency injection and Spring goodness just works and behaves like Tomcat would. The only difference you need to worry about is breaking apart your application into logical chunks (vertically or horizontally, as described in the docs) and deploying them as separate modules to enable hot-deployment.</p><p>You don&#8217;t really appreciate this, until you try and install a Web Application into another container; such as Felix or Equinox alone. The magic that happens behind the scenes is completely transparent to you.</p><h3>Why not Glassfish?</h3><p>In the early investigation stage, it became immediately clear that there were two front-runners when it came to picking a ready-made, OSGi-enabled web server. Glassfish (3.1+) was the obvious competitor as it had a proven track record in the real world of being a fantastic Application Server, and was now OSGi capable (Apache Felix by default). The down side at the time was that it used CDI (JSR-299) heavily internally which conflicted with the Spring Library DI mechanisms. Theoretically it was possible, but it seemed like we were leading down a more difficult path; bending Glassfish and our application in a way that just didn&#8217;t feel like the right way to go. I&#8217;ll certainly keep my eyes on projects such as the <a href="https://github.com/CDISource/cdisource">Spring to CDI bridge</a> which help to make these platforms more open.</p><h2>Learnings</h2><h3>The world is not OSGi ready &#8211; be prepared to Wrap Jars</h3><p>Whilst Spring have provided the somewhat dated <a href="http://ebr.springsource.com/repository">Enterprise Bundle Repository</a> &#8211; a repository of ready made OSGi artifacts of common libraries &#8211; not all libraries you need exist there. In fact, I&#8217;m pretty sure some of the latest Spring releases don&#8217;t either i.e. Spring Security Core 3.0.5 (It&#8217;s not a huge deal though, all Spring bundles now how OSGi manifests created anyway). Not all is lost, however. Both the Maven Bundle Plugin and Bundlor are able to &#8216;wrap&#8217; existing libraries and turn them into OSGi ready bundles. The catch is ensuring that you use the correct bundle version, package export and package import versions. <a href="http://www.ops4j.org/projects/pax/construct/">Pax Construct</a> provides some nice command line tools to get the job done, which uses the Maven Bundle Plugin.</p><h3 id="evil">Import-Bundle is evil</h3><p>OK maybe not evil, but with one exception, and we&#8217;ll get to that, my personal belief is that Import-Bundle should be used with caution. I could pretty much say that all uses-conflict issues (see below) that I&#8217;ve encountered have been as a result of lazily importing an entire bundle, when only a few packages are actually required. Whether right or wrong, some 3rd party bundles are huge and contain many packages that you&#8217;re never going to use. In these cases, importing the entire bundle is like inviting all of Iran and Iraq to a party without telling them &#8211; it&#8217;s going to end badly. Conversely, inviting a few like-minded Iranians &amp; Iraqi&#8217;s will be a pleasant experience.</p><p>What I mean by this, is that all of those extra packages not used in the imported bundle still bring along their baggage &#8211; transitive dependencies that need to be resolved &#8211; the bigger the bundle, the more baggage. This is because Import-Bundle is a shorthand for &#8216;import all packages in the bundle&#8217;. It is an illusion, and at some point, you&#8217;ll have several bloated 3rd party JARs that have been poorly packaged with conflicting dependencies all over the shop.</p><p><a href="http://eclipsesource.com/blogs/2009/07/14/why-i-cant-recommend-using-import-package/">This article</a> would have you believe otherwise, however my view is that Import-Bundle regresses OSGi development to the old-school &#8216;throw the JAR in WEB-INF/lib&#8217; view of the world. Whilst we get the benefit of re-use, we lose out on precision. True, the tooling to get the job done is still in progress, and configuration files that result in package dependencies are the main cause of this. <a href="http://www.springsource.org/bundlor">Bundlor</a> is a great Spring tool that can not only parse byte-code dependencies, but also look in Spring configuration files to determine what needs to be imported.</p><h3>Declarative Services is your friend</h3><p>Simply put, don&#8217;t do things the hard way &#8211; use Spring DM&#8217;s or Blueprint&#8217;s declarative services model to implement OSGi services and avoid boilerplate code. Take the time to <a href="http://www.knopflerfish.org/osgi_service_tutorial.html">understand</a> how they work, and then never think about it again. Not only will it simplify things, it will make your code more modular and loosely coupled. Think of it as Dependency Injection for OSGi. Read more on Spring DM \ Blueprint <a href="http://static.springsource.org/osgi/docs/current/reference/html/why-Spring%20DM.html">here</a>.</p><pre class="brush: xml; title: ;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:osgi=&quot;http://www.springframework.org/schema/osgi&quot; xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd&quot;&gt; &lt;osgi:service ref=&quot;myExposedObject&quot; interface=&quot;com.melbourneit.foo.bar.AwesomeService&quot;/&gt; &lt;osgi:reference id=&quot;someOtherPotentiallyRemoteObject&quot; interface=&quot;com.somebody.something.Useful&quot;/&gt; &lt;/beans&gt;</pre><p>The above Spring DM XML declaration located by convention in META-INF/spring/*.xml (I used the recommended osgi-context.xml naming scheme) is all that is required to trigger Virgo to expose an object of type com.melbourneit.foo.bar.AwesomeService with name &#8216;myExposedObject&#8217; which is now available to any other bundle within the OSGi container. Similarly, it will also search for an object of type com.somebody.something.Useful and wait until it can be found. Once found, it is injected as per any other object injected in Spring.</p><p>Notice how we don&#8217;t care about searching for services, checking for states, runtime exceptions or anything &#8211; the DS library takes care of that for us.</p><h3>Build API and Implementation Bundles</h3><p>You want to be able to hot swap code at runtime? Then make sure you create API bundles separate from your implementation bundles, and expose the implementation bundle as an OSGi Service (as above). Here&#8217;s why:</p><ul><li>It encourages contract-first development, forcing you to think about the design &amp; features you are implementing up front</li><li>It will decouple your application from implementation specifics, meaning they are more modular in nature</li><li>It allows you to create many different providers of that API which, importantly, allows you to hot-swap them at runtime.</li><li>It (interface first) is a basic principle of Dependency Injection and it will mean many Spring things, like AOP, will work out-of-the-box</li></ul><p>So, let&#8217;s say for example you decide you want to move from an XML SOAP/RPC based web service to a RESTful one using JSON. For whatever reason, if the WS API objects changed significantly due to the service protocol change your application using it need not worry about it. The implementation bundle can take on that burden to ensure the API interface (contract) is kept consistent. The application simply expresses a dependency on a particular interface which is satisfied at run time by a provider of that interface (service). Turn on this bundle, turn off the old bundle provider and you application should swap over automagically. It need not be as big a change as this, it could be a matter of fine tuning an existing bundle. OSGi and Declarative Services will take care of the wiring for you.</p><h3>Get to know the console</h3><p>The Virgo OSGi console contains pretty much all of the information and tools you need to fix bugs, introspect state etc. The Admin application provided with Virgo contains a really nice feature to debug uses-constraints, but apart from that in my opinion the console is much faster and easier to use. Read more about it in my <a href="/articles/programming/osgi-debugging…-eclipse-virgo">debugging post</a> or at the <a href="http://www.eclipse.org/virgo/documentation/virgo-documentation-3.0.0.x/docs/virgo-user-guide/htmlsingle/virgo-user-guide.html">official documentation</a>.</p><h3>Bundle-Fragments</h3><p>We had a need to use Terracotta  for HA session and caching replication, which required a specific Valve customisation to Tomcat&#8217;s context.xml configuration file. This is normally really easy &#8211; simply add the Valve and put the relevant JAR&#8217;s into the shared /lib folder. However, in OSGi this process is not possible. As Tomcat is now an OSGi bundle, it explicitly states the packages that it depends on so dumping a JAR in a directory will do nothing. What you need to do is add the Terracotta packages into the Tomcat classpath when Virgo starts it up.</p><p>To achieve this, the most elegant way is to not fiddle with the OSGied Tomcat bundle, nor the boot classpath to make available to all bundles, but to extend it&#8217;s classpath using a Bundle-Fragment. Learn more about them on Glyn Normington&#8217;s <a href="http://underlap.blogspot.com/2011/03/importing-packages-into-3rd-party.html">post</a> or at the <a href="http://static.springsource.org/osgi/docs/1.1.0/reference/html/appendix-tips.html">Spring Documentation</a>.</p><p>In practice, you create a bundle for Terracotta (or bundle X) and create a separate bundle consisting only of a manifest file, with the usual headers and a Fragment-Host header specifying the bundle it should latch on to.</p><pre class="brush: plain; title: ;">Fragment-Host: org.eclipse.gemini.web.tomcat;bundle-version=&quot;[2.0.0.,3.0.0)&quot; Manifest-Version: 1.0 Built-By: Matt Fellows ... Bundle-Version: 1.0.0 Bundle-ManifestVersion: 2Bundle-Description: OSGi Web Self-Service Application Reference Implem entation Bundle-SymbolicName: com.melbourneit.web.eclipse.virgo.web.tomcat.terr acotta Import-Package: ...</pre><p>You may need to get used to these sorts of solutions, especially if enhancing Virgo internals is required.</p><h3>Tools for the trade</h3><p>There are at least 3 tools that I use daily to get the job done</p><ul><li><a href="http://wiki.ops4j.org/display/paxconstruct/Pax+Construct">Pax Construct</a> &#8211; Logically organises your OSGi project into Maven modules, useful tools to wrap 3rd party JARs to make OSGi compliant, sets reasonable defaults, completely transparent if you want it to be.</li><li><a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Maven Bundle Plugin</a> &#8211; Used by Pax Construct by default, I prefer this tool over bundlor for simple bundles</li><li><a href="http://www.springsource.org/bundlor">Bundlor</a> &#8211; Integrates in Eclipse, STS and Maven and can really simplify your Manifest management. Alternative to Maven Bundle Plugin.</li></ul><h3>The Virgo Team are awesome</h3><p>In my experience so far, the Virgo team have been extremely helpful with responding to queries and solving issues and are really working toward building a quality product that meets the needs of their customers. You&#8217;ll find the <a href="http://www.eclipse.org/forums/index.php?t=thread&amp;frm_id=159">forums</a> quite active and a good resource for troubleshooting, and the guys are all over Twitter so you can keep up with the latest news.</p><h3>Read</h3><p>Read the following &#8211; you&#8217;re parallel dimension self with thank you for it.</p><ul><li><a href="http://njbartlett.name/osgibook.html">http://njbartlett.name/osgibook.html </a>- This should get you started on all of the main concepts, problems etc.</li><li><a href="http://njbartlett.name/2011/02/09/uses-constraints.html">http://njbartlett.name/2011/02/09/uses-constraints.html</a> &#8211; A well explained article on debugging uses-constraints.</li><li><a href="http://www.osgi.org/blog/2006/04/misconceptions-about-osgi-headers.html">http://www.osgi.org/blog/2006/04/misconceptions-about-osgi-headers.html</a> &#8211; OSGi headers explained.</li><li><a href="http://www.ibm.com/developerworks/websphere/techjournal/1007_charters/1007_charters.html?ca=drs-">http://www.ibm.com/developerworks/websphere/techjournal/1007_charters/1007_charters.html?ca=drs-</a> &#8211; OSGi Best Practices.</li><li><a href="http://www.eclipse.org/virgo/documentation/">http://www.eclipse.org/virgo/documentation/</a> &#8211; The Virgo Documentation is pretty good, make sure that you read them in detail</li></ul><p>You can find all of my OSGi bookmarks here: <a title="OSGi bookmarks" href="http://www.delicious.com/mefellows/osgi">http://www.delicious.com/mefellows/osgi</a></p><h2>Summary</h2><p>Overall, I&#8217;m quite happy with what Virgo provides in the way of support for Spring and OSGi. I&#8217;m not 100% convinced at this stage, that OSGi is the fastest means of development &#8211; there is most definitely an overhead &#8211; however I believe in the long term this is the direction that the industry is moving in, especially with the upcoming <a href="http://osgithoughts.blogspot.com/2011/05/java-se-8-modularity-requirements.html">Java Modularity</a> requirements for Java 8 I believe OSGi developers will be well prepared for the change.</p><p>I don&#8217;t believe that Virgo has really pushed the envelope with respect to Hot deployments &#8211; one of the main selling points of OSGi. Automating deployments these days is a key activity in Agile project management, indeed it is a best practice full stop. With the current toolset, it is hard to see an elegant solution to automating deployments that allows for hot deployment of modules whilst the Web Application is still running. Plan files significantly reduce the complexity of a deployment, however an artifact within the plan cannot be hot deployed &#8211; the entire plan needs to be redeployed. Doing this by hand is possible and Virgo deals with this as expected; if a service dependency is removed at runtime, the application flow requiring it simply pauses until it comes back online. Better still, if the bundle is an implementation provider of an API, you can hot swap out the service at runtime without the Web Application even knowing. However, the cost is that this is a very manual process and that means it&#8217;s error-prone.</p><p>I&#8217;d like to see the features, like the ability to hot-deploy artifacts within a plan or command-line (aka Glassfish asadmin) tools to perform deployments in an atomic and controlled fashion from deployment scripts, for example.</p><p>For me, coming up with a best-practice and elegant solution to this problem would really ensure Virgo is the stand-out choice as a Web-enabled OSGi server for the community.</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/articles/programming/on-osgi-spring-and-eclipse-virgo/feed</wfw:commentRss>
      <slash:comments>5</slash:comments>
    </item>
    <item>
      <title>OSGi Debugging in Eclipse Virgo</title>
      <link>http://www.onegeek.com.au/articles/programming/osgi-debugging-in-eclipse-virgo</link>
      <comments>http://www.onegeek.com.au/articles/programming/osgi-debugging-in-eclipse-virgo#comments</comments>
      <pubDate>Sat, 18 Jun 2011 05:56:19 +0000</pubDate>
      <dc:creator>Matthew Fellows</dc:creator>
      <category><![CDATA[Java]]></category>
      <category><![CDATA[Programming]]></category>
      <category><![CDATA[console]]></category>
      <category><![CDATA[debugging]]></category>
      <category><![CDATA[eclipse]]></category>
      <category><![CDATA[equinox]]></category>
      <category><![CDATA[osgi]]></category>
      <category><![CDATA[virgo]]></category>
      <guid isPermaLink="false">http://www.onegeek.com.au/?p=378</guid>
      <description><![CDATA[In this article, I take you through some handy-tips for debugging OSGi applications in Eclipse Virgo and take you through a more complicated uses-violation example involving Spring MVC, Apache CXF and JSF.]]></description>
      <content:encoded><![CDATA[<p>Debugging any application can be a pain in the backside and OSGi is no exception. In this article, I show you some tips on how to debug an OSGi application using Eclipse Virgo and run through a more complicated uses-conflict example in a Spring MVC, JSF, Apache CXF Web Application.</p><p>In these examples, I am using Virgo 3.0.0.M05.</p><h2>Enable the Equinox Shell in Virgo</h2><p>By default, the Equinox shell is disabled in Virgo (due to security reasons I suspect). Edit lines 51 and 52 to enable the shell. I&#8217;ve changed the port to 2402 as it conflicts with my local CVS:</p><pre class="brush: plain; first-line: 51; title: ; toolbar: true;"># osgi console support osgi.console=2402</pre><p>You will need to restart Virgo for this to take effect. You can then hop into the console by telneting into localhost on 2402:</p><pre class="brush: plain; title: ; toolbar: true;">telnet localhost 2402</pre><div id="attachment_380" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/Picture-12-e1308363661443.png"><img class="size-full wp-image-380" title="Virgo Equinox Console" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/Picture-12-e1308363846833.png" alt="Virgo Equinox Console" width="629" height="248" /></a><p class="wp-caption-text">Virgo Equinox Console</p></div><p>Now that you&#8217;re in, have a play around. Starting with the &#8216;help&#8217; command is a good idea. See the <a href="http://www.eclipse.org/virgo/documentation/virgo-documentation-3.0.0.x/docs/virgo-user-guide/htmlsingle/virgo-user-guide.html#admin-shell">Virgo User Guide</a> for more info on setting up the shell.</p><h2>Debugging commands</h2><h3>Look at the bundle Manifest headers, as seen by Virgo</h3><p>Sometimes your manifest may not behave as you think, and is often the cause of issues. You can easily check the headers of your bundle by issuing the following:</p><pre class="brush: plain; title: ; toolbar: true;">ss headers 120</pre><div id="attachment_393" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-osgi-command-headers.png"><img class="size-full wp-image-393" title="virgo-osgi-command-headers" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-osgi-command-headers-e1308372156131.png" alt="Virgo OSGi command to view bundle headers" width="629" height="601" /></a><p class="wp-caption-text">Virgo OSGi command to view bundle headers</p></div><p>The first command lists all bundles and their states, the second allows you to view the headers associated with that bundle.</p><h3>Find out importers and exporters of a package</h3><pre class="brush: plain; title: ; toolbar: true;">packages javax.xml.bind</pre><div id="attachment_392" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/import-export.png"><img class="size-full wp-image-392" title="eclipse-virgo-osgi-console-packages-command" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/import-export-e1308371459649.png" alt="Eclipse Virgo OSGi Console Packages Command" width="629" height="370" /></a><p class="wp-caption-text">Eclipse Virgo OSGi Console Packages Command</p></div><p>Using this command, you can see the exporter of a particular package and version, as well as the bundles (indented) that import that package. This is really handy when you&#8217;re trying to work out why a particular package is not wiring to a bundle, or if it&#8217;s wiring to the wrong bundle in a uses-conflict.</p><h3>Examine a bundle \ service in detail</h3><p>Virgo provides a number of extra commands on top of the Equinox console, one of which I find particularly useful to see if my bundle and osgi contexts (module-context.xml and osgi-context.xml files respectively) have been correctly read by Virgo:</p><pre class="brush: plain; title: ; toolbar: true;">vsh bundle examine 121 vsh service examine 200</pre><p>As you can see below, when you examine a bundle you are provided with a listing of all of the packages (and their versions) that are imported\exported and the services it publishes\consumes. In this case, my bundle is exporting a service with id 200 which i can examine with the second command. This allows me to confirm that a) my service is being exposed as I expected and b) it is of the correct class &#8211; it even gives me the Spring Bean name&#8230;</p><div id="attachment_406" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/vsh-service-examine.png"><img class="size-full wp-image-406" title="Virgo OSGi console command - vsh bundle examine &lt;bundle id&gt;" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/vsh-service-examine-e1308375593588.png" alt="Virgo OSGi console command - vsh bundle examine &lt;bundle id&gt;" width="629" height="835" /></a><p class="wp-caption-text">Virgo OSGi console command - vsh bundle examine </p></div><h3>Find out what packages import\export a particular class</h3><pre class="brush: plain; title: ; toolbar: true;">clhas com.melbourneit.web.provisioning.ProvisioningManager clload com.melbourneit.web.provisioning.ProvisioningManager clexport com.melbourneit.web.provisioning.ProvisioningManager</pre><div id="attachment_394" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-osgi-clload-clhas-clexport-e1308372362955.png"><img class="size-full wp-image-394" title="virgo-osgi-commands-clload-clhas-clexport" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-osgi-clload-clhas-clexport-e1308372494275.png" alt="Virgo OSGi commands to view class import\export\users" width="629" height="313" /></a><p class="wp-caption-text">Virgo OSGi commands to view class import\export\users</p></div><p>Useful commands to work out where are class is located and what other bundles try and load that particular class.</p><h2>Diagnosing a Uses Violation &#8211; Spring MVC, Apache CXF, EHCache and JSF</h2><p>These pesky problems are really annoying to fix. <a href="http://njbartlett.name/2011/02/09/uses-constraints.html">Neil Bartlett</a> and <a href="http://blog.springsource.com/2008/10/20/understanding-the-osgi-uses-directive/">Glyn Normington</a> have explained what these are in detail, I&#8217;m going to show you how you might diagnose them using the Eclipse Virgo and Equinox console commands.</p><p>The application in question is a Spring MVC project, that uses the Apache CXF client SOAP\RESTful service communications, JSF for some presentation and uses EHCache and Terracotta for L2 caching. It also takes advantage of JSR-303 validations using the Hibernate RI. The application is multi-layered on a technical level and as you can see has a few moving parts.</p><h3>The problem</h3><p>So, I have a Web Application with multiple bundle dependencies, each with their own dependencies and so on. It is a typical situation that leads to a Uses Violation, so it was only a matter of time before I was confronted with the following:</p><div id="attachment_402" class="wp-caption alignnone" style="width: 640px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/uses-violation-e1308374411512.png"><img class="size-full wp-image-402" title="OSGi Uses-Violation" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/uses-violation-e1308374411512.png" alt="OSGi Uses-Violation" width="630" height="320" /></a><p class="wp-caption-text">OSGi Uses-Violation</p></div><p>The first step in the debugging process is to find the package that is to blame, that is, the package that has multiple exporters. As Neil points out &#8220;This is a necessary precondition for a uses constraint violation&#8221;. Unfortunately this is often the hardest part. So the above log snippet gives a few hints, but is particularly unhelpful: one of the following packages has a dependency on another package which has been exported at least twice: org.hibernate.validator.xml, org.springframework.remoting.jaxws, com.melbourneit.web.provisioning.</p><p>The issue is, you can&#8217;t use the console to assess what packages any of those bundles import at this point because the application has been rejected by the OSGi framework. You could manually install those bundles but it won&#8217;t re-create the set of conditions you need. Enter the <em>State Inspector</em>. Glyn <a href="http://underlap.blogspot.com/2011/03/eclipse-virgo-diagnostics-for-uses.html">teases</a> us with his overview, but I think he under sells it&#8217;s value. Let me try and sell it by demonstrating its usefulness.</p><p>You need to use the Admin application that comes packaged with Virgo, so ensure that the 3 artifacts below are installed or in the pickup directory:</p><pre class="brush: plain; title: ; toolbar: true;">mfellows /usr/local/virgo5/pickup $ ll total 288 -rw-r--r--@ 1 mattfellows staff 95K 27 May 16:04 org.eclipse.virgo.apps.splash-3.0.0.M05.jar -rw-r--r--@ 1 mattfellows staff 658B 27 May 16:04 org.eclipse.virgo.apps.admin-3.0.0.M05.plan -rw-r--r--@ 1 mattfellows staff 42K 27 May 16:05 org.eclipse.virgo.apps.repository-3.0.0.M05.par</pre><p>and navigate to <a href="http://localhost:8080/admin">http://localhost:8080/admin</a> once it loads. Once in, click on the &#8220;OSGi State&#8221; menu item in the top menu.</p><div id="attachment_401" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-state-choice-e1308374331320.png"><img class="size-full wp-image-401" title="Virgo Admin - OSGi State Screen" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-state-choice-e1308374331320.png" alt="Virgo Admin - OSGi State Screen" width="629" height="558" /></a><p class="wp-caption-text">Virgo Admin - OSGi State Screen</p></div><p>Notice how there is the message &#8220;Viewing state &#8216;Live&#8217;&#8221; at the top right-hand of the screen? Click on the select box next to it and choose the item underneath &#8216;live&#8217; and hit &#8216;go&#8217;. In this instance, the item is &#8217;2011-06-18-12-47-096&#8242; which references the dump created when the OSGi framework was unable to resolve the Web Application. Once I&#8217;ve chosen this, I now get a a snapshot of that point in time in the OSGi framework:</p><div id="attachment_400" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-e1308374283896.png"><img class="size-full wp-image-400" title="Virgo Admin Dump State - All Bundles" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-e1308374283896.png" alt="Virgo Admin Dump State - All Bundles" width="629" height="633" /></a><p class="wp-caption-text">Virgo Admin Dump State - All Bundles</p></div><p>So, as I mentioned earlier, I can now browse around at the 3 bundles from before to see what packages might be the cause. Starting with Hibernate, I need to see what packages it imports. I now click on the hibernate link (com.springsource.org.hibernate.validator) to see what packages it imports/exports:</p><div id="attachment_398" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-hibernate-e1308373997187.png"><img class="size-full wp-image-398" title="Virgo Admin Dump State - Bundle com.springsource.org.hibernate.validator" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-hibernate-e1308373997187.png" alt="Virgo Admin Dump State - Bundle com.springsource.org.hibernate.validator" width="629" height="522" /></a><p class="wp-caption-text">Virgo Admin Dump State - Bundle com.springsource.org.hibernate.validator</p></div><p>javax.persistance is an optional import which hasn&#8217;t been provided, so I choose javax.validation instead to see it&#8217;s exporters:</p><div id="attachment_397" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-javax.validation.png"><img class="size-full wp-image-397" title="Virgo Admin Dump State - package javax.validation" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-javax.validation-e1308374727824.png" alt="Virgo Admin Dump State - package javax.validation" width="629" height="363" /></a><p class="wp-caption-text">Virgo Admin Dump State - package javax.validation</p></div><p>Unfortunately, it only has one exporter (com.springsource.javax.validation). How about javax.xml.bind?</p><div id="attachment_399" class="wp-caption alignnone" style="width: 639px"><a href="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-javax.xml_.bind_-e1308374202610.png"><img class="size-full wp-image-399" title="Virgo Admin Dump State - package javax.xml.bind" src="http://www.onegeek.com.au/wp-content/uploads/2011/06/virgo-admin-osgi-dump-state-javax.xml_.bind_-e1308374202610.png" alt="Virgo Admin Dump State - package javax.xml.bind" width="629" height="428" /></a><p class="wp-caption-text">Virgo Admin Dump State - package javax.xml.bind</p></div><p>Bingo! It turns out that this package has two exporters; bundle 0 providing version 0.0.0 of the package (org.eclipse.osgi), and bundle 57 (com.springsource.javax.xml.bind) providing the correct version of 2.2.0, the version Hibernate is currently wired to. Now, here is the tricky part. We know that it has two exporters, but that doesn&#8217;t mean that this IS the problem &#8211; we need to confirm that another bundle in the same classpath uses the other exported package. i.e. We need to find another bundle using the javax.xml.bind package provided by bundle 0.</p><p>Actually, with the Dump Inspector it turns out not to be that difficult. Notice how some bundles have low Bundle Id&#8217;s i.e &lt; 500? These bundles have been resolved  and are not part of the uses-conflict. Bundles with really high Bundle Id&#8217;s have not actually been resolved yet &#8211; as Glyn puts it, Virgo is attempting resolution in a &#8220;side state&#8221;, and will only be committed to the framework if resolved. So, the hint here is to <em>look for bundles with enormous bundle id&#8217;s</em> and see what packages they are trying to import.</p><p>On the javax.xml.bind OSGi state page, you can see that 6 bundles are importing this package but, funnily enough, only 2 packages have really high bundle id&#8217;s: Hibernate and the Web Application. Unfortunately, in my case clicking on the Web Application bundle does not reveal the provider of the javax.xml.bind package however I happen to know that bundle 0 is imported into a Web Application bundle by default if the &#8216;Web-ContextPath&#8217; header is provided, which binds it to a conflicting version of javax.xml.bind. Removing this header removes the uses-conflict.</p><h3>The Solution</h3><p>In my case, because my Web Application was tied to bundle 0, I actually needed to prevent bundle 0 from exporting the packages I needed from another bundle. I needed to modify the lib/java6-server.profile and remove the following lines:</p><pre class="brush: plain; first-line: 93; title: ; toolbar: true;">javax.xml.bind,\ javax.xml.bind;version=&quot;2.0&quot;,\ javax.xml.bind.annotation,\ javax.xml.bind.annotation;version=&quot;2.0&quot;,\ javax.xml.bind.annotation.adapters,\ javax.xml.bind.annotation.adapters;version=&quot;2.0&quot;,\ javax.xml.bind.attachment,\ javax.xml.bind.attachment;version=&quot;2.0&quot;,\ javax.xml.bind.helpers,\ javax.xml.bind.helpers;version=&quot;2.0&quot;,\ javax.xml.bind.util,\ javax.xml.bind.util;version=&quot;2.0&quot;,\</pre><pre class="brush: plain; first-line: 134; title: ; toolbar: true;">javax.xml.ws,\ javax.xml.ws;version=&quot;2.1.1&quot;,\ javax.xml.ws.handler,\ javax.xml.ws.handler;version=&quot;2.1.1&quot;,\ javax.xml.ws.handler.soap,\ javax.xml.ws.handler.soap;version=&quot;2.1.1&quot;,\ javax.xml.ws.http,\ javax.xml.ws.http;version=&quot;2.1.1&quot;,\ javax.xml.ws.soap,\ javax.xml.ws.soap;version=&quot;2.1.1&quot;,\ javax.xml.ws.spi,\ javax.xml.ws.spi;version=&quot;2.1.1&quot;,\</pre><p>These packages are provided by other bundles at runtime (springsource.javax.xml.bind, genonimo etc.). This is not a recommended thing to do, however was required in my particular circumstance. See <a href="http://www.eclipse.org/forums/index.php/m/684687/">this forum post</a> for more details.</p><h3>OSGi State Inspector Summary</h3><p>To sum up, the OSGi state inspector allows you to:</p><ul><li>View a snapshot of the state of Virgo when a bundle fails resolution</li><li>Browse the bundles in that snapshot and view their properties; import, export and services</li><li>Diagnose uses-conflicts and other OSGi issues much more easily</li></ul><p>Read more about it at the <a href="http://www.eclipse.org/virgo/documentation/virgo-documentation-3.0.0.x/docs/virgo-user-guide/htmlsingle/virgo-user-guide.html#admin-console-view-dumps">Virgo Docs</a>.</p><p>Do you have any tips\tricks you&#8217;d like to add? Let me know!</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/articles/programming/osgi-debugging-in-eclipse-virgo/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>Felix console not enabled by default on Glassfish 3.1</title>
      <link>http://www.onegeek.com.au/articles/programming/java/felix-console-not-enabled-by-default-on-glassfish-3-1</link>
      <comments>http://www.onegeek.com.au/articles/programming/java/felix-console-not-enabled-by-default-on-glassfish-3-1#comments</comments>
      <pubDate>Sun, 06 Mar 2011 22:30:20 +0000</pubDate>
      <dc:creator>Matthew Fellows</dc:creator>
      <category><![CDATA[Java]]></category>
      <category><![CDATA[administration]]></category>
      <category><![CDATA[felix]]></category>
      <category><![CDATA[glassfish]]></category>
      <category><![CDATA[osgi]]></category>
      <category><![CDATA[shell]]></category>
      <category><![CDATA[web]]></category>
      <guid isPermaLink="false">http://www.onegeek.com.au/?p=334</guid>
      <description><![CDATA[How to enable the Felix command line shell on Glassfish 3.1 final]]></description>
      <content:encoded><![CDATA[<p>I recently upgraded to Glassfish V3.1 Final from the beta releases and noticed that the Felix shell was no longer activated by default. It wasn&#8217;t completely obvious as to how to enable it. Turns out there has been some discussion around security and the need for it to be enabled by default (see <a href="http://java.net/jira/browse/GLASSFISH-13006">GLASSFISH-13006</a> for more info).</p><p>To fix this, modify your domain config file ($GLASSFISH_HOME/glassfish/domains/domain1/config/domain.xml) and update the entry as per below:</p><pre class="brush: xml; first-line: 198; title: ; toolbar: true; wrap-lines: true;">&lt;jvm-options&gt; -Dorg.glassfish.additionalOSGiBundlesToStart=org.apache.felix.shell, org.apache.felix.gogo.runtime,org.apache.felix.gogo.shell, org.apache.felix.gogo.command, org.apache.felix.shell.remote &lt;/jvm-options&gt;</pre><p>And for those who would prefer the vanilla felix shell sans-gogo, just remove the gogo jar entries:</p><pre class="brush: xml; first-line: 198; title: ; toolbar: true; wrap-lines: true;">&lt;jvm-options&gt; -Dorg.glassfish.additionalOSGiBundlesToStart=org.apache.felix.shell, org.apache.felix.shell.remote &lt;/jvm-options&gt;</pre><p>Start the application server and telnet to localhost:</p><pre class="brush: jscript; gutter: false; title: ;">telnet localhost 6666</pre><p>&#8230; and we&#8217;re done!</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/articles/programming/java/felix-console-not-enabled-by-default-on-glassfish-3-1/feed</wfw:commentRss>
      <slash:comments>1</slash:comments>
    </item>
    <item>
      <title>A new blogging platform for Onegeek: WordPress + HTML5</title>
      <link>http://www.onegeek.com.au/journal/a-new-blogging-platform-wordpress-html5</link>
      <comments>http://www.onegeek.com.au/journal/a-new-blogging-platform-wordpress-html5#comments</comments>
      <pubDate>Wed, 08 Dec 2010 11:32:21 +0000</pubDate>
      <dc:creator>Matthew Fellows</dc:creator>
      <category><![CDATA[Journal]]></category>
      <category><![CDATA[joomla]]></category>
      <category><![CDATA[wordpress]]></category>
      <guid isPermaLink="false">http://www.onegeek.com.au/?p=279</guid>
      <description><![CDATA[The OneGeek blog has now been ported to use Wordpress and all of the Web's semantic goodies: html5, Microformats and CSS3.]]></description>
      <content:encoded><![CDATA[<h2>WordPress</h2><p>So it has been done. After some serious culling of content, deliberation of style, design &amp; procrastination I&#8217;ve ported this blog from Joomla to WordPress &#8211; and couldn&#8217;t be happier.</p><p>Whilst Joomla has a well built, object-oriented, MVC design, I couldn&#8217;t resist the elegance, style, user-friendliness and fantastic community associated with WordPress. It makes me want to publish. The plug-in community is fantastic and the documentation is brilliant.</p><p>I may well regret it in the future when globals are no longer supported and WordPress doesn&#8217;t have an update to remove that dependance but for now I&#8217;m blissfully ignorant&#8230;</p><h2>A new look</h2><p>Whilst I&#8217;m no designer, I&#8217;ve gone for a clean, elegant, typographical look for the new site to emphasize what this site is about: content.</p><h2>Add in semantics: HTML5 + CSS3 + Microformats</h2><p>I&#8217;ve also decided to use HTML5 and CSS3 heavily in the new design along with <a href="http://microformats.org/wiki/hcard">hcard</a> and other Microformats. Given that my audience predominantly uses Firefox, Chrome and Safari, my task was relatively easy, however it also works with IE7+8 with the help of Remy Sharp&#8217;s <a href="http://goo.gl/r4RDM">HTML5 enabler</a>.</p><h2>To infinity, and beyond!</h2><p>To get the most out of this excercise, I decided I would do what I could to make it high-performance.</p><p>With a careful CSS\JS compression cycle built into my standard deployment deployment scripts (another post\project for the future), some nice caching rules implemented in the .htaccess file, heavy use of sprites and the fantastic WP Super Cache plugin I was able to achieve the <strong>YSlow</strong> reading A (93 \ 100) and Google <strong>PageSpeed</strong> score of 85. Note that I lost marks for not having an Expires header, ironically, for the Google Analytics script and for not using a CDN.</p><h3>Performance results</h3><p>I also tested this from US and AU servers using LoadImpact and Webpagetest. The following are 3 independent tests of the OneGeek home page:</p><p style="text-align: left;">LoadImpact (<a href="http://www.loadimpact.com/page_analysis/www.onegeek.com.au-66d157e20eebbb178e6c944e5660fe43">US</a>):</p><ul><li>1.88s (I assume this figure is document loaded, not document ready)</li></ul><p style="text-align: left;">WebPageTest (<a href="http://www.webpagetest.org/result/101205_1T6T/">AU</a>):</p><ul><li>2.782 Document loaded</li><li>3.010s Document ready</li><li>0.702s Subsequent request document ready</li></ul><p style="text-align: left;">WebPageTest (<a href="http://www.webpagetest.org/result/101206_1TK7/">US</a>)</p><ul><li>1.405s Document loaded</li><li>1.840s Document ready</li><li>.806s Subsquent request document ready</li></ul><p>In the end for a blog running on a super-cheap, massively overlcrowded US based hosting provider, I was pretty happy with that.</p><p>Time to get publishing!</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/journal/a-new-blogging-platform-wordpress-html5/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>JavaScript Serialization</title>
      <link>http://www.onegeek.com.au/projects/javascript-serialization</link>
      <comments>http://www.onegeek.com.au/projects/javascript-serialization#comments</comments>
      <pubDate>Thu, 02 Dec 2010 10:37:56 +0000</pubDate>
      <dc:creator>Matt Fellows</dc:creator>
      <category><![CDATA[Projects]]></category>
      <category><![CDATA[gserializer]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[library]]></category>
      <category><![CDATA[serialization]]></category>
      <guid isPermaLink="false">http://onegeek.matt.mit/?p=251</guid>
      <description><![CDATA[GSerializer is a library that allows you to convert complex Javascript objects into simple, serialised, text strings and back. Persist it in cookies or on the server side.]]></description>
      <content:encoded><![CDATA[<h2>JavaScript Serialization</h2><p>Have you ever had the tedious job of pipe delimiting data to a cookie and manually checking and retrieving valid data, all just to re-assemble a JavaScript object? Well think again &#8211; what if you could <strong>serialize</strong> an entire object to a string, store that string (say in a cookie) and read it straight back into a JavaScript object without having to manually rebuild it? <strong>Serialization</strong> offers one way of <strong>persisting</strong> an entire user experience for retrieval at a later time. When that time comes, simply retrieving the object from the store and <strong>deserializing</strong> it gives you back the object that you started with. Provided that an Object Oriented approach is followed, in principle, any object can be serialized and retrieved from an external store.</p><hr id="system-readmore" /><h2>What is serialization?</h2><p>Serialization is the process of storing an object, and it&#8217;s internal state at the point of serialization, in another medium so that it can be transported or retrieved at a later date. When the object is deserialized, the object is recreated with it&#8217;s internal state in tact.</p><h2>Object persistence with GSerializer<a title="gserializer" name="gserializer"></a></h2><p>GSerializer is a JavaScript class that provides methods to serialize and deserialize any JavaScript object, using an XML format.</p><p>These are the basic steps that are required to serialize any JavaScript object with GSerializer:</p><ol><li>Create and populate an object</li><li>Create an instance of the <strong>GSerializer </strong>class</li><li>Call the <strong>serialize </strong>method of the <strong>GSerializer </strong>object and hold onto the serialized string</li><li>Store the serialized string somewhere (i.e. a cookie, database, file etc.)</li></ol><p>To deserialize the object:</p><ol><li>Retrieve the serialized string from it&#8217;s store</li><li>(Optional) Check for code tampering (see <a title="Security" href="#security">Security</a> )</li><li>Call the <strong>deserialize </strong>method of the <strong>GSerializer </strong>and store the resulting object</li><li>You can now use that object!</li></ol><h2>The Code<a title="code" name="code"></a></h2><p><code><br /></code></p><pre class="brush: jscript; title: ; toolbar: true;">// The object to serialize var myObject = new MyObject(); // The Serializer var serializer = new GSerializer(); // Grab the serialized XML var serializedXML = serializer.serialize(myObject, 'MyObject'); // Deserialize the object from the serialized XML string var deserializedObject = serializer.deserialize(serializedXML);</pre><p>Download the library from the <a class="external" title="Download GSerializer from Google Code" href="http://code.google.com/p/gserializer">Google Code </a>project page.</p><h2>The Examples<a title="examples" name="examples"></a></h2><ul><li>See a <a title="View a JavaScript Serialization Demonstration" href="/gserializer/examples/example.html" target="_blank">demonstration</a> of JavaScript deserialization using Cookies to persist the object.</li><li>Download the <a class="external" title="Download the Serialization Example Files from Google Code" href="http://code.google.com/p/gserializer">example</a> from the Google Code project page.</li></ul><h2>Storage<a title="storage" name="storage"></a></h2><p>So where should this serialized object be stored? There are many possibilities, but it comes down to choosing between client-side and server-side:</p><ol><li>Cookie (client-side)</li><li>Database (server-side)</li></ol><h3>Storage in Cookies</h3><p>Since JavaScript is a client-side scripting language, a logical choice is cookies. Cookies are easily accessed via JavaScript and so persisting and retrieving is not limited by bandwidth. In fact, in a recent application I used cookies to store a custom Session object. Each time the Session object was updated (due to an interface change, search etc.) I wrote the object to the cookie so it was essentially storing the live session. This kind of speed and reactivity is not available on the server side where calls to a database are limited by bandwidth.</p><p>However cookies suffer from the following drawbacks:</p><ol><li>Limited storge size (4kb or so)</li><li>Limited number of cookies (30 or so for a particular domain)</li><li>Can be accessed on the clients&#8217; machine and thus are susceptible to tampering. Provided no sensitive data is stored in these objects, this may not be an issue</li></ol><h3>Storage in a database</h3><p>If cookies can&#8217;t get the job done, then perhaps a database can. A database can get around the three main drawbacks of cookies but are more complicated to implement. For example, you would need some service on the server side to store and retrieve the code for a particular user. Live session writing is probably not feasible with a database either; instead the session would likely be retrieved once at the start and saved once when the user is leaving, or perhaps at the users request. If however, the browser crashed or the computer shut down unexpectedly &#8211; the changes since the last save will be lost.</p><h2>Advantages<a title="advantages" name="advantages"></a></h2><p>Using GSerializer provides a number of advantages for a JavaScript application. It;</p><ol><li>Provides a means to fix the back button: Many AJAX applications suffer if someone accidently clicks the back button in their browser. Instance variables are lost, the interface might change etc. Storing their session to an object means that when they come back, you can reload that object and the user never had to know the difference.</li><li>Provides a common way to store and retrieve any object. For example, one could write generic methods to take an object, serialize it and store it. Then provide another method to retrieve the object from it&#8217;s storage and load it back into memory. This could then be used for any object, preventing the need for object specific string manipulation from cookies.</li><li>Provides an extensible and re-usable component</li></ol><h2>Security <a title="security" name="security"></a></h2><p>There are a number of security risks and precautions that you should take when using this technique:</p><ol><li>GSerializer uses the Eval function to generate objects, therefore you must take every precaution to ensure that the data coming into the serializer is from a trusted source, and that that source has not been tampered with. (<a title="Read more about the security risks of the Eval() function" href="http://en.wikipedia.org/wiki/JSON#JavaScript_eval.28.29">read more</a> )</li><li>Interception over a non-secure connection is possible, so think carefully about persisting sensitive information (<a title="Read more about cross site forgery" href="http://en.wikipedia.org/wiki/JSON#Cross-site_request_forgery">read more</a> )</li><li>Malicious tampering of the serialized object is possible</li></ol><h3>Security: Generating a unique key</h3><p>One approach that could be used to increase the confidence that the code has not been tampered with is to generate a <strong>unique key</strong> from an undisclosed server side Hash function (or something similar) based on the contents of the serialized XML string. This unique key can be stored on the server side so when the unserialized object is retrieved, the Hash function can be called again on the data to produce another key. If the two keys are identical then the code has not changed between writing and retrieving.</p><h2>Future</h2><p>I can see a number of ways in which this technique could be enhanced:</p><ol><li>Compression of the output string to save space (particularly in the case of using cookies for persistence)</li><li>Encrpytion methods to prevent interception and tampering</li><li>Better encoding of functions and objects. Currently, objects of the same type are duplicated in the XML rather than referenced. Ideally only the instance variable/value pairs of the class should be stored and the rest of the class should be referenced to be more efficient on space.</li><li>Security function to prevent unsafe code from being evaluated by the <strong>eval() </strong>function during deserialization</li></ol><h2>JSON vs JavaScript Serialization</h2><p>Some of you may be wondering: why not use JSON? Aren&#8217;t there serializer library&#8217;s for JSON similar to this? Well, yes there are (<a title="JSON.js JavaScript file" href="http://www.json.org/json.js" target="_blank">json.js</a> from <a title="JSON home page" href="http://www.json.org/" target="_blank">JSON.org</a> ) to answer simply, but JSON will not work for our purpose because JSON is a notation for passing <strong>data</strong>, and will not encode functions which are crucial to the re-serialization in our case. A second drawback is that one must write in the JavaScript literal notation to produce a JSON string, which in my opinion is awkward and unreadable. But in saying this JSON is an internationally recognised standard for passing JavaScript Object data and may well be the real answer you are looking for.</p><h3>When to use JSON and when to use serialization</h3><p>If you only need to store data and not the objects themselves, then JSON might be the go. For example, perhaps you have a Session Object that takes only a name and some list of parameters describing the user&#8217;s session in it&#8217;s constructor. If however, you require a complex Session object containing a number of other complex objects, then it is much easier to use serialization as it takes the hassle our of re-populating each of the objects. The Session wrapper Object will already contains a reference to these pre-populated objects and their associated methods upon deserialization.</p><h1>Acknowledgements</h1><p>When I first sought the need of a serializer, I naturally started with Google. I found a number of promising starts, but none worked for complex objects and none actually serialized functions along with the objects.</p><p>The code used in GSerializer.js is based on code from (dotnetremoting.com &#8211; which is unfortunately down at the time of writing). I chose this as a starting point because I admired the use of recursion to achieve the goal. With this base, I made the following changes:</p><ol><li>turned it into a proper class</li><li>tidied up the code to use consistent naming conventions, and commented functions</li><li>introduced methods to properly serialize functions</li><li>introduced methods to properly deserialize arrays and functions</li></ol>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/projects/javascript-serialization/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>JavaScript Form Validation</title>
      <link>http://www.onegeek.com.au/projects/javascript-form-validation</link>
      <comments>http://www.onegeek.com.au/projects/javascript-form-validation#comments</comments>
      <pubDate>Wed, 01 Dec 2010 21:54:10 +0000</pubDate>
      <dc:creator>Matt Fellows</dc:creator>
      <category><![CDATA[Projects]]></category>
      <category><![CDATA[accessibility]]></category>
      <category><![CDATA[form]]></category>
      <category><![CDATA[gvalidator]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[library]]></category>
      <category><![CDATA[usability]]></category>
      <category><![CDATA[validation]]></category>
      <guid isPermaLink="false">http://onegeek.matt.mit/?p=243</guid>
      <description><![CDATA[GValidator is a simple, user-friendly and open-source client-side form validation library]]></description>
      <content:encoded><![CDATA[<h2>JavaScript Form Validation</h2><p>Want to <strong>validate user input</strong> for an (X)HTML form without embedding a single line of JavaScript, without having to worry about violating semantics, and all the while providing a reactive, user friendly experience?</p><p>Introducing <strong>GValidator</strong> &#8211; a powerful, extensible yet easy to implement JavaScript library that provides constant feedback during form completion. To use the library all you need to do is include it.</p><p><img style="border: 0pt none;" title="Example form with validation icons" src="/wp-content/uploads/2010/12/javascriptformvalidation-small1.png" border="0" alt="Example form with validation icons" /></p><hr id="system-readmore" /><h2>Table of Contents</h2><ol><li><a title="GValidator project goals" href="#aim">Project goals</a></li><li><a title="Downlead GValidator" href="#code">Download</a></li><li><a title="GValidator Examples" href="#example">Examples</a></li><li><a title="XHTML API" href="#xhtml-api">XHTML API</a></li><li><a title="GValidator Options" href="#options">Options</a></li><li><a href="#design">Design</a></li><li><a title="JavaScript API" href="#js-api">JS API</a></li><li><a title="Extending GValidator with plug-ins" href="#extending">Extending with Plugins</a></li><li><a title="Internationalisation" href="#i8n">I8N</a></li><li><a title="Advanced customisation" href="#extending-advanced">Extending with OOP</a></li><li><a title="Browser support" href="#browser-support">Browser support</a></li></ol><h2>The Aim<a title="aim" name="aim"></a></h2><p>The aim of GValidator is to provide the following:</p><ul><li>A user-friendly experience for our visitors</li><li>Semantically clean code</li><li>Standards compliant code</li><li>Cross browser compatibility</li><li>Reusable JavaScript code</li><li>Code flexibility and extensibility</li></ul><h2>The Code<a title="code" name="code"></a></h2><p>GValidator now has a Google Code project. To get the latest version, post bugs, comment on or just look about visit: <a class="external" title="GValidator Google Code Project location" href="http://code.google.com/p/gvalidator">http://code.google.com/p/gvalidator</a></p><ul><li><a title="Download GValidator, documentation and examples" href="http://code.google.com/p/gvalidator">Download</a> the GValidator library, along with examples and CSS</li></ul><p>Actually, for basic forms no extra coding is needed other than to include the JavaScript file and to add appropriate class names to your form fields. Here is an example form:</p><pre class="brush: jscript; title: ;">&lt;form action=&quot;/action/&quot; method=&quot;post&quot; class=&quot;gform&quot; &gt;&lt;br /&gt; &lt;input type=&quot;text&quot; name=&quot;firstname&quot; class=&quot;firstname required&quot; /&gt; &lt;br /&gt; &lt;input type=&quot;text&quot; name=&quot;lastname&quot; class=&quot;lastname required&quot; id=&quot;lastname&quot;/&gt;&lt;br /&gt; &lt;input type=&quot;text&quot; name=&quot;subject&quot; class=&quot;text required&quot; id=&quot;subject&quot; /&gt;&lt;br /&gt; &lt;input type=&quot;text&quot; name=&quot;captcha&quot; class=&quot;captcha required&quot; id=&quot;captcha&quot; /&gt;&lt;br /&gt; &lt;input type=&quot;text&quot; name=&quot;phone&quot; class=&quot;phone required&quot; id=&quot;phone&quot;/&gt;&lt;br /&gt; &lt;input id=&quot;submit&quot; type=&quot;submit&quot;/&gt;&lt;br /&gt;&lt;/form&gt; </pre><p>There are a few things to notice here;</p><ul><li>The form is given a class attribute of &#8220;gform&#8221;. This is what activates the automatic form validator</li><li>The fields for validation are given various class names depending on how we wish to validate them. i.e. class=&#8221;name&#8221;, class=&#8221;phone&#8221;, class=&#8221;text&#8221; etc.</li><li>To make a field required, simply add the class &#8216;required&#8217;. You can still use the library on non-required fields, as it will cleanse them and validate if not correctly completed.</li><li>There is no embedded JavaScript functions or invalid semantic markup here. Just clean, straightforward XHTML</li></ul><h2>The Example<a title="example" name="example"></a></h2><p>There are 4 examples, ranging from simple to advanced implementation:</p><ul><li>See the <a title="Basic GValidator Example" href="gvalidator/examples/test.html" target="_blank">basic</a> example of GValidator &#8211; no javascript coding required</li><li><a title="GValidator plugin example" href="gvalidator/examples/test-plugin.html">Plugin</a> &#8211; extends GValidator with a validation function type for an IP 4 address</li><li><a title="GValidator internationalization example" href="gvalidator/examples/test_DE.html">Internationalization</a> &#8211; translates GValidator into German using a translation file so that your forms will have internationalized client side validation.</li><li><a title="GValidator Custom Configuration example" href="gvalidator/examples/test-custom-configuration.html">Customization</a> &#8211; tailors the default behaviour of GValidator on a global and a per-form basis, using only basic javascript</li></ul><p>All of the examples are available for <a title="Download the example" href="http://code.google.com/p/gvalidator">download</a> on the Google code <a class="external" title="GValidator on Google Code" href="http://code.google.com/p/gvalidator/">GValidator</a> site.</p><h2>Field Types (default XHTML API)<a title="xhtml-api" name="xhtml-api"></a></h2><p>The following are the field types and corresponding class names that are included in the default implementation. They should cover most cases. If you require more than this or customisation, then you can extend the functionality as discussed below.</p><table class="zebratable" border="0"><thead><tr class="heading"><td class="heading">Form element</td><td class="heading">Description</td><td class="heading">Class name</td></tr></thead><tbody><tr><td>text</td><td>Generic text input. Allows any characters except the following &#8211; ().;&lt;&gt; and is not mandatory</td><td>text</td></tr><tr><td>text</td><td>Name field</td><td>name</td></tr><tr><td>text</td><td>Phone number field. Allows between 8 &#8211; 12 digits only.</td><td>phone</td></tr><tr><td>text</td><td>Email field</td><td>email</td></tr><tr><td>text</td><td>Captcha field</td><td>captcha</td></tr><tr><td>text</td><td>Re-Captcha field</td><td>recaptcha</td></tr><tr><td>select</td><td>Generic combo box. Requires the user to select an option with a non-empty value</td><td>select</td></tr><tr><td>radio group</td><td>Generic radio group. Requires that the user selects at least one of the options</td><td>radio</td></tr><tr><td>check box</td><td>Generic checkbox group. Requires that the user selects at least one of the options</td><td>checkbox</td></tr><tr><td>password</td><td><span>Password field. Requires a password of at least 8 characters, and containing one or more of a digit, lowercase and uppercase characters. </span></td><td>password</td></tr><tr><td>password</td><td>Password confirmation field. Verifies that the confirmed password is the same as &#8216;password&#8217;.</td><td><span>confirmpassword </span></td></tr></tbody></table><h2>GValidator Options<a title="options" name="options"></a></h2><p>GValidator has the following options, passed in to a variable with name <span class="codeformat">ONEGEEK.forms.GValidator.options</span>.<br />This variable is a literal object, containing key/value pairs.</p><table class="zebratable" border="0"><tbody><tr class="heading"><td class="heading">Parameter</td><td class="heading">Accepted Values</td><td class="heading">Description</td></tr><tr><td>reqShow</td><td>[Boolean]</td><td>Automatically add the required char to labels? (Defaults to true)</td></tr><tr><td>reqChar</td><td>[String]</td><td>Character used to indicate a required field (Defaults to &#8216;*&#8217;)</td></tr><tr><td>reqPlacement</td><td>['after', 'before']</td><td>Position of required character. Can be &#8216;before&#8217; or &#8216;after&#8217; (Defaults to &#8216;after&#8217;)</td></tr><tr><td>autoFocus</td><td>[true,false]</td><td>Automatically focus the first form element on page load (Defaults to <span class="codeformat">true</span>)</td></tr><tr><td>supressAlert</td><td>[Boolean]</td><td>Suppresses the javascript alert on an invalid form submission.</td></tr><tr><td>highlightFields</td><td>[Boolean, String]</td><td>If not <span class="codeformat">false</span>, Will apply a class name of &#8216;highlight&#8217; or the value of <span class="codeformat">String</span> to any invalid field on form submission attempt.</td></tr><tr class="heading"><td class="heading" colspan="3">Element level messaging display options</td></tr><tr><td>eMsgFormat</td><td>['open','compact']</td><td>How to display messages next to the field.</td></tr><tr><td></td><td></td><td>&#8216;open&#8217; refers to always showing the message (Default)</td></tr><tr><td></td><td></td><td>&#8216;compact&#8217; only shows if the user performs an event on the field&#8217;s icon.</td></tr><tr><td>eMsgEventOn</td><td>[String (DOM Event)]</td><td>Only used if eMsgFormat&#8217;compact&#8217;. Event used to trigger message display toggle (Defaults to &#8216;click&#8217;)</td></tr><tr><td>eMsgEventOff</td><td>[null, 'click', 'mouseout' ...]</td><td>Only used if eMsgFormat&#8217;compact&#8217;. Event used to trigger message hide toggle. (Defaults to &#8216;click&#8217;)</td></tr><tr><td></td><td></td><td>Cannot be the same as &#8216;eMsgEventOff&#8217; (they would cancel each other out)</td></tr><tr class="heading"><td class="heading" colspan="3">Form level messaging display options</td></tr><tr><td>fMsgFormat</td><td>[null, String, Function]</td><td>How to display errors at the form level.</td></tr><tr><td></td><td></td><td><span class="codeformat">null</span> displays an alert to the user indicating there are errors to be corrected. See <span class="codeformat">fMsg</span> to override default message. (Default)</td></tr><tr><td></td><td></td><td><span class="codeformat">String</span> Pass in an id reference to a container div to place the errors in as a &lt;ul&#8221;gvErrorsList&#8221;&gt;</td></tr><tr><td></td><td></td><td><span class="codeformat">Function</span> To do something custom on form submission error, pass in a function that accepts 1 parameter containing all of the error fields (<span class="codeformat">ONEGEEK.forms.AbstractFormField[]</span>)</td></tr><tr><td>fMsg</td><td>[String]</td><td>A string alert to display on error. (Defaults to &#8220;Please correct the highlighted errors!&#8221;)</td></tr><tr class="heading"><td class="heading" colspan="3">Image parameters for &#8216;compact&#8217; messages</td></tr><tr><td>icons: {</td><td>[Object]</td><td></td></tr><tr><td>ok</td><td>[String]</td><td>Path to the <span class="codeformat">ONEGEEK.forms.FIELD_STATUS_OK</span> state icon</td></tr><tr><td>info</td><td>[String]</td><td>Path to the <span class="codeformat">ONEGEEK.forms.</span><span class="codeformat">FIELD_STATUS_</span><span class="codeformat">INFO</span> state icon</td></tr><tr><td>error</td><td>[String]</td><td>Path to the <span class="codeformat">ONEGEEK.forms</span><span class="codeformat">.FIELD_STATUS_</span><span class="codeformat">ERROR</span> state icon</td></tr><tr><td>}</td><td></td><td></td></tr></tbody></table><h2>GValidator Library Design<a title="design" name="design"></a></h2><p>The library was designed with simplicity and extensibility in mind. The basic idea was to create an abstract class hierarchy that could be quickly and easily extended to change the validation function for a new field type. This hierarchy exists in three tiers allowing for the following object types:</p><ul><li>One third (top) level <span class="codeformat">AbstractFormField</span> class, which provides the skeleton that all <span class="codeformat">AbstractFormField</span> subclasses inherit from</li><li>A finite number of second level <span class="codeformat">AbstractFormElements</span> which provide the basic implementations for the different form elements (i.e. &lt;input&gt;, &lt;select&gt; etc.)</li><li>An infinite number of first level <span class="codeformat">ConcreteFormElement</span> objects which provide the concrete validation implementations (i.e. validate a phone number field)</li></ul><p>By implementing the library in this way, we allow for simple extensibility by implementing subclasses at the lowest level.</p><h2>JavaScript API <a title="js-api" name="js-api"></a></h2><p><a title="GValidator JS API" href="gvalidator/documentation/index.html">Read</a> the JavaScript API or <a title="GValidator" href="http://code.google.com/p/gvalidator">download</a> it from Google Code.</p><h3>Class Hierarchy</h3><p><img title="Basic Form Field Class Hierarchy" src="/wp-content/uploads/2010/12/formDesignHierarchy21.jpg" border="0" alt="Basic Form Field Class Hierarchy" /></p><h2>Extending GValidator <a title="extending" name="extending"></a></h2><p>Creating new validation types for GValidator is easy, they are added in the form of plug-ins. All you need to do is add object keys to the <span class="codeformat">ONEGEEK.forms.GValidator.plugin</span>s object with the class name of the validator you wish to add.</p><h3>Customisation example</h3><p>Let&#8217;s say we&#8217;d like to create a new validator for an IP 4 Address type on a text field, using the class name &#8216;ip4address&#8217;.</p><p>The validation function must:</p><ul><li>Validate the input of an IP 4 Address type i.e. 127.0.0.1</li><li>Remove non-allowed characters from the field before validation</li><li>Have custom messages for each state of the field</li><li>Additionally, the validator must highlight the field on error and clear the highlight on success</li></ul><h4>Step 1: Create the plugins object and add your new type and extend a type</h4><p>Place the following object in an external JavaScript file after gvalidator.js has been added to the page.</p><p>The new type will use the <span class="codeformat">className</span> &#8216;ip4address&#8217; to activate the validator on the field.<br /><code><br /></code></p><pre class="brush: jscript; title: ;">ONEGEEK.forms.GValidator.plugins = { ip4address: {_extends: 'AbstractTextField', ... };</pre><p>Note that the _extends key is set to &#8216;AbstractTextField&#8217; because we are validating a Text field. Other options are &#8216;ComboBox&#8217;, &#8216;RadioButton&#8217; or &#8216;Checkbox&#8217; at the second level (see above). Of course, you can extend existing elements such as &#8216;NameField&#8217; if you wish.</p><h4>Step 2: Override existing instance variables</h4><p>Here we override the <span class="codeformat">AbstractFormField</span> messages and <span class="codeformat">AbstractTextField</span> <span class="codeformat">regex</span> and <span class="codeformat">cleanRegex</span> variables. These last variables are used to validate the data and clean out invalid characters respectively (Note: regex has been shortened for display).</p><pre class="brush: jscript; title: ;">ONEGEEK.forms.GValidator.plugins = { ip4address: {... regex: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2 cleanRegex: /[^0-9\.]/g, contextMsg: 'Please enter a valid IP 4 Address i.e. 127.0.0.1', errorMsg: 'Please enter a valid IP 4 Address i.e. 127.0.0.1', successMsg: 'Thanks', ... }};</pre><h4>Step 3: OPTIONAL Override validation and other functions</h4><p>If you are content to keep the validation behaviour consistent with other text fields, then you can stop here &#8211; how easy was that?</p><p>If, however, you want to do some custom validation behaviour then we need to override the existing <span class="codeformat">validate()</span> function:<br /><code><br /></code></p><pre class="brush: jscript; title: ;">ONEGEEK.forms.GValidator.plugins = { ip4address: {... validate: function() {if(!this._validate()) {this.highlight(); } else {this.unHighlight(); }}, highlight: function() {this.field.style.background = 'rgb(200,0,0)'; }, unHighlight: function() {this.field.style.background = 'rgb(255,255,255)'; }} };</pre><p>Note that the <span class="codeformat">validate</span> function makes a call to<span class="codeformat"> _validate()</span>. This is actually the parent (previously overridden) validate function. In fact, <em>any</em> attribute or function that you override will be preserved with a &#8216;_&#8217; prefix so that you can make reference to it if needed. This means, we can call the existing validation function that we like, and enhance it by highlighting the field. Also note that you can attach any function/attribute that you desire and they will be attached to the <span class="codeformat">this</span> of the new object type, effectively extending the class. Be careful not to clash with existing variables/functions unless you intend to (Check out the <a title="GValidator JS API" href="gvalidator/documentation/index.html" target="_blank">API</a> or download it from Google Code if unsure)</p><h3>Full customisation code</h3><p>Here is the entire code listing for the customisation (regex has been shortened for display):<br /><code><br /></code></p><pre class="brush: jscript; title: ;">ONEGEEK.forms.GValidator.plugins = { ip4address: {_extends: 'AbstractTextField', regex: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2 cleanRegex: /[^0-9\.]/g, contextMsg: 'Please enter a valid IP 4 Address i.e. 127.0.0.1', errorMsg: 'Please enter a valid IP 4 Address i.e. 127.0.0.1', successMsg: 'Thanks', validate: function() {if(!this._validate()) {this.highlight(); } else {this.unHighlight(); }}, highlight: function() {this.field.style.background = 'rgb(200,0,0)'; }, unHighlight: function() {this.field.style.background = 'rgb(255,255,255)'; }} };</pre><h2>Internationalisation<a title="i8n" name="i8n"></a></h2><p>Internationalising <em>client-side validation</em> in GValidator is easy. It should work with any almost any language.</p><p>Note: GValidator does not change the labels on the form or any other content on the page other than content related to the <em>validation </em>of the form. It assumes that the page itself has already been translated and requires form validation specific to that language.</p><p>To specify a language translation for a form, simply do the following:</p><ol><li>Apply the W3C attribute &#8216;lang&#8217; to the &lt;form&gt; with the value of the language to translate into i.e. lang=&#8221;DE&#8221; (see a <a title="Language attribute values" href="http://www.iana.org/assignments/language-subtag-registry" target="_blank">list</a> of valid values)</li><li>Create a variable with the format ONEGEEK.forms.GValidator.translation.&lt;LANG&gt; i.e. <span class="codeformat">ONEGEEK.forms.GValidator.translation.DE</span> (Note that uppercase is important)</li><li>In that Object, override messages/parameters for existing validator types using the <span class="codeformat">className</span> of the field you wish to override i.e. &#8216;username&#8217;</li><li>To set defaults across ALL validator types, use the special top-level key &#8216;defaults&#8217;. These will only be used in the case that a translation is not available for a given class</li></ol><h3>I8N example</h3><p>Create a form specifying the lang attribute:</p><pre class="brush: jscript; title: ;">&lt;form lang=&quot;DE&quot; ...&gt;</pre><p>Create an JS file containing a variable like the following:<br /><code><br /></code></p><pre class="brush: jscript; title: ;">ONEGEEK.forms.GValidator.translation.DE = { defaults: {successMsg: 'Danke', contextMsg: 'Bitte füllen Sie', errorMsg: 'Kaputt! Es wurde ein Fehler beim Überprüfen diesem Bereich', emptyMessage: 'Pflichtfeld, füllen Sie bitte.', }, firstname: {contextMsg: 'Wir möchten Sie von Ihrem Namen zu nennen' }, ... };</pre><p>To set the form level error alert messag, override the &#8216;fMsg&#8217; property in the options config (see Customisation abve) like the following:</p><pre class="brush: jscript; title: ;">ONEGEEK.forms.GValidator.options = { fMsg: &quot;Bitte korrigieren Sie die markierten Fehler!&quot; };</pre><h2>Extending GValidator (Advanced)<a title="extending-advanced" name="extending-advanced"></a></h2><p>If you prefer to code in a more OOP (and I use that term very loosely) type approach, with the added benefit of code completion in some editors, this might be for you.</p><p>To create your own validation types, you need to do the following things:</p><ol><li>Create a class at the first level, overriding the AbstractFormElement of your choice. i.e. To create a UsernameFormField class you would subclass the AbstractTextField class</li><li>Override any instance variables of <span class="codeformat">ONEGEEK.forms.AbstractFormField</span> if necessary (Read the JavaScript <a title="GValidator JS API" href="gvalidator/documentation/index.html">API</a> or download it from Google Code)</li><li>Override the validation function (and any other functions necessary) to do the custom form field validation</li><li>Register the new class with FormFieldFactory</li></ol><p>We will run through these steps in the following example.</p><h3>Customisation example</h3><p>Lets say we would like to create a new field type <strong>username </strong>for validating usernames. The requirements are as follows:</p><ul><li>It must be between 8-10 alpha-numeric characters</li><li>It must not already exist in the database</li><li>It is a required field</li></ul><h4>Step 1: Subclass AbstractFormElement</h4><p>Create a new class  <span class="codeformat">ONEGEEK.forms.GenericTextField</span> and subclass<span class="codeformat"> ONEGEEK.forms.AbstractTextField.</span><br /><code><br /></code></p><pre class="brush: jscript; title: ;">ONEGEEK.forms.UsernameTextField = function(field) {this.field = field; ... }ONEGEEK.forms.UsernameTextField.prototype = new ONEGEEK.forms.AbstractTextField();</pre><h4>Step 2: Override existing instance variables</h4><p>In this case, we need to make the field required and we want to validate the field using a regular expression. We can override the instance variables in <span class="codeformat">ONEGEEK.forms.AbstractTextField</span> to achieve this. Override the following instance variables like so:</p><pre class="brush: jscript; title: ;">this.field = field; // This MUST alwys be here this.regex = /^([a-zA-Z0-9-\'\s]{8,10})$/g this.cleanRegex = /[^0-9a-zA-Z-\'\s]/g this.errorMsg = 'Your username must be between 8 and 10 characters'; this.contextMsg = 'You will use this to login. It must be between 8 and 10 characters';</pre><h4>Step 3: Override the validation function</h4><p>The validation function is where most of the action happens. This function is responsible for checking whether the data has been modified and is valid, and setting the appropriate state of the field (INFO, OK, ERROR). This is where your custom AJAX code would go.</p><p>Assuming that an AJAX function called <strong>checkUsername(name, field)</strong> exists, which takes a username and a ConcreteFormElement object, this is how you would implement the validation function:</p><pre class="brush: jscript; title: ;">this.validate = function() {this.clean(); this.pattern = new RegExp(this.regex); var validated = this.pattern.test(this.field.value);&lt;/p&gt; if (validated) {// Regex validated, now use AJAX to check if username exists // On successfull AJAX validation set state to OK // On failure of AJAX validation set state to ERROR checkUsername(this.field.value, formField); } else {if (this.modified == false) {this.setState(ONEGEEK.forms.FIELD_STATUS_INFO); } else {this.setState(ONEGEEK.forms.FIELD_STATUS_ERROR); }} return true; }</pre><h4>Step 4: Register the class with FormFieldFactory</h4><p>Your custom validation function wil never be used unless you register with <span class="codeformat">FormFieldFactory</span> and tell it which classes you want it to automatically handle. Let&#8217;s say that you want to use the class &#8220;username&#8221; with this function as in the example form element below:</p><pre class="brush: jscript; title: ;">&lt;input type=&quot;text&quot; name=&quot;username&quot; class=&quot;username&quot;/&gt;</pre><p>To get your new function to handle this, you simply do the following:</p><pre class="brush: jscript; title: ;">formFieldFactory.registerFormField('username', 'UsernameTextField');</pre><p>Now when you load the page your new function will handle the validation!</p><h3>Full customisation code</h3><p>Here is the entire code listing for the customisation:</p><pre class="brush: jscript; title: ;">ONEGEEK.forms.UsernameTextField = function(field) {this.field = field; this.regex = /^([a-zA-Z0-9-\'\s]{8,10})$/g this.cleanRegex = /[^0-9a-zA-Z-\'\s]/g this.errorMsg = 'Your username must be between 8 and 10 characters'; this.contextMsg = 'You will use this to login. It must be between 8 and 10 characters';&lt;/p&gt; this.validate = function() {this.clean(); this.pattern = new RegExp(this.regex); var validated = this.pattern.test(this.field.value); if (validated) {// Regex validated, but use AJAX to check if username exists // On successfull AJAX validation set state to OK // On failure of AJAX validation set state to ERROR checkUsername(this.field.value, this); } else {if (this.modified == false) {this.setState(ONEGEEK.forms.FIELD_STATUS_INFO); } else {this.setState(ONEGEEK.forms.FIELD_STATUS_ERROR); }} return true; }} // Subclass Abstract FormField class ONEGEEK.forms.UsernameTextField.prototype = new ONEGEEK.forms.AbstractTextField(); // Register the field types with FormFieldFactory formFieldFactory.registerFormField('username', 'UsernameTextField');</pre><h2>GValidator Browser Support<a title="browser-support" name="browser-support"></a></h2><p>The library has been tested successfully on the following browsers on Windows:</p><ul><li>Internet Explorer 6.0</li><li>Internet Explorer 7.0</li><li>Internet Explorer 8.0</li><li>Mozilla Firefox 1.0.6</li><li>Mozilla Firefox 2.0.0.7</li><li>Mozilla Firefox 3.0</li><li>Mozilla Firefox 3.5</li><li>Safari 3 Beta</li><li>Opera 9.25</li><li>Netscape Navigator 8.0</li><li>Netscape Navigator 9.0.0.1</li><li>Google Chrome</li></ul><p>and the following browsers on Linux:</p><ul><li>Mozilla Firefox 2+</li><li>Opera 9.62</li><li>Epiphany 2.26</li></ul>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/projects/javascript-form-validation/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>Mootools &amp; Internet Explorer: &quot;Object does not support this property or method&quot;</title>
      <link>http://www.onegeek.com.au/articles/programming/javascript/mootools-internet-explorer-object-does-not-support-this-property-or-method</link>
      <comments>http://www.onegeek.com.au/articles/programming/javascript/mootools-internet-explorer-object-does-not-support-this-property-or-method#comments</comments>
      <pubDate>Wed, 01 Dec 2010 21:33:36 +0000</pubDate>
      <dc:creator>Matt Fellows</dc:creator>
      <category><![CDATA[JavaScript]]></category>
      <category><![CDATA[debugging]]></category>
      <category><![CDATA[internet explorer]]></category>
      <category><![CDATA[javascript]]></category>
      <category><![CDATA[mootools]]></category>
      <guid isPermaLink="false">http://onegeek.matt.mit/?p=224</guid>
      <description><![CDATA[This post shows you how to avoid the infamous "Object does not support this property or method" when using Mootools as your JavaScript library.]]></description>
      <content:encoded><![CDATA[<p>As you may or may not know, <a href="http://mootools.net/docs/core/Element/Element">Mootools</a> (MT) cannot extend certain elements in Internet Explorer using the $ syntax or at all. These include non-W3C standard elements such as activeX controls, &lt;object&gt; etc.</p><p>However, the &#8220;Object does not support this property or method&#8221; message can occur in other strange situations. From my experience, this usually comes down to the following:</p><ul><li>Applying MT to non-standards compliant (X)HTML</li><li>Attempting to use the <span class="code">$</span> function on an element that has the same id as another element in the DOM</li></ul><p>I&#8217;ve now found another example with forms. Take for instance the following form that I was fixing for a colleague:</p><pre class="brush: xml; title: ;">&lt;form id=&quot;domainSearch&quot; method=&quot;post&quot; action=&quot;/domain-names&quot; enctype=&quot;application/x-www-form-urlencoded&quot;&gt; &lt;input id=&quot;domain&quot; type=&quot;text&quot; name=&quot;domain&quot; value=&quot;&quot; /&gt; &lt;input id=&quot;domainSubmit&quot; class=&quot;icon_sprite&quot; type=&quot;submit&quot; name=&quot;send&quot; value=&quot;Send&quot;/&gt; &lt;/form&gt;</pre><p>The following JavaScript works fine in normal browsers, but will trigger the above error in IE:</p><pre class="brush: jscript; title: ;">$('domainSearch');</pre><p>As far as I can tell, this is because the name and id fields have different values and IE doesn&#8217;t appreciate it. The following HTML corrects it:</p><pre class="brush: xml; title: ;">&lt;form id=&quot;domainSearch&quot; method=&quot;post&quot; action=&quot;/domain-names&quot; enctype=&quot;application/x-www-form-urlencoded&quot;&gt; &lt;input id=&quot;domain&quot; type=&quot;text&quot; name=&quot;domain&quot; value=&quot;&quot; /&gt; &lt;input id=&quot;domainSubmit&quot; class=&quot;icon_sprite&quot; type=&quot;submit&quot; name=&quot;domainSubmit&quot; value=&quot;Send&quot;/&gt; &lt;/form&gt;</pre><p>Although it is recommended that the name and id attributes have the same value, it technically isn&#8217;t a standards compliance issue. This issue won&#8217;t be picked up  by those who use the Firefox HTMLTidy plugin or the W3C Validator in strict mode to detect poor compliance so this is one to look out for.</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/articles/programming/javascript/mootools-internet-explorer-object-does-not-support-this-property-or-method/feed</wfw:commentRss>
      <slash:comments>1</slash:comments>
    </item>
    <item>
      <title>Zend Studio &#8211; Unit Testing</title>
      <link>http://www.onegeek.com.au/articles/programming/zend-studio-unit-testing</link>
      <comments>http://www.onegeek.com.au/articles/programming/zend-studio-unit-testing#comments</comments>
      <pubDate>Mon, 22 Nov 2010 21:20:52 +0000</pubDate>
      <dc:creator>Matt Fellows</dc:creator>
      <category><![CDATA[PHP]]></category>
      <category><![CDATA[Programming]]></category>
      <category><![CDATA[debugging]]></category>
      <category><![CDATA[IDE]]></category>
      <category><![CDATA[php]]></category>
      <category><![CDATA[unit testing]]></category>
      <category><![CDATA[zend framework]]></category>
      <category><![CDATA[zend studio]]></category>
      <guid isPermaLink="false">http://onegeek.matt.mit/?p=146</guid>
      <description><![CDATA[How to properly configure and setup Zend Studio 6.1 for Unit Testing &#038; debugging.]]></description>
      <content:encoded><![CDATA[<h1>Unit Testing in Zend Studio</h1><p>I though I might share my experience with Zend Studio 6.1.1, whilst debugging and unit testing a recent web application. Although in general, Zend Studio is a fantastic product and can make your life extremely easy, it is not without it&#8217;s nuances. Here are some of the problems (and resolutions of course!) that I came across.</p><h2>Poor Error reporting</h2><p>By far the most frustrating thing about Unit Testing in Zend Studio, is that when your test cases don&#8217;t work, you better be good at debugging, becuause Zend Studio doesn&#8217;t report the errors very well. You then end up wasting time you could spend doing other things. More enjoyable things like having lunch or playing <a class="external" title="Bingo" href="http://www.cheekybingo.com/ " target="_blank">bingo</a>. However, instead you are spending that time trying to figure out what on earth is going on! I  recently had a lot of trouble creating the most basic Zend_Controller test case even though my application appeared to be working correctly. The actual method looked like the following:</p><p><code><br /></code></p><pre class="brush: php; title: ;">public function testHomePageAction() {$this-&gt;dispatch('/'); $this-&gt;assertModule('default'); }</pre><p>But no matter what I tried, it never asserted the correct module, not even the error module. The errors that were coming back from the system were vague and of no help altogether (see <a class="external" title="Zend Test issue" href="http://framework.zend.com/issues/browse/ZF-6013">this issue</a>, at least it is on their radar.</p><p>What I found (after hours of debugging and trying a number of things), was that during postDispatch() the ErrorHandler was catching an Exception that didn&#8217;t appear to be visible during dispatch. If I stepped in to that class in the <strong>PHP Debug perspective</strong> and set a breakpoint at line 226 of Zend_Controller_Plugin_ErrorHandler</p><p><code><br /></code></p><pre class="brush: php; first-line: 220; title: ;">... // Get exception information $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS); $exceptions = $response-&gt;getException(); $exception = $exceptions[0]; $exceptionType = get_class($exception); $error-&gt;exception = $exception; ...</pre><p>I could look into the <strong>Variables</strong> window at the $exceptions variable and see the exception that was occurring. It turns out it was just that the Registry had been initialized twice (probably by the testing framework) and that caused it to dispatch twice &#8211; once for the intended action and once for the &#8216;error&#8217;.</p><p>Lesson: If you are having troubles, cut to the chase and view the Exceptions in Debug mode.</p><h2>Use your own PHP</h2><p>The in-built PHP binaries that come with Zend just don&#8217;t cut it. No mysqli support, no SoapClient and probably a whole bunch of other things that you or I would require in day to day life. The easiest way to get your application working in and out of Zend Studio is to use the same PHP binary. You can add yours easily into the Studio by adding it to:</p><p class="codeformat">Window &gt; Preferences &gt; PHP &gt; PHP Executables &gt; Add</p><p>Just point it to the location of your PHP binary and php.ini files and then add the location of the Zend Debugger to your php.ini file as follows:</p><p><code><br /></code></p><pre class="brush: php; title: ;">zend_extension=/usr/apps/Zend/ZendStudioForEclipse-6.1.1/plugins/org.zend.php.debug.debugger.linux.x86_5.2.15.v20080907/resources/php5/ZendDebugger.so</pre><h2>Really really slow Unit Testing / Debugging</h2><p>If you are finding that to run 1 simple unit test that you have to wait 6 hours (exagerrated) for it to finish and more annoyingly that the IDE itself is unresponsive while doing so, it is probably because you have <strong>Code Coverage Statistics</strong> and <strong>Generate Report</strong> options turned on. Do yourself a favour, if you&#8217;re not using them, turn it off by unchecking them here:</p><p class="codeformat">Window &gt; Preferences &gt; PHP &gt; PHPUnit</p><p>Hopefully I have helped some annoyed / frustrated developers.</p>]]></content:encoded>
      <wfw:commentRss>http://www.onegeek.com.au/articles/programming/zend-studio-unit-testing/feed</wfw:commentRss>
      <slash:comments>0</slash:comments>
    </item>
  </channel>
</rss>
