Doug Hughes, Alagad Inc. Drinkin’ Cold Coffee -or- How to use Java Objects from ColdFusion.
Doug Hughes, Alagad Inc. What are you going to learn? Why you might want to use Java objects from ColdFusion. Why you might want to use Java objects from ColdFusion. How to instantiate Java objects from ColdFusion MX. How to instantiate Java objects from 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 to CFX tags, etc) The advantages of using Java (as opposed to CFX tags, etc) Potential problems and their solutions. Potential problems and their solutions.
Doug Hughes, Alagad Inc. 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 language. Almost anything you can do with the Java language.
Doug Hughes, Alagad Inc. Why do you care about Java? ColdFusion is written in Java ColdFusion is written in Java Macromedia has been pushing CF deployment on J2EE servers Macromedia has been pushing CF deployment on 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 Java. In short, ColdFusion is a friendly version of Java. ColdFusion provides an easy way to use Java without requiring extensive knowledge of Java. ColdFusion provides an easy way to use Java without requiring extensive knowledge of Java. Blackstone may deploy ColdFusion applications as WAR files on J2EE. Blackstone may deploy ColdFusion applications as WAR files on J2EE.
Doug Hughes, Alagad Inc. Where to find the Java Docs dex.html dex.html dex.html dex.html The hardest part of using Java is learning how to read the documentation. The hardest part of using Java is learning how to read the documentation.
Doug Hughes, Alagad Inc. How to invoke Java objects CreateObject() CreateObject() <cfobjecttype="Java"action="Create"class="java.util.zip.ZipFile" name="myZip" />
Doug Hughes, Alagad Inc. Typing in Java vs. ColdFusion Java is a strongly typed language Java is a strongly typed language This means the language always knows what type a variable is This means the language always knows what type a variable is ColdFusion is a typeless language. ColdFusion is a typeless language. This means any variable can be a string, integer, query, whatever. This means any variable can be a string, integer, query, whatever. Because ColdFusion is typeless, CF has to guess what type of data you are passing into Java object methods. Because ColdFusion is typeless, CF has to guess what type of data you are passing into Java object methods. If ColdFusion passes an incorrect type into a Java method you will get errors. If ColdFusion passes an incorrect type into a Java method you will get errors.
Doug Hughes, Alagad Inc. An example typing error
Doug Hughes, Alagad Inc. Use JavaCast() to type CF variables You can use JavaCast() to type data being passed into Java methods. You can use JavaCast() to type data being passed into Java methods. Casts data from ColdFusion to Java. Casts data from ColdFusion to Java. Intended for calling overloaded Java Methods. Intended for calling overloaded Java Methods. Syntax: Syntax: JavaCast(type, variable)
Doug Hughes, Alagad Inc. How to init Java objects Most Java objects require that you call a constructor to configure objects for use. Most Java objects require that you call a constructor to configure objects for use. Use “.init()” to call constructors on Java objects. Use “.init()” to call constructors on Java objects.
Doug Hughes, Alagad Inc. Calling Object Constructors To instantiate a Java zipFile object you have to call one of three constructors To instantiate a Java zipFile object you have to call one of three constructors File.html File.html File.html File.html Example: Example:
Doug Hughes, Alagad Inc. What happens when you don’t init an object?
Doug Hughes, Alagad Inc. What happens when you don’t init an object?
Doug Hughes, Alagad Inc. What happens when there are other problems initing your object?
Doug Hughes, Alagad Inc. Calling Java Object methods We can call methods of the myZip object to perform various actions. We can call methods of the myZip object to perform various actions. util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) For instance: For instance: The Zip File "#myZip.getName()#" contains #myZip.size()# files.
Doug Hughes, Alagad Inc. Another (more complex) example Get a list of files in the myZip object Get a list of files in the myZip object <cfoutput>#myZipEntry.getName()# (#myZipEntry.getSize()# Bytes) (#myZipEntry.getSize()# Bytes) </cfoutput></cfloop>
Doug Hughes, Alagad Inc. Results so far <cfoutput> The Zip File "#myZip.getName()#" contains #myZip.size()# files. The Zip File "#myZip.getName()#" contains #myZip.size()# files. </cfoutput> <cfoutput> #myZipEntry.getName()# (#myZipEntry.getSize()# Bytes) #myZipEntry.getName()# (#myZipEntry.getSize()# Bytes) </cfoutput></cfloop>
Doug Hughes, Alagad Inc. Using to get more information on Java Objects You can use on any ColdFusion variable. You can use on any ColdFusion variable. If the variable a Java object you get: If the variable a Java object you get: The object type name The object type name The object’s methods The object’s methods The object’s properties The object’s properties Example: Example:
Doug Hughes, Alagad Inc. Results from the dump Can be helpful in figuring out what an object is and what methods it provides. Can be helpful in figuring out what an object is and what methods it provides. Helps support the Java documentation. Helps support the Java documentation. Another dump example: romCF/cfdump.cfm Another dump example: romCF/cfdump.cfm romCF/cfdump.cfm romCF/cfdump.cfm
Doug Hughes, Alagad Inc. Wrapping Java in CFCs You can use CFCs to wrap Java objects. You can use CFCs to wrap Java objects. Provides easier access to complex Java objects. Provides easier access to complex Java objects. Extremely reusable and cross platform. Extremely reusable and cross platform.
Doug Hughes, Alagad Inc. Let’s look at a simple Zip CFC Create Zip files Create Zip files Add files to Zip files Add files to Zip files Return a query of all the files in a Zip file Return a query of all the files in a Zip file Name Name If the entry is a directory If the entry is a directory Uncompressed size Uncompressed size Compressed size Compressed size Extract files from the zip Extract files from the zip
Doug Hughes, Alagad Inc. Alagad Image Component A good example of wrapping Java in CFCs. A good example of wrapping Java in CFCs. 100% native ColdFusion Component (CFC) used to create and manipulate image files by invoking and calling methods on Java objects. 100% native ColdFusion Component (CFC) used to create and manipulate image files by invoking and calling methods on Java 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 ColdFusion. Makes lots of use of Java from within ColdFusion
Doug Hughes, Alagad Inc. Advantages of using Java in ColdFusion Cross Platform Cross Platform Write code on any platform and deploy to any platform. Write code on any platform and deploy 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. What did you learn? Why you might want to use Java objects from ColdFusion. Why you might want to use Java objects from ColdFusion. How to instantiate Java objects from ColdFusion MX. How to instantiate Java objects from 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 to CFX tags, etc) The advantages of using Java (as opposed to CFX tags, etc) Potential problems and their solutions. Potential problems and their solutions. That you really need to by the Alagad Image Component. That you really need to by the Alagad Image Component.