Showing posts with label CFML. Show all posts
Showing posts with label CFML. Show all posts
I find it handy to dump out a scope and email it to myself. I use this occasionally in Application.cfc in onError() or onMissingTemplate() to dump cgi and the exception variables.

This weekend I was working on a small ColdFusion project and am using cf9. All my cfc's are in script and I wanted to dump full scopes and email them back to myself. Its not as simple as in tags but its a workable solution using savecontent.

I really expected this to just work:

mail.addPart( type="html", charset="utf-8", body=writedump(application) );

But it wont so here is the solution:

Here is the solution:



The other exception to my project was one cfc had to have a udf for cfexecute which also is not yet in cfscript.


Update.

To answer Steves question here is the code example.

You can just attach the report as a file attachment and specify the content.

I just got a new laptop and have been setting up my local dev environment. After installing IIS and ColdFusion ( 8 and 9 ) I wanted to bump the JDK past the class loader issues with pre 1.6.0_10. I figured I'd go with the most recent ( 1.6.0_18 )

This is a simple process and I'll outline it here. This is the same on Win XP, 2003, Vista, Windows 2008 and Windows 7.

Download the JDK: http://java.sun.com/javase/downloads/index.jsp

Choose the appropriate edition ( I'm running 64bit Windows and CF8 ) so 64 bit edition for me. Make sure you get the JDK not the JRE.

Install the JDK and take note of the install directory. I left it all as default and the install folder is: C:\Program Files\Java\jdk1.6.0_18

Browse to you CFADMIN and select Java and Java Settings:



Replace the Java Virtual Machine path with C:\Program Files\Java\jdk1.6.0_18\jre

Save this and restart your CouldFusion services.

Log back into the CFADMIN click Settings and Summary and confirm the java version is now 1.6.0_18.

 
And that's it.
Ever wanted to save an image to a database with ColdFusion? I see this question asked quite often. I don't have a need to do this but I mocked up a quick demo or reading and writing binary data to and from a mySQL database.

The code is very simple. I read the binary data from a image. Write it to a database, read it back out and write it to the browser. I also rotate the image to show the result from the database acts just like any other image data.

You might note I made use of 2 great new features of ColdFusion 9.

1. Data source specific to the application. I no longer need to specify a datasource in my cfquery tags as I have this setup in Application.cfc.

2. On insert ColdFusion can return the autoincrement id value of the record just inserted. Eg in SQL server select @@identity does the same thing. This was available in CF8 but it was lacking and was different depending on the database type.

In ColdFusion 9 the old values still work ( so code wont break ) but we also can now use queryResult.generatedKey.

I always try include a working demo: http://railo.kukiel.net/imageinsert/

Here is the CF Code:

Here is a sample to create the mySQL table:


Also note as this gets alot of people that by default CFadmin only allows 64k to be passed from CF to the database. You will probably need to increase this value. For this example I just added 2 extra 0's ( 6.4meg ). If you don't half the image will be inserted and you will be confused as to what the image ( or other binary files ) are corrupted.


Download and install java sdk ( I used 1.6.17 ) ( note you need the JDK not JRE )
Download glassFish
Download ColdFusion 8 or 9
Download Apache

I used Windows Server 2008 64bit but the process is the same for 32bit or Win 2k3.  It's also ( obviously ) different on linux or unix but not majorly.

When installing glassFish did not detect java home correctly so I provided the location via the cmd line.


The install is very straight forward I did not select anything unusual and just clicked through:

Once glassFish was installed I started the server:



Then browse to:  http://127.0.0.1:4848 to get to the admin console.


I choose to leave the admin with no authentication so it will just load to the console.

Before you look at the console we will need to create a ColdFusion war.  Run the ColdFusion installer and select "JEE Configuration"

This will produce the war file ready for deployment.  For me the output was c:\coldFusion9\cfusion.war

Now go back to the control panel.  Select "Applications" from the menu.  Then select "Deploy".  Choose a file from the local file system and leave the name as cfusion.


Once the application is deployed we can launch it by clicking launch button and you will see:

404 error's.  This is because there are no .cfm files if you went to /CFID/administrator you would would be at the CFADMIN.

I'll skip the CFADMIN and make a index.cfm:

Contaning:





Reload the Original page that was launched by clicking "Launch"

And ColdFusion 9 is running on GlassFish.

This is on port 8080 but I want it to run behind apache on port 80 without the /cfusion

Make sure IIS is not running. Install Apache.  Apache for windows come with all the modules you're likely to need but most are commented out.  Test to make sure Apache is runnign and http://127.0.0.1 should the standard message "It works".

Now we will setup the proxy.

Around line 110 of httpd.conf uncomment 2 modules mod_proxy.so and mod_proxy_connect.so



Also un-comment


Open {apacheInstallDir}\conf\extrahttpd-vhosts.conf

create a new vhostBlock


I created a dummy entry in the Windows host file so http://cf9gf resolves locally. I'll be using this as the test domain.

Restart apache and look for errors.


If there are no errors you should now be ready to proxy from apache to glassFish on port 80.


Done.

Now GlashFish is no longer listed here: http://www.adobe.com/products/coldfusion/systemreqs/ but it still works fine.

Also note the /flex2gateway works fine aswell.
Ever needed to compare a ColdFusion hash() directly in MS SQL? I did today and I knew I had accomplished this before but forgot how to do it. Searching the net and twitter I came to some dead end or longish solutions. I knew Steve Good and I worked this out once before but I also remember there being an easier way.

Well there is and here is where I took the solution from: http://orangepips.instantspot.com/blog/Database

The funny thing is I found this nearly a year ago and actually commented on the post just forgot all about it.





The result:

I put this together almost immediately after CF9 was released but even before then I had some people contact me to ask how CF9 runs on both Windows 7 and Windows server 2008.

Answer?

It works fine on both.

If you have ever tried to install ColdFusion 7/8 on Windows Vista after Service pack 1, Windows 7 or Windows server 2008 with IIS at one stage you have undoubtedly come across this:

"HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration.
If the page is a script, ad a handler. If the files should be downloaded, add a MIME map."


ColdFusion 9 error

Well with CF9 and Windows Server 2008 your going to get the same issue. It's actually an easy fix and almost identical to the Windows 7 / Vista solution just visually a little different here is a walk through of the installation. Note this is CF9 64 bit on Windows Server 2008 64bit and just in case you missed it 64bit is now available in ColdFusion standard:


High Quality video click here
.



BTW this will also work for CF8.

Oh and if your trying to run CF9 32 bit on Windows server 2008 64bit again don't forget 64 bit is part of standard.
Java and ColdFusion ( well CFML including OpenBD and Railo ) work really well together as you would expect. However there is not a lot of information out there about exactly how to create (or use) existing Java within ColdFusion, which is whyy I decide to make a simple example and a step by step post of how to create java code and execute it from ColdFusion.

All the files use are available for download at the bottom of the post.

You don't need alot of tools. Initially I created the whole demo with notepad++ and compiled the .java files from the CLI but its easier to manager if you use eclipse. The only real component you need is a jdk you can confirm you have this installed by running the following:
javac -version

notice its javac not java as we need to be able to compile .java code. I completed the example on Windows but it will be the same on any operating system.

javaVersion

If you do not have a JDK installed you can download it here Sun JDK

Launch Eclipse, create a project and right click src and select “new” and then “package”. Create com.transport

This is the package structure for this example. In transport you then make 3 new class's Vehicle.java, Car.java and Bicycle.java

eclipse

Vehicle.java:

javaVehicle

Car.java:

javaCar

Bicycle.java:

javaBicycle

I also made a Demo.java to test the code:

javaDemo

You can run the Demo.java from withing eclipse to see if you made any errors, the result will be displayed in the console:

javaConsoleOutput

You can now export the code as a jar.

To extract to a jar to use with ColdFusion, Railo or OpenBD:

Select the .java files you want ( Vehicle, Car, Bicycle ) right click and select “Export” , expand “Java” and select “Jar file” you can export this to any location and jar name. As I have ColdFusion running locally I exported directly to c:\ColdFusion8\lib as paul.jar

( on railo this location is: /var/www/virtual/sitename.com/WEB-INF/railo/lib/ basically the /WEB-INF/railo/lib/ under your webcontext you want access to the jar )

You need to restart ColdFusion to pick up the file.

One CF restarts it should have access to the java class’s we just made:

Create a new .cfm page and we can now create java objects of the classes we just made directly from ColdFusion:

cftojavamethods

The output:

cftojavamethodsresults

I wrote the same code as a cfc to show the comparison:
( I used CF8 so I write the components in tags )

Vehicle.cfc:

cfVehicle


Car.cfc:


cfCar

I then made a page to create a few of each object type ( cf and java ) and show how the code is almost identical.

javacfspeed

I then increased the loops to 10k to see the results:

cfVJavaObjects

Yes java objects are much faster then ColdFusion.

The point of the post is to show how easy it is to jump between java and CF and if you have algorithms, class's or any code that's already built in java you can easily leverage this in ColdFusion.

Also note that I compiled the code on Windows 7 64 bit and tested on ColdFusion 8 I then dropped the jar onto my linux VPS running Railo without a single change in code or recompiling the java code.

Here is the speed test running on ColdFusion9 under Ubuntu: http://demo.kukiel.net/java

Here are the files: DemoFiles.Zip
I still get asked about installing ColdFusion 8 on Windows 7 and Vista from time to time I remember when Vista SP1 came out and ColdFusion would no longer just "install" like it should and it took a bit of tinkering to come up with the solution. Dale Fraser worked this out for us and it works perfectly on Vista and Windows 7 and Windows Server 2008. This is also the same process for ColdFusion 9.

A little while ago I also created a step by step walk through and here it is: Installing ColdFusion on Windows 7 </ a>

Once that is setup you might like to create addition HOST entries so you access your sites locally via host name rather then http://localhost/site1
Steve Withington has a walkthrough on the process here.

There is sound to this as I step through the process.

If you are interested in Windows Server 2008 and ColdFusion 9 install here is a walk through: http://vc.kukiel.net/cf9win2k8

cfmail is script is easy but as the docs are still a bit lacking and there is no mention of the attributes for using mail in script I had a look in the CF9 install directory and in

/opt/ColdFusion9/CustomTags/com/adobe/coldfusion

You will find the cfc's Adobe made to extend script support. I opened up the mail.cfc and worked out how to use the function. Here is a small example.

Of course to, from, subject etc can be strings they don't have to be variables.
ColdFusion 9 is now officially supported on Ubuntu 9.04 I think this is great news. For a while we knew that it did work really easily on Ubuntu and Debain but it's now good to know its officially supported. You can see Adobes ColdFusion 9 Requirements here

Ubuntu is great but for some simple testing is can be quite heavy. If you have been thinking about trying ColdFusion 9 on Linux I might recommend Debian. I like the Debian net install because it's a minimal install of a basic system. The download is ~150meg and installs lean and quickly.

I used VMware EXSi on an old spare PC but Virtualbox, VMware server or Parallels will also work if you don't have a spare machine. Create a new Virtual machine and allocate 512mb ram. For testing and trying the new features this will be totally fine to run the slim Operating system and ColdFusion 9 even mySQL. When you start configuring your virtual machine make sure the network is set to "Bridged" so its connected directly to the physical network so it gets a real IP address from your DHCP server or router.

After the install there are a few commands and updates I usually run:

#Update the apt repository's
apt-get update

#insall some additions to VI ( simple text editor ) that make is more
#usable on Debian
apt-get install vim-nox

#install a ssh server so we can connect over putty and over sftp with ColdFusion #builder
apt-get install ssh

#you may wish to install apache even if you don't its impressive how easy it is
#with apt
apt-get install apache2

Run ifconfig ( if not ip as in windows ) to find the ipaddress ( presuming you selected dhcp )

At this point I drop out of a direct console session and continue the install with putty.

Download the linux .bin file ( 64 or 32 bit depending on the edition of debian you choose )

Use scp to move the file across. As my main machine is currently running Windows 7 I use CoreFTP which has scp support. You can just drop the .bin file to the root home directory.

Start up putty and ssh to the ip of the VM and log in.

The rest of the install I made a short screen capture pausing out the delays.



Once ColdFusion is installed and started you can test it out. In this short screencast I complete the install and log into the ColdFusion admin and setup a connection over SFTP in ColdFusion Builder create a test file and make sure CF9 is working.



This is a simple testing environment so I paid no attention to security and simply logged in as root. This is a simple demonstration how in less then half an hour you can have a slim OS running ColdFusion 9 in linux and edit your files from ColdFusion Builder. This example also uses the inbuilt ColdFusion webserver rather then connecting to apache which is also an option as part of the CF9 install.


Added an entry on installing mySQL here: