1 16 package org.apache.cocoon.portal.event.aspect.impl; 17 18 import java.util.List ; 19 import java.util.ArrayList ; 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 47 public class ConvertableEventAspect extends AbstractLogEnabled 48 implements EventAspect, ThreadSafe, Serviceable { 49 50 protected ServiceManager manager; 51 52 55 public void service(ServiceManager manager) throws ServiceException { 56 this.manager = manager; 57 } 58 59 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 parameterName = config.getParameter("parameter-name", 66 LinkService.DEFAULT_CONVERTABLE_EVENT_PARAMETER_NAME); 67 68 String [] 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 eventKey = parm[i].substring(0, index); 75 String eventParm = 76 SourceUtil.decodePath(parm[i].substring(index+1, parm[i].length()-1)); 77 Event event = getEvent(service, eventKey, eventParm); 78 String key = "org.apache.cocoon.portal." + 79 ((ConvertableEvent)event).getRequestParameterName(); 80 List list = (List )request.getAttribute(key); 81 if (list == null) { 82 list = new ArrayList (); 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 factoryName, String 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 |