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: import java.util.StringTokenizer;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.jetspeed.profiler.ProfileLocator;
025: import org.apache.jetspeed.profiler.Profiler;
026: import org.apache.jetspeed.profiler.rules.ProfileResolvers;
027: import org.apache.jetspeed.profiler.rules.ProfilingRule;
028: import org.apache.jetspeed.profiler.rules.RuleCriterion;
029: import org.apache.jetspeed.profiler.rules.RuleCriterionResolver;
030: import org.apache.jetspeed.request.RequestContext;
031:
032: /**
033: * RoleFallbackProfilingRule
034: *
035: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
036: * @version $Id: RoleFallbackProfilingRule.java 517121 2007-03-12 07:45:49Z ate $
037: */
038: public class RoleFallbackProfilingRule extends AbstractProfilingRule
039: implements ProfilingRule {
040: protected final static Log log = LogFactory
041: .getLog(RoleFallbackProfilingRule.class);
042: private final static long serialVersionUID = 1L;
043:
044: public RoleFallbackProfilingRule() {
045: this .setClassname(this .getClass().getName());
046:
047: }
048:
049: public RoleFallbackProfilingRule(ProfileResolvers resolvers) {
050: super (resolvers);
051: this .setClassname(this .getClass().getName());
052: }
053:
054: /* (non-Javadoc)
055: * @see org.apache.jetspeed.profiler.rules.ProfilingRule#apply(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.profiler.Profiler)
056: */
057: public ProfileLocator apply(RequestContext context, Profiler service) {
058: StringBuffer key = new StringBuffer();
059: int count = 0;
060:
061: // first pass, build the key
062: Iterator criteria = this .getRuleCriteria().iterator();
063: while (criteria.hasNext()) {
064: RuleCriterion criterion = (RuleCriterion) criteria.next();
065: if (criterion.getType() == null) {
066: log
067: .warn("Invalid criterion provided - type null on rule "
068: + this );
069: }
070: RuleCriterionResolver resolver = getResolver(criterion
071: .getType());
072: if (resolver == null) {
073: resolver = getDefaultResolver();
074: }
075: String value = resolver.resolve(context, criterion);
076: if (value != null
077: && (resolver instanceof RoleCriterionResolver || resolver instanceof GroupCriterionResolver)) {
078: StringTokenizer tokenizer = new StringTokenizer(value,
079: StandardResolver.VALUE_DELIMITER);
080: while (tokenizer.hasMoreTokens()) {
081: String token = tokenizer.nextToken();
082: key.append(criterion.getName());
083: key.append(ProfileLocator.PATH_SEPARATOR);
084: key.append(token);
085: if (tokenizer.hasMoreTokens()) {
086: key.append(ProfileLocator.PATH_SEPARATOR);
087: }
088: }
089: } else {
090: key.append(criterion.getName());
091: key.append(ProfileLocator.PATH_SEPARATOR);
092: key.append(value);
093: }
094: if (criteria.hasNext()) {
095: key.append(ProfileLocator.PATH_SEPARATOR);
096: }
097: count++;
098: }
099: // try to get the profile locator from the cache,
100: // request path and key sufficient to generate unique key
101: String requestPath = context.getPath();
102: String locatorKey = ((requestPath != null) ? requestPath : "/")
103: + ProfileLocator.PATH_SEPARATOR + key.toString();
104: ProfileLocator locator = getLocatorFromCache(locatorKey);
105: if (locator != null) {
106: return locator;
107: }
108:
109: // second pass, build the locator object
110: locator = service.createLocator(context);
111: criteria = this .getRuleCriteria().iterator();
112: while (criteria.hasNext()) {
113: RuleCriterion criterion = (RuleCriterion) criteria.next();
114: if (criterion.getType() == null) {
115: log
116: .warn("Invalid criterion provided - name or type null on rule "
117: + this );
118: }
119: RuleCriterionResolver resolver = getResolver(criterion
120: .getType());
121: if (resolver != null) {
122: String value = resolver.resolve(context, criterion);
123: boolean isControl = resolver.isControl(criterion);
124: boolean isNavigation = resolver.isNavigation(criterion);
125: if (value != null
126: && (resolver instanceof RoleCriterionResolver || resolver instanceof GroupCriterionResolver)) {
127: StringTokenizer tokenizer = new StringTokenizer(
128: value, StandardResolver.VALUE_DELIMITER);
129: while (tokenizer.hasMoreTokens()) {
130: String token = tokenizer.nextToken();
131: locator.add(criterion, isControl, isNavigation,
132: token);
133: }
134: } else {
135: locator.add(criterion, isControl, isNavigation,
136: value);
137: }
138: }
139: }
140:
141: addLocatorToCache(locatorKey, locator);
142: return locator;
143:
144: }
145: }
|