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.aspect.impl;
018:
019: import java.util.StringTokenizer;
020:
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.service.Serviceable;
025: import org.apache.avalon.framework.thread.ThreadSafe;
026: import org.apache.cocoon.environment.ObjectModelHelper;
027: import org.apache.cocoon.environment.Request;
028: import org.apache.cocoon.portal.PortalService;
029: import org.apache.cocoon.portal.event.Event;
030: import org.apache.cocoon.portal.event.EventManager;
031: import org.apache.cocoon.portal.event.aspect.EventAspect;
032: import org.apache.cocoon.portal.event.aspect.EventAspectContext;
033: import org.apache.cocoon.portal.layout.Layout;
034:
035: /**
036: *
037: * @author <a href="mailto:juergen.seitz@basf-it-services.com">Jürgen Seitz</a>
038: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
039: *
040: * @version CVS $Id: AbstractContentEventAspect.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042: public abstract class AbstractContentEventAspect extends
043: AbstractLogEnabled implements EventAspect, ThreadSafe,
044: Serviceable {
045:
046: protected ServiceManager manager;
047:
048: /* (non-Javadoc)
049: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
050: */
051: public void service(ServiceManager manager) throws ServiceException {
052: this .manager = manager;
053: }
054:
055: protected abstract String getRequestParameterName();
056:
057: protected abstract int getRequiredValueCount();
058:
059: /**
060: * Custom publishing of an event.
061: * The real values for the event are contained in the array
062: * starting with index 2!
063: * @param layout The corresponding layout
064: * @param values The values contained in the request
065: */
066: protected abstract void publish(EventManager publisher,
067: Layout layout, String[] values);
068:
069: /**
070: * Publish the event.
071: * This method gets the layout object from the first two
072: * values and invokes {@link #publish(EventManager, Layout, String[])}.
073: * @param values The values contained in the request
074: */
075: protected void publish(PortalService service,
076: EventManager publisher, String[] values) {
077: Layout layout = service.getComponentManager()
078: .getProfileManager().getPortalLayout(values[0],
079: values[1]);
080: if (layout != null) {
081: this .publish(publisher, layout, values);
082: }
083: }
084:
085: /* (non-Javadoc)
086: * @see org.apache.cocoon.portal.event.aspect.EventAspect#process(org.apache.cocoon.portal.event.aspect.EventAspectContext, org.apache.cocoon.portal.PortalService)
087: */
088: public void process(EventAspectContext context,
089: PortalService service) {
090: final Request request = ObjectModelHelper.getRequest(context
091: .getObjectModel());
092: String[] values = request.getParameterValues(this
093: .getRequestParameterName());
094: if (values != null) {
095: final EventManager publisher = service
096: .getComponentManager().getEventManager();
097: for (int i = 0; i < values.length; i++) {
098: // first try to make an event out of the value of the parameter
099: final String value = values[i];
100: Event e = null;
101: try {
102: e = context.getEventConverter().decode(value);
103: if (null != e) {
104: publisher.send(e);
105: }
106: } catch (Exception ignore) {
107: // ignroe it
108: }
109: // the event could not be generated, so try different approach
110: if (e == null) {
111: // Use '|' character as delimiter between Group, ID and URI
112: StringTokenizer tokenizer = new StringTokenizer(
113: value, "|");
114: int tokenNumber = 0;
115: int tokenCount = tokenizer.countTokens();
116: // if only 2 params are in the String
117: // the groupKey is missing and defaults to null
118: if (tokenCount == this .getRequiredValueCount() - 1) {
119: tokenNumber = tokenNumber + 1;
120: tokenCount++;
121: }
122:
123: if (tokenCount == this .getRequiredValueCount()) {
124: String[] eventValues = new String[tokenCount];
125:
126: while (tokenizer.hasMoreTokens()) {
127: eventValues[tokenNumber] = tokenizer
128: .nextToken();
129:
130: tokenNumber = tokenNumber + 1;
131: }
132:
133: this .publish(service, publisher, eventValues);
134:
135: } else {
136: this .getLogger().warn(
137: "Data for Event is not set correctly");
138: }
139: }
140: }
141: }
142: // and invoke next one
143: context.invokeNext(service);
144: }
145:
146: }
|