Notes: Migrating from Flex 3 to Flex 4

These are notes from Adobe MAX 2009. This session will help us see what problems we may encounter moving to Flex 4, as well as what new features we can use.

Packages. The old mx packages are still there. But now everything new is in the spark packages. Why two different packages? At first, they had them all in the same package, but the component had a prefix for the new version. It wasn’t received well. So instead, Adobe just put the new components in new namespaces.

The MX namespace is also called “Halo”. Don’t consider it a “legacy” namespace. These are still fully supported. Spark is all of the new stuff. The general MXML has a new 2009 namespace, but the legacy namespace is still supported.

Graphics

You can add graphic primitives enabling you to draw lines, ellipses, and curves, directly within the MXML. With the new FXG file format, you can import a graphic in as a vector graphic that appears as code. Cool. Using a static FXG image is great for icons or other static simple images, because its rendering is faster than an MXML graphic container.

Flex States

Instead of using AddChild and RemoveChild for changing states, you have includeIn=”" and excludeIn=”" attributes that go directly on components. What’s more, you can set various attributes of a component for the different states by using a dot notation. For instance, title.mystate=”Title”. This seems like a pretty drastic change.

With states, you have to use the OLD way with the 2006 namespace, and you have to use the NEW way with the 2009 namespace.

Skinning

Components all have separate skin files. This will allow you to create a custom skin for any component easily.

Common migration issues

Type selectors need namespaces now in your Flex CSS. For instance, if you’re styling a button, Flex doesn’t know if it is a Spark button or a Halo button. So you need to define the namespace in your stylesheet and update your selectors accordingly.

You must compile against Flash Player 10. This won’t be an issue if you use Flash Builder 4 with Flex 4.

Application.application should now be renamed to FlexGlobals.topLevelApplication. Just a direct rename.

RSLs are on by default.

The “Declarations” tag is a problem child. In Flex 4, anything that is not a display object or default property must be within the fx:Declarations tag. Even tags like RadioButtonGroup. Formatters, Effects, RPC stuff, Validators, etc. need to go in the Declarations.

“If Looks Could Kill.” The default theme is now Spark, which looks different than the old Halo theme. You can switch back to the Halo theme. To do so, turn on “Use Flex 3 compatibility mode” in the project properties, or go into the “Flex Theme” panel and change the theme (this is the preferred way).

If moving to Flex 4, try to use as many Spark components as you can. They should play nicely with MX components. You can intermix them together. Caveats: (1) The graphic primitives cannot go in an MX component, just wrap them with a group; (2) you cannot put MX components in Spark containers; (3) MX effects do not work on Spark graphic primitives; (4) MX navigators (ViewStack, Accordion, etc) should use NavigatorContent component.

Other things to watch out for:

  • Spark components don’t support the Halo theme.
  • Cannot create spark component based itemRenderers for MX List based components
  • APIs between MX and Spark components are different (e.g. addChild vs. addElement)!
  • When using MX and Spark components together, you may see a text difference since they are themed differently. There is a TLFText theme that can help with this.

So, migrating an app from Flex 3 to Flex 4 is a bit of a slippery slope. And even just beginning to use Flex 4 with current Flex 3 knowledge will require a bit of a slowdown as you undo some of those brain synapses to do things the new Flex 4 way. But these changes going forward are good for the Flex framework.

Notes: What’s New in ColdFusion 9

These are notes from Adobe MAX 2009. The talk was given by Adam Lehman.

First, ColdFusion 9 also introduces the new ColdFusion Builder product, an Eclipse-based IDE “to rule them all”. Because it is based on Eclipse, you can also combine other products, like Flash Builder, so that one IDE gets the job done for everything, whether it is HTML, CFML, JS, Flex, ActionScript, CSS, etc.

ColdFusion Builder also supports some interesting server integration features. You can access ColdFusion Administrator and other functionality right from ColdFusion Builder; you can see and browse all of the databases that ColdFusion has datasources for. With such functionality, you can do cool stuff like auto-generate ORM-based CFCs by pointing it to an existing database table! Now, it doesn’t generate just the bean CFC. It generates the entire service layer: DAO and Gateway beans as well!! Now, this obviously is just a starter step. It’s expected that you’d tweak it from that point. With the Flash Builder integration, you can take this to the next step by also generating ActionScript objects to help with Flex integration as well.

CFML Enhancements

Well, in CF7, they introduced Application.cfc that supported hooks for application events. But now in CF9, there is a Server.cfc with an onServerStart() method for handling that event.

Various other holes have been filled. You can now have nested CFTRANSACTION tags. Error catching now supports “finally”. And looping now supports CFCONTINUE.

Variable level enhancements: You can now pass implicit structures/arrays to tags and functions. Before you had to assign it to a var and then pass it. There is assignment chaining (a=b=c). Direct access to elements of returned arrays (i.e. myFunction()[x] couldn’t be done previously). And, finally, ternary operators! For instance, you can do something like: a = (b<c)?b:c

CFSCRIPT enhancements. Starting in CF9, you can now write 100% script-based code, including classes/components.

CFC enhancements. There is now an explicit LOCAL scope. Instead of doing var myvar=1, you can do LOCAL.myvar=1. What’s more, you can declare a variable with var anywhere in the code, not just at the top. CFCs also support implicit getters/setters by using the CFPROPERTY tag. These are worth using if there is no special handling going on, because they’re 7x faster than your own getters/setters.

Here’s an awesome feature. You no longer have to use CreateObject() to create a CFC. You can use the IMPORT keyword to point to a directory of CFCs and have them be first-class citizens in your code. And you can use the NEW keyword to create CFCs, like user = new User(). Looking more Java-like all the time.

How to convince your boss to upgrade

First, with CFIMAP, you can now access email with IMAP support, such as GMail.

There are many PDF support enhancements. Create PDF packages, add/remove headers and footers, optimize PDFs (down-sample images, for instance), extract text/images, high-quality thumbnails, convert Word documents to PDF.

There are presentation options. The CFPRESENTATION tag will generate PPT files from CFML/HTML content. And you can go the other way: Convert PPT to HTML or Flash. Supports Microsoft PowerPoint ’97 to ’08 and Open Office Presentations.

Spreadsheet support. The CFSPREADSHEET helps you create, read, and merge native Excel spreadsheets. This isn’t just a CSV file. This is a real Excel spreadsheet with formulas and formatting support.

SharePoint integration. Native access to SharePoint data and services (Sites, templates, sub-sites, web parts, and workspaces). You can get lists including data, views, and alerts and querying against lists. Users, permissions, site groups, cross-site groups, security groups, and distribution groups. Build web parts with CFML. Integrate with SharePoint Single sign-on. You may wonder, “Why is this necessary since SharePoint has web services and exposes information as XML?” Because it’s painful. ColdFusion makes it significantly easier and faster.

ColdFusion Server Manager. A Flex-based AIR application that helps you manage multiple ColdFusion servers. And it receives system notifications and alerts. Helps apply settings for multiple servers.

The next generation of applications

Coldfusion 9 ORM functionality. Powered by the Java Hibernate framework, so it is industry standard. Various ormXXX() and entityXXX() functions provide this functionality for you. With this feature, no more SQL is necessary, so apps can be developed faster (and potentially with fewer errors). CFCs will just be automagically saved. But it’s not just a matter of saving time on development; it also makes it so that your app is no longer database-dependent. It can point to any database server and it should just work.

Enhanced caching. ColdFusion is already pretty optimized for speed (esp. between CF7 to CF8). So to get more speed, developers need to use these new caching features. Using cacheGet(), cachePut(), and cacheGetMetaData(), you can save/retrieve objects from a built-in cache. Page fragment caching allows you to have a combination of static and dynamic content on a page. This provides a HUGE performance boost, depending on what you’re doing. For instance, you only want to generate a menu dynamically the first time, but then another area of the page is dynamic every time.

Performance gains. Okay, Adam lied. ColdFusion 9 is actually 40% faster than ColdFusion 8, with no changes to code, just upgrading ColdFusion. CFC creation is 8x faster, method invocation is 3x faster. And UUID creation is 100x faster. These are some huge bottlenecks that have been blown away. There is a performance brief that Adobe will be releasing on ColdFusion 9 in the near future.

Search engine. ColdFusion 9 now includes Apache Solr. Verity is still included, but so is Solr, which is actually even faster than Verity. So you may choose to upgrade to Solr! They even provide a Verity-to-Solr migration utility.

ColdFusion Server API. You can have direct access to ColdFusion services for CFCHART, CFDOCUMENT, CFPDF, CFIMAGE, and more. This makes it even easier to access ColdFusion mail/pdf/images services directly within Flex without having to custom code some ColdFusion code.

What’s more, Flash Remoting is 9x faster. It may arguably be the fastest flash remoting server-side technology around.

There are various JavaScript/Ajax tags, like CFMAP for Google Maps, CFMESSAGEBOX, CFSLIDER, and more. Ajax functionality in CF is powered by ExtJS 3.0 (which is nice, because ExtJS isn’t free anymore, so getting CF9 gets you a copy of ExtJS basically), and JQuery is also supported.

This presentation was very rapid fire, which is testimony to how feature-rich of an upgrade ColdFusion 9 is.

Notes: What’s Coming in AIR 2

These are notes taken at MAX 2009. This is obviously subject to change before AIR 2 is launched.

Multi-touch gestures. This is especially useful for Flash’s new ability to compile down to iPhone, but it will even work on tablets or laptops that support multi-touch, such as the track pad on MacBooks.

Opening Files. There is now a File.openWithDefaultApplication() method which does just what it says. So you can open data document of any type in its native app from your AIR app, i.e. open a Word document in Word from your AIR app. Executables are blacklisted, such as *.exe, *.bat, or other executables.

File “downloaded”. You can use myFile.downloaded=true to set the bit on a file that will notify the user with a message like, “This file has been downloaded from the Internet. Are you sure you want to open it?” the first time it opens.

Volume Detection. Listen for mounting and unmounting of volumes. Query system for volume info. So this gives you the ability to work with external volumes including optical media or attached cameras.

Native processes. You can start native processes outside of AIR, or even reference it and launch it. This includes passing in arguments and utilizing stdin, stdout, stderr! Now, you can’t use this ability from an app that has been installed straight from an AIR file. You have to have the app in a native installer. This way, someone on a Mac won’t try to run an app that wants to run a Windows executable, or something similar. In the demo, the Mac screenshot process was invoked, and in another demo, Spotlight functionality was invoked and its output was incorporated into the app!

File promises. Ability to tell the OS about files you don’t have yet. Gives you the ability to drag and drop files in cases when they are on the network or Internet.

Listening on a socket. Ability for inter-application communication. Better protocol support (such as FTP). Networking utilities. You just use ServerSocket.bind() and ServerSocket.listen() methods in conjunction with an event listener to act on activity. Demonstrated it with an HTTP proxy app called HTTPeek (on Google Code). Very fast. Handles the socket connection well.

Advanced Networking. Get network information. Support for IPv6. UDP support. A DNS resolver. More coming.

Audio Encoding. Surprisingly, this wasn’t supported before. Now you can encode data directly from the microphone.

Global Error Handling. You can now catch all uncaught errors and error events in your app. This is useful because on a client that does not have the debugger version of Flash, the app will just stop working. With this global error handling feature, you can log the errors to a file or over the network, or do other things.

Accessibility. Functionality from Flash Player 10 will now be supported in AIR. It has 28 accessible Flex components, supporting MSAA (Microsoft Active Accessibility) in JAWS or Microsoft Narrator, for example.

JavaScript Profiling. Can access profiling information via Aptana.

Optimization. Lower memory consumption, lower CPU utilization especially when the app is idle.

Misc. Improved printing. SQLite supports nested transactions. Exit even on shutdown (can now see when the event is not cancelable). Various WebKit enhancements.

Flex 3 will work for AIR 2, but Flex 4 with Flash Builder 4 will be a cleaner, more set up approach for developing AIR 2. You’ll have to overlay AIR 2 SDK on your Flex Builder 3 installation if you want to develop for AIR 2 in Flex 3.

Importance of Understanding Factories, Part 2: Transient Factories

In my last post, I discussed some of the mistakes that can be made when using ColdSpring as our factory. It became evident that using ColdSpring to inject transient objects—that is, objects that have a state for a momentary use and then are discarded—into our service objects or other related singletons is problematic. This is largely due to the concurrency issue with reusing that injected bean in the service object's methods, and mistakes that can be made if we forget about objects being passed by reference. Ray Camden also discussed this recently in ColdFusion and Pass by Reference versus Value.

The answer is to build a transient factory for those beans. We can do it without losing the benefits of ColdSpring's dependency and configuration management. And it doesn't take much to make a factory. Take a look at this sample that works with my previous post's code.

CFM:
  1. <cfcomponent>
  2.  
  3.   <!----------------- Constructor ---------------------->   
  4.   <cffunction name="init" returntype="BeanFactory">
  5.     <cfset variables.config=StructNew() />
  6.     <cfreturn this />
  7.   </cffunction>
  8.    
  9.   <!------------------ Config -------------------------->   
  10.   <cffunction name="setConfig" returntype="void">
  11.     <cfargument name="Settings" type="struct" />
  12.     <cfset variables.config=arguments.settings />
  13.   </cffunction>
  14.   <cffunction name="getConfig" returntype="struct">
  15.     <cfreturn variables.config />
  16.   </cffunction>
  17.    
  18.   <!--------------- Create Methods --------------------->
  19.   <cffunction name="createBean" returntype="any">
  20.     <cfset var com=CreateObject("component","Bean") />
  21.     <cfset com.setConfig(getConfig()) />
  22.     <cfreturn com.init(argumentCollection=arguments) />
  23.   </cffunction>
  24.  
  25. </cfcomponent>

The factory has a setConfig() method, just like the Bean does, and can be used to conveniently inject all configuration information with ColdSpring. Now, let's look at the createBean() method where the action is. It performs 3 key actions: (a) Create a fresh object. (b) Pass in configuration. (c) Call the init() method and return the object.

Our ColdSpring file can now be rewritten like this:

XML:
  1. <bean id="BeanService" class="com.BeanService">
  2.   <constructor-arg name="BeanFactory">
  3.     <ref bean="BeanFactory" />
  4.   </constructor-arg>
  5. </bean>
  6. <bean id="BeanFactory" class="com.BeanFactory">
  7.   <property name="Config">
  8.     <map>
  9.       <entry key="dsn"><value>MyDSN</value></entry>
  10.     </map>
  11.   </property>
  12. </bean>

See, we use the same XML for injecting configuration, it's just injected it into BeanFactory. We then inject BeanFactory into BeanService, instead of the Bean itself. It can use the factory to get a fresh bean whenever it needs one—properly instantiated and configured.

An example of BeanService tweaked to accept and use the factory:

CFM:
  1. <cfcomponent hint="Handles beans." output="false">
  2.   <cffunction name="init">
  3.     <cfargument name="BeanFactory" type="BeanFactory" />
  4.     <cfset variables.BeanFactory=Arguments.BeanFactory />
  5.     <cfreturn this />
  6.   </cffunction>
  7.   <cffunction name="workWithBean">
  8.     <cfscript>
  9.       var myBean=variables.BeanFactory.createBean();
  10.       myBean.setX("I changed X on the bean!");
  11.       myBean.setY("I changed Y on the bean!");
  12.       return myBean;
  13.     </cfscript>
  14.   </cffunction>
  15. </cfcomponent>

Let's make the factory better. First, your app probably has more than one type of transient object. You don't need a separate transient factory for each type of object. Just have a different createBeanName() method for each object type, with creation code specific for each object. And if your objects are all essentially created with the same conventions, you can handle create methods for all of them with a single onMissingMethod handler. Something like this:

CFM:
  1. <!--- Create Methods --->
  2. <cffunction name="onMissingMethod" returntype="any">
  3.   <cfargument name="MissingMethodName" />
  4.   <cfargument name="MissingMethodArguments" />
  5.   <cfscript>
  6.     var method=Arguments.MissingMethodName ;
  7.     var args=Arguments.MissingMethodArguments ;
  8.     var classList="MyBean,DiffBean,AnotherBean";
  9.     var className=RemoveChars(method,1,6);
  10.     var com="";
  11.     if( Left(method,6)=="create" &&
  12.         ListFindNoCase(classList,className) )
  13.     {
  14.       com=CreateObject("component",className);
  15.       com.setConfig(getConfig());
  16.       if(StructKeyExists(com,"setFactory"))
  17.         com.setFactory(this);
  18.       return com.init(argumentCollection=args);
  19.     }
  20.   </cfscript>
  21. </cffunction>

See how the first if statement checks to make sure the method is a valid method name: One that starts with "create" and ends with an acceptable class name as we define in our classList variable. So this onMissingMethod handler will work with createMyBean(), createDiffBean(), and createAnotherBean(). Note how it also checks for the existence of a setFactory() method in the object it creates. If it is there, it will use the method to inject itself into the new object! This is a simple example of how autowiring is accomplished.

Take it even further! Head over to Paul Marcotte's blog post, A Coldfusion Transient Factory Example, which shows how to make a very generic transient factory that receives configuration on which classes it should create through ColdSpring. Very cool stuff.

Review. Transient factories don't require a lot of code, and are a vital step to simplifying and centralizing our instantiation and configuration of transient objects, or objects that have a state. Doing so doesn't negate the need for ColdSpring, in fact it complements it as it was intended, since ColdSpring works well with instantiation of singletons, and transient factories as described here are used as singletons. By utilizing ColdSpring and a transient factory, we can be well on our way to cleaner and more organized development.

Get the files. Download sample code to play with: ColdSpring Bean Test. It walks us through the issues discussed in the previous post as well as using transient factories like we just discussed.

This post has gotten pretty long, so I'll save the race condition demonstration for "Part 3". :-)

  Theme Brought to you by Directory Journal and Elegant Directory.