1 16 package org.apache.cocoon.portal.event.impl; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.avalon.framework.CascadingRuntimeException; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.service.ServiceException; 24 import org.apache.avalon.framework.service.ServiceManager; 25 import org.apache.avalon.framework.service.Serviceable; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 import org.apache.cocoon.portal.PortalService; 28 import org.apache.cocoon.portal.event.Event; 29 import org.apache.cocoon.portal.event.EventConverter; 30 31 38 public class DefaultEventConverter 39 extends AbstractLogEnabled 40 implements EventConverter, Serviceable, ThreadSafe { 41 42 protected static final String DECODE_LIST = DefaultEventConverter.class.getName() + "D"; 43 protected static final String ENCODE_LIST = DefaultEventConverter.class.getName() + "E"; 44 45 protected ServiceManager manager; 46 47 50 public void service(ServiceManager manager) throws ServiceException { 51 this.manager = manager; 52 } 53 54 57 public String encode(Event event) { 58 PortalService service = null; 59 try { 60 service = (PortalService)this.manager.lookup(PortalService.ROLE); 61 List list = (List )service.getAttribute(ENCODE_LIST); 62 if ( null == list ) { 63 list = new ArrayList (); 64 service.setAttribute(ENCODE_LIST, list); 65 } 66 int index = list.indexOf(event); 67 if ( index == -1 ) { 68 list.add(event); 69 index = list.size() - 1; 70 } 71 return String.valueOf(index); 72 } catch (ServiceException ce) { 73 throw new CascadingRuntimeException("Unable to lookup component.", ce); 74 } finally { 75 this.manager.release(service); 76 } 77 78 } 79 80 83 public Event decode(String value) { 84 if (value != null) { 85 PortalService service = null; 86 try { 87 service = (PortalService)this.manager.lookup(PortalService.ROLE); 88 List list = (List )service.getAttribute(DECODE_LIST); 89 if ( null != list ) { 90 int index = new Integer (value).intValue(); 91 if (index < list.size()) { 92 return (Event)list.get(index); 93 } 94 } 95 } catch (ServiceException ce) { 96 throw new CascadingRuntimeException("Unable to lookup component.", ce); 97 } finally { 98 this.manager.release(service); 99 } 100 } 101 return null; 102 } 103 104 107 public void start() { 108 PortalService service = null; 109 try { 110 service = (PortalService)this.manager.lookup(PortalService.ROLE); 111 List list = (List )service.getAttribute(ENCODE_LIST); 112 if ( null != list ) { 113 service.setAttribute(DECODE_LIST, list); 114 service.removeAttribute(ENCODE_LIST); 115 } 116 } catch (ServiceException ce) { 117 throw new CascadingRuntimeException("Unable to lookup component.", ce); 118 } finally { 119 this.manager.release(service); 120 } 121 } 122 123 126 public void finish() { 127 PortalService service = null; 128 try { 129 service = (PortalService)this.manager.lookup(PortalService.ROLE); 130 service.removeAttribute(DECODE_LIST); 131 } catch (ServiceException ce) { 132 throw new CascadingRuntimeException("Unable to lookup component.", ce); 133 } finally { 134 this.manager.release(service); 135 } 136 } 137 138 141 public boolean isMarshallEvents() { 142 return false; 143 } 144 } 145 | Popular Tags |