<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>AaronHardy.com &#187; flash</title>
	<atom:link href="http://aaronhardy.com/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaronhardy.com</link>
	<description>For all your Aaron Hardy needs.</description>
	<lastBuildDate>Fri, 20 Apr 2012 14:53:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Loading a Remote Module into a Local App</title>
		<link>http://aaronhardy.com/flex/loading-a-remote-module-into-a-local-app/</link>
		<comments>http://aaronhardy.com/flex/loading-a-remote-module-into-a-local-app/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 02:39:24 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[crossdomain]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[IModuleInfo]]></category>
		<category><![CDATA[loadResourceModule]]></category>
		<category><![CDATA[loadStyleDeclarations]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[ModuleInfo]]></category>
		<category><![CDATA[ModuleManager]]></category>
		<category><![CDATA[ModuleMarshaller]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[StyleManager]]></category>
		<category><![CDATA[SWF is not a loadable module]]></category>
		<category><![CDATA[Unable to load resource module]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=295</guid>
		<description><![CDATA[At work, we deal a lot with loading modules residing on remote servers into our applications running locally.  By locally I mean from a local path on our hard drives (the default Flex Builder run/debug settings), not on a local web server instance (localhost).  Depending on what type of module we are loading, we would [...]]]></description>
			<content:encoded><![CDATA[<p>At work, we deal a lot with loading modules residing on remote servers into our applications running locally.  By locally I mean from a local path on our hard drives (the default Flex Builder run/debug settings), not on a local web server instance (localhost).  Depending on what type of module we are loading, we would normally use one of Flex&#8217;s built-in functions to load modules:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Loading a language resource module:</span>
resourceManager.<span style="color: #006600;">loadResourceModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'http://aaronhardy.com/en_US_ResourceModule.swf'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Loading a style module (e.g., a compiled font swf):</span>
StyleManager.<span style="color: #006600;">loadStyleDeclarations</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://aaronhardy.com/fonts/Astroid.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Loading miscellaneous modules:</span>
<span style="color: #000000; font-weight: bold;">var</span> moduleInfo:IModuleInfo = ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'http://aaronhardy.com/MyModule.swf'</span><span style="color: #66cc66;">&#41;</span>;
moduleInfo.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>But these methods of importing modules result in errors like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">Unable to <span style="color: #0066CC;">load</span> resource module from http:<span style="color: #808080; font-style: italic;">//aaronhardy.com/en_US_ResourceModule.swf</span>
<span style="color: #0066CC;">Error</span>: Unable to <span style="color: #0066CC;">load</span> style<span style="color: #66cc66;">&#40;</span>SWF is <span style="color: #0066CC;">not</span> a loadable module<span style="color: #66cc66;">&#41;</span>: http:<span style="color: #808080; font-style: italic;">//aaronhardy.com/fonts/Astroid.swf.</span></pre></div></div>

<p><span id="more-295"></span>One way to solve these issues is by <a href="http://bradywhite.net/2008/05/05/flex-3-how-to-load-a-complied-css-swf/" target="_blank">using a crossdomain policy file on the server and then using a local apache instance to run the application</a>.  In my talks with the folks at Adobe, this seems to be their standard workflow which may be why we haven&#8217;t heard more about these problems.  However, my workflow is to NOT have a local web <a href="http://www.hostingobserver.com/server-hosting.php" target="_blank">hosting server</a> running and I don&#8217;t want to have one running just to get around these errors.</p>
<p>As of Flex SDK 3.2, Adobe introduced a new parameter to IModuleInfo.load() for raw module bytes.  As a result, we can now retrieve the remote module&#8217;s raw bytes using a URLLoader and then load them directly as a module using the IModuleInfo.load() method.  The module is now part of the same security domain as the main application and no errors are thrown.</p>
<p>I&#8217;ve bundled up this logic into a class called ModuleMarshaller which is found below.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">aaronhardy</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">ErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">SecurityErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoaderDataFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">ModuleEvent</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">modules</span>.<span style="color: #006600;">IModuleInfo</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">modules</span>.<span style="color: #006600;">ModuleManager</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Dispatched once module loading is complete.
	 */</span>
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;ready&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;mx.events.ModuleEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Dispatched if an error is encountered while loading the module.
	 */</span>
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;error&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;mx.events.ModuleEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Loads a remote module.  Specifically, this is a workaround to allow an application being run
	 * locally (not using a web server) to load a module residing on a remote server.
	 * Usually, when attemping to do so using resourceManager.loadResourceModule(),
	 * StyleManager.loadStyleDeclarations(), or IModuleInfo.load() without passing in bytes
	 * results in errors such as the following:
	 *
	 * Error: Unable to load resource module from http://aaronhardy.com/locales/en_US.swf
	 * Error: Unable to load style(SWF is not a loadable module): http://aaronhardy.com/fonts/Astroid.swf.
	 *
	 * Be aware that any module imported with this class may have access to anything within
	 * your application's security sandbox.
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ModuleMarshaller <span style="color: #0066CC;">extends</span> EventDispatcher
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Appears to need to be stored in the class scope, otherwise garbage collection
		 * wipes out some of the event handling within IModuleInfo.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> module:IModuleInfo;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Constructor.
		 * @param url The external module to load into the application.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ModuleMarshaller<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">url</span> = <span style="color: #0066CC;">url</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Starts the loading process by loading in the module as bytes.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadModule<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:URLLoader
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> urlRequest:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> urlLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span>urlRequest<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #006600;">dataFormat</span> = URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>;
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, bytesLoadedHandler<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, errorHandler<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SecurityErrorEvent.<span style="color: #006600;">SECURITY_ERROR</span>, errorHandler<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> urlLoader;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Once the module bytes are loaded, convert them back into a module within the
		 * application's security domain.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> bytesLoadedHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> styleModuleBytes:ByteArray = ByteArray<span style="color: #66cc66;">&#40;</span>URLLoader<span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			module = ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			module.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ModuleEvent.<span style="color: #006600;">READY</span>, modReadyHandler<span style="color: #66cc66;">&#41;</span>;
			module.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ModuleEvent.<span style="color: #0066CC;">ERROR</span>, errorHandler<span style="color: #66cc66;">&#41;</span>;
			module.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, styleModuleBytes<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Once the module information is loaded, use the factory to create an instance of the
		 * module.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> modReadyHandler<span style="color: #66cc66;">&#40;</span>event:ModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dispatchEvent<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Reports errors that may have occurred.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> errorHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>event is ModuleEvent<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>event is ErrorEvent<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ModuleEvent<span style="color: #66cc66;">&#40;</span>ModuleEvent.<span style="color: #0066CC;">ERROR</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>,
					ErrorEvent<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you&#8217;re loading a style module, you may also want this class, which ensures the style module takes effect in your application immediately after the module is loaded.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">aaronhardy</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">ModuleEvent</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Provides the module marshalling functionality of ModuleMarshaller with the ability to
	 * update styles immediately after the module is loaded.
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StyleModuleMarshaller <span style="color: #0066CC;">extends</span> ModuleMarshaller
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		 * If true, forces an immediate update of the application's styles after the module is
		 * loaded.
		 */</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> updateStylesImmediately:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Constructor.
		 * @param url The external module to load into the application.
		 * @param update If true, forces an immediate update of the application's styles after
		 *        the module is loaded.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StyleModuleMarshaller<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>, updateStylesImmediately:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">updateStylesImmediately</span> = updateStylesImmediately;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * @inheritDoc
		 */</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> modReadyHandler<span style="color: #66cc66;">&#40;</span>event:ModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ModuleManager.<span style="color: #006600;">getModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>updateStylesImmediately<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">Singleton</span>.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mx.styles::IStyleManager2&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">styleDeclarationsChanged</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			dispatchEvent<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>The Dirty Details</h3>
<p>So, why does the ModuleMarshaller function without errors while Flex&#8217;s built-in classes do not?  You might not care, but in case you do, here&#8217;s what I know&#8211;and don&#8217;t know.  First, all the built-in functions end up using ModuleInfo.load() found in ModuleManager.as.  This function uses a Loader to load in the contents of the module.  When the loaded SWF file is accessible and ready for use (i.e., the Loader&#8217;s LoaderInfo object dispatches an Event.INIT event), ModuleInfo.initHandler() is called.  In this function we have the following segment of code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">factoryInfo = <span style="color: #000000; font-weight: bold;">new</span> FactoryInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">try</span>
<span style="color: #66cc66;">&#123;</span>
    factoryInfo.<span style="color: #006600;">factory</span> = loader.<span style="color: #006600;">content</span> as IFlexModuleFactory;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">error</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>factoryInfo.<span style="color: #006600;">factory</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> moduleEvent:ModuleEvent = <span style="color: #000000; font-weight: bold;">new</span> ModuleEvent<span style="color: #66cc66;">&#40;</span>
        ModuleEvent.<span style="color: #0066CC;">ERROR</span>, event.<span style="color: #006600;">bubbles</span>, event.<span style="color: #006600;">cancelable</span><span style="color: #66cc66;">&#41;</span>;
    moduleEvent.<span style="color: #0066CC;">bytesLoaded</span> = <span style="color: #cc66cc;">0</span>;
    moduleEvent.<span style="color: #0066CC;">bytesTotal</span> = <span style="color: #cc66cc;">0</span>;
    moduleEvent.<span style="color: #006600;">errorText</span> = <span style="color: #ff0000;">&quot;SWF is not a loadable module&quot;</span>;
    dispatchEvent<span style="color: #66cc66;">&#40;</span>moduleEvent<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Notice how it takes the content from the loader and attempt to cast it as an IFlexModuleFactory object.  In this case, loader.content is NOT an IFlexModuleFactory instance, resulting in factoryInfo.factory being set to null.  Later in the function, it checks to see if factoryInfo.factory is null.  If it is, an error is thrown.  This is the origin of the errors you see in your application.</p>
<p>So why does ModuleMarshaller work?  When ModuleMarshaller makes this call:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">module.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, styleModuleBytes<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It ultimately ends up in the same ModuleInfo.initHandler() function as we saw above.  In both cases the Flex Builder debugger shows that loader.content is of type GeneratedResourceModule1975496797516746634_mx_core_FlexModuleFactory.  However, when we load the module using the raw bytes loaded with a URLLoader, it IS able to cast the object as an IFlexModuleFactory object, resulting in a non-null factoryInfo.factory object and avoiding any error.</p>
<p>Why such similarity yet one is able to be cast while the other is not?  I don&#8217;t know.  That&#8217;s as far as I can go in my debugging quests with what&#8217;s open source.  I assume it has to do with the <a href="http://www.adobe.com/devnet/flashplayer/articles/flash_player_9_security.pdf" target="_blank">Flash Player security</a> but I haven&#8217;t read anything that would specifically explain why the content cannot be cast as an IFlexModuleFactory object when using built-in Flex module loading methods.  If you can fill in the gap or have questions, please feel free to post a comment.  Bon appetit!</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/flex/loading-a-remote-module-into-a-local-app/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Rain SVG</title>
		<link>http://aaronhardy.com/flex/rain-svg/</link>
		<comments>http://aaronhardy.com/flex/rain-svg/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 17:46:20 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[graphic editor]]></category>
		<category><![CDATA[product designer]]></category>
		<category><![CDATA[raster]]></category>
		<category><![CDATA[scalable vector graphics]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://aaronhardy.com/?p=255</guid>
		<description><![CDATA[Businesses win customers when they deliver what customers want. Customers get what they want when they can customize products to their own preferences. At Rain, where I work, we recognize the profit potential that clients can achieve with this concept and have created many applications allowing users to design their own products. How do we [...]]]></description>
			<content:encoded><![CDATA[<p>Businesses win customers when they deliver what customers want.  Customers get what they want when they can customize products to their own preferences.  At <a href="http://mediarain.com" target="_blank">Rain</a>, where I work, we recognize the profit potential that clients can achieve with this concept and have created many applications allowing users to design their own products.  How do we do it?  Let&#8217;s talk SVG.</p>
<p>Suppose we create a simple product designer containing a single rectangle.  The user can rotate, scale, position, and color the rectangle. The rectangle will then be printed in spectacular fashion on the customer&#8217;s very own poster.  So, Jim hops into the application and moves the rectangle to the center of the poster, doubles its size, colors it blue, then rotates it 45 degrees.  He then saves his design.</p>
<p>How is Jim&#8217;s design saved?  That is, how is Jim&#8217;s design described in such a way that (1) he can later re-open it and continue modifying his rectangle and (2) a printing shop can print his poster at virtually any size without degrading its quality?<span id="more-255"></span></p>
<p>The most common way to save an image is by using a <a href="http://en.wikipedia.org/wiki/Raster_graphics" target="_blank">raster format</a> such as gif or jpg.  While there are very good use cases for such formats, this is not one of them.  A raster format describes an image by using a grid of pixels with each pixel carrying a color value.  For example, the pixel at 1, 1 is red; the pixel at 1, 2 is green; and so on.  Put all the pixels together and your eye sees an image.</p>
<p>Raster images don&#8217;t fulfill our requirements for a couple reasons.  First, raster images have no knowledge of internal elements.  It doesn&#8217;t store information about which pixels belong to a rectangle and which belong to a circle.  An application may be able to analyze the pixels and make a good guess, but because the &#8220;grid of pixels&#8221; is only two-dimensional, once elements begin to overlap and become more complex even guessing turns impossible.  This would greatly restrict the ability for our application to re-load a user&#8217;s work for further editing.</p>
<p>Second, because raster images are a grid of pixels with each pixel carrying a color value, the image cannot scale to an arbitrary size without loss of apparent quality, otherwise known as pixelation.  To illustrate, imagine an image one inch wide that has 300 pixels on the first row of the pixel grid. You later decide you would like to print the image at two inches wide instead of one inch wide.  The 300 pixels now must be spread across an area twice as wide as the original.  While the image is made larger, no additional detail is available for the added area.  The human eye begins to notice this extrapolation of data resulting in a loss of apparent quality.</p>
<p>So how do we store the data then?  We use a <a href="http://en.wikipedia.org/wiki/Vector_graphics" target="_blank">vector format</a>.  Rather than storing image data in a pixel grid, vector graphics store data by using points, lines, curves, and shapes.  Because the descriptors are all based on mathematics, the image can be enlarged to an infinite size without loss of quality.  Likewise, a certain knowledge of the design&#8217;s individual elements is retained and can be used when loading a design for further editing.  Below is a graphical comparison of vector and raster (bitmap) art:</p>
<div style="background-color:#ffffff; padding: 12px; text-align: center">
<img src="http://aaronhardy.com/wp-content/uploads/2009/07/vectorbitmapexample.png" alt="Vector vs. Raster (bitmap)" title="Vector vs. Raster (bitmap)" width="350" height="450" class="aligncenter size-full wp-image-266" style="display:block; margin-left: auto; margin-right:auto" />
</div>
<p>In 1999, a group of industry leaders collaborated to make a vector specification called <a href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" target="_blank">Scalable Vector Graphics</a> (SVG). Although other vector formats existed previously, they each carried various restrictions or encumbrances.  SVG was designed as a lightweight, open standard that could easily be used amongst a variety of applications on a variety of platforms.  Design and printing applications could easily store and exchange vector design information.</p>
<p>While SVG is a great format for storing design data, users still need an environment where they can interact with design elements and have all the functionality of a full application.  Using Flex, Rain is able to leverage both the ubiquitous Flash player browser plugin for interactivity and SVG for persistent storage.  Rain has developed a library which translates what users see in the browser to a SVG file stored on the server.  If users wish to continue editing their design, the SVG can be loaded back into the product designer.  When the user is ready to order, the SVG document can then be shipped off to the printer.</p>
<p>While that might seem impressive, the real power of Rain&#8217;s SVG library comes from the complementary tools built to empower clients and their users.  For starters, users can rotate, scale, crop, pan, color, add text, mask, draw, and drag-and-drop.  Clients can guide users by specifying specific areas to drag-and-drop photos, enabling or disabling certain tools for individual elements or types of elements, and providing pre-built design templates.</p>
<p>Because the library is our own, we can tweak it as necessary.  We understand that every product designer is unique and carries its own set of business rules and capabilities.  We have experience implementing product designers for a wide variety of products from logos to shirts to sports trading cards to keychains.  Dream away and we&#8217;ll make it happen, cap&#8217;n.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/flex/rain-svg/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

