Upload
tamsyn-kelley
View
218
Download
1
Tags:
Embed Size (px)
Citation preview
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Drinkin’ Cold CoffeeDrinkin’ Cold Coffee
-or--or-
How to use Java Objects from How to use Java Objects from ColdFusion.ColdFusion.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
What are you going to What are you going to learn?learn?
Why you might want to use Java objects Why you might want to use Java objects from ColdFusion.from ColdFusion.
How to instantiate Java objects from How to instantiate Java objects from ColdFusion MX.ColdFusion MX.
How to call the constructors of Java objects.How to call the constructors of Java objects. How to call methods on Java objects.How to call methods on Java objects. The advantages of using Java (as opposed The advantages of using Java (as opposed
to CFX tags, etc)to CFX tags, etc) Potential problems and their solutions.Potential problems and their solutions.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
What can you do with Java?What can you do with Java?
Use strong encryption.Use strong encryption. Manipulate Images.Manipulate Images. Edit audio clips.Edit audio clips. Zip and Unzip files.Zip and Unzip files. Create network socket connections.Create network socket connections. Low level programming.Low level programming. Almost anything you can do with the Java Almost anything you can do with the Java
language.language.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Why do you care about Why do you care about Java?Java?
ColdFusion is written in JavaColdFusion is written in Java Macromedia has been pushing CF deployment Macromedia has been pushing CF deployment
on J2EE serverson J2EE servers In short, an easy way to write J2EE applications.In short, an easy way to write J2EE applications.
In short, ColdFusion is a friendly version of In short, ColdFusion is a friendly version of Java.Java.
ColdFusion provides an easy way to use Java ColdFusion provides an easy way to use Java without requiring extensive knowledge of Java.without requiring extensive knowledge of Java.
Blackstone may deploy ColdFusion Blackstone may deploy ColdFusion applications as WAR files on J2EE.applications as WAR files on J2EE.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Where to find the Java DocsWhere to find the Java Docs
http://java.sun.com/j2se/1.4.2/docs/ahttp://java.sun.com/j2se/1.4.2/docs/api/index.htmlpi/index.html
The hardest part of using Java is The hardest part of using Java is learning how to read the learning how to read the documentation.documentation.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
How to invoke Java objectsHow to invoke Java objects
CreateObject()CreateObject()<cfset myZip = CreateObject("Java", <cfset myZip = CreateObject("Java", "java.util.zip.ZipFile") />"java.util.zip.ZipFile") />
<cfobject><cfobject><cfobject<cfobject
type="Java"type="Java"action="Create"action="Create"class="java.util.zip.ZipFile"class="java.util.zip.ZipFile"name="myZip" />name="myZip" />
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Typing in Java vs. Typing in Java vs. ColdFusionColdFusion
Java is a strongly typed languageJava is a strongly typed language This means the language always knows what This means the language always knows what
type a variable istype a variable is ColdFusion is a typeless language. ColdFusion is a typeless language.
This means any variable can be a string, This means any variable can be a string, integer, query, whatever.integer, query, whatever.
Because ColdFusion is typeless, CF has to Because ColdFusion is typeless, CF has to guess what type of data you are passing guess what type of data you are passing into Java object methods.into Java object methods.
If ColdFusion passes an incorrect type into If ColdFusion passes an incorrect type into a Java method you will get errors.a Java method you will get errors.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
An example typing errorAn example typing error
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Use JavaCast() to type CF Use JavaCast() to type CF variablesvariables
You can use JavaCast() to type data You can use JavaCast() to type data being passed into Java methods.being passed into Java methods.
Casts data from ColdFusion to Java.Casts data from ColdFusion to Java. Intended for calling overloaded Java Intended for calling overloaded Java
Methods.Methods. Syntax:Syntax:JavaCast(JavaCast(typetype, , variablevariable))
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
How to init Java objectsHow to init Java objects
Most Java objects require that you Most Java objects require that you call a constructor to configure call a constructor to configure objects for use.objects for use.
Use “.init()” to call constructors on Use “.init()” to call constructors on Java objects.Java objects.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Calling Object ConstructorsCalling Object Constructors
To instantiate a Java zipFile object you have To instantiate a Java zipFile object you have to call one of three constructorsto call one of three constructors http://java.sun.com/j2se/1.4.2/docs/api/java/util/zhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/z
ip/ZipFile.htmlip/ZipFile.html
Example:Example:<cfset pathToZip = "c:\example.zip" /><cfset pathToZip = "c:\example.zip" /><!--- create the zipFile object ---><!--- create the zipFile object ---><cfset myZip = CreateObject("Java", <cfset myZip = CreateObject("Java", "java.util.zip.ZipFile") />"java.util.zip.ZipFile") />
<!--- call a constructor on the zipFile object ---><!--- call a constructor on the zipFile object ---><cfset myZip.init(JavaCast("string",pathToZip)) /><cfset myZip.init(JavaCast("string",pathToZip)) />
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
What happens when you don’t What happens when you don’t init an object?init an object?
<cfset FileInputStream = CreateObject("Java", <cfset FileInputStream = CreateObject("Java", "java.io.FileInputStream") />"java.io.FileInputStream") />
<cfset FileInputStream.read() /><cfset FileInputStream.read() />
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
What happens when you don’t What happens when you don’t init an object?init an object?
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
What happens when there are What happens when there are other problems initing your other problems initing your
object?object?
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Calling Java Object methodsCalling Java Object methods
We can call methods of the myZip We can call methods of the myZip object to perform various actions.object to perform various actions. http://java.sun.com/j2se/1.4.2/docs/api/jhttp://java.sun.com/j2se/1.4.2/docs/api/j
ava/util/zip/ZipFile.html#getInputStreamava/util/zip/ZipFile.html#getInputStream(java.util.zip.ZipEntry)(java.util.zip.ZipEntry)
For instance:For instance:<cfoutput>The Zip File "#myZip.getName()#" contains #myZip.size()# files.
</cfoutput>
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Another (more complex) Another (more complex) exampleexample
Get a list of files in the myZip objectGet a list of files in the myZip object
<!--- returns an Enumeration object ---><!--- returns an Enumeration object ---><cfset zipFiles = myZip.entries() /><cfset zipFiles = myZip.entries() />
<!--- loop while there are elements in the Enumeration ---><!--- loop while there are elements in the Enumeration ---><cfloop condition="#zipFiles.hasMoreElements()#"><cfloop condition="#zipFiles.hasMoreElements()#">
<cfset myZipEntry = zipFiles.nextElement() /><cfset myZipEntry = zipFiles.nextElement() /><cfoutput><cfoutput>
#myZipEntry.getName()##myZipEntry.getName()#(#myZipEntry.getSize()# Bytes)<br>(#myZipEntry.getSize()# Bytes)<br>
</cfoutput></cfoutput></cfloop></cfloop>
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Results so farResults so far
<cfset pathToZip = "c:\example.zip" /><cfset pathToZip = "c:\example.zip" /><cfset myZip = CreateObject("Java", <cfset myZip = CreateObject("Java",
"java.util.zip.ZipFile") />"java.util.zip.ZipFile") /><cfset myZip.init(JavaCast("string", <cfset myZip.init(JavaCast("string",
pathToZip)) />pathToZip)) />
<cfoutput><cfoutput><p>The Zip File "#myZip.getName()#" contains <p>The Zip File "#myZip.getName()#" contains
#myZip.size()# files.</p>#myZip.size()# files.</p></cfoutput></cfoutput>
<!--- returns an Enumeration object ---><!--- returns an Enumeration object ---><cfset zipFiles = myZip.entries() /><cfset zipFiles = myZip.entries() />
<!-- loop while there are elements in the <!-- loop while there are elements in the Enumeration --->Enumeration --->
<cfloop <cfloop condition="#zipFiles.hasMoreElements()#">condition="#zipFiles.hasMoreElements()#"><cfset myZipEntry = <cfset myZipEntry = zipFiles.nextElement() />zipFiles.nextElement() /><cfoutput><cfoutput>
#myZipEntry.getName()# #myZipEntry.getName()# (#myZipEntry.getSize()# Bytes)<br>(#myZipEntry.getSize()# Bytes)<br></cfoutput></cfoutput>
</cfloop></cfloop>
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Using <cfdump> to get more Using <cfdump> to get more information on Java Objectsinformation on Java Objects
You can use <cfdump> on any ColdFusion You can use <cfdump> on any ColdFusion variable.variable.
If the variable a Java object you get:If the variable a Java object you get: The object type nameThe object type name The object’s methodsThe object’s methods The object’s propertiesThe object’s properties
Example:Example:<cfset FileInputStream = CreateObject("Java", <cfset FileInputStream = CreateObject("Java",
"java.io.FileInputStream") />"java.io.FileInputStream") />
<cfset FileInputStream.init("c:\Collin 4 copy.jpg") /><cfset FileInputStream.init("c:\Collin 4 copy.jpg") />
<cfdump var="#FileInputStream#"><cfdump var="#FileInputStream#">
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Results from the dumpResults from the dump
Can be helpful in Can be helpful in figuring out what an figuring out what an object is and what object is and what methods it provides.methods it provides.
Helps support the Helps support the Java documentation.Java documentation.
Another dump Another dump example: example: http://http://alagad.com/JavaFromalagad.com/JavaFromCF/cfdump.cfmCF/cfdump.cfm
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Wrapping Java in CFCsWrapping Java in CFCs
You can use CFCs to wrap Java You can use CFCs to wrap Java objects.objects.
Provides easier access to complex Provides easier access to complex Java objects.Java objects.
Extremely reusable and cross Extremely reusable and cross platform.platform.
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Let’s look at a simple Zip Let’s look at a simple Zip CFCCFC
Create Zip filesCreate Zip files Add files to Zip filesAdd files to Zip files
Return a query of all the files in a Zip Return a query of all the files in a Zip filefile NameName If the entry is a directoryIf the entry is a directory Uncompressed sizeUncompressed size Compressed sizeCompressed size
Extract files from the zipExtract files from the zip
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Alagad Image ComponentAlagad Image Component
A good example of wrapping Java in CFCs.A good example of wrapping Java in CFCs. 100% native ColdFusion Component (CFC) 100% native ColdFusion Component (CFC)
used to create and manipulate image files used to create and manipulate image files by invoking and calling methods on Java by invoking and calling methods on Java objects.objects.
Over 60 methods for working with Images.Over 60 methods for working with Images. Platform independent.Platform independent. Makes lots of use of Java from within Makes lots of use of Java from within
ColdFusion.ColdFusion. http://alagad.com/index.cfm/name-aichttp://alagad.com/index.cfm/name-aic
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
Advantages of using Java in Advantages of using Java in ColdFusionColdFusion
Cross PlatformCross Platform Write code on any platform and deploy Write code on any platform and deploy
to any platform.to any platform. The power of Java at your fingertips.The power of Java at your fingertips. It’s Free!It’s Free! It’s not CFX!It’s not CFX!
Doug Hughes, Alagad Inc. Doug Hughes, Alagad Inc. http://www.alagad.comhttp://www.alagad.com
What did you learn?What did you learn? Why you might want to use Java objects Why you might want to use Java objects
from ColdFusion.from ColdFusion. How to instantiate Java objects from How to instantiate Java objects from
ColdFusion MX.ColdFusion MX. How to call the constructors of Java objects.How to call the constructors of Java objects. How to call methods on Java objects.How to call methods on Java objects. The advantages of using Java (as opposed The advantages of using Java (as opposed
to CFX tags, etc)to CFX tags, etc) Potential problems and their solutions.Potential problems and their solutions. That you really need to by the Alagad Image That you really need to by the Alagad Image
Component.Component.