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.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Map;
26  import java.util.prefs.BackingStoreException;
27  import java.util.prefs.Preferences;
28  
29  import org.apache.jetspeed.cache.JetspeedCache;
30  import org.apache.jetspeed.cache.JetspeedCacheEventListener;
31  import org.apache.jetspeed.components.dao.InitablePersistenceBrokerDaoSupport;
32  import org.apache.jetspeed.factory.PortletFactory;
33  import org.apache.jetspeed.om.common.MutableLanguage;
34  import org.apache.jetspeed.om.common.Support;
35  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
36  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
37  import org.apache.jetspeed.om.impl.LanguageImpl;
38  import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
39  import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
40  import org.apache.ojb.broker.query.Criteria;
41  import org.apache.ojb.broker.query.QueryFactory;
42  import org.apache.pluto.om.common.Language;
43  import org.apache.pluto.om.common.ObjectID;
44  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
45  import org.apache.pluto.om.portlet.PortletDefinition;
46  import org.springframework.dao.DataAccessException;
47  
48  /***
49   * <p>
50   * OjbPortletRegistry
51   * </p>
52   * <p>
53   * 
54   * </p>
55   * 
56   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
57   * @version $Id: PersistenceBrokerPortletRegistry.java 516448 2007-03-09 16:25:47Z ate $
58   *  
59   */
60  public class PersistenceBrokerPortletRegistry 
61      extends InitablePersistenceBrokerDaoSupport 
62      implements PortletRegistry, JetspeedCacheEventListener
63  {
64      /***
65       * The separator used to create a unique portlet name as
66       * {portletApplication}::{portlet}
67       */
68      static final String PORTLET_UNIQUE_NAME_SEPARATOR = "::";
69  
70      private JetspeedCache applicationOidCache = null;
71      private JetspeedCache portletOidCache = null;
72      private JetspeedCache applicationNameCache = null;
73      private JetspeedCache portletNameCache = null;
74      private Map nameCache = new HashMap(); // work in progress (switch to JetspeedCache)
75      private List listeners = new ArrayList();
76      
77      // for testing purposes only: no need for the portletFactory then
78      public PersistenceBrokerPortletRegistry(String repositoryPath)
79      {
80          this(repositoryPath, null, null, null, null, null);
81      }
82      
83      /***
84       *  
85       */
86      public PersistenceBrokerPortletRegistry(String repositoryPath, PortletFactory portletFactory, 
87              JetspeedCache applicationOidCache, JetspeedCache portletOidCache, 
88              JetspeedCache applicationNameCache, JetspeedCache portletNameCache)
89      {
90          super(repositoryPath);
91          PortletDefinitionImpl.setPortletRegistry(this);
92          PortletDefinitionImpl.setPortletFactory(portletFactory);
93          this.applicationOidCache = applicationOidCache;
94          this.portletOidCache = portletOidCache;
95          this.applicationNameCache = applicationNameCache;
96          this.portletNameCache = portletNameCache;
97          MutablePortletApplicationProxy.setRegistry(this);
98          RegistryApplicationCache.cacheInit(this, applicationOidCache, applicationNameCache, listeners);
99          RegistryPortletCache.cacheInit(this, portletOidCache, portletNameCache, listeners);
100         this.applicationNameCache.addEventListener(this, false);
101         this.portletNameCache.addEventListener(this, false);        
102     }
103     
104     public Language createLanguage( Locale locale, String title, String shortTitle, String description,
105             Collection keywords ) throws RegistryException
106     {
107         try
108         {
109             MutableLanguage lc = new LanguageImpl();
110             lc.setLocale(locale);
111             lc.setTitle(title);
112             lc.setShortTitle(shortTitle);
113             lc.setKeywords(keywords);
114             return lc;
115         }
116         catch (Exception e)
117         {
118             throw new RegistryException("Unable to create language object.");
119         }
120     }
121     
122     public Collection getAllPortletDefinitions()
123     {
124         Criteria c = new Criteria();
125         Collection list = getPersistenceBrokerTemplate().getCollectionByQuery(
126                 QueryFactory.newQuery(PortletDefinitionImpl.class, c));
127         postLoadColl(list);
128         return list;
129     }
130 
131     public MutablePortletApplication getPortletApplication( ObjectID id )
132     {
133         Criteria c = new Criteria();
134         c.addEqualTo("id", new Long(id.toString()));
135         MutablePortletApplication app = (MutablePortletApplication) getPersistenceBrokerTemplate().getObjectByQuery(
136                 QueryFactory.newQuery(PortletApplicationDefinitionImpl.class, c));
137         postLoad(app);
138         return app;
139     }
140 
141     public MutablePortletApplication getPortletApplication(String name)
142     {
143         Criteria c = new Criteria();
144         c.addEqualTo("name", name);
145         MutablePortletApplication app = (MutablePortletApplication) getPersistenceBrokerTemplate().getObjectByQuery(
146                 QueryFactory.newQuery(PortletApplicationDefinitionImpl.class, c));
147         postLoad(app);
148         return app;
149     }
150 
151     public MutablePortletApplication getPortletApplicationByIdentifier( String identifier )
152     {
153         Criteria c = new Criteria();
154         c.addEqualTo("applicationIdentifier", identifier);
155         MutablePortletApplication app = (MutablePortletApplication) getPersistenceBrokerTemplate().getObjectByQuery(
156             QueryFactory.newQuery(PortletApplicationDefinitionImpl.class, c));
157         postLoad(app);
158         return app;
159     }
160 
161     public Collection getPortletApplications()
162     {
163         Criteria c = new Criteria();
164         Collection list = getPersistenceBrokerTemplate().getCollectionByQuery(
165                 QueryFactory.newQuery(PortletApplicationDefinitionImpl.class, c));
166         postLoadColl(list);
167         return list;
168     }
169 
170     public PortletDefinitionComposite getPortletDefinitionByIdentifier( String identifier )
171     {
172         Criteria c = new Criteria();
173         c.addEqualTo("portletIdentifier", identifier);
174         PortletDefinitionComposite def = (PortletDefinitionComposite) getPersistenceBrokerTemplate().getObjectByQuery(
175                 QueryFactory.newQuery(PortletDefinitionImpl.class, c));
176         if (def != null && def.getPortletApplicationDefinition() == null)
177         {
178             final String msg = "getPortletDefinitionByIdentifier() returned a PortletDefinition that has no parent PortletApplication.";
179             throw new IllegalStateException(msg);
180         }
181 
182         postLoad(def);
183         return def;
184     }
185 
186     public PortletDefinitionComposite getPortletDefinitionByUniqueName( String name )
187     {
188         String appName = PortletRegistryHelper.parseAppName(name);
189         String portletName = PortletRegistryHelper.parsePortletName(name);
190 
191         Criteria c = new Criteria();
192         c.addEqualTo("app.name", appName);
193         c.addEqualTo("name", portletName);
194 
195         PortletDefinitionComposite def = (PortletDefinitionComposite) getPersistenceBrokerTemplate().getObjectByQuery(
196                 QueryFactory.newQuery(PortletDefinitionImpl.class, c));
197         if (def != null && def.getPortletApplicationDefinition() == null)
198         {
199             final String msg = "getPortletDefinitionByIdentifier() returned a PortletDefinition that has no parent PortletApplication.";
200             throw new IllegalStateException(msg);
201         }
202 
203         postLoad(def);
204         return def;
205     }
206 
207     public boolean portletApplicationExists( String appIdentity )
208     {
209         return getPortletApplicationByIdentifier(appIdentity) != null;
210     }
211     
212     public boolean namedPortletApplicationExists( String appName )
213     {
214         return getPortletApplication(appName) != null;
215     }
216 
217     public boolean portletDefinitionExists( String portletName, MutablePortletApplication app )
218     {
219         return getPortletDefinitionByUniqueName(app.getName() + "::" + portletName) != null;
220     }
221 
222     public boolean portletDefinitionExists( String portletIdentity )
223     {
224         return getPortletDefinitionByIdentifier(portletIdentity) != null;
225     }
226 
227     public void registerPortletApplication( PortletApplicationDefinition newApp ) throws RegistryException
228     {
229         getPersistenceBrokerTemplate().store(newApp);
230     }
231 
232     public void removeApplication( PortletApplicationDefinition app ) throws RegistryException
233     {
234         getPersistenceBrokerTemplate().delete(app);
235         
236         String appNodePath = MutablePortletApplication.PREFS_ROOT + "/" +((MutablePortletApplication)app).getName();
237         try
238         {
239             if(Preferences.systemRoot().nodeExists(appNodePath))
240             {                   
241                 Preferences node = Preferences.systemRoot().node(appNodePath);
242                // log.info("Removing Application preference node "+node.absolutePath());
243                 node.removeNode();
244             }
245         }
246         catch (BackingStoreException e)
247         {
248            throw new RegistryException(e.toString(), e);
249         }
250 
251     }
252 
253     public void updatePortletApplication( PortletApplicationDefinition app ) throws RegistryException
254     {
255         getPersistenceBrokerTemplate().store(app);
256 
257     }
258 
259     private void postLoad( Object obj )
260     {
261         if (obj != null)
262         {
263 
264             if (obj instanceof Support)
265             {
266                 try
267                 {
268                     ((Support) obj).postLoad(obj);
269                 }
270                 catch (Exception e)
271                 {
272                 }
273             }
274         }
275 
276     }
277 
278     private void postLoadColl( Collection coll )
279     {
280 
281         if (coll != null && !coll.isEmpty())
282         {
283             Iterator itr = coll.iterator();
284             Object test = itr.next();
285             if (test instanceof Support)
286             {
287                 Support testSupport = (Support) test;
288                 try
289                 {
290                     testSupport.postLoad(testSupport);
291                 }
292                 catch (Exception e1)
293                 {
294 
295                 }
296                 while (itr.hasNext())
297                 {
298                     Support support = (Support) itr.next();
299                     try
300                     {
301                         support.postLoad(support);
302                     }
303                     catch (Exception e)
304                     {
305                     }
306                 }
307             }
308 
309         }
310 
311     }
312 
313     public void savePortletDefinition( PortletDefinition portlet ) throws FailedToStorePortletDefinitionException
314     {
315         try
316         {
317             getPersistenceBrokerTemplate().store(portlet);
318         }
319         catch (DataAccessException e)
320         {
321             
322            throw new FailedToStorePortletDefinitionException(portlet, e);
323         }
324 
325     }
326 
327     public PortletDefinitionComposite getPortletDefinition(ObjectID id)
328     {
329         Criteria c = new Criteria();
330         c.addEqualTo("id", new Long(id.toString()));
331         PortletDefinitionComposite portlet = (PortletDefinitionComposite) getPersistenceBrokerTemplate().getObjectByQuery(
332                 QueryFactory.newQuery(PortletDefinitionImpl.class, c));
333         
334         postLoad(portlet);
335         return portlet;
336     }
337     
338     public void notifyElementAdded(JetspeedCache cache, boolean local, Object key, Object element)
339     {
340     }
341 
342     public void notifyElementChanged(JetspeedCache cache, boolean local, Object key, Object element)
343     {
344     }
345 
346     public void notifyElementEvicted(JetspeedCache cache, boolean local, Object key, Object element)
347     {
348         //notifyElementRemoved(cache,local,key,element);
349     }
350 
351     public void notifyElementExpired(JetspeedCache cache, boolean local, Object key, Object element)
352     {
353         //notifyElementRemoved(cache,local,key,element);           
354     }
355 
356     public void notifyElementRemoved(JetspeedCache cache, boolean local, Object key, Object element)
357     {    
358        
359         if (cache == this.portletNameCache)
360         {
361             //System.out.println("%%% portlet remote removed " + key);            
362             RegistryPortletCache.cacheRemoveQuiet((String)key, (RegistryCacheObjectWrapper)element);
363             PortletDefinitionComposite pd = this.getPortletDefinitionByUniqueName((String)key);
364             if (listeners != null)
365             {
366                 for (int ix=0; ix < listeners.size(); ix++)
367                 {
368                     RegistryEventListener listener = (RegistryEventListener)listeners.get(ix);
369                     listener.portletRemoved(pd);
370                 }        
371             }           
372         }
373         else
374         {
375             //System.out.println("%%% PA remote removed " + key);
376             RegistryApplicationCache.cacheRemoveQuiet((String) key, (RegistryCacheObjectWrapper)element);
377             MutablePortletApplication pa = this.getPortletApplication((String)key);
378             if (listeners != null)
379             {
380                 for (int ix=0; ix < listeners.size(); ix++)
381                 {
382                     RegistryEventListener listener = (RegistryEventListener)listeners.get(ix);
383                     listener.applicationRemoved(pa);
384                 }        
385             }
386             
387         }
388     }
389         
390     public void addRegistryListener(RegistryEventListener listener)
391     {
392         this.listeners.add(listener);
393     }
394 
395     public void removeRegistryEventListner(RegistryEventListener listener)
396     {
397         this.listeners.remove(listener);
398     }
399     
400 }