How can I read the default application.datasource from my code?
A dump of application does not show me the datasource, and trying to read application.datasource gives an error.
Try dumping application.getApplicationSettings()
Try re-start the applciation. You can't just dump application and see the datasource. But if you have this set in application.cfc it "should" be working.
component output="false" {
this.name = "MyDemo";
this.datasource = "DemoDB";
}
Edit: Nice Tony I didn't know that.
The this scope in the Application.cfc only persists for the duration of the onApplicationStart(), onSessionStart() onRequestStart() etc.
If you want to expose variables, in your onApplicationStart(), simply store the in the application scope.
application.myVariable = myVariable;
Related
I wrote an application at work that uses a few CFC's- I'm having some errors but I want to actually mail the errors out because the application is live and they are happening rarely.
I can't stop what's currently running to troubleshoot.
Does anyone have any experience/can point me towards doing cfmail inside of a cfc/cfscript? More specifically, inside of a try/catch within cfscript.
We're running ColdFusion 9 which doesn't support <cfmail> inside of <cfscript>.
The mail CFC is built in to CF9 and above. You can use it in cfscript and it shouldn't matter where you implement it - within a try/catch block should be fine in onError.
Adobe has an example of the usage in their documentation for CF9:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSe9cbe5cf462523a0693d5dae123bcd28f6d-7ff9.html
Write a new cfc with a function to send an email and then include that within the try/catch. Something like below will work
sendMail.cfc (missing a lot of things, but this is the structure)
<cfcomponent output="false">
<cffunction name="sendEmail">
<cfargument name="errorMessage">
<cfmail>
<cfdump var="#arguments.errorMessage#">
</cfmail>
</cffunction>
</cfcomponent>
try {
//code here
} catch (any e) {
var sendEmail = new sendMail();
sendEmail.sendEmail(e);
}
Maybe the Title seems bit odd, but my question is straight, how can reinitialize the application with Application.cfm, i know how we do in Application.cfc like
<cfscript>
if(structKeyExists(url, 'reinit')) {
onApplicationStart();
}
</cfscript>
But how in Application.cfm, not sure, please guide
Thanks
Firstly, running onApplicationStart() no more restarts the application than running an onClick() mouse-click event handler causes your mouse button to depress. onApplicationStart() is called as a result of the application starting, not the other way around.
Secondly, Application.cfm has nothing to do with the application lifecycle, it is merely a CFML file that is included at the beginning of every request. it is more closely associated with onRequestStart() than onApplicationStart(): the file is, unfortunately, misnamed. Its counterpart onRequestEnd.cfm demonstrates this.
I presume your requirement here is to re-initialise your application scope, yes? Do you have all your application-scope setting isolated in a specific CFML file, eg: applicationSettings.cfm, and then have logic like this in your Application.cfm:
// Application.cfm
if (!structKeyExists(application, "inited")){
include "applicationSettings.cfm";
}
(then as a last thing in applicationSettings.cfm set application.inited to true).
If so you simply need to modify your condition to include your URL reinit variable, eg:
if (!structKeyExists(application, "inited") || structKeyExists(URL, "reinit")){
include "applicationSettings.cfm";
}
In OnRequestStart() put something like this:
param name='url.reloadApp' default='no';
if(url.reloadApp == 'yes')
{
applicationStop();
}
In the adobe coldfusion 10 documentation, Defining the application and its event handlers in Application.cfc, there is a sample Application.cfc containing the function below. After looking at the code, I am wondering if there is a typo/bug in the following code:
<cffunction name="onSessionStart">
...
<cflock timeout="5" throwontimeout="No" type="EXCLUSIVE" scope="SESSION">
<cfset Application.sessions = Application.sessions + 1>
</cflock>
...
</cffunction>
Should it be:
(A) cflock ... scope="SESSION"
or
(B) cflock ... scope="APPLICATION"
?
If it is (A) then I am confused. Can someone explain why?
This is a duplicate of my answer to the same question asked on the Adobe forums:
Don't be confused... it's an error in the docs. You could do Adobe a
favour by commenting at the bottom of the page: they do monitor those
comments (they don't always react, but the do monitor them).
onSessionStart() is intrinsically single-threaded as far as the
session scope goes: it's only ever run once per session (when the
session starts...). On the other hand the code in question def wants
to single-thread access to that application-scoped variable as we
don't want two simultaneous sessions hitting it for any given single
value of it (if that makes sense).
You always lock the SCOPE that you are writing to. In this case it would be APPLICATION.
My question seems to be related to this one:
Coldfusion memcached connections
however, i have been unable to solve it.
if i put this code in application.cfm:
<cfif not IsDefined("application.memcached")>
<cfset application.memcachedFactory = createObject("component","memcachedFactory").init("192.168.2.91:11211")>
<cfset application.memcached = application.memcachedFactory.getmemcached()>
</cfif>
the page will work for maybe 270 calls. then it will start to fail with an error "Object Instantiation Exception " The code is properly talking to memcached. I can send and receive data. it seems like java is running out of something .. threads, sockets, handles of some sort. I know little about java, and am stuck.
This seems wrong to me Don. Why would this code run again after the very first call? It should be running 1 time after which you have a reference to your object. What does the rest of your application.cfm look like? Have you added a cfapplication tag with a "name"?
If you fail to set an application "name" (via the cfapplication tag or "this.name" in application.cfc), the an "application.x" variable is treated just like a regular variable. After the page request ends it will "go away" and require the next request to reinstantiate the object over again.
The purpose of the "isDefined()" in this case is to insure it runs only once - providing you with a singleton (single reference) you can use again and again without reinstantiating it. It sounds like you are not "inside" an application.
So I found some code
<cfset x509 = GetPageContext().getRequest().getAttribute("javax.servlet.request.X509Certificate") />
<cfoutput>not before = #x509[1].getNotBefore()#</cfoutput><br/>
<cfoutput>not after = #x509[1].getNotAfter()#<br></cfoutput>
<cfoutput>#ToBase64(x509[1].getEncoded())#<br></cfoutput>
<cfoutput>#x509[1].getIssuerDN()#<br></cfoutput>
<cfoutput>#x509[1].getIssuerX500Principal()#<br></cfoutput>
What I want this code to do is display the information from the CAC, instead I am getting an error Variable X509 is undefined...
Edit
An alternate solution seems to be to use CGI.CERT_SUBJECT however I am not exacly sure how to get this variable to be anything other than an empty string. FWIW I am try to get this to work on a standalone coldfusion server.
The getAttribute() method returns a NULL variable when the attribute is not found, which is what's happening in this case. You have to see if it's defined before you can display it.
<cfif StructKeyExists( variables, "x509" )>
{ Code }
</cfif>
On how to get the attribute you're after, I couldn't tell you, but this will prevent the error from occuring.
Depending on your web server (I know Apache does this if you set SSLOptions +StdEnvVars +ExportCertData), you can get the PEM-encoded cert as an environment variable (i.e., cgi.ssl_client_cert) and you can get other info (DN, issuer's DN, etc) as well. Here's a list of the environment variables.