KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > event > aspect > impl > ConvertableEventAspect


1 /*
2  * Copyright 1999-2002,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.event.aspect.impl;
17
18 import java.util.List JavaDoc;
19 import java.util.ArrayList JavaDoc;
20
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23 import org.apache.avalon.framework.service.Serviceable;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceSelector;
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.cocoon.environment.ObjectModelHelper;
29 import org.apache.cocoon.environment.Request;
30 import org.apache.cocoon.portal.PortalService;
31 import org.apache.cocoon.portal.LinkService;
32 import org.apache.cocoon.portal.event.Event;
33
34 import org.apache.cocoon.portal.event.ConvertableEventFactory;
35 import org.apache.cocoon.portal.event.ConvertableEvent;
36 import org.apache.cocoon.portal.event.aspect.EventAspect;
37 import org.apache.cocoon.portal.event.aspect.EventAspectContext;
38 import org.apache.excalibur.source.SourceUtil;
39
40 /**
41  * Process all convertable event request parameters, creating the events and saving
42  * them in request attributes for processing by EventAspects that follow.
43  *
44  * @author <a HREF="mailto:rgoers@apache.org">Ralph Goers</a>
45  * @version SVN $Id: $
46  */

47 public class ConvertableEventAspect extends AbstractLogEnabled
48     implements EventAspect, ThreadSafe, Serviceable {
49
50     protected ServiceManager manager;
51
52     /**
53      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
54      */

55     public void service(ServiceManager manager) throws ServiceException {
56         this.manager = manager;
57     }
58
59     /* (non-Javadoc)
60      * @see org.apache.cocoon.portal.event.aspect.EventAspect#process(org.apache.cocoon.portal.event.aspect.EventAspectContext, org.apache.cocoon.portal.PortalService)
61      */

62     public void process(EventAspectContext context, PortalService service) {
63         final Request request = ObjectModelHelper.getRequest(context.getObjectModel());
64         final Parameters config = context.getAspectParameters();
65         final String JavaDoc parameterName = config.getParameter("parameter-name",
66             LinkService.DEFAULT_CONVERTABLE_EVENT_PARAMETER_NAME);
67
68         String JavaDoc[] parm = request.getParameterValues(parameterName);
69
70         if (parm != null) {
71             for (int i=0; i < parm.length; ++i) {
72                 int index = parm[i].indexOf('(');
73                 if (index > 0 && parm[i].endsWith(")")) {
74                     String JavaDoc eventKey = parm[i].substring(0, index);
75                     String JavaDoc eventParm =
76                         SourceUtil.decodePath(parm[i].substring(index+1, parm[i].length()-1));
77                     Event event = getEvent(service, eventKey, eventParm);
78                     String JavaDoc key = "org.apache.cocoon.portal." +
79                         ((ConvertableEvent)event).getRequestParameterName();
80                     List JavaDoc list = (List JavaDoc)request.getAttribute(key);
81                     if (list == null) {
82                         list = new ArrayList JavaDoc();
83                     }
84                     list.add(event);
85                     request.setAttribute(key, list);
86                 }
87             }
88         }
89
90         context.invokeNext( service );
91     }
92
93     private ConvertableEvent getEvent(PortalService service, String JavaDoc factoryName, String JavaDoc eventData) {
94         ServiceSelector selector = null;
95         ConvertableEventFactory factory = null;
96         ConvertableEvent event;
97         try {
98             selector = (ServiceSelector) this.manager.lookup(ConvertableEventFactory.ROLE + "Selector");
99             factory = (ConvertableEventFactory) selector.select(factoryName);
100             event = factory.createEvent(service, eventData);
101         } catch (ServiceException ce) {
102             if (getLogger().isErrorEnabled()) {
103                 getLogger().error("Unable to create event for " + factoryName +
104                     " using " + eventData);
105             }
106             event = null;
107         } finally {
108             if (selector != null) {
109                 selector.release(factory);
110             }
111             this.manager.release(selector);
112         }
113         return event;
114     }
115 }
116
Popular Tags