Wednesday, June 20, 2012

CFX(Java) Custom tags in ColdFusion-9(Part-1)

For  ColdFusion - 9 some sample java source files are present in “{ColdFusion_Root}/cfx\java\distrib\examples\” for windows and in “{ColdFusion_Root}/cfx/java/examples” directory for linux system.

<cfx_HelloColdFusion name="Mr. X">

Take the example of “CFX_HelloColdFusion” from the specified source codes present. If you directly execute that CFX tag with the above code without doing anything to the server you will get the following error message.

"Error processing CFX custom tag CFX_HelloColdFusion.
The CFX custom tag CFX_HelloColdFusion was not found in the custom tag database. You must add custom tags to the database before using them. If you have added your tag to the database, check the spelling of the tag within your template to ensure that it matches the database entry."

So you need to follow the following steps in order to install that source code as a CFX tag.

  1. Compile the source code ("HelloColdFusion.java") file  from the sample files of the ColdFusion installation  by using the command prompt or by using any IDE and generate the "HelloColdFusion.class" file.
  2. For testing the CFX tag place the  HelloColdFusion.class  file in “cf_root/wwwroot/WEB-INF/lib” in Server configuration and place in “cf_webapp_root/WEB-INF/lib” for J2EE configuration. In this place when you make any changes to the java code you only need to replace the new .class file with the existing one. You don't need to restart the ColdFusion server on each changes to the .class built.
  3. You can also place your .class files in your specific directory by changing the Class path settings in ColdFusion administrator.(Server Settings->Java and JVM -> ColdFusion Class Path). After that you need to restart your ColdFusion server.
  4. Then you need to register your CFX tag in ColdFusion admin:
  • Go to Extensions =>CFX Tags => Click Register Java CFX in ColdFusion admin. You will see a screen like below. 
  •  CFX tag name starts with the prefix "cfx_" then the class name of the java code in our case the class name is "HelloColdFusion". So the vale for tag name input will be "cfx_HelloColdFusion".
  •  In Class Name field we need to provide the class name without any .class extension. e.g-"HelloColdFusion".
  •  In Description text area we need to add some description for that tag. e.g-This tag shows greeting message. This field is Optional we can skip it. But, it helps us when we have very large no of CFX tags.
After all these steps if I will run the CFX code which I have written on the top of this page, I will get the following message as the output.
"Hello, Mr. X"

For more details on CFX(java)  in ColdFusion-9 have to wait for the next post. Thanks :)

1. CFX Custom Tag Part II

Wednesday, June 13, 2012

Understanding writeOutPut function in ColdFusion

I was doing some some funny testing with writeOutPut() method of ColdFusion with iif() method.
My Code was:
<cfset iif(false, WriteOutput("It will execute IF TRUE"), WriteOutput("It will execute IF FALSE"))>

I got the out put:

It will execute IF TRUEIt will execute IF FALSE
then Error

Variable YES is undefined.

Here the question is why I am getting this error?

Then I made the testing with the following code:

<cfset c = writeoutput("hi")><cfoutput>#c#</cfoutput>

The out put of the above code was:

"hi YES".

After this out put I got my answer to first question i.e the writeOutPut() function returning some Boolean value but in "YES" and "NO"  format and iif() function treats that YES and NO as variable not as a Boolean value.

But, another question arises here When we gets the value NO/false in calling writeOutPut()?


If anyone know the answer of my question or my understanding is wrong . Please write your comment.

Saturday, June 09, 2012

ColdFusion Query Of Queries and local scope

Few days ago I was working with  QueryOfQuery and I faced one situation where I got stucked for few hour . After few Googling I found the silly mistake I was making.

I am just trying to generate the same situation with some sample code below.

<cfset local.qryGetArtists = queryNew("") />
<cfset local.qryGetSelctedArtist = queryNew("") />

<cfquery name="local.qryGetArtists" datasource="cfartgallery">
    SELECT ARTISTID, FIRSTNAME, LASTNAME, EMAIL, PHONE FROM ARTISTS
</cfquery>
<cfdump var="#local.qryGetArtists#">

<cfquery name="local.qryGetSelctedArtist" dbtype="query">
    SELECT * FROM local.qryGetArtists WHERE LOWER(LASTNAME) LIKE '%#lCase("Buntel")#%'
</cfquery>
<cfdump var="#local.qryGetSelctedArtist#">

In this code, the first dump give the query Object which contains the list of artist details and after that I am just filtering that query object using Query Of Queries(QoQ) and dumping that result.

For the second dump, I got one big error message:


"Query Of Queries syntax error. Encountered "local"

Where and Why We Got This Error?
The error is due to the local scope as per the error message then why this error. In ColdFusion there are some reserved keywords are there and we can't use that reserve keyword  inside Query Of Queries directly.

This is also mentioned in the ColdFusion Documentation here.

How To Solve This Issue?

The solution is escape the reserve keyword like this:

"SELECT * FROM [local].qryGetArtists WHERE LOWER(LASTNAME) LIKE '%#lCase("Buntel")#%'"


So, the final Code for the Query Of Queries will be like below.

<cfquery name="local.qryGetSelctedArtist" dbtype="query">
    SELECT * FROM [local].qryGetArtists WHERE LOWER(LASTNAME) LIKE '%#lCase("Buntel")#%'
</cfquery>
<cfdump var="#local.qryGetSelctedArtist#">


For more details about Query Of Queries go to the ColdFusion live document. (ColdFusion Query Of Queries)

Friday, June 08, 2012

Extract Tar File In ColdFusion

In ColdFusion it doesn't have any built in function to extract a ".tar" or any ".gz" Compression. For ".gz" compression I have already posted a tip. Now, I am just going to give show you a ColdFusion function using which can be used to extract tar files. This function uses Apache java library to extract the ".tar" file.
Lets start:

<cfscript>
    /*
    @inTarFilePath = input tar file absolute path
    @outExtractPath = The out put directory to which the files will be extract.
    
    Example:
    tarExtract("D:\Temp\pics-leaserental-20120530.tar", "D:\Temp\TarResult\");
    */
    function tarExtract(inTarFilePath, outExtractPath)
    {
        var fileInStream = createObject("java", "java.io.FileInputStream");
        var fileOutStream = createObject("java", "java.io.FileOutputStream");
        var tarInStream = createObject("java", "org.apache.tools.tar.TarInputStream");
        var tarEntry = createObject("java", "org.apache.tools.tar.TarEntry");
        var destFileName = "";

        try{

            //Read tar stream
            fileInStream.init(arguments.inTarFilePath);
            tarInStream.init(fileInStream);
            tarEntry = tarInStream.getNextEntry();

            //Loop each tar entry and write in the specific directory
            while(! isNull(tarEntry)){
                destFileName = arguments.outExtractPath & '/' & tarEntry.getName();

                //If the tar entry is not a directory(means it is a file) then write write that in a file
                if(! tarEntry.isDirectory()){
                    fileOutStream.init(destFileName);
                    tarInStream.copyEntryContents(fileOutStream);
                    fileOutStream.close();
                }
                tarEntry = tarInStream.getNextEntry();

            }

            //Close all opened file streams
            fileInStream.close();
            tarInStream.close();

            return true;
        } catch(Any e){
            return false;
        }
    }
</cfscript>


Hope it will help you in sometime...

Followers