Spare Cycles

Implementing the Singleton Pattern in Flex

May 6, 2009 · Leave a Comment

One of the things that wasn’t immediately clear to me when I started working with Flex was how to implement a Singleton. In ActionScript 3, a developer can’t write static classes. So it takes some discipline on the developer’s part to to set up a class correctly so that it can be used as a singleton.

After trying a couple of different approaches, I settled on the following approach that takes advantage of accessor methods, and the ability of static methods to reference private members inside of their own class.

Let’s create a Carpenter class, which contains a set of tools: hammer, screwdriver, and clamp. We only need one carpenter and one set of tools throughout our application, but we’re going to make use of them frequently.

Here’s the class definition:

package com.risingspiral.example {

	public class Carpenter {

		private static var _hammer : Hammer;
		private static var _screwdriver : Screwdriver;
		private static var _clamp : Clamp;

		private static function get hammer() : Hammer {
			if (!_hammer) {
				_hammer = new Hammer;
			}
                        return _hammer;
		}

		private static function get screwdriver() : Screwdriver {
			if (!_screwdriver) {
				_screwdriver = new Screwdriver;
			}
                        return _screwdriver;
		}

		private static function get clamp() : Clamp {
			if (!_clamp) {
				_clamp = new Clamp;
			}
                        return _clamp;
		}

		public static function turnTheScrew(screw : Screw) : void {
			screwdriver.screw(screw);
		}

		public static function poundTheNail(nail : Nail) : void {
			hammer.pound(nail);
		}

		public static function tightenJoint(joint : Joint) : void {
			clamp.apply(joint);
		}

	}
}

Here’s the implementation:

var nail : Nail = new Nail();
var screw : Screw = new Screw();
var joint : Joint = new Joint();

Carpenter.poundTheHammer(nail);
Carpenter.turnTheScrew(screw);
Carpenter.tightenJoint(joint);

Ok, let’s breakdown how this is working.

First I define a set of tools that are private and static to the class:

private static var _hammer : Hammer;
private static var _screwdriver : Screwdriver;
private static var _clamp : Clamp;

Then, I define a private getter function for each of these members. This prevents the implementation from getting a copy of any of these tools and taking up memory with more instances of a tool class than we need.

private static function get hammer() : Hammer {
	if (!_hammer) {
		_hammer = new Hammer;
	}
        return _hammer;
}

Note that I don’t have a corresponding setter defined for the variable. Instead, the first time that the member is accessed, I check to see if it’s null. If it is, then we create a new instance of the tool.

I can now reference the hammer property from any of the public static methods defined in my class and exercise its methods, like pound().

I hope you find this useful. Please feel free to point out any improvements or potential downsides of this approach.

→ Leave a CommentCategories: Flex

RTFM #81: Removing a property from an object in Flex

March 25, 2009 · 2 Comments

I spent an hour plus today trying to figure out how to remove a property from a generic object in Flex. I was just about to extend ObjectProxy and try to make use of its protected deleteProperty() method when I found just the right set of search terms on google that led me to the ActionScript ‘delete’ operator

delete myObject.myProperty;

Voila. Works as advertised.

→ 2 CommentsCategories: Flex

Flock Moving to Chrome?

March 3, 2009 · Leave a Comment

I just caught up with the story that Flock may be switching to chrome as its browser engine. I can’t say I’m excited. I love Flock, but part of the reason I love it is I can incorporate all of my firefox plug-ins with a lot of social media hooks.

A Flock without the web developer toolbar, Firebug, yslow, del.icio.us integration, or the opportunity to add any other cool firefox plug-ins doesn’t interest me.

→ Leave a CommentCategories: Uncategorized

Making FlexMonkey and Mate Property Injection Play Nice Together

February 28, 2009 · 3 Comments

For the past month I’ve had my head buried in the world of Flex programming. It’s an exciting world where rich interfaces can be easily prototyped and built out. Overall, I’m finding that the general enterprise development architecture for the platform is very young. And this can lead to some problems.

Two areas I’ve been focusing on are implementing a test strategy and framework for Flex applications, and implementing an MVC solution for flex applications.

For testing, I’ve been using FlexMonkey, which despite its youth, is a very handy application for testing stand-alone flex applications. It’s not so handy when it comes to testing an integrated application of traditional HTML based web-app which interacts with a Flex-driven application, but that’s an issue for another blog post.

I’ve also been using Mate for MVC implementation. But Mate is more than simply an MVC impl for Flex. It also provides IoC implementation, and a nice tag-based event handling and mapping architecture for interacting with the server.

For the past 24 hours I’ve been wrestling with an issue that emerged when our application was being tested inside of the FlexMonkeyLauncher.swf. We were encountering numerous ActionScript-style NPEs. Objects that were supposed to be bound and populated after a login event, were coming back null. But this was only happening when run inside of the FlexMonkey test runner. When our application was running standalone everything behaved fine.

Each of the bindable variables had one thing in common. They were being populated by Mate property injectors. Think of spring bean injection, but for Flex. When the application was run in FlexMonkey the property injection was silently failing. Eventually, I determined that because our app was not the parent app when FlexMonkey was involved, the Mate binder was not finding the target views in the run-time display list, and property injection was failing silently. It would be nicer if there was a debug log saying something like “Hey, that view you said had that one variable I should use? Remember that one? I can’t find it, so I’m exiting. Have a nice day!”

In the end, the solution was to use the InjectorRegistry singleton provided by Mate to explicitly register any views in my application which contained public variables which needed to be hooked up for bindable property injection. Here’s an example:

<!–
Here’s my Flex UIComponent instance, in this case a VBox.
Under normal circumstances it would have more attributes, but
I’m only calling out the necessary one for the example
–>
<mx:VBox initialize=”onInit()” xmlns:mate=”http://mate.asfusion.com/”>

<mx:Script>
<![CDATA[
// Import the injector registry
import com.asfusion.mate.ioc.InjectorRegistry;

// Define the function to run when the component/view is initialized
private function onInit() : void {
InjectorRegistry.register(this); // Add this VBox to the injector registry
}

[Bindable]

public var myBindableArray : ArrayCollection;
]]>
</mx:Script>

</mx:VBox>

I tried a couple of other approaches, including the <InjectorTarget /> tag, but that didn’t work how I expected it to. I’d definitely be happy to hear from Mate or FlexMonkey folks who have a better workaround for this. But if you’ve run into this incompatibility, hopefully this solution will save you some time.

→ 3 CommentsCategories: Flex

My Experience with Windows

February 28, 2009 · Leave a Comment

On the first day of my new job I was handed a Windows laptop. This came as a bit of a surprise because prior to my start date they had asked me whether I wanted to work on Mac or Windows, and I said Mac.

it was quickly explained to me that a certain set of circumstances had resulted in me receiving a bulky Dell machine with Windows XP loaded on it, in lieu of an Apple machine, and that some time over the next few weeks I would be getting a Mac.

In the meantime, I would have to make due with Windows. I actually hadn’t used Windows as my primary operating system for almost eight years. The last time I’d used it was when I was managing the web site at Carthage College. I was dreading having to work on the machine as my main development environment while learning my role in a new company, and learning a new programming platform (Flex).

I was actually surprised at how (relatively) comfortable I was on the machine. I’ve of course used Windows for years to do testing and debugging for web applications, but not as my main development machine. Cygwin was definitely handy. There are lots of powerful command-line interactions that I can achieve much quicker in a *nix shell than in DOS. So whenever I needed to do something familiar on the command-line, but didn’t know the best way to do it in DOS, I could always fall back on the Cygwin prompt.

After a couple of weeks, I finally got my nice 17″ Macbook Pro and dropped the Dell off at the sys. admin.’s desk. The experience reminded me of why I don’t use Windows. One of the major annoyances was having to start and stop my resin server with each deployment, even though I had hot deployment turned on. This is more of a deficiency in Resin, I think, than in Windows.

But one of the frustrating aspects of this excercise was that after a resin server was stopped, Windows still hadn’t unlocked the files that the Resin server was using. There was always some indeterminate lag-time between the time the server stopped and the time that the files in its deployment directory would become available again. This is a tiny thing in the scope of an overall operating system, but in iterative development it’s HUGE. Enough to remind a developer like me why I use a *nix environment.

This blog post created on a Mac.

→ Leave a CommentCategories: Flex

Eliminating Feed Noise

January 29, 2009 · Leave a Comment

Last month I did a major overhaul of my feed reader’s subscriptions. I was tired of the repeated content and stories I was seeing across tech feeds. Whenever a company had an announcement or bit of news, every single publication I followed would release a write-up.

I resolved to trim my tech feedss down to no more than 5 as part of an overall RSS pruning. Here were the criteria that I used to determine which feeds to keep:

  • I find myself gravitating towards that feed’s stories in general
  • Engaging editorial perspective
  • Provides a broad overview of relevant industry content
  • Provides actionable information
  • Professional (not gossip-y)
  • Does more than simply replay the same press releases out there

For tech news, this generally meant clearing out all of the personality-driven blogs: TechCrunch, Scobleizer, GigaOM, Silicon Alley Insider, Valleywag, etc. They’re all gone. The sacrifice is that I’m not totally up to date on the latest rounds of fundings of all the bay area start ups. I don’t know what the new feature is coming out of kyte.tv as soon as everyone else does, and I don’t have to sift through piles and piles and piles of unsubstantiated hear-say and gossip to find some relevant kernel of information.

For tech news I now just follow these sites:

  • TechMeme – it gives me a quality broad overview of what’s going on in the tech industry and pulls in the best of the sites that I dropped from my feed list
  • Wired – Quality original content alongside good coverage of major industry events
  • Ars Technica – There’s a lot of bloat in this publication, but I have to keep it around for its quality coverage of privacy, security, and innovation in tech.
  • ReadWriteWeb – The best, IMO, of the Web 2.0 coverage sites. I learn about new tools and their usefulness to me. This is contrary to TechCrunch which generally seems more concerned with the personalities and business implications of any new application released on-line. Plus RWW has more intelligent commentary than Arrington’s blathering.
  • Slashdot – Simply because I continue to learn interesting things from this site that I generally don’t find anywhere else.

There are some other publications that I’ve enjoyed for years which didn’t make the cut. The Register is one. There’s just TOO much content coming through on their main feed. It’s too much noise.

If you had only five tech feeds you needed to follow, what would they be and why?

→ Leave a CommentCategories: Uncategorized

iPhone and iCal ToDo Sync with Appigo

January 19, 2009 · 2 Comments

Today I finally have ToDo syncing between iCal and my iPhone. It’s a feature that I’ve had on my cellular phones since, oh… about 2003. It is something I lost when I switched to an iPhone, and is one of the features that I can’t believe Apple decided not to tackle.

Thanks to appigo’s sync software, and their ToDo iPhone app, I can now manage my tasks from my desktop and my iPhone. The downside is that I had to pay $9.99 to get the iPhone app and this feature.

Well done to appigo for picking up where Apple left off. Now I can get back to efficiently adding items to my ToDo list from where every I am, and very inefficiently checking them off.

→ 2 CommentsCategories: Uncategorized

Playing around with maatkit

January 13, 2009 · 1 Comment

I was doing some MySQL query performance tuning on DGM Live today using maatkit. Maatkit is a toolkit for measuring, what else, MySQL performance. The two tools that I played around with specifically were the slow query log parser and the query profiler.

I don’t use perl a whole lot, so the first thing I had to do was install perl’s DBI module.

I jumped into the cpan prompt:

shell> cpan

And then installed the module:

cpan> install DBI

And then force installed DBD::mysql. I had to use force install so that when the tests to connect to my local MySQL server failed, it didn’t rollback the install. I know that the MySQL server is working and can be reached from my local box, so I don’t care about the tests failing:

cpan> force install DBD::mysql

I then exit out of the cpan shell and the maatkit scripts are now part of my executable path. First, I ran the mk-query-profiler script over a couple of sluggish queries that were stored in a .sql file. NOTE: It’s important that each query in the SQL file not only have a semi-colon at the end of the statement, but if you’re analyzing multiple queries then there as to be a full line of white space between the queries, not just a line break.

shell> mk-query-profiler --user backend --askpass --database dgmms ~/queries.sql

The output shows an overview of the information provided by the show status call provided by MySQL.

+----------------------------------------------------------+
|                      2 (3.1201 sec)                      |
+----------------------------------------------------------+

__ Overall stats _______________________ Value _____________
   Total elapsed time                        6.231
   Questions                                 2
     COMMIT                                  0
     DELETE                                  0
     DELETE MULTI                            0
     INSERT                                  0
     INSERT SELECT                           0
     REPLACE                                 0
     REPLACE SELECT                          0
     SELECT                                  2
     UPDATE                                  0
     UPDATE MULTI                            0
   Data into server                        944
   Data out of server                   169031
   Optimizer cost                        10919.360

__ Table and index accesses ____________ Value _____________
   Table locks acquired                      6
   Table scans                               0
     Join                                    0
   Index range scans                         0
     Join without check                      0
     Join with check                         0
   Rows sorted                             137
     Range sorts                             0
     Merge passes                            0
     Table scans                             2
     Potential filesorts                     2

The key value that’s helpful to see here, either as a bulk value for all queries or on a per-query basis, is the optimizer cost. I want to get this number as low as possible. I can add a -s flag to my command to break the results out by query.

+----------------------------------------------------------+
|                   QUERY 1 (3.0744 sec)                   |
+----------------------------------------------------------+
SELECT COUNT...

__ Overall stats _______________________ Value _____________
   Elapsed time                              3.074
   Data into server                        478
   Data out of server                    11224
   Optimizer cost                         5459.680

__ Table and index accesses ____________ Value _____________
   Table locks acquired                      3
   Table scans                               0
     Join                                    0
   Index range scans                         0
     Join without check                      0
     Join with check                         0
   Rows sorted                              10
     Range sorts                             0
     Merge passes                            0
     Table scans                             1
     Potential filesorts                     1

+----------------------------------------------------------+
|                   QUERY 2 (3.0618 sec)                   |
+----------------------------------------------------------+
SELECT COUNT...

__ Overall stats _______________________ Value _____________
   Elapsed time                              3.062
   Data into server                        466
   Data out of server                   157807
   Optimizer cost                         5459.680

__ Table and index accesses ____________ Value _____________
   Table locks acquired                      3
   Table scans                               0
     Join                                    0
   Index range scans                         0
     Join without check                      0
     Join with check                         0
   Rows sorted                             127
     Range sorts                             0
     Merge passes                            0
     Table scans                             1
     Potential filesorts                     1

+----------------------------------------------------------+
|                   QUERY 2 (3.0618 sec)                   |
+----------------------------------------------------------+

__ Overall stats _______________________ Value _____________
   Total elapsed time                        6.136
   Questions                                 2
     COMMIT                                  0
     DELETE                                  0
     DELETE MULTI                            0
     INSERT                                  0
     INSERT SELECT                           0
     REPLACE                                 0
     REPLACE SELECT                          0
     SELECT                                  2
     UPDATE                                  0
     UPDATE MULTI                            0
   Data into server                        944
   Data out of server                   169031
   Optimizer cost                        10919.360

__ Table and index accesses ____________ Value _____________
   Table locks acquired                      6
   Table scans                               0
     Join                                    0
   Index range scans                         0
     Join without check                      0
     Join with check                         0
   Rows sorted                             137
     Range sorts                             0
     Merge passes                            0
     Table scans                             2
     Potential filesorts                     2

The difference between these two queries is that one of them has a LIMIT clause to reduce the result set size, which is the reason for the difference in teh size of the ‘Data out of server’, but it doesn’t make any difference to the optimization cost.

This was a difficult query to tune because the data was joined across three tables that all had a high cardinality, and the row traversals were minimal. I determined that a GROUP BY clause on the largest of the tables was causing a lot of CPU consumption which was slowing the query down. In the end, I decided to break the larger complex query up into two simpler queries. The total execution time was cut down to almost a tenth of the original query execution time, and resulted in less work for the processor overall.

The query profiler didn’t lead me to that conclusion right away, but it provided a very useful tool to quickly try different approaches and test them for performance.

The query profiler can do more than just take a file or standard input of queries. It can also analyze the performance of queries run against the server, by aggregating the show status information for all the queries. You simply use the –external flag instead of passing in a file and press ENTER when you’re done profiling the server.

This is very handy for getting a quick sense of the database overhead on a single page or action in your web application. I used ab, the apache benchmarking utility, to load test the server and ran the query profiler against the load test to get a better picture of how many queries are run and their performance when the server is under heavy load.

The query profiler is only one piece of the maatkit toolkit, and I look forward to playing with the rest of the available scripts to further tune the database layer of my applications.

→ 1 CommentCategories: MySQL
Tagged: , , , ,

Purely Anecdotal #256: The Power of Facebook

January 7, 2009 · Leave a Comment

This is purely anecdotal…

But, recently I was reading on a friend’s blog about how much they liked the facebook chat tool and how it had changed their lives.

Basically, this person had never really used IM chat before. The reason was that it involved installing new software and learning a new communications paradigm.

Facebook’s footer toolbar forces chat on its users, and it does so in a way that’s more intuitive than the built-in chat interfaces in google mail, yahoo! mail, or AOL.

Facebook continues to grow exponentially and attract a wider demographic of users. It may struggle to monetize as effectively as more established content powerhouses, but the latent power of the platform to achieve experiences like that of my friend are really amazing successes that will continue to entrench Facebook as THE social network, regardless of its current size relative to MySpace, Google, or Yahoo! in terms of the number of users.

→ Leave a CommentCategories: Uncategorized

Internet Authentication Needs To Change

January 5, 2009 · Leave a Comment

Today’s big tech news is the hijacking of several prominent twitter accounts. The hijack method isn’t confirmed, but it was most likely the result of a recent phishing exploit.

Twitter is very vulnerable to phishing attacks. In order for third parties to interact with a twitter account they need the authentication credentials. There are a number of useful services, Snaptweet for instance, which require your twitter user name and password to operate.

Twitter does not yet support a token-based API authentication protocol, though twitter has announced support for OAuth, but it has not been implemented yet.

Authentication is one of those internet-wide thorny problems. Not only is it a primary vector for technological security problems, but it’s vulnerable to sociological exploits as well. A technically secure application can be easily infiltrated simply by tricking a user to give you their password.

There are a number of initiatives on the internet to simplify the authentication problem, including Facebook Connect and OpenID. While “single sign-on” architectures may be ideal within an organization, like a large company or university, I think they’re inherently dangerous as a means of granting authentication potentially to any service on the internet.

I would like to see a key chain style approach to service authentication. This would have the following features:

  • Would maintain unique credentials for any service, preventing users from recycling passwords.
  • Would require identification for usage. Ideally this would incorporate eye or fingerprint recognition, or some other method of physically verifying the identity of the user, while falling back on a passphrase.
  • Would be portable, by syncing the key chain with a mobile device or networked drive.
  • Usage of the key chain away from an unregistered computer (like your home or office computers) would result in text, email, or voice mail notifications sent to points of contact. You would get alerted by unauthorized usage.
  • Implementation of the key chain would be built into the browser’s architecture.
  • Usage of your keychain would be logged, and you could look at a usage statement similar to a credit card statement at the end of the month.

Obviously there are a number of big tech hurdles there, but overcoming them would lead to an improvement of the security of the internet as a whole.

→ Leave a CommentCategories: Security