JAlbum 5.2 enables skins that rely heavily on BeanShell scripting (interpreted Java) to have the scripts compiled into java classes for increased speed (expect between 50-100% speed increase if the skin makes use of a lot of scripts). Here's a short tutorial on how to do it.
First, if you haven't installed the "Java SDK" that allows you to compile java files into java classes, please get yours at
http://java.sun.com
I recommend that you install the 1.4 version.
The next step is to create an empty directory called "plugins" in the directory of a skin. JAlbum will then look for java class files there.
To get going quickly, put the attached files in the plugins directory.
Create an init.bsh file in the skin directory. Make sure it has this line:
Hello hello = new Hello(engine);
Now edit index.htt for instance and add the following just after <body>:
<%
hello.updateVars();
hello.helloWorld();
%>
Make an album and observe how "Hello world" has been printed on each index page.
So far so good I assume. Now let me explain a little bit what happened.
The line in init.bsh instanciated a Java class called hello. The class, of type Hello, contains the method helloWorld that prints "Hello world" to the page when called, but in order for it to gain access to the many implicit variables that JAlbum scripts have access to (like out, engine, title etc) it is a subclass of the class CompiledScript, which declares many of them. The instanciation done in init.bsh passes a reference to "engine" which makes CompiledScript set up most of the constant variables that one may need.
In index.htt there is a call to hello.updateVars(). This call is being handled by the CompiledScript super class. Its purpose is to set up those variables that are truly "context sensitive", like "out" for instance, or variables that are specific to each image. For performance reasons, only call this method if needed.
Finally we have the call to hello.helloWorld() which produces the output. Let's have a peek at the Hello.java source code (attached):
import se.datadosen.jalbum.*;
public class Hello extends CompiledScript {
public Hello(AlbumBean engine) {
super(engine);
}
public void helloWorld() {
out.println("Hello world");
}
}
When accelerating your own skins, keep this basic model, just vary the class name and off course replace the pretty straight forward helloWorld() method with the methods that you already use in your scripts. Some scripts are not contained in methods as this is not required by BeanShell, but in Java, all code need to be contained in a method, so just wrap them up in methods.
You don't have to subclass the CompiledScript class, but by doing so you can basically copy and paste your scripts into java files that can be compiled into a faster class files.
To simplify the compilation proceess, please use the attached bat file. It will set the right classpath for you during compilation (so the compiler can locate the CompiledScript class)
(If you are new to Java and classes, remember that the name of a Java file must match the name of the class it contains, so if you make a Utils class, make sure its file is named Utils.java etc)