KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > wsrp > consumer > PortletRegistryImpl


1 /*
2  * Copyright 2005 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 package org.apache.cocoon.portal.wsrp.consumer;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.avalon.framework.logger.LogEnabled;
22 import org.apache.avalon.framework.logger.Logger;
23 import org.apache.wsrp4j.consumer.PortletKey;
24 import org.apache.wsrp4j.consumer.WSRPPortlet;
25 import org.apache.wsrp4j.consumer.driver.ConsumerPortletContext;
26 import org.apache.wsrp4j.consumer.driver.GenericPortletRegistryImpl;
27 import org.apache.wsrp4j.exception.WSRPException;
28 import org.apache.wsrp4j.util.StateChangedEvent;
29 import org.apache.wsrp4j.util.StateChangedListener;
30 import org.apache.wsrp4j.util.StateChangedService;
31
32 /**
33  * This class is the <code>PortletRegistry</code> implementation used
34  * to administer, store, load and manage portlets.<br/>
35  *
36  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
37  * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
38  *
39  * @version $Id: PortletRegistryImpl.java 264755 2005-08-30 10:29:21Z cziegeler $
40  **/

41 public class PortletRegistryImpl
42     extends GenericPortletRegistryImpl
43     implements StateChangedListener, LogEnabled {
44
45     /** The logger. */
46     protected Logger logger;
47
48     /** maps portlet keys to portlet context. */
49     private Hashtable JavaDoc contextMap = new Hashtable JavaDoc();
50
51     /**
52      * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
53      */

54     public void enableLogging(Logger logger) {
55         this.logger = logger;
56     }
57
58     /**
59      * Add a portlet to the registry<br/>
60      *
61      * @param portlet The portlet to add
62      * @throws WSRPException on error
63      */

64     public void addPortlet(WSRPPortlet portlet) throws WSRPException {
65         if (portlet != null) {
66             super.addPortlet(portlet);
67
68             // store PortletContext to persistent file
69
ConsumerPortletContext consumerPortletContext = new ConsumerPortletContext();
70             consumerPortletContext.setPortletContext(portlet.getPortletContext());
71             consumerPortletContext.setPortletKey(portlet.getPortletKey());
72             this.contextMap.put(portlet.getPortletKey().toString(), consumerPortletContext);
73
74             // add as listener
75
if (portlet instanceof StateChangedService) {
76                 ((StateChangedService)portlet).addListener(this);
77             }
78         }
79     }
80
81     /**
82      * Remove the portlet with the given portlet key<br/>
83      *
84      * @param portletKey The portlet key identifying the portlet
85      * @return returns the removed instance of WSRPPortlet
86      **/

87     public WSRPPortlet removePortlet(PortletKey portletKey) {
88         WSRPPortlet portlet = null;
89         if (portletKey != null) {
90             portlet = super.removePortlet(portletKey);
91             contextMap.remove(portletKey.toString());
92         }
93         return portlet;
94     }
95
96     /**
97      * Remove all portlets from the registry and delete them in the
98      * persistent store.
99      **/

100     public void removeAllPortlets() {
101         Iterator JavaDoc iterator = getAllPortlets();
102         while (iterator.hasNext()) {
103             WSRPPortlet portlet = (WSRPPortlet)iterator.next();
104
105             // remove ConsumerPortletContext from map
106
contextMap.remove(portlet.getPortletKey().toString());
107         }
108         super.removeAllPortlets();
109     }
110
111     /**
112      * StateChanged Event occured by a registered WSRPPortlet. The
113      * input source object, contained in the event will be
114      * updated in the persistence store.
115      *
116      * @param event
117      * @see StateChangedEvent
118      */

119     public void stateChanged(StateChangedEvent event) {
120         WSRPPortlet portlet = null;
121         try {
122             portlet = (WSRPPortlet)event.getSource();
123
124             //store PortletContext to persistent file
125
ConsumerPortletContext consumerPortletContext =
126                 (ConsumerPortletContext)contextMap.get(portlet.getPortletKey().toString());
127
128             consumerPortletContext.setPortletContext(portlet.getPortletContext());
129             consumerPortletContext.setPortletKey(portlet.getPortletKey());
130         } catch (ClassCastException JavaDoc ce) {
131             logger.error("StateChanged-error in portlet: " + portlet.getPortletKey().getPortletHandle(), ce);
132         }
133     }
134 }
135
Popular Tags