Friday, September 14, 2007

Application template generation with "Archetypes" program (Scriptlandia, Maven2, Beanshell)

Maven 2 has the notion of archetype. It is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.

Some other projects are trying to achieve same goal by using similar or slightly different approaches:

- AppFuse 1.x; (light) - uses Ant scripts to generate template;

- AppFuse 2.x; - uses Maven2 scripts to generate template;

- Able Project (https://svn.opensymphony.com/svn/sandbox/able);

- HSE (Hibernate, Spring, Echo2) Project;

- Archy project.

Archetypes program is based on my previous scripts (see my
previous post) and also inspired by Archy project.

All definitions of archetypes and repository locations are decoupled from source code into external xml file (archetypes.xml).

All archetypes are divided by groups. Group is the list or archetypes that has
same groupId, version (e.g. AppFuse, Maven2 or WebTidy groups).

Each archetype has name, description and version (if different from group version). All archetypes from same group are located in same repository. For example AppFuse fragment looks like:


<groups>
  ...

  <group name="Appfuse" groupId="org.appfuse" prefix="appfuse-" version="2.0-m5" >
    <archetypes>
      <archetype name="basic-jsf"
            description="Archetype for creating a web application with Hibernate, Spring and JSF"/>

      <archetype name="basic-spring"
            description="Archetype for creating a web application with Hibernate, Spring and Spring MVC"/>
      ...
    </archetypes>

    <repositories>
      <repository>http://static.appfuse.org/repository</repository>
    </repositories>
  </group>
  ...
</groups>


In order to run the application you have ho have Scriptlandia
project installed on your computer. It will take care of downloading all required dependencies, installing them locally on your computer and then executing Beanshell script. Otherwise, you have to be ready to do this manually.

Program can function in 2 modes: console and gui. For console mode you have to execute the following command:


>create-archetype.bsh -console


Program will ask the user about archetype group name and archetype name. Then, for your application, you have to specify groupId, artifactId and version. For these parameters Archetypes program will generate the resulting template in current directory.

For "gui" mode you have to execute the following command:


>create-archetype.bsh -wait


"wait" parameter is used my Scriptlandia framework to indicate that our script should wait for completion of gui/swing thread execution. Otherwise, frame will appear for very short time and just after that script execution will be terminated.

In "gui" mode you will see same input parameters, just layed out in more convenient for typical Windows user. After selecting appropriate archetype and clicking on "Create archetype" button, new project will be created in the current directory.

This program uses behind the scene
Scriptlandia API to execute maven2 tool:


ScriptlandiaHelper.executeMaven(args);


The source for this script is located here or within examples for Scriptlandia.

Hope tis program will save you time for your family and friends!

Article about JLaunchPad in German Java Magazine

Saturday, September 08, 2007

Sony PRS (Portable Reader System) and how to read russian books

I got new gadget for my wife - Sony book reader. Unfortunately, it does not support Russian language (and any other languages except English). What to do?

The solution would be to use pdf file with embedded fonts. In order to generate pdf file I use Java pdf library that is available for everybody: iText.

In this example I will use Scriptlandia feature to download and install required dependencies automatically. I use beanshell for writing the program:


// txt2pdf.bsh

import org.sf.scriptlandia.ScriptlandiaHelper;

ScriptlandiaHelper.resolveDependencies("itext", "itext", "2.0.4");

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

if(bsh.args.length == 0) {
  print("Please specify input file.");

  return;
}

String inFileName = bsh.args[0];

int index = inFileName.indexOf(".");

String title = inFileName.substring(0, index);

String outFileName = title + ".pdf";

Document document = new Document();

// Creates a writer that listens to the document and directs a PDF-stream to a file.
PdfWriter.getInstance(document, new FileOutputStream(outFileName));

document.addTitle(title);

document.open();

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName), "Cp1251"));

boolean done = false;

Paragraph paragraph = null;

while(!done) {
  String line = reader.readLine();

  if(line == null) {
    done = true;
  }
  else {
    BaseFont baseFont = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", "CP1251", BaseFont.EMBEDDED);
    Font font = new Font(baseFont, 28);

    if(line.startsWith(" ")) {
      paragraph = new Paragraph();
      paragraph.setLeading(28);
      document.add(paragraph);
    }

    document.add(new Chunk(" ", font));
    document.add(new Chunk(line, font));

  }
}

document.close();

This program produces simple pdf that is executed very fast on the reader comparing to standard process (text-to-word-to-pdf).

Tuesday, September 04, 2007

New version of Scriptlandia: 2.2.3 has been released!

Scriptlandia is the effort to build scripting command line environment on top of JVM. The user don't have to worry how to install or configure libraries for different scripting languages. It will be done automatically at installation and/or at execution time.

This release includes:

1. Integration with JLaunchPad;

2. Support for latest versions for languages such as Groovy, JRuby, Jython, Scala etc.

3. Added support for platforms not supported by JDIC library (see documentation);

4. Bug fixes.