Is there any difference between these ColdFusion components? - coldfusion

I know the result is the same but is there any real difference? Maybe speed or something?
component {
remote function getMath(){
math = 2 + 2;
return math;
}
}
or
<cfcomponent>
<cfscript>
remote function getMath(){
math = 2 + 2;
return math;
}
</cfscript>
</cfcomponent>
or
<cfcomponent>
<cffunction name="getMath" access="remote">
<cfscript>
math = 2 + 2;
return math;
</cfscript>
</cffunction>
</cfcomponent>

Not especially.
Version 3, full tags, will be backwards compatible with ColdFusion 8 and the open source versions of ColdFusion server eg. Railo or OpenBD.
Version 2 is neither something or nothing.
Version 1 is the full ColdFusion 9 script version.
I would recommend that you choose between the first and last versions and stick to it. Version 2 is not backwards compatible to coldfusion 8 and is neither tag nor script. Coding like this will get messy quickly.

If you plan on writing everything in script, then example 1 is the way to go.
You can do anything in script that you wish, and if something is missing you can write a cfc that will implement the missing functionality and then invoke it with the new syntax.
If your starting fresh with a new codebase i'd be trying to avoid any tags all together, thus option 1.

In terms of execution speed, they all compile to the same byte code, so should be identical.
In terms of number of characters typed (excluding line breaks/tabs):
eg 1: 64
eg 2: 100
eg 3: 129
If you are running Adobe CF9, go with option 1. It's much more succinct. You can pretty much do everything in <cfscript> these days.
If you want to check the compiled byte code for each, switch on saving .class files in your cf admin and view the files in the /Classes dir with a decompiler. eg. JD-Gui

The cfscript is probably a bit faster, and more consistent with other languages while the approach is simpler (hides complexity more) and more like .
CF started as a based language and has evolved to include a complete scripting style alternative to the approach.
Differences are a question of developer style.

Related

Using docx4j with ColdFusion

I am attempting to create Word documents with ColdFusion, but it does not seem there is any way to do it with only ColdFusion. The best solution seems to be docx4j. However, I can't seem to find any in-depth docx4j and ColdFusion examples (Aside from this question). Where can I get some doc4jx and ColdFusion examples?
pulling the data from a database.
https://stackoverflow.com/a/10845077/1031689 shows one approach to doing this. There are other ways, as to which see http://www.slideshare.net/plutext/document-generation-2012osdcsydney
The document needs page numbers and to
Typically you'd add these via a header or footer. You might find it easier to start with an almost empty docx structured appropriately, rather than creating the necessary structures via ColdFusion calling docx4j. You could still do it this way in conjunction with the final paragraph of this answer below.
create a table of contents.
Search the docx4j forums for how to do this.
In general, it looks like the easiest approach would be to create a Java class file which does everything you want (by invoking docx4j), and for your ColdFusion to just invoke that Java class. In other words, do a bit of Java programming first, get that working, then hook it up to your ColdFusion stuff.
I am not sure what exactly you mean with creating word document, which in my opinion is pretty simple. Manipulating yes, a bit tricky with docx4j or so.
<cfsavecontent variable="variables.mydoc">
Your content here
</cfsavecontent>
<cffile action="write" file="#yourFile.doc#" output="#variables.mydoc#">
Also see this post
Creating a Word document in Coldfusion - how to have pagenumbering?

applicationtimeout - ColdFusion 8

I inherited a coldfusion website that is written in ColdFusion 8 and it was written VERY poorly.
First: the Application.cfm is encrypted
Second: no one know how to decrypt the Application.cfm so I have no idea what are listed in this Application.cfm
Is there a way to get and display the applicationtimeout set in cfapplication?
I'm able to cfdump the "#Application.applicationName#" but not sure how to cfdump the applicationtimeout, sessiontimeout attributes from the cfapplication
Can anyone help?
It's easy enough to decrypt the files. Just google "coldfusion cfdecrypt.exe" and track one down. Examples:
cfdecrypt.exe
AdeptCFDecrypt
Note that the encryption scheme used changed for - I think - CF10, so these solutions won't work on that or later versions of CF. However it should be fine for your purposes.

Backward compatibility between ColdFusion 9 and ColdFusion 7 with regards to CFScript?

I am a complete ColdFusion newbie so apologies in advance for my upcoming ignorance.
We are having an issue with an existing CFScript. The problematic script contains the following line:
...
if (fields.length() != 0) {
// do something
}
...
The script runs successfully in ColdFusion 9, but we see the following message when trying to run the script in ColdFusion 7:
...
Invalid token '!' found on line...
...
I'm guessing that ColdFusion 7 does not like the '!=' operator, am I correct?
If so, are there any other backward compatibility issues with CFScript that could cause us to trip up? I've been searching for resources but there doesn't seem to be anything definitive.
Thanks.
Yes, in CF7 you need to use ColdFusion's native comparison operators, in your case neq.
Replace
== with eq
!= with neq
> with gt
< with lt
>= with gte
<= with lte
% with mod
and you're good to go. These operators are upward-compatible, CF9 will understand them.
Other than that,
you need to group all your local variables (those declared with var) at the top of a function in ColdFusion 7. This restriction has gone away in later editions of ColdFusion, but scripts written that way will of course continue to run.
there is an automatic local scope as of CF9. This scope was not available in CF7 and CF8, but by convention people added a var local = StructNew(); at the top of their CF7 functions, which will also work in CF > 7.
You're right - the Javascript-like operators (!=, ==, ||, etc.) were only introduced in ColdFusion 9, along with a whole lot more scripting support.
This mostly relates to full script support for CFCs, but there are probably plenty of other other gotchas out there...

Safe to call underlying java method on String in ColdFusion?

Adobe ColdFusion is built on Java. Almost all simple variables in CFML/CFSCRIPT are java.lang.String until the operation needs it to be of a certain type.
I've always want to use startsWith() in String instead of the more bulky CFML variant.
left(str,4) EQ "test"
However, what's the general consensus of using underlying Java method in ColdFusion?
Would this be any safer to javacast() the var first?
javacast("String",x).startsWith("test");
What if the CF engine is not built on top of Java?
Thanks
Yes, you can do this with Adobe ColdFusion and other CFML engines that are built on Java. It's actually simpler than you thought.
<cfset str = "hello what's up" />
#str.startsWith("hello")# <!--- returns "YES" --->
<cfif str.startsWith("h")>
This text will be output
</cfif>
#str.startsWith("goodbye")# <!--- returns "NO" --->
<cfif str.startsWith("g")>
This text will NOT be output
</cfif>
This is possible because CFML strings in ColdFusion are the same as Java strings. You can use any native string method (Java.lang.String) on a CFML string.
If you haven't guessed, this also works with CFML arrays (some kind of list, probably a java.util.Vector) and structs (probably a java.util.Map). Experiment with data types and the cfdump tag, you will find a lot of secrets.
One word of warning, this is not standard CFML, so if your underlying engine changes, including just upgrading to a new version, there are no guarantees that it will still work.
That said, string.startsWith() is native to Java as well as .NET, so this will also work if your CFML engine is BlueDragon.NET. The only CFML engines it will not work on are ColdFusion 5 and previous.
Is it safe to use? I would say yes. As long as CFML engines run on Java or .NET, it's perfectly safe. It's undocumented, but easy to understand, so I would say use it freely.
I have found that using built in cf functions in most cases is faster than leveraging their java counterparts, mainly as it costs so much in cf wrapping the java methods.
If you are using .startsWith(), remember it's case sensitive, whereas cf's eq isn't.
Same goes for most of the other java String methods - .endsWith(), .contains() etc.
Unless you can bundle large sets of functionality as roll your own java util classes, mixing cf and java calls seems slow. If you are in some java code, and you have a string, and you call its startsWith() method, it just executes. Done. In cf code, you have to javaCast or blindly hope the variable is in the correct data type, which is risky with things like entirely numeric strings, and when you call a .startsWith(), there is a bunch of cf code that runs before it even gets down to the java level, which is where the slowness lives. Eg. Cf's dynamic arguments means that it has to check if there is a method on the supplied object with that many args, and of those data types (or compatible types). There is just a whole bunch of code that unavoidably runs, bridging the two languages.
But don't trust our experiences, benchmark for yourselves. eg.
<cfscript>
var sys = createObject( 'java', 'java.lang.System' );
var timer = sys.nanoTime();
// run some code here
timer = sys.nanoTime() - timer;
writeDump( var: timer );
</cfscript>
If you are using the Adobe cf engine, watch out of entirely numeric strings, they bounce between java Doubles and Strings, and don't get me started with serializeJSON()...

How do I determine which files a ColdFusion application uses?

I'm starting some work on an existing ColdFusion application with no version control and what look like unused cfm files (test.cfm, test2.cfm etc.). I'd like to get a picture of what files are actually part of the application so I can get it into git or subversion in a manageable state.
How would you go about this? A regex and some methods to find and map cfinclude and cfcomponent tags? Is there some existing tool that does this?
Ben Nadel has a method to examine the live stack trace from a running template. It seems to me that you could easily plop this into your application and log the results to a database. Once you've done that, you've got a good idea of what's in use and what's not.
I think the easiest way, however, is to enable debugging (standard caveat here about development server, etc). The standard ColdFusion debugger will give you a complete list of every file used during the execution of a single page. ColdFire will do the same thing in a handy Firebug extension (click ColdFusion then click Exec Times).
It should be pointed out that the built-in debugger even shows you the files included from CFC calls, and the files included from within those calls as well. It is all inclusive.
Ben Nadel on Stack Traces
Ray Camden's ColdFire
Sample of CF Debugging from a live page:
Put it into git first! Then, if you screw up, you can easily roll back.
(If you're concerned about having a 'clean' repository, when you're finished and fully tested, you have the option to just remove the single .git folder and create a new one.)
Then, as Tomalak suggests, use cflog on every file. Infact I'd say maybe even log twice, at the top and bottom of each script, could potentially help you to map out how the application runs.
A regex is not advisable. Since ColdFusion is quite flexible in the way files can be included or referenced, there will be no way to determine the definitive list of dependencies from the source code alone.
You could insert a <cflog> into each file and build a log from the running application. Examine the log after the application was active for a while and all functionality had been accessed at least once.
Don't bother instrumenting each file, just cflog the page name in OnRequest inside application.cfc - the target page is an argument.
Of course then the issue becomes code coverage and the ability to fully excercise the app.
<cffunction name="onRequest" returnType="void">
<cfargument name="targetPage" type="String" required=true/>
<cflog file="Usedpage" text="#Arguments.targetPage#">
<cfinclude template="#Arguments.targetPage#">
...
</cffunction>
cfinclude won't tell you if a url is supposed to load the file directly. I've seen system where some files are not included via an index.cfm even when the framework expects it. I have it in my own work where index.cfm loads most code but reset.cfm bypasses the framework to reset configs and session data.
Download a trial of Dreamweaver and define a ColdFusion site. DW can create a site map and tell you which files are not included, linked, cfmoduled and so forth. I don't know if it can figure out unused CFCs, but CFMs should be easy. Note that I haven't used DW for years, but it had this functionality around CF 4/5.