2012-06-10 27 views
0

我想讓javaLoader在Coldfusion8應用程序中運行,我需要一些幫助才能讓我穿越終點線。如何在Coldfusion8中使用javaloader設置java庫?

這是我到目前爲止有:

的Application.cfc

... 
THIS.mappings["/javaloader"] = GetDirectoryFromPath(GetCurrentTemplatePath()) & "tools/javaloader"; 
... 

<cffunction name="onApplicationStart" returnType="boolean" output="false" hint="application initalizer"> 
    <cfscript> 
    Application.str = structNew(); 
    Application.str.myJavaLoaderKey = "someUUID_javaloader"; 
    Application.str.jarPaths = arrayNew(1); 
    </cfscript> 
    <cfif (NOT structKeyExists(server, Application.str.myJavaLoaderKey))> 
     <!--- add path to class files to jarPath Array ---> 
     <cfset Application.str.jarPaths[1] = expandPath("/classes/BCrypt.class")> 
     <!--- this will map out to: ...htdocs/classes/BCrypt.class ---> 

     <cfif (NOT structKeyExists(server, Application.str.myJavaLoaderKey))> 
      <cflock name="#Hash(Application.str.myJavaLoaderKey)#" type="exclusive" timeout="10"> 
       <cfset server[Application.str.myJavaLoaderKey] = createObject("component", "javaloader.JavaLoader")> 
       <!--- tried .init(Application.str.jarPaths) here, but didn't do anything ---> 
      </cflock> 
     </cfif> 
    </cfif> 
    <cfreturn true /> 
</cffunction> 

這樣做以下從herehere說明。

在我handler.cfc,我試圖訪問javaloader和BCrypt類,像這樣:

<cfsript> 
    pass = "some_password"; 
    <!--- this is accessible ---> 
    cryptonite = server[Application.str.myJavaLoaderKey]; 
    <!--- now trying to call init() with respective path to create an instance ---> 
    <!--- BREAKS HERE ---> 
    bCrypt = cryptonite.init(Application.str.jarPaths[1]); 

    hashed = bCrypt.hashpw(pass, bcrypt.gensalt());   
</cfscript>        

我可以轉儲cryptonite變量好吧,但是當我嘗試創建的實例BCrypt,腳本失敗。

問題
我很高興我做到了這一步,但我一直沒有什麼線索,我做錯了,現在坐在這幾個小時。希望有更多見解的人可以將我指向一個方向?

感謝您的幫助!

+0

很高興你解決它。但下一次,「腳本失敗」是模棱兩可的:)它是如何失敗的:空白頁面,錯誤,...?總是包含完整的錯誤信息(並且在使用java對象時,也包括完整的'stack trace')。 – Leigh

回答

2

好的。有幾個錯誤。

要設置Javaloader與Coldfusion8和BCrypt或您選擇的Java類,請執行下列操作:

1)把任何Java類(在java文件,而不是.class文件)中的文件夾中根目錄/ htdocs中(Apache)的。我對BCrypt路徑是這樣的:

htdocs/classes/jBCrypt/ 

2)執行相同的javaloader。我國的路徑是這樣的:

htdocs/tools/javaloader/ 

3)在的Application.cfc

<!--- create mapping to javaloder ---> 
<cfscript>   
    THIS.mappings["/javaloader"] = GetDirectoryFromPath(GetCurrentTemplatePath()) & "tools/javaloader"; 
</cfscript> 

<!--- Application start ---> 
<cffunction name="onApplicationStart" returnType="boolean" output="false" hint=""> 
    <cfscript>  
     <!--- store a UUID and emptry path array in Application scope ---> 
     Application.str = structNew(); 
     Application.str.myJavaLoaderKey = "your_uuid_javaloader"; 
     Application.str.jarPaths = arrayNew(1); 
    </cfscript> 
    <!--- check if exists ---> 
    <cfif (NOT structKeyExists(server, Application.str.myJavaLoaderKey))> 

     <!--- put all paths to your .java files here, this is for JBCrypt ---> 
     <cfset Application.str.jarPaths[1] = expandPath("/classes/jBCrypt-0.3")> 
     <cfif (NOT structKeyExists(server, Application.str.myJavaLoaderKey))> 

      <cflock name="#Hash(Application.str.myJavaLoaderKey)#" type="exclusive" timeout="10"> 
       <!--- create javaloader object and init with all submitted paths ---> 
       <cfset server[Application.str.myJavaLoaderKey] = createObject("component", "javaloader.JavaLoader").init(sourceDirectories=Application.str.jarPaths)> 
      </cflock> 
     </cfif> 
    </cfif> 
</cffunction> 

的設置應該是在應用範圍按here。這應該設置所有你現在可以從其他地方引用,像這樣的.java類:

<cfscript> 
    var pass = "a_password"; 
    javaLoader = server[Application.str.myJavaLoaderKey]; 
    // create an instance of javaloader-BCrypt 
    bcrypt = javaLoader.create("BCrypt").init(); 
    // now you can call methods from bcrypt like so: 
    hashed = bcrypt.hashpw(pass, bcrypt.gensalt()); 
</cfscript> 

想通了通過here閱讀。原來你必須參考.java文件,而不是.class文件,這是我最初做的。

以下鏈接也可能會有所幫助:
http://blog.mxunit.org/2011/02/hashing-passwords-with-bcrypt-in.html
http://www.compoundtheory.com/javaloader/docs/
http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/using-bcrypt-in-coldfusion-10-370

+0

..或者你可以把'* .class'文件放在一個'* .jar'中並像往常一樣加載jar。雖然在飛行中編譯'* .java'文件的能力當然是一個很好的功能:) – Leigh

+0

我想我需要的不止是一個白色帶在Coldfusion待辦事項...我會嘗試下一次 – frequent

相關問題