Skip to content

Instantly share code, notes, and snippets.

@cameroncf
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save cameroncf/10487348 to your computer and use it in GitHub Desktop.

Select an option

Save cameroncf/10487348 to your computer and use it in GitHub Desktop.
component
output="false"
{
public SingletonFactory function init()
output = 'false'
{ return this; }
public any function getObject(required String objectName)
output = 'false'
{
// it exists, return it
// return object
lock
timeout = '10'
throwontimeout = 'No'
name = 'SingletonFactory'
type = 'READONLY'
{
if (structKeyExists(application,'SingletonFactory') and
structKeyExists(application.SingletonFactory,arguments.objectName))
{
return application.SingletonFactory[arguments.objectName];
}
}
// create if needed
lock
timeout = '10'
throwontimeout = 'No'
name = 'SingletonFactory'
type = 'EXCLUSIVE'
{
if (not structKeyExists(application,'SingletonFactory') or
not structKeyExists(application.SingletonFactory,arguments.objectName))
{
if (not structKeyExists(application,'SingletonFactory'))
{ application.SingletonFactory = structNew(); }
if (not structKeyExists(application.SingletonFactory,arguments.objectName))
{ application.SingletonFactory[arguments.objectName] = createObject("component",arguments.objectName).init(); }
} // end create
return application.SingletonFactory[arguments.objectName];
} // end lock
} // end getObject()
public void function clearObject(String objectName)
output = 'false'
{
lock
timeout = '10'
throwontimeout = 'No'
name = 'SingletonFactory'
type = 'EXCLUSIVE'
{
if (structKeyExists(arguments,'objectName') and len(trim(arguments.objectName)))
{
structDelete(application.SingletonFactory,'arguments.objectName');
} else {
structDelete(application,'SingletonFactory');
}
}
}// end clearObject()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment