ColdFusion での Java の統合

Java の統合

Java は CFML の中核となるものです。このリリースでは、CFML コードのブロックを含んだ Java クラスを作成し、そのコードを実行することができます。CFML ブロック内で Java オブジェクトのインスタンスを作成し、中核となる Java コンストラクトを書き込むことができます。

スクリプトのシンタックス

<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>
<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>
<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> 

出力

5

タグのシンタックス

<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 --->
<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 --->
<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 ---> 

出力

--new content from java added content from cf

Java インターフェイスの実装と拡張のサポート

コンポーネントおよび ColdFusion インターフェイスで Java インターフェイスの機能を拡張することができます。ColdFusion コンポーネントでは、指定された Java インターフェイスリストを実行時に実装するだけで、他の Java オブジェクトと同様に動作させることができるようになりました。また、ColdFusion インターフェイスで Java インターフェイスを拡張することができます。Java オブジェクトを想定していたメソッドに CFC を直接渡すこともできるようになりました。

使用方法

次のいずれかの方法になります。

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.
}
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. }
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>
<cfinterface extends = "java:java.util.Map"> </cfinterface>
<cfinterface extends = "java:java.util.Map"> 
</cfinterface>
<cfcomponent implements = "java:java.util.Map">
</cfcomponent>
<cfcomponent implements = "java:java.util.Map"> </cfcomponent>
<cfcomponent implements = "java:java.util.Map">  
</cfcomponent>

CFC でインターフェイスまたはインターフェイスリストを実装する場合は、CFC または抽象 CFC の実装対象インターフェイスのすべての抽象メソッドを実装する必要があります。

必ずしもすべての抽象メソッドを実装しない場合は、onMissingMethod を実装するだけでも十分です。

同じ名前と同じパラメーターシグネチャを持つメソッドがクラスの 2 つ以上のインターフェイスに含まれている場合、インターフェイスの順序が重要になります。

例 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();
}
}
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(); } }
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>
<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>
<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>
<cfscript> newObj=new HelloWorld() obj = createObject("java","Case1").init() result = obj.getListAndReturnSize(newObj) writeOutput(result) </cfscript>
<cfscript> 
    newObj=new HelloWorld() 
    obj = createObject("java","Case1").init() 
    result = obj.getListAndReturnSize(newObj) 
    writeOutput(result) 
</cfscript>

出力

53

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

UDF および CFC 関数内の Java コード

タグベースの例

<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>
<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>
<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>
<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>
<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>

ヘルプをすばやく簡単に入手

新規ユーザーの場合