001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.components.portletregistry;
018:
019: import java.util.List;
020: import java.util.Properties;
021:
022: import org.apache.jetspeed.cache.CacheElement;
023: import org.apache.jetspeed.cache.DistributedCacheObject;
024: import org.apache.jetspeed.cache.JetspeedCache;
025: import org.apache.jetspeed.cache.impl.EhCacheElementImpl;
026: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
027: import org.apache.ojb.broker.Identity;
028: import org.apache.ojb.broker.PersistenceBroker;
029: import org.apache.ojb.broker.cache.ObjectCache;
030:
031: /**
032: * OJB cache
033: *
034: * @author dtaylor
035: *
036: */
037: public class RegistryPortletCache implements ObjectCache {
038: private static JetspeedCache oidCache;
039: private static JetspeedCache nameCache;
040: private static PortletRegistry registry;
041: private static List listeners = null;
042:
043: public RegistryPortletCache(PersistenceBroker broker,
044: Properties props) {
045: }
046:
047: public synchronized static void cacheInit(PortletRegistry r,
048: JetspeedCache o, JetspeedCache n, List l) {
049: registry = r;
050: oidCache = o;
051: nameCache = n;
052: listeners = l;
053: }
054:
055: public Object lookup(Identity oid) {
056: return cacheLookup(oid);
057: }
058:
059: public synchronized static Object cacheLookup(Identity oid) {
060: CacheElement element = oidCache.get(oid);
061: if (element != null) {
062: return element.getContent();
063: }
064: return null;
065: }
066:
067: /* (non-Javadoc)
068: * @see org.apache.ojb.broker.cache.ObjectCache#cache(org.apache.ojb.broker.Identity, java.lang.Object)
069: */
070: public void cache(Identity oid, Object obj) {
071: cacheAdd(oid, obj);
072: }
073:
074: public synchronized static void cacheAdd(Identity oid, Object obj) {
075: oidCache.remove(oid);
076: CacheElement entry = new EhCacheElementImpl(oid, obj);
077: oidCache.put(entry);
078:
079: PortletDefinitionComposite pd = (PortletDefinitionComposite) obj;
080: DistributedCacheObject wrapper = new RegistryCacheObjectWrapper(
081: oid, pd.getUniqueName());
082: nameCache.remove(pd.getUniqueName());
083: CacheElement nameEntry = nameCache.createElement(pd
084: .getUniqueName(), wrapper);
085: nameCache.put(nameEntry);
086:
087: if (listeners != null) {
088: for (int ix = 0; ix < listeners.size(); ix++) {
089: RegistryEventListener listener = (RegistryEventListener) listeners
090: .get(ix);
091: listener
092: .portletUpdated((PortletDefinitionComposite) obj);
093: }
094: }
095: }
096:
097: /* (non-Javadoc)
098: * @see org.apache.ojb.broker.cache.ObjectCache#clear()
099: */
100: public void clear() {
101: cacheClear();
102: }
103:
104: public synchronized static void cacheClear() {
105: oidCache.clear();
106: nameCache.clear();
107: }
108:
109: /* (non-Javadoc)
110: * @see org.apache.ojb.broker.cache.ObjectCache#remove(org.apache.ojb.broker.Identity)
111: */
112: public void remove(Identity oid) {
113: cacheRemove(oid);
114: }
115:
116: /**
117: * cacheRemove
118: *
119: * Remove identified object from object and node caches.
120: *
121: * @param oid object identity
122: */
123: public synchronized static void cacheRemove(Identity oid) {
124: PortletDefinitionComposite pd = (PortletDefinitionComposite) cacheLookup(oid);
125: if (pd == null)
126: return;
127:
128: oidCache.remove(oid);
129: nameCache.remove(pd.getUniqueName());
130:
131: if (listeners != null) {
132: for (int ix = 0; ix < listeners.size(); ix++) {
133: RegistryEventListener listener = (RegistryEventListener) listeners
134: .get(ix);
135: listener.portletRemoved(pd);
136: }
137: }
138: }
139:
140: public synchronized static void cacheRemoveQuiet(String key,
141: RegistryCacheObjectWrapper w) {
142: RegistryCacheObjectWrapper wrapper = w;
143: if (wrapper == null) {
144: wrapper = (RegistryCacheObjectWrapper) nameCache.get(key);
145: if (wrapper == null)
146: return;
147: }
148: Identity oid = wrapper.getId();
149:
150: PortletDefinitionComposite pd = (PortletDefinitionComposite) cacheLookup(oid);
151: if (pd == null)
152: return;
153:
154: oidCache.removeQuiet(oid);
155: nameCache.removeQuiet(pd.getUniqueName());
156: }
157:
158: }
|