Saturday, December 20, 2008

Try to Add Code Syntax Highlighter

Try to add code syntax highlighter for my Java or XML code.

XML :

The html code for above block is

<pre name="code" class="xml:nocontrols">

   4.0.0
   org.simple
   org.simple.web
   jar
   Simple Web Bundle
   0.0.1-SNAPSHOT

</pre>

Java :

public class Member {
   private String name;

   public Member(String name){
       this.name = name;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}
Find the instruction from fahd.blog.

I also added a new brush for manifest.mf file highlighter, the code is very simple:

dp.sh.Brushes.Manifest = function()
{
    var javaHeaders = 'Manifest-Version Created-By Signature-Version Class-Path Main-Class ' +
                      'Extension-List Extension-Name ' +
                      'Implementation-Title Implementation-Version Implementation-Vendor Implementation-Vendor-Id ' +
                      'Implementation-URL Specification-Title Specification-Version Specification-Vendor Sealed ' +
                      'Content-Type Java-Bean x-Digest-y Magic';

    var osgiHeaders = 'Bundle-ActivationPolicy Bundle-Activator Bundle-Category Bundle-Classpath Bundle-ContactAddress ' +
                      'Bundle-Copyright Bundle-Description Bundle-DocURL Bundle-Icon Bundle-Localization Bundle-ManifestVersion ' +
                      'Bundle-Name Bundle-NativeCode Bundle-RequiredExecutionEnvironment Bundle-SymbolicName Bundle-UpdateLocation ' +
                      'Bundle-Vendor Bundle-Version Dynamic-ImportPackage Export-Package Export-Service Fragment-Host ' +
                      'Import-Package Import-Service Require-Bundle';
    
    var s2dmHeaders = 'Import-Bundle Import-Library Module-Scope Module-Type Web-ContextPath Web-DispatcherServletUrlPatterns Web-FilterMappings';
    
    var aquteHeaders = 'Include-Resource Private-Package';

    this.regexList = [
                    {regex: new RegExp(this.GetKeywords(javaHeaders), 'gm'), css: 'header1'},
                    {regex: new RegExp(this.GetKeywords(osgiHeaders), 'gm'), css: 'header2'},
                    {regex: new RegExp(this.GetKeywords(s2dmHeaders), 'gm'), css: 'header3'},
                    {regex: new RegExp(this.GetKeywords(aquteHeaders), 'gm'), css: 'header4'}
                    ];

    this.CssClass = 'dp-manifest';

    this.Style =    '.dp-manifest .header1 {color: maroon;}' +
                    '.dp-manifest .header2 {color: maroon; font-weight: bold;}' +
                    '.dp-manifest .header3 {color: green; font-weight: bold;}' +
                    '.dp-manifest .header4 {color: gray; font-weight: bold;}';

}

dp.sh.Brushes.Manifest.prototype = new dp.sh.Highlighter();
dp.sh.Brushes.Manifest.Aliases = ['manifest'];

So the manifest will be beautifully formatted:
Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: Simple Web Bundle
Bundle-ManifestVersion: 2
Bundle-Description: A very simple Spring DM Web Bundle
Bundle-SymbolicName: org.simple.web
Import-Bundle: com.springsource.javax.servlet
 ,com.springsource.javax.servlet.jsp
 ,com.springsource.org.apache.taglibs.standard
 ,org.springframework.core
 ,org.springframework.beans
 ,org.springframework.context
 ,org.springframework.context.support
 ,org.springframework.web
 ,org.springframework.web.servlet
Module-Type: Web
Web-ContextPath: simple
Web-DispatcherServletUrlPatterns: *.htm

First, upload all css and js files to a public accessible location, I use Open Toast Project (Google Code) subversion trunk to store those.

Then add following to my Blogger template right before </body>:

<!-- SYNTAX HIGHLIGHTER --> 
<link href='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>  
<script language='javascript' src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shCore.js'/>  
<script language='javascript' src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushJava.js'/>  
<script language='javascript' src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushXml.js'/>  
<script language='javascript' src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushSql.js'/>
<script language='javascript' src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushManifest.js'/>
<script language='javascript' src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushJScript.js'/>
<script language='javascript'>  
dp.SyntaxHighlighter.ClipboardSwf = &#39;http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/clipboard.swf&#39;;

dp.SyntaxHighlighter.HighlightAll(&#39;code&#39;);  
</script>
<!-- END SYNTAX HIGHLIGHTER --> 

Notes: I have to add mime type "text/css" to SyntaxHighlighter.css so it can work with FireFox!

Sunday, December 14, 2008

Simple Web Application with SpringSource dm Server

(Please see the updated tutorial. 2009/03/25)

Finally I got a very simple Web application working with SpringSource dm Server. The goal is to set up a complete Web application development environment with Eclipse as IDE, Maven as build tool, SpringSource dm Server as deployment environment.

The process is very simple, but still took me a lot of time to figure out the best practice.

1. Set up IDE. Extract downloaded eclipse newest version eclipse-jee-ganymede-SR1-win32.zip to S2DM. And install Spring IDE (2.2.1.v200811281800), Spring dm Server Tool (1.1.0.v200811281800). I also installed M2Eclipse plugin (0.9.6.20080905-0917). 
Extract downloaded SpringSource dm Server 1.0.1.RELEASE to S2DM. Set up a server runtime in Eclipse for SpringSource dm Server v1.0 at localhost.

2. Create a new SpringSource dm Server Bundle project in Eclipse. Name is "org.simple.web"
The module type is "Web", context path is "simple", and dispatcher servlet url pattern is "*.htm".

3. After project creation, move the META-INF folder from src/main/jave to src/main/resources folder and add the resources folder to Java build source path in Eclipse.

Update the MANIFEST.MF :
Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: Simple Web Bundle
Bundle-ManifestVersion: 2
Bundle-Description: A very simple Spring DM Web Bundle
Bundle-SymbolicName: org.simple.web
Import-Bundle: com.springsource.javax.servlet
 ,com.springsource.javax.servlet.jsp
 ,com.springsource.org.apache.taglibs.standard
 ,org.springframework.core
 ,org.springframework.beans
 ,org.springframework.context
 ,org.springframework.context.support
 ,org.springframework.web
 ,org.springframework.web.servlet
Module-Type: Web
Web-ContextPath: simple
Web-DispatcherServletUrlPatterns: *.htm
The SpringSource dm Server Tool automatically sets up Bundle Dependencies.
4. Copy spring config files from PetClinic sample project. Create folder src/main/resources/META-INF/spring, and copy module-context.xml, osgi-context.xml.

5. Follow Craig Walls "Spring In Action" Second Edition Chapter 13 Handling wweb request, to create a simple Spring MVC Web application, but using new SpringSource dm Server OSGi-centric web application directory structure.

Create folder "src/main/resources/MODULE-INF", this is the folder for all the web content.

Add index.html to MODULE-INF folder:
<html>
<head>
  <meta http-equiv="Refresh" content="0; URL=home.htm">
</head>
</html>
The index.html redirects to home page "home.htm"

Add a very simple jsp file for the home page, just display welcome messag and list all members from backend:
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html
 <head><title>Open Toast</title></head>
 
 <body>
  <h2>Welcome to Open Toast Project</h2>
  
  <h3>Current Members:</h3>
  <ul>
  <c:forEach items="${members}" var="member">
  <li><c:out value="${member.name}"/>
  </li>
  </c:forEach>
  </ul>
 </body>
</html>

And I have to change taglib uri to get rid of this exception:
org.apache.jasper.JasperException: /WEB-INF/jsp/home.jsp(11,2) According to TLD or attribute directive in tag file, attribute items does not accept any expressions

Add Spring MVC controller for home page:
src/main/java/org/simple/web/HomePageController.java
@Controller
@RequestMapping("/home.htm")
public class HomePageController{
  public HomePageController(){}

  @RequestMapping(method=RequestMethod.GET)
  public ModelAndView handleRequest(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    List members = new ArrayList();
    members.add(new Member("Yuan Ji"));
    return new ModelAndView("home", "members", members);
  }
}
src/main/java/org/simple/web/Member.java
public class Member {
  private String name;

  public Member(String name){
    this.name = name;
  }
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
So when redirect to "/home.htm", it will map to HomePageController, and the return view is "home", which is mapped to WEB-INF/jsp/home.jsp

6. Add org.simple.web project to SpringSource dm Server, and start the server.

In Admin Console, you can see the deployed applications include org.simple.web. 

Click the link "/simple", open the simple web application.