View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
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  
17  package org.apache.jetspeed.services.persistence;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  // Jetspeed classes
24  import org.apache.jetspeed.om.profile.Entry;
25  import org.apache.jetspeed.om.profile.PSMLDocument;
26  import org.apache.jetspeed.om.profile.Portlets;
27  import org.apache.jetspeed.om.profile.Profile;
28  import org.apache.jetspeed.portal.JetspeedPortletInstance;
29  import org.apache.jetspeed.portal.Portlet;
30  import org.apache.jetspeed.portal.PortletException;
31  import org.apache.jetspeed.portal.PortletInstance;
32  import org.apache.jetspeed.services.PortletFactory;
33  import org.apache.jetspeed.services.PsmlManager;
34  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
35  import org.apache.jetspeed.services.logging.JetspeedLogger;
36  import org.apache.jetspeed.services.rundata.JetspeedRunData;
37  
38  // Turbine classes
39  import org.apache.turbine.services.InitializationException;
40  import org.apache.turbine.services.TurbineBaseService;
41  import org.apache.turbine.util.RunData;
42  
43  /***
44   * Implementation of the Portal Persistence Service for storing and
45   * retrieving portlet instances.
46   *
47   * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
48   * @version $Id: JetspeedPortalPersistenceService.java,v 1.5 2004/02/23 03:33:52 jford Exp $
49   */
50  public class JetspeedPortalPersistenceService
51      extends TurbineBaseService
52      implements PortalPersistenceService
53  {    
54      /***
55       * Static initialization of the logger for this class
56       */    
57      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortalPersistenceService.class.getName());    
58      
59      /***
60       * Use this to verify the RunData object in use is going to be the Jetspeed
61       * RunData object.  This way we know right off, whether or not things are going
62       * to work.
63       * 
64       */
65      public void init(RunData data) throws InitializationException
66      {
67          if (!(data instanceof JetspeedRunData))
68          {
69              logger.error(
70                  "[PortalPersistenceService] The current RunData implenetation does not implement the JetspeedRunData interface.");
71              setInit(false);
72              throw new InitializationException("The current RunData implenetation does not implement the JetspeedRunData interface.");
73          }
74  
75          super.init(data);
76      }    
77      
78      /***
79       * @see PortalPersistenceService#getInstance(Portlet, Profile)
80       */
81      public PortletInstance getInstance(Portlet portlet, Profile profile)
82      {
83          return new JetspeedPortletInstance(portlet, profile);
84      }
85  
86      /***
87       * @see PortalPersistenceService#getInstance(Portlet, RunData)
88       */
89      public PortletInstance getInstance(Portlet portlet, RunData data)
90      {
91       	String attrKey = "portlet_instance:"+portlet.getID();
92      	// optimize portlet instance retreival by saving it to the request
93      	// this also guarantees the PortletInstance object is the same
94      	// object for the entire life of the request
95      	PortletInstance instance = (PortletInstance) data.getRequest().getAttribute(attrKey);
96      	if(instance != null)
97      	{
98      		return instance;
99      	}
100     	else
101     	{
102          	instance=  new JetspeedPortletInstance(portlet, data);
103          	data.getRequest().setAttribute(attrKey, instance);
104          	return instance;
105     	}
106 
107     }
108 
109     /***
110      * @see PortalPersistenceService#store(PortletInstance)
111      */
112     public void store(PortletInstance pPortlet) throws PortalPersistenceException
113     {
114         Profile profile = pPortlet.getProfile();
115         profile.setDocument(pPortlet.getDocument());
116         
117         if(!PsmlManager.store(profile))
118         {
119             throw new PortalPersistenceException("Unable to write portlet information.");
120         }
121     }
122 
123     /***
124      * @see PortalPersistenceService#getInstances(Portlet, Profile)
125      */
126     public List getInstances( Profile profile) throws PortletException
127     {
128         PSMLDocument doc = profile.getDocument();
129         Portlets portlets =  doc.getPortlets();
130         ArrayList pList = new ArrayList();
131        
132        buildAllEntries(portlets, pList, profile);
133         
134         return pList;
135     }
136 
137     /***
138      * @see PortalPersistenceService#getInstances(Portlet, RunData)
139      */
140     public List getInstances( RunData data) throws PortletException
141     {
142         JetspeedRunData jData = (JetspeedRunData) data;
143         return getInstances(jData.getProfile());
144     }
145     
146     protected void buildAllEntries(Portlets portlets, ArrayList entries, Profile profile) throws PortletException
147     {
148         // First let's add all the Entries in the current level
149         Iterator eItr = portlets.getEntriesIterator();
150         while(eItr.hasNext())
151         {
152             Object obj =  eItr.next();
153             Entry entry = (Entry)obj;
154             Portlet portlet = PortletFactory.getPortlet(entry);
155             entries.add(new JetspeedPortletInstance(portlet, profile));
156         }
157         
158         //Now if there are child levels, drill down recursively
159         if(portlets.getPortletsCount() > 0)
160         {
161             Iterator pItr = portlets.getPortletsIterator();
162             while(pItr.hasNext())
163             {
164                 Portlets childPortlets = (Portlets)pItr.next();
165                 buildAllEntries(childPortlets, entries, profile);
166             }
167             
168             return;
169         }
170         else
171         // bootstrap
172         {
173             return;
174         }
175         
176         
177     }
178 
179 }