You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by gh...@apache.org on 2003/07/15 04:02:02 UTC

cvs commit: cocoon-2.1/src/blocks/eventcache/java/org/apache/cocoon/caching/impl DefaultEventRegistryImpl.java EventAwareCacheImpl.java

ghoward     2003/07/14 19:02:02

  Modified:    src/blocks/eventcache/java/org/apache/cocoon/caching/impl
                        DefaultEventRegistryImpl.java
                        EventAwareCacheImpl.java
  Log:
  stab at thread safety, and fix an NPE
  
  Revision  Changes    Path
  1.2       +66 -56    cocoon-2.1/src/blocks/eventcache/java/org/apache/cocoon/caching/impl/DefaultEventRegistryImpl.java
  
  Index: DefaultEventRegistryImpl.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/eventcache/java/org/apache/cocoon/caching/impl/DefaultEventRegistryImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultEventRegistryImpl.java	14 Jul 2003 02:50:45 -0000	1.1
  +++ DefaultEventRegistryImpl.java	15 Jul 2003 02:02:02 -0000	1.2
  @@ -104,10 +104,21 @@
        * @param key
        */
       public void register(Event e, PipelineCacheKey key) {
  -        m_keyMMap.put(key,e);
  -        m_eventMMap.put(e,key);
  +        synchronized(this) {
  +            m_keyMMap.put(key,e);
  +            m_eventMMap.put(e,key);
  +        }
       }
   
  +    /**
  +     * Remove all registered data.
  +     */
  +    public void clear() {
  +        synchronized(this) {
  +            m_keyMMap.clear();
  +            m_eventMMap.clear();
  +        }
  +    }
   
       /**
        * Retrieve all pipeline keys mapped to this event.
  @@ -125,6 +136,15 @@
       }
   
       /**
  +     * Return all pipeline keys mapped to any event
  +     */
  +    public PipelineCacheKey[] allKeys() {
  +        Set keys = this.m_keyMMap.keySet();
  +        return (PipelineCacheKey[])keys.toArray(
  +                        new PipelineCacheKey[keys.size()]);
  +    }
  +
  +    /**
        * When a CachedResponse is removed from the Cache, any entries 
        * in the event mapping must be cleaned up.
        */
  @@ -132,9 +152,10 @@
           Collection coll = (Collection)m_keyMMap.get(key);
           if (coll==null || coll.isEmpty()) {
               return;
  -        } else {
  -            // get the iterator over all matching PCK keyed 
  -            // entries in the key-indexed MMap.
  +        } 
  +        // get the iterator over all matching PCK keyed 
  +        // entries in the key-indexed MMap.
  +        synchronized(this) {
               Iterator it = coll.iterator();
               while (it.hasNext()) {
                   /* remove all entries in the event-indexed map where this
  @@ -148,59 +169,14 @@
                       m_eventMMap.remove((Event)o,key);            
                   }
               }
  +    
  +            // remove all entries in the key-indexed map where this PCK key 
  +            // is the key -- confused yet?
  +            m_keyMMap.remove(key);
           }
  -        // remove all entries in the key-indexed map where this PCK key 
  -        // is the key -- confused yet?
  -        m_keyMMap.remove(key);
  -    }
  -
  -    /**
  -     * Return the keys held as an array
  -     */
  -    public PipelineCacheKey[] allKeys() {
  -        Set keys = this.m_keyMMap.keySet();
  -        return (PipelineCacheKey[])keys.toArray(
  -                        new PipelineCacheKey[keys.size()]);
       }
   
       /**
  -     * Remove all registered data.
  -     */
  -	public void clear() {
  -        m_keyMMap.clear();
  -        m_eventMMap.clear();
  -	}
  -
  -    /** 
  -     * We must persist the data at container shutdown.  If the serialization 
  -     * fails, an error is logged but not thrown.  The missing/invalid state is 
  -     * handled at startup.
  -     */
  -    public void dispose() {
  -        ObjectOutputStream oos = null;
  -		try {
  -			oos = new ObjectOutputStream(
  -			                            new FileOutputStream(this.m_persistentFile));
  -            EventRegistryDataWrapper ecdw = new EventRegistryDataWrapper();
  -            ecdw.setupMaps(this.m_keyMMap, this.m_eventMMap);
  -            oos.writeObject(ecdw);
  -            oos.flush();
  -		} catch (FileNotFoundException e) {
  -			getLogger().error("Unable to persist EventRegistry", e);
  -		} catch (IOException e) {
  -            getLogger().error("Unable to persist EventRegistry", e);
  -		} finally {
  -            try {
  -                if (oos != null) oos.close();
  -            } catch (IOException e) {}
  -		}
  -        m_keyMMap.clear();
  -        m_keyMMap = null;
  -        m_eventMMap.clear();
  -        m_eventMMap = null;
  -	}
  -
  -    /**
        * Set up the persistence file.
        */
   	public void contextualize(Context context) throws ContextException {
  @@ -226,7 +202,41 @@
   	public boolean init() {
           return recover();
   	}
  -    
  +
  +    /** 
  +     * Clean up resources at container shutdown.  An EventRegistry must persist 
  +     * its data.  If the serialization fails, an error is logged but not thrown  
  +     * because missing/invalid state is handled at startup.
  +     */
  +    public void dispose() {
  +        ObjectOutputStream oos = null;
  +        try {
  +            oos = new ObjectOutputStream(
  +                                        new FileOutputStream(this.m_persistentFile));
  +            EventRegistryDataWrapper ecdw = new EventRegistryDataWrapper();
  +            ecdw.setupMaps(this.m_keyMMap, this.m_eventMMap);
  +            oos.writeObject(ecdw);
  +            oos.flush();
  +        } catch (FileNotFoundException e) {
  +            getLogger().error("Unable to persist EventRegistry", e);
  +        } catch (IOException e) {
  +            getLogger().error("Unable to persist EventRegistry", e);
  +        } finally {
  +            try {
  +                if (oos != null) oos.close();
  +            } catch (IOException e) {}
  +        }
  +        m_keyMMap.clear();
  +        m_keyMMap = null;
  +        m_eventMMap.clear();
  +        m_eventMMap = null;
  +    }
  +
  +    /* 
  +     * I don't think this needs to get synchronized because it should 
  +     * only be called during initialize, which should only be called 
  +     * once by the container.
  +     */
       private boolean recover() {
           if (this.m_persistentFile.exists()) {
               ObjectInputStream ois = null;
  
  
  
  1.2       +4 -1      cocoon-2.1/src/blocks/eventcache/java/org/apache/cocoon/caching/impl/EventAwareCacheImpl.java
  
  Index: EventAwareCacheImpl.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/eventcache/java/org/apache/cocoon/caching/impl/EventAwareCacheImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EventAwareCacheImpl.java	14 Jul 2003 02:50:45 -0000	1.1
  +++ EventAwareCacheImpl.java	15 Jul 2003 02:02:02 -0000	1.2
  @@ -137,7 +137,9 @@
        * @param e The Event to be processed.
        */
       public void processEvent(Event e) {
  +        if (e == null) return;
           PipelineCacheKey[] pcks = m_eventRegistry.keysForEvent(e);
  +        if (pcks == null) return;
           for (int i=0;i<pcks.length; i++) {
               if (pcks[i] != null) {
                   if (getLogger().isDebugEnabled()) {
  @@ -178,6 +180,7 @@
        */
       public void veryifyEventCache() {
           PipelineCacheKey[] pcks = m_eventRegistry.allKeys();
  +        if (pcks == null) return;
           for (int i=0; i<pcks.length; i++) {
               if (!this.containsKey(pcks[i])) {
                   m_eventRegistry.removeKey(pcks[i]);