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.
<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>
<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 --->
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.
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(); } }
<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>
<cfscript> newObj=new HelloWorld() obj = createObject("java","Case1").init() result = obj.getListAndReturnSize(newObj) writeOutput(result) </cfscript>
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(); } }
component implements = "java:java.util.Map" { numeric function size() { return 53.8; } public any function onMissingMethod(string MissingMethodName, struct MissingMethodArguments) { abort; return; } }
<cfscript> newObj=new HelloWorld(); obj = createObject("java","Case2").init(); result = obj.getListAndReturnSize(newObj) writeOutput(result) </cfscript>
<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>
<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>