Showing posts with label ColdFusion. Show all posts
Showing posts with label ColdFusion. Show all posts

April 19, 2012

Scope precedence (from function perspective)

Scoping. Very common topic among CF developers.
Which scope has precedence? When and why I should scope variable?
No matter how many times I read or heard answer on these question, no matter how many I answered to myself, I always forget it after few months.
Thus I decided to find a way how to remember it once and for all :)
Magic formula (for me) is in answer: "Scope precedence is same as it is in Java"
Here I talk only about precedence from function perspective.
Inside method (function) body we deal with 3 major scopes (fake this I'll ignore)

Argument scope - hold references to function arguments
Local scope - hold references to local variables, variables declared (vared) inside function body
Variables scope - it contains references to protected instance variables

In same order scopes are listed here, Adobe ColdFusion 9.x (and I beleive Railo 3.x) will go through when looking for variable referenced inside function body. This means:
Referenced variable, which is not explicitally scoped inside function body, ACF first look for in argument scope, then if it could not be found there, in local and, at the end, in variable scope.

At the same time, we should have 2 things in mind :
1) Local variable cannot be named same as argument (same as in Java)
2) CF, as dynamic language with weak typing, allows dynamic argument list which means, that argument does not need to be defined, unless it is explicitly set (required = true)

Example 1: Argument value not set

//ScopingTest.cfc
component{
 myVar = "instance variable value";
 
 //no default value set for argument myVar
 function myMethod(myVar){
  myVar = myVar;
  writeOutput(myVar);
 }
}

//call
new ScoptinTest().myMthod();

//output
"instance variable value"

Example 2: Argument value set

//call
new ScoptinTest().myMethod("argument value");

//output
"argument value"

From these examples, we clearly see, if we don't explicitly scope variable:
a) If argument is not defined, instance variable will take presedence
b) If argument is defined, it is going to take presedence

To summarize:
1. Argument and Local variables shares the throne (there can be only one)
2. Variable comes right after

Knowing this, much less characters we'll write in the future and much less noise in the code.

March 16, 2010

BlazeDS: Creating endpoint at runtime

Why? Because I do not want to bother end users with xml configuration files. Simple as that.
Biggest enemy of your application is user itself. Therefore, reducing possibility to make a mistake is most important goal if you wish to left good impression with your app. This was one of steps that I take on the road to make Collyba installation procedure as easy as possible.
As I said in previous blog, while I was working on this, insight  to BlazeDS source files was of great help. Actually that is where I found a solution.

What are channels and endpoints?
Channels are client-side objects that encapsulate the connection behavior between Flex components and the BlazeDS server. Channels communicate with corresponding endpoints on the BlazeDS server. You configure the properties of a channel and its corresponding endpoint in the services-config.xml file.
But in our case, we won't configure it in XML file but programmatically.

BlazeDS: Creating destinations at runtime

Because it is already said a lot on this subject, I won't waste much words, but go straight to the point/code.
/**
* Create blazeds destination at runtime. Follow SVN link to java source files for more details.
* @name hint="Destination name" 
*/
function createDestination(destname,channelId){
 var local = structNew();
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/MessageBroker.java
 local.brokerClass = createObject('java','flex.messaging.MessageBroker');
 local.broker = local.brokerClass.getMessageBroker( javacast('null','') );
 local.service = local.broker.getService('message-service');
 local.destinations = local.service.getDestinations();
 
 //check if destination already exists. If it does just return it
 if (structKeyExists(local.destinations,arguments.destname)){ //check if destination already exist
  return local.destinations[arguments.destname]; 
 }
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/Destination.java
 local.destination = local.service.createDestination(arguments.destname);
 // Creates a ServiceAdapter instance and sets its id, sets if destination  is manageable
 local.destination.createAdapter('cfgateway');
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/common/src/flex/messaging/config/ConfigMap.java
 local.configMap = createObject('java','flex.messaging.config.ConfigMap').init();
 local.configMap.addProperty('gatewayid',application.CFEventGatewayId);
 
 //svn: http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/flex/messaging/config/ServerSettings.java
 local.ss = createObject("java","flex.messaging.config.ServerSettings");
 local.ss.setAllowSubtopics(true); 
 local.ss.setSubtopicSeparator('.');
 local.ss.setDurable(false);
 
 local.destination.setServerSettings(ss);
 
 local.adapter = local.destination.getAdapter();
 
 //Initializes the adapter with the properties.
 local.adapter.initialize('cfgateway',configMap);

 local.destination.addChannel(arguments.channelId);
 
 //Starts the destination if its associated Service is started and if the destination is not already running. 
 local.destination.start();
 
 return local.destination;
}

March 7, 2010

Deploy ColdFusion 8/9 on Glassfish v3

Deploying ColdFusion 8/9 on Sun Glassfish 3 is not much different than it is on IBM WebSphere 7 AS. Except, imho, it is far more optimal, because it takes much less resources (especially if it is setup for dev) and cost nothing. I did this on Win7 platform, but it looks completely the same on Linux platforms. So, let's move.

March 4, 2010

Proud to be second!

Not that I wouldn't like to be a first, but last 2 second places were awesome!
On 29th January I won second place on "Best Of CF9" contest. Contest was  organized by Ray and other Adobe Community Experts. Thank you for that. I won't talk about much about that here, just to emphize that I have "big" plans for Collyba. "Big" actually means that I'm really going to continue development on it, which is, for itself, enormous accomplishment:)

January 9, 2010

Everything is dead these days


Sounds morbid, but, really, in last few weeks everything died :)

As a ColdFusion developer most noticeable to me was spree of articles & blogs which tried to explain how CF is dead (or is about to die). But that's something I used to. Each time new CF Server comes out, "CF-is-dead wave" hit the coast. What really surprised me was that everything else died.

Few days ago, I read an "old" (for some reason it became popular again) article how UML died and 13 reasons why is so. According to author, this technology is already dead. After reading it, my only question was: What is an alternative? Read a blog, read dozens of comments and no one suggested what can be an alternative to this.

June 5, 2009

ColdFusion Regular Expression Optimization

Last night I bumped on article talking about regular expression performance tuning. After reading it and since we extensivly use regex to parse article & community content, I decided to see can we do something to bit boost performance on that side. So, here we go.

Startup facts and implications:

a) „ColdFusion is built on top of Java. ColdFusion data types (String, Query, Struct, Array, etc.) are really just custom Java classes that are built on top of things like strings, record sets, hash tables, collections, etc. „
=>
We can use java.lang.String methods on ColdFusion strings. This is more a fact then something we are going to use and you’ll see later why ;)

April 25, 2009

ColdFusion on Rails

First of all I am not talking here about porting CF in Rails framework, but porting of ideas.

After I heard for millionth time "Ruby on Rails is cool", I've decided to try it myself. Well, that was 6+ months ago: read Ruby language tutorial, few tuts on Rails framework, and that's where I stopped. Don't get me wrong, it is not because I didn't like it, but because of lack of time. Anyway, I think I got point, and point where I sincerely agree: "freedom yes, but not too much" or "too much freedom can kill you".
How is this related with ColdFusion.
Plus/minus some time since I start learning RoR, I also heard (at that time rumor) that Adobe plans to integrate Hibernate in new version of CF Server (code name Centaur).
I'll stop here (for now) and conclude: CF needs to make a choice on MVC framework and CF on Rails is there.

to be continued...

CF Wheels, framework inspired by Ruby on Rails, hit its first final version.

April 23, 2009

Pingback

Wikipedia:
A Pingback is one of three types of Linkbacks, methods for Web authors to request notification when somebody links to one of their documents. This enables authors to keep track of who is linking to, or referring to their articles.


Pingback is an XML-RPC request sent from Site A to Site B. This functionality usually comes as part of blogging softwares since it's most commonly used there. Almost all open source blogging applications are written in PHP and only a few in CF and none of them have support for pingback. Therefore I decided to write an CF implementation by myself.

Download source code

pingback.cfc - Pingback implementation
tools.cfc - Various tools used by pingback.cfc
xmlrpc.cfc - Translates XML-RPC packets to and from CFML data structures
xmlrpc.cfm - XML-RPC Server
test.cfm - Test template. Start from this file.

Successfully tested on Railo 3.1 and ColdFusion 8 Enterprise Server.
Should not be a problem to integrate it into blogCFC.

Pingback specification:
http://www.hixie.ch/specs/pingback/pingback


November 17, 2008

Unit testing in ColdFusion

Why would CF community differentiate from any other community? That's why "all" Unit testing platforms are derivatives of xUnit project. My choice is CFUnit, because CFCUnit looks abandoned (not sure is it really), it has Eclipse plugin and it's better documented. But, after all, both environments do the job well.
So much about installation. Nnow, when you have everything you need, you just need to know what to do with it. Well, go and read CFUnit primer. It's the fastest way to learn how to use CFUnit.

More to read:
Test Driven Development with ColdFusion
(part I-III)

CFC variables.instance

When I saw for the first time, in cfc code, variables.instance = StructNew(), I asked myself "What, what, what?" :) Well, purpose of "subinstancing" of variables structure, is nothing more but teh convenience:
1. It's organized better. All variables of same kind can be grouped in that manner.
2. Easier for cfdump-ing. Do and you'll see what I'm talking about.
3. And it is more obvious what you do: variables.instance.myVar="A" means "I am adding (assigning value to) myVar into instance of variables structure"

Not necessity but can be handy

November 12, 2008

Preparing Eclipse 3.x for ColdFusion Development

CFEclipse - An Eclipse plugin for ColdFusion

The best plugin I found, for Eclipse IDE. Simply it gears Eclipse with all-you-need for CF development: color-coding, content outline, code & bracket highlighting, code folding, tag code completion, snippets, etc... full feature list

Uber fast install:

1. Select the "Help->Software Updates->Find and install" menu option
2. On the screen that is displayed, select 'Search for new features to install' and click the 'Next' button
3. Now click the 'New Remote Site' button
4. Enter a name for the update site, for example "CFEclipse". In the URL box, enter "http://www.cfeclipse.org/update" and click the OK button


Note: This is the moment when you are going to install CFUnit too.

ColdFusion 8 Debugging

Well, not much to say but to advise you to sit and read this ADC step-by-step tutorial.
Read it!

Developing ColdFusion Extensions in Java

To make this short and effective:

1. You cannot reload CFX developed in Java without server restart. Empirically proven and confirmed by other CF developers (as some ppl claim, even by Adobe tech support)
ref: http://dreamweaverforum.info/advanced-techniques/78901-how-reload-java-cfx-without-restarting-coldfusion.html

2. Article to read to get into this subject fast and "good enough"
http://www.intermedia.net/support/coldfusion/cf5docs/Developing_ColdFusion_Applications/CFXTags4.html

3. Example
import com.allaire.cfx.*;

public class test implements CustomTag {

public void processRequest(Request request, Response response) throws Exception {
// TODO Auto-generated method stub
String strName = request.getAttribute( "NAME" );
response.write( "Cao, " + strName );

Query qr = request.getQuery();

if ( qr == null )
{
throw new Exception(
"Missing QUERY parameter. " +
"You must pass a QUERY parameter in "+
"order for this tag to work correctly." ) ;
}

int i=1,j=1;
while(i<=qr.getRowCount()){ j=1; response.write(" "); while(j<=qr.getColumns().length){ response.write(qr.getData(i, j)+" | "); j++; } i++; } } }


Voila! Now you know everything :)))

January 18, 2007

Can ColdFusion "continue"?

Believe it or not, ColdFusion does not support “continue” (nor tag: “cfcontinue”).

This script demonstrates one of possible ways how you can simulate “continue” behavior:

<cfloop from="1" to="10" index="i">
<cftry>
<cfif>
<cfthrow type="continue">
<cfelse>
#i#
</cfif>
<cfcatch type="continue">
<!--- ignore --->
</cfcatch>
</cftry>
</cfloop>


Reference:
http://weblogs.macromedia.com/cantrell/archives/2003/08/living_without.cfm

Update:
Since 5th October 2009, day when CF9 has been released, developer can, finally, cfcontinue ;) Hooray. Ah, yes, "lupus in fabula", Adobe introduce cffinally as well.