KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > acting > BookmarkAction


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.portal.acting;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
29 import org.apache.avalon.framework.parameters.ParameterException;
30 import org.apache.avalon.framework.parameters.Parameterizable;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.avalon.framework.service.ServiceException;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.cocoon.ProcessingException;
35 import org.apache.cocoon.acting.ServiceableAction;
36 import org.apache.cocoon.components.source.SourceUtil;
37 import org.apache.cocoon.environment.ObjectModelHelper;
38 import org.apache.cocoon.environment.Redirector;
39 import org.apache.cocoon.environment.Request;
40 import org.apache.cocoon.environment.Session;
41 import org.apache.cocoon.environment.SourceResolver;
42 import org.apache.cocoon.portal.PortalService;
43 import org.apache.cocoon.portal.acting.helpers.CopletMapping;
44 import org.apache.cocoon.portal.acting.helpers.FullScreenMapping;
45 import org.apache.cocoon.portal.acting.helpers.LayoutMapping;
46 import org.apache.cocoon.portal.acting.helpers.Mapping;
47 import org.apache.excalibur.source.Source;
48 import org.xml.sax.SAXException JavaDoc;
49
50 /**
51  * This action helps you in creating bookmarks
52  *
53  * The definition file is:
54  * <bookmarks>
55  * <events>
56  * <event type="jxpath" id="ID">
57  * <targetid>tagetId</targetid>
58  * <targettype>layout|coplet</targettype>
59  * <path/>
60  * </event>
61  * <event type="fullscreen" id="ID">
62  * <targetid>copletId</targetid>
63  * <layoutid>layoutId</layoutid>
64  * </event>
65  * </events>
66  * </bookmarks>
67  *
68  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
69  * @version CVS $Id: BookmarkAction.java 158790 2005-03-23 16:02:48Z cziegeler $
70 */

71 public class BookmarkAction
72 extends ServiceableAction
73 implements ThreadSafe, Parameterizable {
74
75     protected Map JavaDoc eventMap = new HashMap JavaDoc();
76     
77     protected String JavaDoc historyParameterName;
78     
79     protected String JavaDoc configurationFile;
80     
81     /* (non-Javadoc)
82      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
83      */

84     public void parameterize(Parameters parameters) throws ParameterException {
85         this.historyParameterName = parameters.getParameter("history-parameter-name", "history");
86         this.configurationFile = parameters.getParameter("src", null);
87         if ( this.configurationFile == null ) return;
88         
89         // The "lazy-load" parameter allows to defer loading of the config from "src" at
90
// the first call to act. This is for now undocumented until
91
// That was needed in the case of a dynamic source ("cocoon://blah") produced by the sitemap where
92
// the action is defined. Loading immediately in that case leads to an infinite loop where the sitemap
93
// is constantly reloaded.
94
if (!parameters.getParameterAsBoolean("lazy-load", false)) {
95             loadConfig();
96         }
97     }
98        
99     private void loadConfig() throws ParameterException {
100         Configuration config;
101         org.apache.excalibur.source.SourceResolver resolver = null;
102         try {
103             resolver = (org.apache.excalibur.source.SourceResolver) this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);
104             Source source = null;
105             try {
106                 source = resolver.resolveURI(configurationFile);
107                 SAXConfigurationHandler handler = new SAXConfigurationHandler();
108                 SourceUtil.toSAX(source, handler);
109                 config = handler.getConfiguration();
110             } catch (ProcessingException se) {
111                 throw new ParameterException("Unable to read configuration from " + configurationFile, se);
112             } catch (SAXException JavaDoc se) {
113                 throw new ParameterException("Unable to read configuration from " + configurationFile, se);
114             } catch (IOException JavaDoc ioe) {
115                 throw new ParameterException("Unable to read configuration from " + configurationFile, ioe);
116             } finally {
117                 resolver.release(source);
118             }
119         } catch (ServiceException se) {
120             throw new ParameterException("Unable to lookup source resolver.", se);
121         } finally {
122             this.manager.release(resolver);
123         }
124         Configuration[] events = config.getChild("events").getChildren("event");
125         if ( events != null ) {
126             for(int i=0; i<events.length;i++) {
127                 try {
128                     final String JavaDoc type = events[i].getAttribute("type");
129                     final String JavaDoc id = events[i].getAttribute("id");
130                     if ( "jxpath".equals(type) ) {
131                         if ( this.eventMap.containsKey(id)) {
132                             throw new ParameterException("The id for the event " + id + " is not unique.");
133                         }
134                         final String JavaDoc targetType = events[i].getChild("targettype").getValue();
135                         final String JavaDoc targetId = events[i].getChild("targetid").getValue();
136                         final String JavaDoc path = events[i].getChild("path").getValue();
137                         if ( "layout".equals(targetType) ) {
138                             LayoutMapping mapping = new LayoutMapping();
139                             mapping.layoutId = targetId;
140                             mapping.path = path;
141                             this.eventMap.put(id, mapping);
142                         } else if ( "coplet".equals(targetType) ) {
143                             CopletMapping mapping = new CopletMapping();
144                             mapping.copletId = targetId;
145                             mapping.path = path;
146                             this.eventMap.put(id, mapping);
147                         } else {
148                             throw new ParameterException("Unknown target type " + targetType);
149                         }
150                     } else if ( "fullscreen".equals(type) ) {
151                         if ( this.eventMap.containsKey(id)) {
152                             throw new ParameterException("The id for the event " + id + " is not unique.");
153                         }
154                         final String JavaDoc targetId = events[i].getChild("targetid").getValue();
155                         final String JavaDoc layoutId = events[i].getChild("layoutid").getValue();
156                         FullScreenMapping mapping = new FullScreenMapping();
157                         mapping.copletId = targetId;
158                         mapping.layoutId = layoutId;
159                         this.eventMap.put(id, mapping);
160                     } else {
161                         throw new ParameterException("Unknown event type for event " + id + ": " + type);
162                     }
163                 } catch (ConfigurationException ce) {
164                     throw new ParameterException("Configuration exception" ,ce);
165                 }
166             }
167         }
168         
169         // Nullify config filename so as not to reload it.
170
this.configurationFile = null;
171     }
172
173     public Map JavaDoc act(Redirector redirector,
174                    SourceResolver resolver,
175                    Map JavaDoc objectModel,
176                    String JavaDoc source,
177                    Parameters par)
178     throws Exception JavaDoc {
179         if (this.getLogger().isDebugEnabled() ) {
180             this.getLogger().debug("BEGIN act resolver="+resolver+
181                                    ", objectModel="+objectModel+
182                                    ", source="+source+
183                                    ", par="+par);
184         }
185
186         if (this.configurationFile != null) {
187             loadConfig();
188         }
189
190         Map JavaDoc result;
191         PortalService service = null;
192         try {
193             service = (PortalService)this.manager.lookup(PortalService.ROLE);
194
195             service.getComponentManager().getPortalManager().process();
196             
197             final Request request = ObjectModelHelper.getRequest(objectModel);
198             final Session session = request.getSession(false);
199             final List JavaDoc events = new ArrayList JavaDoc();
200             
201             // is the history invoked?
202
final String JavaDoc historyValue = request.getParameter(this.historyParameterName);
203             if ( historyValue != null && session != null) {
204                 // get the history
205
final List JavaDoc history = (List JavaDoc)session.getAttribute("portal-history");
206                 if ( history != null ) {
207                     final int index = Integer.parseInt(historyValue);
208                     final List JavaDoc state = (List JavaDoc)history.get(index);
209                     if ( state != null ) {
210                         final Iterator JavaDoc iter = state.iterator();
211                         while ( iter.hasNext() ) {
212                             Mapping m = (Mapping)iter.next();
213                             events.add(m.getEvent(service, null));
214                         }
215                         while (history.size() > index ) {
216                             history.remove(history.size()-1);
217                         }
218                     }
219                 }
220             }
221             Enumeration JavaDoc enumeration = request.getParameterNames();
222             while (enumeration.hasMoreElements()) {
223                 String JavaDoc name = (String JavaDoc)enumeration.nextElement();
224                 String JavaDoc value = request.getParameter(name);
225                 
226                 Mapping m = (Mapping) this.eventMap.get(name);
227                 if ( m != null ) {
228                     events.add(m.getEvent(service, value));
229                 }
230             }
231             String JavaDoc uri = service.getComponentManager().getLinkService().getLinkURI(events);
232             result = new HashMap JavaDoc();
233             result.put("uri", uri.substring(uri.indexOf('?')+1));
234
235         } catch (ServiceException ce) {
236             throw new ProcessingException("Unable to lookup portal service.", ce);
237         } finally {
238             this.manager.release(service);
239         }
240
241         if (this.getLogger().isDebugEnabled() ) {
242             this.getLogger().debug("END act map={}");
243         }
244
245         return result;
246     }
247
248 }
249
Popular Tags