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