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.Iterator;
020:
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.service.Serviceable;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.cocoon.environment.ObjectModelHelper;
028: import org.apache.cocoon.environment.Request;
029: import org.apache.cocoon.portal.PortalService;
030: import org.apache.cocoon.portal.impl.PageLabelManager;
031: import org.apache.cocoon.portal.event.Event;
032: import org.apache.cocoon.portal.event.EventManager;
033: import org.apache.cocoon.portal.event.aspect.EventAspect;
034: import org.apache.cocoon.portal.event.aspect.EventAspectContext;
035:
036: /**
037: * Converts links generated by the PageLabelLinkService into events and publishes them.
038: * Used in conjunction with the PageLabelLinkService, links generated from the layout
039: * portal.xml will be based upon the names of the named items.
040: *
041: * @author Ralph Goers
042: *
043: * @version CVS $Id: $
044: */
045: public class PageLabelEventAspect extends AbstractLogEnabled implements
046: EventAspect, ThreadSafe, Serviceable, Disposable {
047:
048: protected ServiceManager manager;
049:
050: protected PageLabelManager labelManager;
051:
052: /**
053: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
054: */
055: public void service(ServiceManager manager) throws ServiceException {
056: this .manager = manager;
057: this .labelManager = (PageLabelManager) manager
058: .lookup(PageLabelManager.ROLE);
059: }
060:
061: /**
062: * @see org.apache.avalon.framework.activity.Disposable#dispose()
063: */
064: public void dispose() {
065: if (this .manager != null) {
066: if (this .labelManager != null) {
067: this .manager.release(this .labelManager);
068: this .labelManager = null;
069: }
070: this .manager = null;
071: }
072: }
073:
074: /**
075: * @see org.apache.cocoon.portal.event.aspect.EventAspect#process(org.apache.cocoon.portal.event.aspect.EventAspectContext, org.apache.cocoon.portal.PortalService)
076: */
077: public void process(EventAspectContext context,
078: PortalService service) {
079: if (this .labelManager != null) {
080: final EventManager publisher = service
081: .getComponentManager().getEventManager();
082: final Request request = ObjectModelHelper
083: .getRequest(context.getObjectModel());
084: final String parameterName = this .labelManager
085: .getRequestParameterName();
086: final boolean useUrlPath = this .labelManager.isUrlPath();
087:
088: String label = (useUrlPath) ? request.getSitemapURI()
089: : request.getParameter(parameterName);
090:
091: // The pageLabel must be single valued
092: if (label != null) {
093: String previous = this .labelManager.getPreviousLabel();
094: if (previous != null && previous.equals(label)) {
095: // Already on this page. Don't publish the pageLabel events
096: } else {
097: Iterator iter = this .labelManager
098: .getPageLabelEvents(label).iterator();
099: // Publish all the events for this page label.
100: while (iter.hasNext()) {
101: Event event = (Event) iter.next();
102: publisher.send(event);
103: }
104: // return;
105: }
106: }
107: }
108:
109: context.invokeNext(service);
110: }
111: }
|