You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ov...@apache.org on 2002/03/22 00:12:58 UTC

cvs commit: xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/components/flow ContinuationsManagerImpl.java

ovidiu      02/03/21 15:12:57

  Added:       src/scratchpad/schecoon/src/org/apache/cocoon/components/flow
                        ContinuationsManagerImpl.java
  Log:
  Added. The implementation of the ContinuationsManager interface.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/components/flow/ContinuationsManagerImpl.java
  
  Index: ContinuationsManagerImpl.java
  ===================================================================
  package org.apache.cocoon.components.flow;
  
  import java.security.SecureRandom;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.logger.AbstractLoggable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import java.util.Iterator;
  
  /**
   * The default implementation of {@link ContinuationsManager}.
   *
   * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
   * @since March 19, 2002
   * @see ContinuationsManager
   */
  public class ContinuationsManagerImpl
    extends AbstractLoggable
    implements ContinuationsManager, Component, Configurable, ThreadSafe
  {
    static final int CONTINUATION_ID_LENGTH = 20;
  
    protected SecureRandom random = null;
    protected byte[] bytes;
  
    /**
     * How long does a continuation exist in memory since the last
     * access? The time is in seconds, and the default is 3600 (1 hour).
     *
     * FIXME: Not used for the moment.
     */
    protected int timeToLive;
  
    /**
     * Maintains the forrest of <code>WebContinuation</code> trees.
     */
    protected Set forrest = Collections.synchronizedSet(new HashSet());
  
    /**
     * Association between <code>WebContinuation</code> ids and the
     * corresponding <code>WebContinuation</code> object.
     */
    protected Map idToWebCont = Collections.synchronizedMap(new HashMap());
  
    public ContinuationsManagerImpl()
      throws Exception
    {
      random = SecureRandom.getInstance("SHA1PRNG");
      random.setSeed(System.currentTimeMillis());
      bytes = new byte[CONTINUATION_ID_LENGTH];
    }
  
    public void configure(Configuration config)
    {
      this.timeToLive = config.getAttributeAsInteger("time-to-live", 3600);
    }
  
    public WebContinuation createWebContinuation(Object kont,
                                                 WebContinuation parentKont)
    {
      WebContinuation wk = new WebContinuation(kont, parentKont, this);
  
      if (parentKont == null)
        forrest.add(wk);
  
      // No need to add the WebContinuation in idToWebCont as it was
      // already done during its construction.
  
      return wk;
    }
  
    public void invalidateWebContinuation(WebContinuation wk)
    {
      WebContinuation parent = wk.getParentContinuation();
      if (parent == null)
          forrest.remove(wk);
      else {
        List parentKids = parent.getChildren();
        parentKids.remove(wk);
      }
  
      _invalidate(wk);
    }
  
    protected void _invalidate(WebContinuation wk)
    {
      idToWebCont.remove(wk.getId());
      
      // Invalidate all the children continuations as well
      List children = wk.getChildren();
      int size = children.size();
      for (int i = 0; i < size; i++)
        _invalidate((WebContinuation)children.get(i));
    }
  
    public WebContinuation lookupWebContinuation(String id)
    {
      return (WebContinuation)idToWebCont.get(id);
    }
  
    /**
     * Generate a unique identifier for a
     * <code>WebContinuation</code>. The identifier is generated using a
     * cryptographically strong algorithm to prevent people to generate
     * their own identifiers.
     *
     * <p>It has the side effect of interning the continuation object in
     * the <code>idToWebCont</code> hash table.
     *
     * @param wk a <code>WebContinuation</code> object for which the
     * identifier should be generated.
     * @return the <code>String</code> identifier of the
     * <code>WebContinuation</code>
     */
    public String generateContinuationId(WebContinuation wk)
    {
      char[] result = new char[bytes.length * 2];
      String continuationId = null;
      
      while (true) {
        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);
        synchronized(idToWebCont) {
          if (!idToWebCont.containsKey(continuationId)) {
            idToWebCont.put(continuationId, wk);
            break;
          }
        }
      }
  
      return continuationId;
    }
  
    public void displayAllContinuations()
    {
      Iterator iter = forrest.iterator();
      while (iter.hasNext())
        ((WebContinuation)iter.next()).display();
    }
  }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org