View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.facade.assetapi.listener;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import COM.FutureTense.Interfaces.ICS;
22  
23  import com.fatwire.assetapi.data.AssetId;
24  import com.fatwire.gst.foundation.facade.ics.ICSFactory;
25  import com.fatwire.gst.foundation.facade.install.AssetListenerInstall;
26  import com.fatwire.gst.foundation.facade.logging.LogUtil;
27  import com.openmarket.basic.event.AbstractAssetEventListener;
28  
29  import org.apache.commons.logging.Log;
30  
31  /**
32   * AssetEventListener that protects from multiple event fires for the same
33   * asset. It does so by registering the assets on a list on the ICS scope.
34   * 
35   * @author Dolf Dijkstra
36   * 
37   */
38  
39  public abstract class RunOnceAssetEventListener extends AbstractAssetEventListener {
40      protected final Log LOG = LogUtil.getLog(getClass());
41      private ICS ics;
42  
43      private static class RunOnceList {
44          private final Set<String> assets = new HashSet<String>();
45  
46          boolean seenBefore(final AssetId id) {
47              return !assets.add(id.toString());
48          }
49  
50          static RunOnceList find(final ICS ics, final Class<?> z) {
51              final String name = z.getName() + "-RunOnceList";
52              Object o = ics.GetObj(name);
53              if (o instanceof RunOnceList) {
54                  return (RunOnceList) o;
55              } else {
56                  o = new RunOnceList();
57                  ics.SetObj(name, o);
58                  return (RunOnceList) o;
59              }
60          }
61  
62      }
63  
64      @Override
65      public final void assetAdded(final AssetId id) {
66          LOG.debug("Asset added event received for " + id);
67          if (!seen(id)) {
68              doAssetAdded(id);
69          }
70      }
71  
72      private boolean seen(final AssetId id) {
73          final boolean s = RunOnceList.find(getICS(), getClass()).seenBefore(id);
74          LOG.debug("An event for asset " + id + " was " + (s ? "" : " not ") + " executed before.");
75          return s;
76      }
77  
78      protected abstract void doAssetAdded(AssetId id);
79  
80      @Override
81      public final void assetDeleted(final AssetId id) {
82          LOG.debug("Asset deleted event received for " + id);
83          if (!seen(id)) {
84              doAssetDeleted(id);
85          }
86      }
87  
88      protected abstract void doAssetDeleted(AssetId id);
89  
90      @Override
91      public final void assetUpdated(final AssetId id) {
92          LOG.debug("Asset updated event received for " + id);
93          if (!seen(id)) {
94              doAssetUpdated(id);
95          }
96      }
97  
98      protected abstract void doAssetUpdated(AssetId id);
99  
100     /**
101      * Install self into AssetListener_reg table
102      */
103     public final void install(final ICS ics) {
104         AssetListenerInstall.register(ics, getClass().getName(), true);
105     }
106     protected ICS getICS() {
107         return ics != null ? ics : ICSFactory.getOrCreateICS();
108     }
109 
110 
111     /* (non-Javadoc)
112      * @see com.openmarket.basic.event.AbstractAssetEventListener#init(COM.FutureTense.Interfaces.ICS)
113      */
114     @Override
115     public void init(ICS ics) {
116        this.ics=ics;
117 
118     }
119 
120     public final boolean isInstalled(final ICS ics) {
121         return AssetListenerInstall.isRegistered(ics, getClass().getName());
122     }
123 }