Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access one application scope from a separate application in ColdFusion 9?

I have my own answer to this question, which I'll post, but I wanted to see if I missed a simpler way. I have two application's running on the same coldfusion server, and I want to access the application scope of one from the other. How might I do this?

UPDATE:

After Reading @Daniel and @Ben's answers, I went back and approached my problem from the standpoint of sub-applications which turned out to be a better solution to my initial problem. My answer is still the "quick and dirty" way to access other application scopes, but putting data in the server scope is a better practice.

like image 966
Ryan Lynch Avatar asked May 12 '11 14:05

Ryan Lynch


3 Answers

I think you should probably think about why it is that you want to do this... Architecturally this doesn't seem very sound, even if it is possible. The Server scope would be better for resources that you want to share across applications.

You might even want to consider whether the two applications should actually be one single application with two small sub-applications.

like image 158
Daniel Sellers Avatar answered Dec 01 '22 20:12

Daniel Sellers


I put my answer together from two sources. First, Ben Nadel's Massive Exploration of the ColdFusion PageContext object (Thanks Ben). Second, the ColdFusion help page on Interoperating with JSP pages and servlets. Put the two together and I get this:

Directory structure:

Root
 |_ App1
   |_ Application.cfc
   |_ index.cfm
 |_ App2
   |_ Application.cfc
   |_ index.cfm

App1/Application.cfc:

component
{
 this.name="App1";
 this.application.foo = "bar"
}

App2/Application.cfc:

component
{
 this.name="App2"
}

App2/index.cfm

<cfscript>
writeDump(getPageContext().getFusionContext().getServletContext().getAttribute('App1'))
</cfscript>

After hitting the index.cfm in the App1 directory, you can see the application scope from app1 dumped in the index of app2.

like image 34
Ryan Lynch Avatar answered Dec 01 '22 19:12

Ryan Lynch


I completely agree with @Daniel about the architecture.
@Ryan's answer is a good one.

I thought I would offer the alternative I thought of, which has a few advantages, and a few drawbacks.

Basically, any data to be shared between the apps can be written to the server scope. For example:

// In App 1
application.foo = "bar";
server.sharedData.app1.fpp = "bar";

// In App 2
application.bar = "foo";
server.sharedData.app2.bar = "foo";
// Use shared data from App 1
writeOutput(server.sharedData.app1.foo);

Advantages:

  • simpler syntax
  • shared data available to all applications

Disadvantages

  • You have to remeber to update the server.sharedData scope whenever you update the application scoe.
  • be careful choosing your master struct key. You don't want to overwerite existing server scope data.
  • Shared data is available to all applications. :-)

Anyway, this was my first thought but after reading @Ryan's answer, I'd probaly just write a UDF that takes an application name and a var name, and use it as a facade for what he did.

But seriously, consider whether sharing data across appications is smarter/wiser than merging them.

like image 40
Ben Doom Avatar answered Dec 01 '22 18:12

Ben Doom