View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.components.portletregistry;
18  
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.apache.jetspeed.cache.CacheElement;
23  import org.apache.jetspeed.cache.DistributedCacheObject;
24  import org.apache.jetspeed.cache.JetspeedCache;
25  import org.apache.jetspeed.cache.impl.EhCacheElementImpl;
26  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
27  import org.apache.ojb.broker.Identity;
28  import org.apache.ojb.broker.PersistenceBroker;
29  import org.apache.ojb.broker.cache.ObjectCache;
30  
31  /***
32   * OJB cache 
33   * 
34   * @author dtaylor
35   *
36   */
37  public class RegistryPortletCache implements ObjectCache
38  {
39      private static JetspeedCache oidCache;
40      private static JetspeedCache nameCache;
41      private static PortletRegistry registry;
42      private static List listeners = null;
43      
44      public RegistryPortletCache(PersistenceBroker broker, Properties props)
45      {
46      }
47      
48      public synchronized static void cacheInit(PortletRegistry r, JetspeedCache o, JetspeedCache n, List l)
49      {
50          registry = r;
51          oidCache = o;
52          nameCache = n;
53          listeners = l;
54      }
55  
56      public Object lookup(Identity oid)
57      {
58          return cacheLookup(oid);
59      }    
60      public synchronized static Object cacheLookup(Identity oid)
61      {
62          CacheElement element = oidCache.get(oid);
63          if (element != null)
64          {
65              return element.getContent();
66          }
67          return null;
68      }        
69      
70      /* (non-Javadoc)
71       * @see org.apache.ojb.broker.cache.ObjectCache#cache(org.apache.ojb.broker.Identity, java.lang.Object)
72       */
73      public void cache(Identity oid, Object obj)
74      {
75          cacheAdd(oid, obj);
76      }
77      public synchronized static void cacheAdd(Identity oid, Object obj)
78      {
79          oidCache.remove(oid);
80          CacheElement entry = new EhCacheElementImpl(oid, obj);
81          oidCache.put(entry);
82          
83          PortletDefinitionComposite pd = (PortletDefinitionComposite)obj;
84          DistributedCacheObject wrapper = new RegistryCacheObjectWrapper(oid, pd.getUniqueName());
85          nameCache.remove(pd.getUniqueName());
86          CacheElement nameEntry = nameCache.createElement(pd.getUniqueName(), wrapper);
87          nameCache.put(nameEntry);
88                 
89          if (listeners != null)
90          {        
91              for (int ix=0; ix < listeners.size(); ix++)
92              {
93                  RegistryEventListener listener = (RegistryEventListener)listeners.get(ix);
94                  listener.portletUpdated((PortletDefinitionComposite)obj);
95              }
96          }
97      }
98      
99      /* (non-Javadoc)
100      * @see org.apache.ojb.broker.cache.ObjectCache#clear()
101      */
102     public void clear()
103     {
104         cacheClear();
105     }
106     public synchronized static void cacheClear()
107     {
108         oidCache.clear();
109         nameCache.clear();
110     }
111 
112 
113     /* (non-Javadoc)
114      * @see org.apache.ojb.broker.cache.ObjectCache#remove(org.apache.ojb.broker.Identity)
115      */
116     public void remove(Identity oid)
117     {
118         cacheRemove(oid);
119     }
120     /***
121      * cacheRemove
122      *
123      * Remove identified object from object and node caches.
124      *
125      * @param oid object identity
126      */
127     public synchronized static void cacheRemove(Identity oid)
128     {
129         PortletDefinitionComposite pd = (PortletDefinitionComposite)cacheLookup(oid);
130         if (pd == null)
131             return;
132         
133         oidCache.remove(oid);
134         nameCache.remove(pd.getUniqueName());
135         
136         if (listeners != null)
137         {
138             for (int ix=0; ix < listeners.size(); ix++)
139             {
140                 RegistryEventListener listener = (RegistryEventListener)listeners.get(ix);
141                 listener.portletRemoved(pd);
142             }        
143         }
144     }
145 
146     public synchronized static void cacheRemoveQuiet(String key, RegistryCacheObjectWrapper w)
147     {
148         RegistryCacheObjectWrapper wrapper = w;
149         if (wrapper == null)
150         {
151             wrapper = (RegistryCacheObjectWrapper)nameCache.get(key);
152             if (wrapper == null)
153                 return;
154         }
155         Identity oid = wrapper.getId();
156 
157         PortletDefinitionComposite pd = (PortletDefinitionComposite)cacheLookup(oid);
158         if (pd == null)
159             return;
160      
161         oidCache.removeQuiet(oid);       
162         nameCache.removeQuiet(pd.getUniqueName());        
163     }
164     
165 }