compiling in memory

4 views
Skip to first unread message

musachy

unread,
Aug 1, 2008, 10:56:39 PM8/1/08
to gxp-users
Is there an easy way(or an example) to compile a template, and keep
the generated/compiled class in some sort of cache, without generating
a .java file? I am trying to do this using the test cases as examples,
but haven't got it to work yet.

Laurence Gonsalves

unread,
Aug 2, 2008, 12:14:02 AM8/2/08
to gxp-users
Just making sure I understand the question: you want to compile a .gxp
file into the corresponding Java code, but you don't want the Java
code
to be written to disk?

If that's what you want, then this can be done using an
InMemoryFileSystem. You may want to look at what StubGxpTemplate does
in its compileGxp method:

// This is the InMemoryFileSystem where we will write all output
// from both gxpc and javac
InMemoryFileSystem memFs = new InMemoryFileSystem();

// build gxp source file filesystem
SourcePathFileSystem sourcePathFs = new SourcePathFileSystem(
systemFS, srcPaths, srcGxps, memFs.getRoot());

One bit that's probably a bit surprising about this is how
SourcePathFileSystem works. SourcePathFileSystem does two things:

- Maps attempts to open a file for reading though the "source path",
so that attempts to read "foo/bar/Baz.gxp" will actually read
"/some/dir/in/your/source/path/foo/bar/Baz.gxp".

- Maps attempts to write through a destination directory, so that
attempts to write to "foo/bar/Baz.java" will actually write to
"/your/output/dir/foo/bar/Baz.java".

The second point is the interesting one here, because your output
directory can actually be on a different virtual file system. In the
StubGxpTemplate case the source path is on a SystemFileSystem, and
the output directory is an InMemoryFileSystem.

musachy

unread,
Aug 2, 2008, 10:11:56 AM8/2/08
to gxp-users
Thanks for the pointers, at this point assuming that I know what I am
doing is a long shot ;). Here's what I need to do and maybe you can
suggest a better way. The struts tags use a template engine that is
pluggable. I want to write a template manager for GXP, and here is
what it needs to do, given some template path:

1. Check if the template was compiled already (the class can be
loaded), and it wasn't modified since last compilation(good for
development), if it is, go to step 3
2. Compile the template and load the class (maybe saving on the FS is
a good idea?)
3. Run the template

thanks
musachy

On Aug 2, 12:14 am, Laurence Gonsalves <laurence.gonsal...@gmail.com>
wrote:

harryh

unread,
Aug 2, 2008, 12:21:38 PM8/2/08
to gxp-users
So, GXP supports runtime compilation "out of the box" that you might
want to just use. If you are using ant you turn this on by adding
dynamic="true" to your <gxpc> rule. Let's say we were working with
HelloWorld.gxp. We still generate a HelloWorld.java (which javac
turns into a class file) but when in dynamic mode this file doesn't
know to output the results of the template (spit out <b>Hello World</
b> or whatever). Instead if knows how to compile HelloWorld.gxp
(knows where the souce file is, what arguments were originally passed
to the compiler, etc). So when you call HelloWorld#write() it goes
off and does a runtime GXP compile cycle AND a runtime java compile
cycles (using javax.tools.JavaCompiler). You can see the code that
does this in java/src/com/google/gxp/base/dynamic/
StubGxpTemplate.java. It (obviously) checks to see if the source has
changed so it doesn't have to compile every time, just when the source
has changed.

I'm not entirely clear on your design requirements, but putting GXP in
this mode may get you all that you need. If it does not, taking a
look at StubGxpTemplate should give you some ideas about how to invoke
the compiler at runtime.

Note, however, that I will (as soon as Laurence reviews the code) be
checking in some code to make invoking the compiler a bit more
standardized. Right now ant, the command line interface, and
StubGxpTemplate all do it a little differently. My change will make
it so that they all use GxpcConfiguration. So whenever you want to
invoke the compiler you'll just need to build up a GxpcConfiguration
and pass that to an appropriate place and things will just work.

Hopefully this has been a somewhat helpful note. Definitely ask more
questions if you have them!

-harryh

Musachy Barroso

unread,
Aug 2, 2008, 12:53:44 PM8/2/08
to gxp-...@googlegroups.com
That's great (because I wont have to do much), I will wait for your
check in; creating a GxpcConfiguration like the ant task does seems
like the most elegant way to go.

thanks
musachy

--
"Hey you! Would you help me to carry the stone?" Pink Floyd

harryh

unread,
Aug 5, 2008, 9:17:54 PM8/5/08
to gxp-users
FYI: this checkin is in (http://code.google.com/p/gxp/source/detail?
r=83).

Basically create a Configuration and pass it to Compiler and you're
good to go. StubGxpTemplate doesn't do it this way yet but it will
very soon.

-harryh

musachy

unread,
Aug 7, 2008, 10:18:06 AM8/7/08
to gxp-users
Thanks a bunch, following the ant task example and stealing some code
from StubGxpTemplate I was able to get this working. One suggestion
would be to refactor the code that compiles the java files, in
StubGxpTemplate to a helper class, so it can be reused.

musachy
Reply all
Reply to author
Forward
0 new messages