001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.event.aspect.impl;
018:
019: import java.util.List;
020: import java.util.ArrayList;
021:
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.thread.ThreadSafe;
024: import org.apache.avalon.framework.service.Serviceable;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.avalon.framework.service.ServiceSelector;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.cocoon.environment.ObjectModelHelper;
030: import org.apache.cocoon.environment.Request;
031: import org.apache.cocoon.portal.PortalService;
032: import org.apache.cocoon.portal.LinkService;
033: import org.apache.cocoon.portal.event.Event;
034:
035: import org.apache.cocoon.portal.event.ConvertableEventFactory;
036: import org.apache.cocoon.portal.event.ConvertableEvent;
037: import org.apache.cocoon.portal.event.aspect.EventAspect;
038: import org.apache.cocoon.portal.event.aspect.EventAspectContext;
039: import org.apache.excalibur.source.SourceUtil;
040:
041: /**
042: * Process all convertable event request parameters, creating the events and saving
043: * them in request attributes for processing by EventAspects that follow.
044: *
045: * @author <a href="mailto:rgoers@apache.org">Ralph Goers</a>
046: * @version SVN $Id: $
047: */
048: public class ConvertableEventAspect extends AbstractLogEnabled
049: implements EventAspect, ThreadSafe, Serviceable {
050:
051: protected ServiceManager manager;
052:
053: /**
054: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
055: */
056: public void service(ServiceManager manager) throws ServiceException {
057: this .manager = manager;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.apache.cocoon.portal.event.aspect.EventAspect#process(org.apache.cocoon.portal.event.aspect.EventAspectContext, org.apache.cocoon.portal.PortalService)
062: */
063: public void process(EventAspectContext context,
064: PortalService service) {
065: final Request request = ObjectModelHelper.getRequest(context
066: .getObjectModel());
067: final Parameters config = context.getAspectParameters();
068: final String parameterName = config.getParameter(
069: "parameter-name",
070: LinkService.DEFAULT_CONVERTABLE_EVENT_PARAMETER_NAME);
071:
072: String[] parm = request.getParameterValues(parameterName);
073:
074: if (parm != null) {
075: for (int i = 0; i < parm.length; ++i) {
076: int index = parm[i].indexOf('(');
077: if (index > 0 && parm[i].endsWith(")")) {
078: String eventKey = parm[i].substring(0, index);
079: String eventParm = SourceUtil
080: .decodePath(parm[i].substring(index + 1,
081: parm[i].length() - 1));
082: Event event = getEvent(service, eventKey, eventParm);
083: String key = "org.apache.cocoon.portal."
084: + ((ConvertableEvent) event)
085: .getRequestParameterName();
086: List list = (List) request.getAttribute(key);
087: if (list == null) {
088: list = new ArrayList();
089: }
090: list.add(event);
091: request.setAttribute(key, list);
092: }
093: }
094: }
095:
096: context.invokeNext(service);
097: }
098:
099: private ConvertableEvent getEvent(PortalService service,
100: String factoryName, String eventData) {
101: ServiceSelector selector = null;
102: ConvertableEventFactory factory = null;
103: ConvertableEvent event;
104: try {
105: selector = (ServiceSelector) this .manager
106: .lookup(ConvertableEventFactory.ROLE + "Selector");
107: factory = (ConvertableEventFactory) selector
108: .select(factoryName);
109: event = factory.createEvent(service, eventData);
110: } catch (ServiceException ce) {
111: if (getLogger().isErrorEnabled()) {
112: getLogger().error(
113: "Unable to create event for " + factoryName
114: + " using " + eventData);
115: }
116: event = null;
117: } finally {
118: if (selector != null) {
119: selector.release(factory);
120: }
121: this.manager.release(selector);
122: }
123: return event;
124: }
125: }
|