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.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026: import java.util.prefs.BackingStoreException;
027: import java.util.prefs.Preferences;
028:
029: import org.apache.jetspeed.cache.JetspeedCache;
030: import org.apache.jetspeed.cache.JetspeedCacheEventListener;
031: import org.apache.jetspeed.components.dao.InitablePersistenceBrokerDaoSupport;
032: import org.apache.jetspeed.factory.PortletFactory;
033: import org.apache.jetspeed.om.common.MutableLanguage;
034: import org.apache.jetspeed.om.common.Support;
035: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
036: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
037: import org.apache.jetspeed.om.impl.LanguageImpl;
038: import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
039: import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
040: import org.apache.ojb.broker.query.Criteria;
041: import org.apache.ojb.broker.query.QueryFactory;
042: import org.apache.pluto.om.common.Language;
043: import org.apache.pluto.om.common.ObjectID;
044: import org.apache.pluto.om.portlet.PortletApplicationDefinition;
045: import org.apache.pluto.om.portlet.PortletDefinition;
046: import org.springframework.dao.DataAccessException;
047:
048: /**
049: * <p>
050: * OjbPortletRegistry
051: * </p>
052: * <p>
053: *
054: * </p>
055: *
056: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
057: * @version $Id: PersistenceBrokerPortletRegistry.java 516448 2007-03-09 16:25:47Z ate $
058: *
059: */
060: public class PersistenceBrokerPortletRegistry extends
061: InitablePersistenceBrokerDaoSupport implements PortletRegistry,
062: JetspeedCacheEventListener {
063: /**
064: * The separator used to create a unique portlet name as
065: * {portletApplication}::{portlet}
066: */
067: static final String PORTLET_UNIQUE_NAME_SEPARATOR = "::";
068:
069: private JetspeedCache applicationOidCache = null;
070: private JetspeedCache portletOidCache = null;
071: private JetspeedCache applicationNameCache = null;
072: private JetspeedCache portletNameCache = null;
073: private Map nameCache = new HashMap(); // work in progress (switch to JetspeedCache)
074: private List listeners = new ArrayList();
075:
076: // for testing purposes only: no need for the portletFactory then
077: public PersistenceBrokerPortletRegistry(String repositoryPath) {
078: this (repositoryPath, null, null, null, null, null);
079: }
080:
081: /**
082: *
083: */
084: public PersistenceBrokerPortletRegistry(String repositoryPath,
085: PortletFactory portletFactory,
086: JetspeedCache applicationOidCache,
087: JetspeedCache portletOidCache,
088: JetspeedCache applicationNameCache,
089: JetspeedCache portletNameCache) {
090: super (repositoryPath);
091: PortletDefinitionImpl.setPortletRegistry(this );
092: PortletDefinitionImpl.setPortletFactory(portletFactory);
093: this .applicationOidCache = applicationOidCache;
094: this .portletOidCache = portletOidCache;
095: this .applicationNameCache = applicationNameCache;
096: this .portletNameCache = portletNameCache;
097: MutablePortletApplicationProxy.setRegistry(this );
098: RegistryApplicationCache.cacheInit(this , applicationOidCache,
099: applicationNameCache, listeners);
100: RegistryPortletCache.cacheInit(this , portletOidCache,
101: portletNameCache, listeners);
102: this .applicationNameCache.addEventListener(this , false);
103: this .portletNameCache.addEventListener(this , false);
104: }
105:
106: public Language createLanguage(Locale locale, String title,
107: String shortTitle, String description, Collection keywords)
108: throws RegistryException {
109: try {
110: MutableLanguage lc = new LanguageImpl();
111: lc.setLocale(locale);
112: lc.setTitle(title);
113: lc.setShortTitle(shortTitle);
114: lc.setKeywords(keywords);
115: return lc;
116: } catch (Exception e) {
117: throw new RegistryException(
118: "Unable to create language object.");
119: }
120: }
121:
122: public Collection getAllPortletDefinitions() {
123: Criteria c = new Criteria();
124: Collection list = getPersistenceBrokerTemplate()
125: .getCollectionByQuery(
126: QueryFactory.newQuery(
127: PortletDefinitionImpl.class, c));
128: postLoadColl(list);
129: return list;
130: }
131:
132: public MutablePortletApplication getPortletApplication(ObjectID id) {
133: Criteria c = new Criteria();
134: c.addEqualTo("id", new Long(id.toString()));
135: MutablePortletApplication app = (MutablePortletApplication) getPersistenceBrokerTemplate()
136: .getObjectByQuery(
137: QueryFactory.newQuery(
138: PortletApplicationDefinitionImpl.class,
139: c));
140: postLoad(app);
141: return app;
142: }
143:
144: public MutablePortletApplication getPortletApplication(String name) {
145: Criteria c = new Criteria();
146: c.addEqualTo("name", name);
147: MutablePortletApplication app = (MutablePortletApplication) getPersistenceBrokerTemplate()
148: .getObjectByQuery(
149: QueryFactory.newQuery(
150: PortletApplicationDefinitionImpl.class,
151: c));
152: postLoad(app);
153: return app;
154: }
155:
156: public MutablePortletApplication getPortletApplicationByIdentifier(
157: String identifier) {
158: Criteria c = new Criteria();
159: c.addEqualTo("applicationIdentifier", identifier);
160: MutablePortletApplication app = (MutablePortletApplication) getPersistenceBrokerTemplate()
161: .getObjectByQuery(
162: QueryFactory.newQuery(
163: PortletApplicationDefinitionImpl.class,
164: c));
165: postLoad(app);
166: return app;
167: }
168:
169: public Collection getPortletApplications() {
170: Criteria c = new Criteria();
171: Collection list = getPersistenceBrokerTemplate()
172: .getCollectionByQuery(
173: QueryFactory.newQuery(
174: PortletApplicationDefinitionImpl.class,
175: c));
176: postLoadColl(list);
177: return list;
178: }
179:
180: public PortletDefinitionComposite getPortletDefinitionByIdentifier(
181: String identifier) {
182: Criteria c = new Criteria();
183: c.addEqualTo("portletIdentifier", identifier);
184: PortletDefinitionComposite def = (PortletDefinitionComposite) getPersistenceBrokerTemplate()
185: .getObjectByQuery(
186: QueryFactory.newQuery(
187: PortletDefinitionImpl.class, c));
188: if (def != null
189: && def.getPortletApplicationDefinition() == null) {
190: final String msg = "getPortletDefinitionByIdentifier() returned a PortletDefinition that has no parent PortletApplication.";
191: throw new IllegalStateException(msg);
192: }
193:
194: postLoad(def);
195: return def;
196: }
197:
198: public PortletDefinitionComposite getPortletDefinitionByUniqueName(
199: String name) {
200: String appName = PortletRegistryHelper.parseAppName(name);
201: String portletName = PortletRegistryHelper
202: .parsePortletName(name);
203:
204: Criteria c = new Criteria();
205: c.addEqualTo("app.name", appName);
206: c.addEqualTo("name", portletName);
207:
208: PortletDefinitionComposite def = (PortletDefinitionComposite) getPersistenceBrokerTemplate()
209: .getObjectByQuery(
210: QueryFactory.newQuery(
211: PortletDefinitionImpl.class, c));
212: if (def != null
213: && def.getPortletApplicationDefinition() == null) {
214: final String msg = "getPortletDefinitionByIdentifier() returned a PortletDefinition that has no parent PortletApplication.";
215: throw new IllegalStateException(msg);
216: }
217:
218: postLoad(def);
219: return def;
220: }
221:
222: public boolean portletApplicationExists(String appIdentity) {
223: return getPortletApplicationByIdentifier(appIdentity) != null;
224: }
225:
226: public boolean namedPortletApplicationExists(String appName) {
227: return getPortletApplication(appName) != null;
228: }
229:
230: public boolean portletDefinitionExists(String portletName,
231: MutablePortletApplication app) {
232: return getPortletDefinitionByUniqueName(app.getName() + "::"
233: + portletName) != null;
234: }
235:
236: public boolean portletDefinitionExists(String portletIdentity) {
237: return getPortletDefinitionByIdentifier(portletIdentity) != null;
238: }
239:
240: public void registerPortletApplication(
241: PortletApplicationDefinition newApp)
242: throws RegistryException {
243: getPersistenceBrokerTemplate().store(newApp);
244: }
245:
246: public void removeApplication(PortletApplicationDefinition app)
247: throws RegistryException {
248: getPersistenceBrokerTemplate().delete(app);
249:
250: String appNodePath = MutablePortletApplication.PREFS_ROOT + "/"
251: + ((MutablePortletApplication) app).getName();
252: try {
253: if (Preferences.systemRoot().nodeExists(appNodePath)) {
254: Preferences node = Preferences.systemRoot().node(
255: appNodePath);
256: // log.info("Removing Application preference node "+node.absolutePath());
257: node.removeNode();
258: }
259: } catch (BackingStoreException e) {
260: throw new RegistryException(e.toString(), e);
261: }
262:
263: }
264:
265: public void updatePortletApplication(
266: PortletApplicationDefinition app) throws RegistryException {
267: getPersistenceBrokerTemplate().store(app);
268:
269: }
270:
271: private void postLoad(Object obj) {
272: if (obj != null) {
273:
274: if (obj instanceof Support) {
275: try {
276: ((Support) obj).postLoad(obj);
277: } catch (Exception e) {
278: }
279: }
280: }
281:
282: }
283:
284: private void postLoadColl(Collection coll) {
285:
286: if (coll != null && !coll.isEmpty()) {
287: Iterator itr = coll.iterator();
288: Object test = itr.next();
289: if (test instanceof Support) {
290: Support testSupport = (Support) test;
291: try {
292: testSupport.postLoad(testSupport);
293: } catch (Exception e1) {
294:
295: }
296: while (itr.hasNext()) {
297: Support support = (Support) itr.next();
298: try {
299: support.postLoad(support);
300: } catch (Exception e) {
301: }
302: }
303: }
304:
305: }
306:
307: }
308:
309: public void savePortletDefinition(PortletDefinition portlet)
310: throws FailedToStorePortletDefinitionException {
311: try {
312: getPersistenceBrokerTemplate().store(portlet);
313: } catch (DataAccessException e) {
314:
315: throw new FailedToStorePortletDefinitionException(portlet,
316: e);
317: }
318:
319: }
320:
321: public PortletDefinitionComposite getPortletDefinition(ObjectID id) {
322: Criteria c = new Criteria();
323: c.addEqualTo("id", new Long(id.toString()));
324: PortletDefinitionComposite portlet = (PortletDefinitionComposite) getPersistenceBrokerTemplate()
325: .getObjectByQuery(
326: QueryFactory.newQuery(
327: PortletDefinitionImpl.class, c));
328:
329: postLoad(portlet);
330: return portlet;
331: }
332:
333: public void notifyElementAdded(JetspeedCache cache, boolean local,
334: Object key, Object element) {
335: }
336:
337: public void notifyElementChanged(JetspeedCache cache,
338: boolean local, Object key, Object element) {
339: }
340:
341: public void notifyElementEvicted(JetspeedCache cache,
342: boolean local, Object key, Object element) {
343: //notifyElementRemoved(cache,local,key,element);
344: }
345:
346: public void notifyElementExpired(JetspeedCache cache,
347: boolean local, Object key, Object element) {
348: //notifyElementRemoved(cache,local,key,element);
349: }
350:
351: public void notifyElementRemoved(JetspeedCache cache,
352: boolean local, Object key, Object element) {
353:
354: if (cache == this .portletNameCache) {
355: //System.out.println("%%% portlet remote removed " + key);
356: RegistryPortletCache.cacheRemoveQuiet((String) key,
357: (RegistryCacheObjectWrapper) element);
358: PortletDefinitionComposite pd = this
359: .getPortletDefinitionByUniqueName((String) key);
360: if (listeners != null) {
361: for (int ix = 0; ix < listeners.size(); ix++) {
362: RegistryEventListener listener = (RegistryEventListener) listeners
363: .get(ix);
364: listener.portletRemoved(pd);
365: }
366: }
367: } else {
368: //System.out.println("%%% PA remote removed " + key);
369: RegistryApplicationCache.cacheRemoveQuiet((String) key,
370: (RegistryCacheObjectWrapper) element);
371: MutablePortletApplication pa = this
372: .getPortletApplication((String) key);
373: if (listeners != null) {
374: for (int ix = 0; ix < listeners.size(); ix++) {
375: RegistryEventListener listener = (RegistryEventListener) listeners
376: .get(ix);
377: listener.applicationRemoved(pa);
378: }
379: }
380:
381: }
382: }
383:
384: public void addRegistryListener(RegistryEventListener listener) {
385: this .listeners.add(listener);
386: }
387:
388: public void removeRegistryEventListner(
389: RegistryEventListener listener) {
390: this.listeners.remove(listener);
391: }
392:
393: }
|