ovidiu 02/02/08 16:19:50 Added: src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap ContinuationsManager.java Log: Manages the continuation objects. Revision Changes Path 1.1 xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap/ContinuationsManager.java Index: ContinuationsManager.java =================================================================== package org.apache.cocoon.scheme.sitemap; import java.lang.Math; import java.security.SecureRandom; import java.util.HashMap; import sisc.ContinuationException; import sisc.Interpreter; import sisc.ModuleAdapter; import sisc.data.ImmutableString; import sisc.data.Value; /** * Manages the continuation objects. * * @author <a href="mailto:[EMAIL PROTECTED]">Ovidiu Predescu</a> * @since February 8, 2002 */ public class ContinuationsManager extends ModuleAdapter { static protected int CONTINUATION_ID_LENGTH = 20; public String getModuleName() { return "Continuations Manager"; } public float getModuleVersion() { return 1.0f; } public static final int REGISTER = 1, GET = 2, REMOVE = 3; /** * Random generator for continuation identifiers. */ protected SecureRandom random = null; /** * Array of bytes used internally to generate the continuation * identifier. */ protected byte[] bytes; /** * Association between string identifiers and continuation objects. */ protected HashMap continuations = new HashMap(); public ContinuationsManager() throws Exception { random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(System.currentTimeMillis()); bytes = new byte[CONTINUATION_ID_LENGTH]; define("continuations:register", REGISTER); define("continuations:get", GET); define("continuations:remove", REMOVE); } /** * The SISC evaluator function for this module. Executes the actual * Java code corresponding to the Scheme functions defined by the * module. * * @param primid an <code>int</code> value * @param r an <code>Interpreter</code> value * @return a <code>Value</code> value * @exception ContinuationException if an error occurs */ public Value eval(int primid, Interpreter r) throws ContinuationException { switch (r.vlr.length) { // One argument functions case 1: switch (primid) { case REGISTER: return registerContinuation(r.vlr[0]); case GET: return getContinuationFromId(r.vlr[0]); case REMOVE: return removeContinuationId(r.vlr[0]); } } throw new RuntimeException("Invalid number of arguments to function " + r.acc + ", got " + r.vlr.length); } /** * Registers a continuation object and returns an identifier * associated with it. * * @param kont a continuation object * @return a <code>Value</code> value, representing the string identifier */ public Value registerContinuation(Value kont) { Value kontId; char[] result = new char[bytes.length * 2]; String continuationId = null; synchronized(continuations) { do { random.nextBytes(bytes); for (int i = 0; i < CONTINUATION_ID_LENGTH; i++) { byte ch = bytes[i]; result[2 * i] = Character.forDigit(Math.abs(ch >> 4), 16); result[2 * i + 1] = Character.forDigit(Math.abs(ch & 0x0f), 16); } continuationId = new String(result); } while (continuations.containsKey(continuationId)); continuations.put(continuationId, kont); } return new ImmutableString(continuationId); } /** * Given a Scheme string identifier, return the corresponding * continuation. * * @param id the string identifier * @return the continuation object, or <code>null</code> if no such * continuation is registered. */ public Value getContinuationFromId(Value id) { String continuationId = string(id); return (Value)continuations.get(continuationId); } /** * Given a continuation id, remove the associated continuation from * the store. * * @param id the string identifier for the continuation * @return the continuation object removed */ public Value removeContinuationId(Value id) { synchronized(continuations) { return (Value)continuations.remove(string(id)); } } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]