Notes - ColdFusion

Apache Railo Tomcat Windows installation

February 10, 2012 by Jean

Some notes on our standard Windows + Apache + Railo + Tomcat + Apache (=WART?) install.

Railo can be installed 2 different ways:

  • Common class install: makes Railo global to the Tomcat server. All hosts share the same Railo server context, and have a unique dedicated Railo web context. That's the setup you end up with when using the Railo installers.
  • WAR install: requires a new war deployment for each host, and each host ends up with its own Railo server and web context.

We are going for the war install, to keep the option to run other Tomcat webapps (like solr) on the same server.

In: ColdFusion 

Read more  Comments (0)

Create CF admin mapping

November 3, 2011 by Jean

Using railo admin api to create mapping:

<cfadmin action="updateMapping"
    type="web"
    password="****"
    virtual="/tags"
    physical="#replace('#expandPath('../')#service/utility/tags','\','/','all')#"
    archive=""
    primary="physical"
    trusted="false">

In: ColdFusion 

Comments (0)

Get a host IP address using ColdFusion

December 11, 2010 by Jean

Here is how to retrieve the IP address of a host using ColdFusion:

function getHostIP(host)
{
    var address=createObject("java", "java.net.InetAddress").getByName(host);
    return address.getHostAddress();
}
writeOutput(getHostIP('www.google.com'));

In: ColdFusion 

Comments (0)

Get ColdFusion server host information

December 11, 2010 by Jean

Here is how to get the ColdFusion server host name and IP address:

iaclass = CreateObject("java", "java.net.InetAddress");addr=iaclass.getLocalHost();
hostName = iaclass.localHost.getHostName();
hostName2 = addr.getHostName();
hostIp = addr.getHostAddress();

In: ColdFusion 

Comments (0)

Get ColdFusion locale information

December 11, 2010 by Jean

Getting information about a locale is pretty straightforward:

<cfset pc = getPageContext() />
<cfset setLocale("Chinese (China)") />
<cfoutput>
     <ul>
          <li>Current Locale: #getLocale()#</li>
          <li>Language: #pc.getResponse().getLocale().getLanguage().toString()#</li>
          <li>Country: #pc.getResponse().getLocale().getCountry().toString()#</li>
     </ul>
</cfoutput>

Create an instance of a Java locale:

myLocale = createObject("java","java.util.Locale").init("fr","FR"); 

See all available java.util.Locale methods:

<cfdump var="#createObject("java","java.util.Locale").init("fr","FR")#">

<cfdump var="#getPageContext().getResponse().getLocale()#">

In: ColdFusion 

Comments (0)

Key order in ColdFusion structures

December 10, 2010 by Jean

One issue with ColdFusion structures is that they do not preserve the order of their keys. Considering:

variables.myStruct = structNew();
variables.myStruct.one = 1;
variables.myStruct.two = 2;

The output of:

<cfoutput>#structKeyList(variables.myStruct)#</cfoutput>

is not always the same. Sometimes "one,two" and sometimes "two,one". It depends on server conditions that only unicorns understand. For the rare cases where the order of the keys does matter, the cure is to use a Java hash map:

variables.myStruct = createObject("java", "java.util.LinkedHashMap").init();
variables.myStruct.one = 1;
variables.myStruct.two = 2;

The output of #structKeyList(variables.myStruct)# will always be "one,two" - the order in which the keys have been created.

Notes:

  • Most (if not all) structure functions (structKeyList(), structKeyExists() ...) can be applied to the Java hash maps. YAY!
  • A cfdump of a Java hash map does not respect the order of the keys, but seems to order the keys alphabetically.
  • To be tested, but it looks like Railo preserve the order of keys in structures.

In: ColdFusion 

Comments (4)

Spell out numbers using icu4j

December 9, 2010 by Jean

Needed to spell out numbers (421 → four hundred twenty-one) for printing on receipts. Ended using icu4j, a Java internalization library developed by IBM.

Once the icu4j.jar is available to your ColdFusion class path, something like this should work:

function spellOut(int)
{
locale = createObject("java", "com.ibm.icu.util.ULocale").init('en_US');
ruleBased = createObject("java","com.ibm.icu.text.RuleBasedNumberFormat").init(locale,1); return ruleBased.format(javacast("long", arguments.int),"%spellout-numbering"); }

In: ColdFusion 

Read more  Comments (0)

Get file information

October 30, 2009 by Jean

Get information on a file using Java functions:

variables.myFile = CreateObject("java", "java.io.File");
variables.myFile.init("/path/to/file");
variables.last_modified = myFile.lastModified();

Sometimes more convenient than CFFILE.

In: ColdFusion 

Comments (0)

Get ColdFusion memory usage

October 30, 2009 by Jean

Retrieve the memory usage of the ColdFusion server using Java functions:

variables.runtime = createObject("java","java.lang.Runtime").getRuntime();
variables.freeMemory = variables.runtime.freeMemory() / 1024 / 1024;
variables.totalMemory = variables.runtime.totalMemory() / 1024 / 1024;
variables.percentFreeAllocated = round((variables.freeMemory / variables.totalMemory) * 100);

In: ColdFusion 

Comments (0)

Sleep for a while

October 30, 2009 by Jean

Make the current request sleep for a while:

thread = createObject("java", "java.lang.Thread");
thread.sleep(javaCast("long", 1000*attributes.interval));

In: ColdFusion 

Comments (0)

Client variables purge query

October 30, 2009 by Jean

Queries run by ColdFusion to purge database stored client variables in CDATA:

delete FROM CDATA WHERE CFID in (SELECT CFID from CGLOBAL where CGLOBAL.lvisit < '2008-11-19 14:18:03')

and then in CGLOBAL:

delete FROM CGLOBAL WHERE CGLOBAL.lvisit < '2008-11-19 14:18:03'

In: ColdFusion 

Comments (0)

Set JVM encoding to UTF-8

October 30, 2009 by Jean

Add to the JVM arguments:

-Dfile.encoding=UTF-8

In: ColdFusion 

Comments (0)

Get rid of duplicates in lists

October 30, 2009 by Jean

Regex to remove dupliactes from a list. Not sure where it comes from, but it works:

function DeDupe(list,type)
{
     return reReplaceNoCase(ListSort(list,type),"([^,]+)(,1)*","1","ALL");
}

In: Regex ColdFusion 

Comments (0)