View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.profiler.rules.impl;
18  
19  import java.util.Iterator;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.jetspeed.profiler.ProfileLocator;
24  import org.apache.jetspeed.profiler.Profiler;
25  import org.apache.jetspeed.profiler.rules.ProfileResolvers;
26  import org.apache.jetspeed.profiler.rules.ProfilingRule;
27  import org.apache.jetspeed.profiler.rules.RuleCriterion;
28  import org.apache.jetspeed.profiler.rules.RuleCriterionResolver;
29  import org.apache.jetspeed.request.RequestContext;
30  
31  /***
32   * StandardProfilingRule applies the standard Jetspeed-1 profiling rules.
33   * The result is an ordered list of Profile Locator name/value pairs.
34   * 
35   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36   * @version $Id: StandardProfilingRule.java 516448 2007-03-09 16:25:47Z ate $
37   */
38  public class StandardProfilingRule
39      extends AbstractProfilingRule
40      implements ProfilingRule
41  {
42      protected final static Log log = LogFactory.getLog(StandardProfilingRule.class);            
43      private static final long serialVersionUID = 1;    
44              
45      public StandardProfilingRule()
46      {        
47          this.setClassname(this.getClass().getName());
48      }
49      
50      public StandardProfilingRule(ProfileResolvers resolvers) 
51      {
52          super(resolvers);
53          this.setClassname(this.getClass().getName());
54      }
55      
56      /* (non-Javadoc)
57       * @see org.apache.jetspeed.profiler.rules.ProfilingRule#apply(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.profiler.Profiler)
58       */    
59      public ProfileLocator apply(RequestContext context, Profiler service)
60      {
61          StringBuffer key = new StringBuffer();
62          int count = 0;
63          
64          // first pass, build the key
65          Iterator criteria = this.getRuleCriteria().iterator();
66          while (criteria.hasNext())
67          {
68              RuleCriterion criterion = (RuleCriterion)criteria.next();
69              if (criterion.getType() == null)
70              {
71                  log.warn("Invalid criterion provided - type null on rule " + this);
72              }
73              RuleCriterionResolver resolver = getResolver(criterion.getType());
74              if (resolver == null)
75              {
76                  resolver = getDefaultResolver();
77              }
78              String value = resolver.resolve(context, criterion);
79              key.append(criterion.getName());
80              key.append(ProfileLocator.PATH_SEPARATOR);
81              key.append(value);
82              if (criteria.hasNext())
83              {
84                  key.append(ProfileLocator.PATH_SEPARATOR);
85              }
86              count++;                                                                                                    
87          }
88          
89          // try to get the profile locator from the cache,
90          // request path and key sufficient to generate unique key
91          String requestPath = context.getPath();
92          String locatorKey = ((requestPath != null) ? requestPath : "/") + ProfileLocator.PATH_SEPARATOR + key.toString();
93          ProfileLocator locator = getLocatorFromCache(locatorKey); 
94          if (locator != null)
95          {
96              return locator;
97          }
98          
99          // second pass, build the locator object         
100         locator = service.createLocator(context);
101         criteria = this.getRuleCriteria().iterator();
102         while (criteria.hasNext())
103         {
104             RuleCriterion criterion = (RuleCriterion)criteria.next();
105             if (criterion.getType() == null)
106             {
107                 log.warn("Invalid criterion provided - type null on rule " + this);
108             }
109             RuleCriterionResolver resolver = getResolver(criterion.getType());
110             if (resolver != null)
111             {
112                 String value = resolver.resolve(context, criterion);
113                 boolean isControl = resolver.isControl(criterion);
114                 boolean isNavigation = resolver.isNavigation(criterion);                
115                 locator.add(criterion, isControl, isNavigation, value);
116             }                
117         }               
118              
119         addLocatorToCache(locatorKey, locator);
120         return locator; 
121     }
122         
123 }
124 
125