KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > event > impl > DefaultEventConverter


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.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
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 /**
32  *
33  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
34  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
35  *
36  * @version CVS $Id: DefaultEventConverter.java 125127 2005-01-14 01:21:53Z rgoers $
37  */

38 public class DefaultEventConverter
39     extends AbstractLogEnabled
40     implements EventConverter, Serviceable, ThreadSafe {
41
42     protected static final String JavaDoc DECODE_LIST = DefaultEventConverter.class.getName() + "D";
43     protected static final String JavaDoc ENCODE_LIST = DefaultEventConverter.class.getName() + "E";
44     
45     protected ServiceManager manager;
46     
47     /* (non-Javadoc)
48      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
49      */

50     public void service(ServiceManager manager) throws ServiceException {
51         this.manager = manager;
52     }
53
54     /* (non-Javadoc)
55      * @see org.apache.cocoon.portal.event.EventConverter#encode(org.apache.cocoon.portal.event.Event)
56      */

57     public String JavaDoc encode(Event event) {
58         PortalService service = null;
59         try {
60             service = (PortalService)this.manager.lookup(PortalService.ROLE);
61             List JavaDoc list = (List JavaDoc)service.getAttribute(ENCODE_LIST);
62             if ( null == list ) {
63                 list = new ArrayList JavaDoc();
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     /* (non-Javadoc)
81      * @see org.apache.cocoon.portal.event.EventConverter#decode(java.lang.String)
82      */

83     public Event decode(String JavaDoc value) {
84         if (value != null) {
85             PortalService service = null;
86             try {
87                 service = (PortalService)this.manager.lookup(PortalService.ROLE);
88                 List JavaDoc list = (List JavaDoc)service.getAttribute(DECODE_LIST);
89                 if ( null != list ) {
90                     int index = new Integer JavaDoc(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     /* (non-Javadoc)
105      * @see org.apache.cocoon.portal.event.EventConverter#start()
106      */

107     public void start() {
108         PortalService service = null;
109         try {
110             service = (PortalService)this.manager.lookup(PortalService.ROLE);
111             List JavaDoc list = (List JavaDoc)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     /* (non-Javadoc)
124      * @see org.apache.cocoon.portal.event.EventConverter#finish()
125      */

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     /* (non-Javadoc)
139      * @see org.apache.cocoon.portal.event.EventConverter#isMarshallEvents()
140      */

141     public boolean isMarshallEvents() {
142         return false;
143     }
144 }
145
Popular Tags