Java integration
Java is core to CFML. In this release, you can create Java classes with a block of CFML code and execute the code. You can instantiate Java objects and write core Java constructs within a CFML block.
Script syntax
<cfscript> classInstance = java{ public class class1{ public int execute () { int[] arr; arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; return arr.length; } } } writeoutput(classInstance.execute()) </cfscript>
Output
5
Tag syntax
<cfjava handle="classInstance" > import java.io.*; public class Harmless{ private String ss = "John"; public Harmless(String s){ this.ss = s; } public void write(String path) throws Exception{ File file = new File(path); file.createNewFile(); FileWriter fr = new FileWriter(file, true); BufferedWriter br = new BufferedWriter(fr); PrintWriter pr = new PrintWriter(br); pr.println("new content from java added " + this.ss); pr.close(); br.close(); fr.close(); } public String read (String path) throws Exception{ File file = new File(path); BufferedReader objReader = null; String strCurrentLine; String cont = ""; objReader = new BufferedReader(new FileReader(path)); while ((strCurrentLine = objReader.readLine()) != null) { cont= cont + "--" + strCurrentLine; } objReader.close(); return cont; } } </cfjava> <cfset classInstance.init("content from cf")> <!--- calling constructor using init()---> <cfset path = ExpandPath('./') & 'temp.txt'> <cfset classInstance.write(path)> <!--- Calling the method write() of Class Harmless ---> <cfoutput>#classInstance.read(path)#</cfoutput> <!--- Calling the method read() of Class Harmless --->
Output
--new content from java added content from cf
Support for implementing/extending Java Interfaces
You can extend the capabilities of the Java interface in Components and ColdFusion interfaces. ColdFusion components can now simply implement a list of Java interfaces specified at runtime and behave like any other Java Object. Also, ColdFusion Interfaces can extend Java Interfaces. Now a CFC can also be passed directly to a method that was expecting a Java Object.
Usage
One of the following ways:
component implements = “java:java.util.List, Test.AnotherCFInterface, java:com.adobe.MyInterface” { // provide mandatory implementation to all abstract method listed in interface OR provide implementation of onMissingMethod here. }
<cfinterface extends = "java:java.util.Map"> </cfinterface>
<cfcomponent implements = "java:java.util.Map"> </cfcomponent>
While implementing an interface or list of interfaces in a CFC, you must provide implementations of all abstract methods of the implemented interface in CFC or Abstract CFC.
If you do not wish to implement all the abstract methods, simply providing an implementation of onMissingMethod would suffice.
When two or more interfaces of a class contain a method with the same name and parameter signature, the order of the interfaces becomes significant.
Example 1
Case1.java
import java.util.List; import java.util.Map; public class TestCase1 { public int getListAndReturnSize(List l){ return l.size(); } public int getMapAndReturnSize(Map l){ return l.size(); } }
HelloWorld.cfc
<cfcomponent implements = "java:java.util.List"> <cffunction name="size" returntype = "Numeric" > <cfreturn 53.8> </cffunction> <cffunction name="OnMissingMethod" access="public" returntype="any" output="false" hint="Handles missing method exceptions."> <!--- Define arguments. ---> <cfargument name="MissingMethodName" type="string" required="true" hint="The name of the missing method." /> <cfargument name="MissingMethodArguments" type="struct" required="true" hint="The arguments that were passed to the missing method. This might be a named argument set or a numerically indexed set." /> <!--- Dump out the arguments. ---> <cfabort /> <!--- Return out. ---> <cfreturn /> </cffunction> </cfcomponent>
test.cfm
<cfscript> newObj=new HelloWorld() obj = createObject("java","Case1").init() result = obj.getListAndReturnSize(newObj) writeOutput(result) </cfscript>
Output
53
Example 2
Case2.java
import java.util.List; import java.util.Map; public class Case2 { public int getListAndReturnSize(List l){ return l.size(); } public int getMapAndReturnSize(Map l){ return l.size(); } }
HelloWorld.cfc
component implements = "java:java.util.Map" { numeric function size() { return 53.8; } public any function onMissingMethod(string MissingMethodName, struct MissingMethodArguments) { abort; return; } }
Test.cfm
<cfscript> newObj=new HelloWorld(); obj = createObject("java","Case2").init(); result = obj.getListAndReturnSize(newObj) writeOutput(result) </cfscript>
Java code inside UDFs and CFC Functions
Tag-based example
<cfset x = custName('John', 'Doe')> <cfoutput>#x#</cfoutput> <cffunction name="custName" type ='java'> <cfargument name="customerID" required="false" restargsource="Path" type="string"/> <cfargument name="name" required="false" restargsource="Path" type="string"/> return new java.lang.StringBuffer(customerID).reverse().toString() + name; </cffunction>
Script-based example
<cfscript> x = custName('John', 'Doe') writeOutput(x) function custName(string customerID, string name) type=”java” { return new java.lang.StringBuffer(customerID).reverse().toString() + name; } </cfscript>