Welcome!

AJAX & REA Authors: Lee Novak, Brad Abrams, Alin Irimie, Jonny Defh, RealWire News Distribution

Related Topics: Adobe Flex, ColdFusion

Adobe Flex: Article

Flex and ColdFusion Hybrid Application with ServiceFactory

Levering ColdFusion ServiceFactory to get work done

Whether it's in factories or workshops, in mines or forests, in offices or homes, or even in our sacred ColdFusion Administrator, anything that impairs the efficiency of workers is always worthy of improvements in the process or changing the way of doing things.

Levering ColdFusion ServiceFactory to get work done is one example. Figure 1 is a screenshot of an example Flex-ColdFusion hybrid application that allows manipulation of ColdFusion mapping without having (or even needing) access to the ColdFusion Administrator.

Look at the code. The Flex UI code is shown in Listing 1. The initialize attribute in the main MXML file calls the getIP() function during initialization of the Flex application. This function triggers a RemoteObject invocation of the roMapping.getVisitorIP() method. The getVisitorIP() method is the first function shown in Listing 2. ( Listing 2 can be downloaded from the online version of this article at http://coldfusion.sys-con.com.) By examining the IP address of the user, we can programmatically allow Flex to decide if the CFC mapping utility is visible. This is a way to limit CFC mapping changes to users on your internal network. For instance, in the Flex code, the getVisitorIP_handler has a conditional that ensures that only IP addresses on an internal network that start with 192.168 can call the RemoteObject's method, roMapping.getMappings(). Of course, you can change that to whatever your internal network's IP addresses start with once you download the code. Speaking of the code, the full source code is available at http://labs.insideflex.com/flextraining/cfmapping/bin/srcview/index.html.

Provided the user is coming from the appropriate IP address, the RemoteObject method, roMapping.getMappings(), instantiates a factory object and calls the ColdFusion servers ServiceFactory getMappings method:

<cfset factory=createObject("java","coldfusion.server.ServiceFactory")>
<cfset mappings = factory.runtimeService.getMappings()>

A structure of arrays stores the mapping information returned from the ServiceFactory and is returned to the Flex UI as a structure:

<cfset stMapping = structNew()>
<cfset aLogicalPath = ArrayNew(1)>
<cfset aDirectoryPath = ArrayNew(1)>
<cfloop collection="#mappings#" item="thismapping">
    <cfscript>
       ArrayAppend(aLogicalPath, thismapping);
       ArrayAppend(aDirectoryPath, mappings[thismapping]);
    </cfscript>
</cfloop>
<cfscript>
       StructInsert(stMapping, "logicalpath", aLogicalPath);
       StructInsert(stMapping, "directorypath", aDirectoryPath);
</cfscript>
<cfreturn stMapping/>

Methods for creating, updating, and deleting mappings are also included in the CFC.

Warning
Updating or deleting ColdFusion mappings should be done with caution, especially on shared ColdFusion servers.

Flex Form validation is used and shown in Figure 2 and Figure 3.

For additional information, my Flex Cookbook post, "Get a Client IP Address with a RemoteObject Call," demonstrates limiting access to Flex content on your internal network or by selected IP addresses. It uses the ColdFusion CFML CGI.Remote_Addr variable passed to the Flex UI via AMF. www.adobe.com/cfusion/communityengine/index.cfm?
event=showdetails&postId=3462&productId=2
.

I hope you found this article useful. If you have any follow-up questions, feel free to contact me.

More Stories By Michael Givens

Mike Givens is the CTO of U Saw It Enterprises, a Web technology consulting firm based in Marietta, GA. He is an Adobe Corporate Champion known to share his experience and evangelism of all things Adobe. Certified in both ColdFusion 5 and as an Advanced CFMX Developer, he has been using ColdFusion since the days of Allaire Spectra. For the last 11 years, he has been seen with his head down - deep in the code.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.