Server Side Rendering with FLEX
From Coder's Log
I've recently started working with flex, and I have to say that it is one of the more enjoyable tools to work with. The platform is very open, and integrating with your Java back-end is a breeze. The platform is very open, and the architecture is clean fairly understandable. When integrated with a J2EE server, for JSP tag and servlet support, flex compiler is kept out of the main classpath. Only the bootstrap jars are visible to the application and sit inside the WEB-INF/lib folder all other elements necessary for compilation are located outside of lib and classes folders and are conveniently seperated from your application. Might not sound like a big deal at first, but when considering large scale application deployments with dozens of open source modules each requiring its own version of the common components, think BeanUtils or worse remember how Hibernate and EasyMock had conflicting version of ASM. Sure you can spend the time repackaging the jars but why? Seperating large modules into its own classloader takes up more memory and takes a bit more time to startup but once things are going, there is much less chance that you will run into unknown conflicts between your libraries.
So whats needed to integrate flex with java.
1. Download FLEX MPL open source edition web module FlexModule_j2ee.zip 2. Unzip the download 3. Unzip the WAR file 4. If you have a working J2EE application, then merge the two deployment structures otherwise the war file can be come the base for your new application. 5. Read the readme file on how to use the Tag/Servlet 6. Deploy to your favorite container.
Now you can simply put MXML code right into your JSP using the MXML tag or have the .mxml files compiled as they are requested by the user.
Unfortunately my world isn't quite that simple, and I couldn't just take and merge Flex war file with a huge application. So my answer was to sandbox the enviroment. I didn't want to invoke flex out of process, sounds pretty lame to invoke java code with "java -jar ..." from a container. But at the same time I wanted to be isolated from the enviroment. Haven't quite worked out all the details yet, but in a nutshell here is my process
1. Unzip the Opensource SDK from the download page above. 2. Create a new URLClassLoader with urls to the jars as a parameter. 3. Use reflection to get a hold of the Mxmlc or the Compiler class depending on the version of Flex. 4. Invoke main(String[] args) method
One might ask why would I go through the trouble of running flex compiler in-process rather then just launch another JVM? simple, the classloader can be cached for a speedier startup, I don't have to worry about process control or input/output redirection or any kind of threading and down the line it is always possible to improve on the invocation method, afterall thats what Open Source is all about.
