KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > event > aspect > impl > AbstractContentEventAspect


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.aspect.impl;
17
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.service.Serviceable;
24 import org.apache.avalon.framework.thread.ThreadSafe;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.portal.PortalService;
28 import org.apache.cocoon.portal.event.Event;
29 import org.apache.cocoon.portal.event.EventManager;
30 import org.apache.cocoon.portal.event.aspect.EventAspect;
31 import org.apache.cocoon.portal.event.aspect.EventAspectContext;
32 import org.apache.cocoon.portal.layout.Layout;
33
34 /**
35  *
36  * @author <a HREF="mailto:juergen.seitz@basf-it-services.com">J&uuml;rgen Seitz</a>
37  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
38  *
39  * @version CVS $Id: AbstractContentEventAspect.java 291593 2005-09-26 10:49:55Z cziegeler $
40  */

41 public abstract class AbstractContentEventAspect
42     extends AbstractLogEnabled
43     implements EventAspect, ThreadSafe, Serviceable {
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     protected abstract String JavaDoc getRequestParameterName();
55     
56     protected abstract int getRequiredValueCount();
57     
58     /**
59      * Custom publishing of an event.
60      * The real values for the event are contained in the array
61      * starting with index 2!
62      * @param layout The corresponding layout
63      * @param values The values contained in the request
64      */

65     protected abstract void publish(EventManager publisher, Layout layout, String JavaDoc[] values);
66     
67     /**
68      * Publish the event.
69      * This method gets the layout object from the first two
70      * values and invokes {@link #publish(EventManager, Layout, String[])}.
71      * @param values The values contained in the request
72      */

73     protected void publish( PortalService service, EventManager publisher, String JavaDoc[] values) {
74         Layout layout = service.getComponentManager().getProfileManager().getPortalLayout(values[0], values[1] );
75         if ( layout != null ) {
76             this.publish( publisher, layout, values);
77         }
78     }
79     
80     /* (non-Javadoc)
81      * @see org.apache.cocoon.portal.event.aspect.EventAspect#process(org.apache.cocoon.portal.event.aspect.EventAspectContext, org.apache.cocoon.portal.PortalService)
82      */

83     public void process(EventAspectContext context, PortalService service) {
84         final Request request = ObjectModelHelper.getRequest(context.getObjectModel());
85         String JavaDoc[] values = request.getParameterValues(this.getRequestParameterName());
86         if (values != null) {
87             final EventManager publisher = service.getComponentManager().getEventManager();
88             for (int i = 0; i < values.length; i++) {
89                 // first try to make an event out of the value of the parameter
90
final String JavaDoc value = values[i];
91                 Event e = null;
92                 try {
93                     e = context.getEventConverter().decode(value);
94                     if (null != e) {
95                         publisher.send(e);
96                     }
97                 } catch (Exception JavaDoc ignore) {
98                     // ignroe it
99
}
100                 // the event could not be generated, so try different approach
101
if (e == null) {
102                     // Use '|' character as delimiter between Group, ID and URI
103
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, "|");
104                     int tokenNumber = 0;
105                     int tokenCount = tokenizer.countTokens();
106                     // if only 2 params are in the String
107
// the groupKey is missing and defaults to null
108
if (tokenCount == this.getRequiredValueCount()-1) {
109                         tokenNumber = tokenNumber + 1;
110                         tokenCount++;
111                     }
112                     
113                     if ( tokenCount == this.getRequiredValueCount() ) {
114                         String JavaDoc [] eventValues = new String JavaDoc[tokenCount];
115                                         
116                         while (tokenizer.hasMoreTokens()) {
117                             eventValues[tokenNumber] = tokenizer.nextToken();
118                         
119                             tokenNumber = tokenNumber + 1;
120                         }
121                     
122                         this.publish( service, publisher, eventValues );
123
124                     } else {
125                         this.getLogger().warn("Data for Event is not set correctly");
126                     }
127                 }
128             }
129         }
130         // and invoke next one
131
context.invokeNext(service);
132     }
133
134 }
135
Popular Tags