PageRenderTime 33ms CodeModel.GetById 23ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/wheels/vendor/memcached/com/_base.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 156 lines | 130 code | 11 blank | 15 comment | 13 complexity | f8825f0aa68eec45b9e6a56be3ae432d MD5 | raw file
  1<cfcomponent output="false">
  2
  3<!---------
  4	this is the extender class for the memcached classes.  this class mostly exists to share 
  5	methods across both the memcached object and the future task object.  Mainly, what needs 
  6	to be shared is the serialize and deserialze component.  Well, really the deserialize component
  7	which needs to be able to deserialize results when they arrive through the futureTask component.
  8	
  9------->
 10
 11	variables.timeUnit = "";
 12	variables.defaultTimeoutUnit = "";
 13	variables.defaultRequestTimeout = "";
 14
 15	<cffunction name="init" access="public" output="false" returntype="any">
 16		<cfscript>
 17			/*
 18				If you want to set the default timeouts, use the set functions below.
 19			*/
 20			variables.timeUnit = createObject("java", "java.util.concurrent.TimeUnit");
 21			variables.defaultTimeoutUnit = setDefaultTimeoutUnit();
 22			variables.defaultRequestTimeout = setDefaultRequestTimeout();
 23		</cfscript>
 24		<cfreturn this>
 25	</cffunction>
 26
 27	<cffunction name="setDefaultTimeoutUnit" access="public" output="false" returntype="boolean">
 28		<cfargument name="timeoutUnit" type="string" required="false" default="MILLISECONDS"/>
 29		<cfset var isSet = false>
 30		<cfif listfind("MILLISECONDS,NANOSECONDS,MICROSECONDS,SECONDS",ucase(arguments.timeoutUnit))>
 31			<cfset variables.defaultTimeoutUnit = arguments.timeoutUnit>
 32			<cfset isSet = true>
 33		</cfif>
 34		<cfreturn isSet>
 35	</cffunction>
 36	
 37	<cffunction name="setDefaultRequestTimeout" access="public" output="false" returntype="boolean">
 38		<cfargument name="timeout" type="numeric" required="false" default="400"/>
 39		<cfset var isSet = false>
 40		<cfif arguments.timeout gt -1>
 41			<cfset variables.defaultRequestTimeout = arguments.timeout>
 42			<cfset isSet = true>
 43		</cfif>
 44		<cfreturn isSet>
 45	</cffunction>
 46
 47	<cffunction name="serialize" access="private" output="false" returntype="any"
 48		hint="Serializes the given value from a byte stream.">
 49		<cfargument name="value" required="true" />
 50		<cfscript>
 51			var byteOutStream = CreateObject("java", "java.io.ByteArrayOutputStream").init();
 52			var objOutputStream = CreateObject("java", "java.io.ObjectOutputStream").init(byteOutStream);
 53			var ret = "";
 54			if (isSimpleValue(arguments.value))	{
 55				ret = arguments.value;
 56			} else {
 57				objOutputStream.writeObject(arguments.value);
 58				ret = byteOutStream.toByteArray();
 59				objOutputStream.close();
 60				byteOutStream.close();
 61			}
 62		</cfscript>
 63		<cfreturn ret>
 64	</cffunction>
 65
 66	<cffunction name="deserialize" access="private" output="false" returntype="any"
 67		hint="Deserializes the given value from a byte stream. this works with multiple keys being returned" >
 68		<cfargument name="value" required="true" type="any" default="" />
 69		<cfscript>
 70			var ret = "";
 71			var byteInStream = CreateObject("java", "java.io.ByteArrayInputStream");
 72			var objInputStream = CreateObject("java", "java.io.ObjectInputStream");
 73			var keys = "";
 74			var i =1;
 75			// all these trys in here are to catch null values that come across from java
 76			if ( isStruct(arguments.value) )	{
 77				// got a struct here.  go over the struct of keys and return
 78				// values for each of the items
 79				ret = structNew();
 80				keys = listToArray(structKeyList(arguments.value));
 81				for (i=1; i lte arrayLen(keys);i=i+1)	{
 82					try 	{
 83						if (structKeyExists(arguments.value,keys[i]))	{
 84							ret[keys[i]] = doDeserialize(arguments.value[keys[i]],objInputStream,byteInStream);
 85						} else {
 86							ret[keys[i]] = "";
 87						}
 88					} catch(Any excpt)	{
 89						ret[keys[i]] = "";
 90					}
 91				}
 92			}  else if ( isArray(arguments.value) and not isBinary(arguments.value) )	{
 93				// if the returned value is an array, then we need to loop over the array
 94				// and return the value  we have to check against the isBinary
 95				// because apparently coldfusion can't differentiate between an array and a binary
 96				// value
 97				ret = arrayNew(1);
 98				for (i=1; i lte arrayLen(arguments.value); i=i+1)	{
 99					try	{
100						// this try is necessary because null values can be returned
101						// from java and this is the only way we have to check for them
102						arrayAppend(ret,doDeserialize(arguments.value[i],objInputStream,byteInStream));	
103					} catch (Any excpt)	{
104						arrayAppend(ret,"");
105					}
106				}
107			} else {
108				// we either got a simple value here or we've gotten nothing returned
109				// if we get an empty value, then we pretty much assume that it's 
110				// a bum value and we'll return a false
111				try {
112					ret = doDeserialize(arguments.value,objInputStream,byteInStream);
113				} catch(Any excpt)	{
114					ret = "";
115				}
116			}
117		</cfscript>
118		<cfreturn ret />
119	</cffunction>	
120	
121	<cffunction name="doDeserialize" access="private" output="false" returntype="Any"
122			hint="this is pretty much for use by the deserialize method  please 
123			don't use this function unless absolutely necessary.  if you do use this function,
124			please remember to use try - catch around it.  java returns null values which can
125			be deadly for coldfusion.">
126		<cfargument name="value" required="true" type="any" default="" />
127		<cfargument name="objInputStream" required="false" default="#CreateObject('java', 'java.io.ObjectInputStream')#">
128		<cfargument name="byteInStream" required="false" default="#CreateObject('java', 'java.io.ByteArrayInputStream')#">
129		<Cfscript>
130			var ret = "";
131			
132				if ( isSimpleValue(arguments.value) )	{
133					ret = arguments.value;
134				} else {
135					objInputStream.init(byteInStream.init(arguments.value));
136					ret = objInputStream.readObject();
137					objInputStream.close();
138					byteInStream.close();
139				} 
140		</Cfscript>
141		<cfreturn ret>
142	</cffunction>	
143
144	<cffunction name="getTimeUnitType" output="false" access="private" returntype="any">
145		<cfargument name="timeUnit" type="string" required="true"/>
146		<cfif arguments.timeUnit eq "nanoseconds">
147			<cfreturn variables.timeUnit.NANOSECONDS>
148		<cfelseif arguments.timeUnit eq "microseconds">
149			<cfreturn variables.timeUnit.MICROSECONDS>
150		<cfelseif arguments.timeUnit eq "milliseconds">
151			<cfreturn variables.timeUnit.MILLISECONDS>
152		<cfelse>
153			<cfreturn variables.timeUnit.SECONDS>
154		</cfif>
155	</cffunction>
156</cfcomponent>