Using the CFC Proxy

ColdFusion MX 7 Updater

September 27, 2005

About the CFC proxy

Starting with the ColdFusion MX 7 Updater, you can call ColdFusion Components (CFCs) from Java classes using the CFCProxy. To call a CFC, you need to be within the ColdFusion web application. This means that ColdFusion's classloader must be the current classloader.

CFCProxy API

Constructors

CFCProxy(String str)
Creates a CFCProxy from the given file location.

Arguments

String str - The fully qualified path of the CFC file.


CFCProxy(String str initialThis)
Creates a CFCProxy from the given file location.

Arguments

String str - The fully qualified path of the CFC file.

initialThis - Map of name-value pairs to populate the initial This scope of the CFC.


invoke(String method, Object[] args): Object

Invokes the specified CFC method. Pass method arguments using the args array.

Returns

The result of the CFC method call. You must cast this value to the data type returned by the CFC method.

Arguments

Object[] args - Argument values for the CFC in the order in which they are declared in the CFC.


invoke(String method, Object[] args, HttpServletRequest request, HttpServletResponse response): Object

Invokes the specified CFC method, passing request and response objects. Pass method arguments using the args array. Use this method if you are calling the CFC method from a Java Servlet.

You can retrieve the request and response arguments in your servlet through the ServletContext object.

Returns

The result of the CFC method call. You must cast this value to the data type returned by the CFC method.

Arguments

Object[] args - Argument values for the CFC in the order in which they are declared in the CFC.

HttpServletRequest request - The request object.

HttpServletResponse response - The response object.


invoke(String method, Object[] args, HttpServletRequest request, HttpServletResponse response, OutputStream out): Object

Invokes the specified CFC method, passing request, response, and output stream objects. Pass method arguments using the args array. Use this method if you are calling the CFC method from a Java Servlet and you wish to capture the output of the CFC method (not what is returned by the CFC method).

You can retrieve the request and response arguments in your servlet through the ServletContext object.

Returns

The result of the CFC method call. You must cast this value to the data type returned by the CFC method.

Arguments

Object[] args - Argument values for the CFC in the order in which they are declared in the CFC.

HttpServletRequest request - The request object.

HttpServletResponse response - The response object.

OutputStream out - The output of the CFC.


inInvoke()

Determines whether this thread is in the midst of an invoke..

Returns

Boolean

Arguments

None


getThisScope(): java.util.Map

Retrieves the This scope of the CFC.

Returns

java.util.Map, which contains variable names and variable values.

arguments

None


setThisScope(java.util.Map map)

Sets the This scope of the CFC.

Returns

Void

arguments

java.util.Map map - Contains variable names and variable values.


getMethod(String method): coldfusion.runtime.UDFMethod

Returns the coldfusion.runtime.UDFMethod for the specified method. This can be used to get more information about this method. Do not use the invoke or callFunction in UDFMethod. Use CFCProxy.invoke() instead. Returns null if the method doesn't exist.

Returns

coldfusion.runtime.UDFMethod

Arguments

String method - The method for which CFCProxy returns information.


flush()

Flush the CFC's writer.

Returns

Void

arguments

None


createContext(HttpServletRequest req HttpServletResponse resp OutputStream os)

Create FusionContext to use across multiple invokes, instead of having to set up a new FusionContext for each call. You can set the Fusioncontext with FusionContext.setCurrent().

Returns

FusionContext

Arguments

HttpServletRequest req - The request object.

HttpServletResponse resp - The response object.

OutputStream os - The output of the CFC.


setAutoFlush(Boolean flush)

Specifies whether to automatically flush the CFC's writer at the end of each invoke. The default value is true.

Returns

Void

Arguments

Boolean flush - Set the value to true to automatically flush the CFC writer.


 

Writing and compiling the Java class

Use the sample, later in this document as a guide. To compile the Java class, include the cfusion.jar file and other necessary JAR files in the classpath, as the following example shows:

C:\CFusionMX7\cfcproxytest>javac -classpath "C:\CFusionMX7\lib\cfusion.jar;C:\CFusionMX7\wwwroot\WEB-INF\lib\cfmx_bootstrap.jar;C:\CFusionMX7\runtime\lib\jrun.jar" *.java

Configuring ColdFusion MX

To communicate with the CFC, the Java class must be in ColdFusion's Java classpath. The steps you perform to do this, vary depending on your configuration:

Sample Java class

The following sample Java class passes a single string argument to a CFC:

import coldfusion.cfc.CFCProxy;
import coldfusion.bootstrap.BootstrapClassLoader;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
 *
 */
   public class CFCInvoker
   {
     public coldfusion.runtime.Struct invoke()
     {
       coldfusion.runtime.Struct map = null;
       try {
   
         //declare variable for storing reference to CFCProxy
         CFCProxy myCFC;
   
         //instantiate CFC
         myCFC = new CFCProxy("C:\\Inetpub\\wwwroot\\skunkworks\\cfcproxytest\\javacall.cfc");
   
         //Build arguments array
         Object[] myArgs = {"Java invocation working"};
   
         //invoke method from CFC. It returns a structure.
         map = (coldfusion.runtime.Struct) myCFC.invoke("getData", myArgs);
   
       } catch (Throwable ex) {
       ex.printStackTrace();        }
   
     return map;
  }
  

Sample CFC

The following example shows javacall.cfc, which is called in the previous example:

<cfcomponent>
  <cffunction name="getData" returntype="struct">
    <cfargument name="msg" required="Yes">
     <cfscript>
       st = StructNew();
       st.name = "michael";
       st.email = "remin@macromedia.com";
       st.date = "#now()#";
       st.message = msg;
       return st;
     </cfscript>
   </cffunction> 
 </cfcomponent>