我正在尝试使javaLoader在Coldfusion8应用程序中运行,我需要一些帮助才能使我越过终点。
这是我到目前为止所拥有的:
内部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>
这是按照here和here中的说明进行的。
在我的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实例时,脚本将失败。
题:
我很高兴我做到了这一步,但是现在我已经坐了几个小时了,不知道我在做什么错。希望有更多见识的人能为我指明方向?
感谢帮助!
最佳答案
好。有几个错误。
要使用Coldfusion8和BCrypt或您选择的Java类设置Javaloader,请执行以下操作:
1)将任何Java类(.java文件,而不是.class文件)放在webroot / 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
关于java - 如何在Coldfusion8中使用javaloader设置Java库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10970133/