Permlink Replies: 2 - Pages: 1 - Last Post: 14 Oct 07, 11:40 Last Post By: twistedtwig Threads: [ Previous | Next ]
davidekholm

Posts: 3,711
Registered: 18-Oct-2002
Compiling scripts to java classes
Posted: 11 Feb 05, 00:43
  Click to reply to this thread Reply
Attachment Compile.bat (76 bytes)
Attachment Hello.class (435 bytes)
Attachment Hello.java (208 bytes)
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)
NO-USER

Posts: 1
Registered: 18-Nov-2025
Re: Compiling scripts to java classes
Posted: 12 Feb 05, 22:40   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Hi

Thank you David for this excellent introduction. You not only make a great program, but you explain thing well and with great examples. I'm a teacher, I can tell.

I immediately tried it on the skin Cholula I'm developing. The time needed to generate the album (without rescaling images) for my test folder was reduced to 25 % of what was needed before (32.5 instead of 129 seconds). I was thrilled!

However, I found that in a real skin there are some little problems to be solved, which are probably somewhat different for each installation (I'm on system XP Home). Since I had downloaded version 1.5.0 of Java SDK, I hat to change "compile.bat" a little bit deviating from David's suggestion. Try his version first and if it doesn't work, take ideas out of mine and make your own.

I'm attaching the present version of my skin. It's not intended for production, because it still has errors, but maybe the pre-release helps others to see how I solved certain problems. If you find a better way to do certain things, please post them.

Before compiling was possible, I had already put all methods or functions into the file "init.bsh" and only put calls to functions and variables in the "htt"-files, so putting everything into a class was fairly easy. My old "init.bsh" file is added as "z-init.bsh" for comparison if you're interested. It can't be used because I did't include the old "htt"-files.

The main thing I had to worry about were the skin and user variables. I solved this by reading them into public String variables which bear the same name:
pre
aaaa = getSkinVariable ("aaaa");
xxxx = getUserVariable ("xxxx");
etc.

with the methods
pre
private String getSkinVariable (String token) {
Object object = engine.getSkinVariables().get(token);
if (object == null) return null;
else return object.toString();
}
private String getUserVariable (String token) {
Object object = engine.getUserVariables().get(token);
if (object == null) return null;
else return object.toString();
}

For Java-newbies: it's important to declare variables you want to use outside of the class (e.g. in your htt-files) as public. I also hide those I don't want to be accessed outside the class by declaring them private. Java-experts don't like public variables, but at the moment I'm using them for the sake of simplicity.

Message was edited by: Robert 14-Feb-05

twistedtwig

Posts: 1
Registered: 13-Oct-2007
Re: Compiling scripts to java classes
Posted: 14 Oct 07, 11:40   in response to: NO-USER in response to: NO-USER
  Click to reply to this thread Reply
Hi When I try and use the Comiple.bat (found in JAlbum/plugins) I get the following errors

jonh@UbuntuWiggly:~/JAlbum/skins/JonTest/plugins$ ./Compile.bat 
javac: no source files
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files
  -cp <path>                 Specify where to find user class files
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -d <directory>             Specify where to place generated class files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version
  -version                   Version information
  -help                      Print a synopsis of standard options
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
 
./Compile.bat: line 1: ../../../jalbum.jar: No such file or directory
./Compile.bat: line 1: ../../../plugins/: is a directory


JAlbum is installed in /home/jonh/JAlbum/

my skin is in /home/jonh/JAlbum/skins/JonTest
I have changed Chameleon.java which is in plugins, as is the compile.bat

I am using Linux (Ubuntu), when I run the bat file ./Compile.bat I get the above output.

here is the bat file:

javac -target 1.4 -source 1.4 -classpath .;../../../jalbum.jar;../../../plugins/ *.java


Thanks for any help you could provide
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums