Wednesday, August 4, 2010

How Coldfusion works and it’s Key Features( Compilation and precompile )


Where Does the Compilation Go?
CFMX compiles cfm (and cfc) templates into .class files, which are Java byte code files. The files are written to (and executed from) the cfclasses subdirectory of [cfusionmx]\wwwroot\WEB-INF\ directory where CFMX is installed. This occurs whether you are using another Web server or have located your file outside the default wwwroot location. CFMX compiles and runs the code from this cfclasses directory, regardless of the location of the source file. The file names for these class files may not be at all apparent. A CF template named Setsession.cfm might lead to a class file named cfsetsession2ecfm1011928410.class. All templates from all directories end up in this one cfclasses subdirectory. They’re not stored here in any subdirectories related to their original location. Instead, CF includes a hash of the directory name in that set of numbers after the file name. Keep that in mind when trying to associate a given class file with its original cfm template. The hashing process is a bit convoluted. Perhaps the easiest way to detect which class file goes with which source file is to simply edit the file and then execute (or precompile) it. Look in the cfclasses directory for the most recently created class file. Assuming your server is not too busy with many compilations taking place, it should be pretty easy to associate the classname with the CF source code name.



Saving Java Source Code Produced by CFMX (Earlier coldfusion Versions)
So that’s where the compiled source code goes. But what about seeing the actual uncompiled Java source code that your CF template is converted into? Normally it’s of no concern to CF developers what CFMX is doing under the covers in converting our CFML to Java. For the ardently curious among you, did you know that you can ask CFMX to save the Java code it creates, in source form? You can. It’s an undocumented feature, and while I’ve had no trouble doing it. The setting can only be enabled by someone with administrative control of the server, and the setting is also server-wide. It will add a slight additional time to the compile process, so it’s not something you’d want to turn on in production. It probably ought not be left on in development either. You need to edit the file web.xml in the [cfusionmx]\wwwroot\WEB-INF directory. There, if you’re familiar with XML files, you’ll find a parameter called “coldfusion.compiler.saveJava”. Change its value from false to true. Save the file. Restart the server. Now, whenever a new or recently edited file is compiled (whether automatically by CFMX or by our precompile.bat file), CFMX will also create a “.java” file along with the “.class” file. This “.java” file will be found in that same [cfusionmx]\wwwroot\WEB-INF\cfclasses\ directory as the “.class” files (and will be subject to that same issue of the curious file naming mentioned above).

The Idea of Deleting the Generated Class Files
Some have proposed that instead of precompiling their code they’d just as soon delete the underlying Java class file that was created when it was last compiled. That may seem like overkill, but there are times when it might be worth trying. Just note that, as the previous sections discussed, finding the class file that’s associated with a given source template can be challenging. While some may simply delete all the class files, that’s certainly overkill. There is a -f directive you can pass to the compile process (by modifying the precompile.bat file now cfcompile.bat). That will force a recompile of a file even if CF doesn’t think it’s necessary. Sometimes that solves the same problem that deleting the class file would solve.


Precompiling ColdFusion pages

You can use the cfcompile utility to precompile ColdFusion pages (CFM, CFC, and CFR files). This can enhance initial page loading time at runtime.

Use the following command to compile ColdFusion pages into Java classes:

cfcompile webroot [directory-to-compile]

Sourceless distribution
You can use the cfcompile utility with the -deploy option to create ColdFusion pages (CFM, CFC, and CFR files) that contain Java bytecode. You can then deploy the bytecode versions of the ColdFusion pages instead of the original CFML source code.

Use the following command to compile CFML files into bytecode format that you can deploy instead of CFML source code:

cfcompile -deploy webroot directory-to-compile output-directory

After you run the cfcompile utility, perform the following steps:

Back up your original CFML files
Copy the generated bytecode CFML files to the original directory
Deploy the application.

No comments:

Post a Comment