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.impl;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.avalon.framework.CascadingRuntimeException;
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.Serviceable;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.cocoon.portal.PortalService;
029: import org.apache.cocoon.portal.event.Event;
030: import org.apache.cocoon.portal.event.EventConverter;
031:
032: /**
033: *
034: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
035: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
036: *
037: * @version CVS $Id: DefaultEventConverter.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class DefaultEventConverter extends AbstractLogEnabled implements
040: EventConverter, Serviceable, ThreadSafe {
041:
042: protected static final String DECODE_LIST = DefaultEventConverter.class
043: .getName()
044: + "D";
045: protected static final String ENCODE_LIST = DefaultEventConverter.class
046: .getName()
047: + "E";
048:
049: protected ServiceManager manager;
050:
051: /* (non-Javadoc)
052: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
053: */
054: public void service(ServiceManager manager) throws ServiceException {
055: this .manager = manager;
056: }
057:
058: /* (non-Javadoc)
059: * @see org.apache.cocoon.portal.event.EventConverter#encode(org.apache.cocoon.portal.event.Event)
060: */
061: public String encode(Event event) {
062: PortalService service = null;
063: try {
064: service = (PortalService) this .manager
065: .lookup(PortalService.ROLE);
066: List list = (List) service.getAttribute(ENCODE_LIST);
067: if (null == list) {
068: list = new ArrayList();
069: service.setAttribute(ENCODE_LIST, list);
070: }
071: int index = list.indexOf(event);
072: if (index == -1) {
073: list.add(event);
074: index = list.size() - 1;
075: }
076: return String.valueOf(index);
077: } catch (ServiceException ce) {
078: throw new CascadingRuntimeException(
079: "Unable to lookup component.", ce);
080: } finally {
081: this .manager.release(service);
082: }
083:
084: }
085:
086: /* (non-Javadoc)
087: * @see org.apache.cocoon.portal.event.EventConverter#decode(java.lang.String)
088: */
089: public Event decode(String value) {
090: if (value != null) {
091: PortalService service = null;
092: try {
093: service = (PortalService) this .manager
094: .lookup(PortalService.ROLE);
095: List list = (List) service.getAttribute(DECODE_LIST);
096: if (null != list) {
097: int index = new Integer(value).intValue();
098: if (index < list.size()) {
099: return (Event) list.get(index);
100: }
101: }
102: } catch (ServiceException ce) {
103: throw new CascadingRuntimeException(
104: "Unable to lookup component.", ce);
105: } finally {
106: this .manager.release(service);
107: }
108: }
109: return null;
110: }
111:
112: /* (non-Javadoc)
113: * @see org.apache.cocoon.portal.event.EventConverter#start()
114: */
115: public void start() {
116: PortalService service = null;
117: try {
118: service = (PortalService) this .manager
119: .lookup(PortalService.ROLE);
120: List list = (List) service.getAttribute(ENCODE_LIST);
121: if (null != list) {
122: service.setAttribute(DECODE_LIST, list);
123: service.removeAttribute(ENCODE_LIST);
124: }
125: } catch (ServiceException ce) {
126: throw new CascadingRuntimeException(
127: "Unable to lookup component.", ce);
128: } finally {
129: this .manager.release(service);
130: }
131: }
132:
133: /* (non-Javadoc)
134: * @see org.apache.cocoon.portal.event.EventConverter#finish()
135: */
136: public void finish() {
137: PortalService service = null;
138: try {
139: service = (PortalService) this .manager
140: .lookup(PortalService.ROLE);
141: service.removeAttribute(DECODE_LIST);
142: } catch (ServiceException ce) {
143: throw new CascadingRuntimeException(
144: "Unable to lookup component.", ce);
145: } finally {
146: this .manager.release(service);
147: }
148: }
149:
150: /* (non-Javadoc)
151: * @see org.apache.cocoon.portal.event.EventConverter#isMarshallEvents()
152: */
153: public boolean isMarshallEvents() {
154: return false;
155: }
156: }
|