开发者

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

开发者 https://www.devze.com 2023-03-05 04:41 出处:网络
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 applica

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 Re开发者_JAVA技巧ading @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.


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.


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.


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.


ColdFusion application scopes are handled by the ApplicationScopeTracker java class.

You can access an application scope of another app.

It's undocumented and I wouldn't use it for anything in production!

Lets say you have 2 web apps with the names app1 and app2.

Run this on app1 to access app2's application scope:

<cfscript>
appTracker = createObject( 'java', 'coldfusion.runtime.ApplicationScopeTracker' );
app2 = appTracker.getApplicationScope('app2');
</cfscript>

If the app2 application has not started, or has timed out, getApplicationScope() will return undefined.

You can also get an enumeration of all current application scopes using appTracker.getApplicationKeys()

0

精彩评论

暂无评论...
验证码 换一张
取 消