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.jetspeed.profiler.rules.impl;
018:
019: import java.util.Iterator;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.jetspeed.profiler.ProfileLocator;
024: import org.apache.jetspeed.profiler.Profiler;
025: import org.apache.jetspeed.profiler.rules.ProfileResolvers;
026: import org.apache.jetspeed.profiler.rules.ProfilingRule;
027: import org.apache.jetspeed.profiler.rules.RuleCriterion;
028: import org.apache.jetspeed.profiler.rules.RuleCriterionResolver;
029: import org.apache.jetspeed.request.RequestContext;
030:
031: /**
032: * StandardProfilingRule applies the standard Jetspeed-1 profiling rules.
033: * The result is an ordered list of Profile Locator name/value pairs.
034: *
035: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
036: * @version $Id: StandardProfilingRule.java 516448 2007-03-09 16:25:47Z ate $
037: */
038: public class StandardProfilingRule extends AbstractProfilingRule
039: implements ProfilingRule {
040: protected final static Log log = LogFactory
041: .getLog(StandardProfilingRule.class);
042: private static final long serialVersionUID = 1;
043:
044: public StandardProfilingRule() {
045: this .setClassname(this .getClass().getName());
046: }
047:
048: public StandardProfilingRule(ProfileResolvers resolvers) {
049: super (resolvers);
050: this .setClassname(this .getClass().getName());
051: }
052:
053: /* (non-Javadoc)
054: * @see org.apache.jetspeed.profiler.rules.ProfilingRule#apply(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.profiler.Profiler)
055: */
056: public ProfileLocator apply(RequestContext context, Profiler service) {
057: StringBuffer key = new StringBuffer();
058: int count = 0;
059:
060: // first pass, build the key
061: Iterator criteria = this .getRuleCriteria().iterator();
062: while (criteria.hasNext()) {
063: RuleCriterion criterion = (RuleCriterion) criteria.next();
064: if (criterion.getType() == null) {
065: log
066: .warn("Invalid criterion provided - type null on rule "
067: + this );
068: }
069: RuleCriterionResolver resolver = getResolver(criterion
070: .getType());
071: if (resolver == null) {
072: resolver = getDefaultResolver();
073: }
074: String value = resolver.resolve(context, criterion);
075: key.append(criterion.getName());
076: key.append(ProfileLocator.PATH_SEPARATOR);
077: key.append(value);
078: if (criteria.hasNext()) {
079: key.append(ProfileLocator.PATH_SEPARATOR);
080: }
081: count++;
082: }
083:
084: // try to get the profile locator from the cache,
085: // request path and key sufficient to generate unique key
086: String requestPath = context.getPath();
087: String locatorKey = ((requestPath != null) ? requestPath : "/")
088: + ProfileLocator.PATH_SEPARATOR + key.toString();
089: ProfileLocator locator = getLocatorFromCache(locatorKey);
090: if (locator != null) {
091: return locator;
092: }
093:
094: // second pass, build the locator object
095: locator = service.createLocator(context);
096: criteria = this .getRuleCriteria().iterator();
097: while (criteria.hasNext()) {
098: RuleCriterion criterion = (RuleCriterion) criteria.next();
099: if (criterion.getType() == null) {
100: log
101: .warn("Invalid criterion provided - type null on rule "
102: + this );
103: }
104: RuleCriterionResolver resolver = getResolver(criterion
105: .getType());
106: if (resolver != null) {
107: String value = resolver.resolve(context, criterion);
108: boolean isControl = resolver.isControl(criterion);
109: boolean isNavigation = resolver.isNavigation(criterion);
110: locator.add(criterion, isControl, isNavigation, value);
111: }
112: }
113:
114: addLocatorToCache(locatorKey, locator);
115: return locator;
116: }
117:
118: }
|