Alfresco Flex ConfigService gotcha: instantiate early!
Posted Under: Alfresco, Cairngorm, Flex, PureMVC
The ConfigService class in the Alfresco Flex SDK is responsible for getting server configuration information from an external XML file, and making that information available to other SDK classes. The class works well generally, but it does have a quirk that can really trip you up: it sets default values for the configuration variables, and these default values match the default development setup. As a result, you can have your application working like a charm on your development machine, and assume everything is functioning as designed, only to find that it fails to even start when you upload it to a server.
The reason this happens is that the application is reading the server configuration values from the ConfigService instance before it has had time to retrieve those values from the external XML file. This works fine on your development machine if you’re using the default configuration, but since the server doesn’t use the default configuration, the application fails. The solution, thankfully, is quite simple. The ConfigService class is a Singleton, and it attempts to retrieve the values from the XML file immediately after instantiation. So, all you need to do to avoid this issue is to instantiate ConfigService early enough that it gets the correct values before any other application objects need them.
Let’s look at how to do this in three different scenarios: using the PureMVC framework, the Cairngorm microarchitecture, and no framework at all. I’ll start with the latter, since it’s the simplest.
No Framework
If you’re building a simple UI where separation of concerns is not an issue, then you can simply instantiate ConfigService in the <script> block of your main Application MXML file. If you declare a member variable to hold the instance, and initialize the variable at the same time that you declare it, then your ConfigService class will be instantiated very early - and therefore it will begin retrieving the configuration data from the XML file - well before the creationComplete event is dispatched.
<mx:Script>
<![CDATA[
import org.alfresco.framework.service.webscript.ConfigService;
// Instantiate ConfigService early so the config data
// will be available to the various proxies
private var configService:ConfigService = ConfigService.instance;
]]>
</mx:Script>
PureMVC
In PureMVC applications, you would not want a direct reference to the Alfresco SDK in an MXML file, as this would create a very tight coupling between the view and the service tiers. So, instantiating the ConfigService class directly in the main Application MXML is not a good idea. However, PureMVC applications typically instantiate the ApplicationFacade in the main Application MXML’s <script> block, like so:
// ApplicationFacade instance
private var _facade:ApplicationFacade = ApplicationFacade.getInstance();
That being the case, if you instantiate the ConfigService class in the ApplicationFacade’s constructor, you will have your ConfigService instance very early indeed.
/**
* Singleton Constructor
*/
public function ApplicationFacade()
{
// Instantiate ConfigService early so the config data
// will be available to the various proxies
var configService:ConfigService = ConfigService.instance;
}
As in the no-framework example above, it will be instantiated well before your main Application MXML’s creationComplete handler is called, and will be initialized with the external XML data before your users have time to enter their usernames and passwords.
Cairngorm
With Cairngorm, you typically have a tag in your Application MXML that looks something like this:
<business:Services id=”service” />
This instantiates the Services component, and as in the examples above, this will happen before the Application’s creationComplete event is dispatched. So you can declare and initialize a member variable to hold the ConfigService instance in the <script> block of Services.mxml, and this should be early enough. (The code will look the same as the no-framework example above, so I won’t repeat it here.)
Nuff Sed
Hopefully, these examples will be sufficient to get you going in whatever framework you use. If not, feel free to contact me and I’ll do my best to help you out.






