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: */
018:
019: /* $Id: UsecaseRegistrationMatcher.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.cocoon.matching;
022:
023: import java.util.Collections;
024: import java.util.Map;
025:
026: import org.apache.avalon.excalibur.pool.Poolable;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.cocoon.environment.ObjectModelHelper;
033: import org.apache.cocoon.environment.Request;
034: import org.apache.cocoon.matching.Matcher;
035: import org.apache.cocoon.sitemap.PatternException;
036: import org.apache.lenya.cms.usecase.UsecaseResolver;
037: import org.apache.lenya.util.ServletHelper;
038:
039: /**
040: * Matches if the request calls a usecase which is registered for the Usecase Framework.
041: */
042: public class UsecaseRegistrationMatcher extends AbstractLogEnabled
043: implements Matcher, Serviceable, Poolable {
044:
045: /**
046: * @see org.apache.cocoon.matching.Matcher#match(java.lang.String,
047: * java.util.Map, org.apache.avalon.framework.parameters.Parameters)
048: */
049: public Map match(String pattern, Map objectModel,
050: Parameters parameters) throws PatternException {
051:
052: Request request = ObjectModelHelper.getRequest(objectModel);
053: String usecaseName = request.getParameter("lenya.usecase");
054:
055: Map result = null;
056:
057: if (usecaseName == null) {
058: if (getLogger().isDebugEnabled()) {
059: getLogger()
060: .debug(
061: "match() called, usecase parameter is empty - returning false");
062: }
063: } else {
064: if (getLogger().isDebugEnabled()) {
065: getLogger()
066: .debug(
067: "match() called for request parameter lenya.usecase = ["
068: + usecaseName
069: + "]; note that pattern is not relevant for this matcher");
070: }
071: // Parameter for usecase is set, see if this is a registered component
072: UsecaseResolver resolver = null;
073: try {
074: resolver = (UsecaseResolver) this .manager
075: .lookup(UsecaseResolver.ROLE);
076: String url = ServletHelper.getWebappURI(request);
077: if (resolver.isRegistered(url, usecaseName)) {
078: result = Collections.EMPTY_MAP;
079: }
080: if (getLogger().isDebugEnabled()) {
081: getLogger().debug(
082: "Usecase [" + usecaseName + "] exists: ["
083: + !(result == null) + "]");
084: }
085: } catch (ServiceException e) {
086: throw new PatternException(e);
087: } finally {
088: if (resolver != null) {
089: this .manager.release(resolver);
090: }
091: }
092: }
093: return result;
094: }
095:
096: private ServiceManager manager;
097:
098: /**
099: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
100: */
101: public void service(ServiceManager _manager)
102: throws ServiceException {
103: this.manager = _manager;
104: }
105:
106: }
|