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.components.profiler;
018:
019: import org.apache.avalon.framework.configuration.Configurable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.thread.ThreadSafe;
024:
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * Profiler component implementation. Stores profiler data for
031: * all pipelines.
032: *
033: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
034: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
035: * @version CVS $Id: ProfilerImpl.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class ProfilerImpl extends AbstractLogEnabled implements
038: Profiler, ThreadSafe, Configurable {
039:
040: // Maximal count of entries, which should be stored.
041: private int results_count = 10;
042:
043: private Map results;
044:
045: public ProfilerImpl() {
046: results = new HashMap();
047: }
048:
049: /**
050: * Pass the Configuration to the Configurable class. This method must
051: * always be called after the constructor and before any other method.
052: *
053: * @param configuration the class configurations.
054: */
055: public void configure(Configuration configuration)
056: throws ConfigurationException {
057:
058: this .results_count = configuration.getAttributeAsInteger(
059: "results", 10);
060: }
061:
062: /**
063: * Clear the results.
064: */
065: public void clearResults() {
066: results.clear();
067: }
068:
069: /**
070: * Remove the specified result.
071: */
072: public void clearResult(Object key) {
073: results.remove(key);
074: }
075:
076: /**
077: * Returns a collection of all keys
078: *
079: * @return Keys of all results.
080: */
081: public Collection getResultKeys() {
082: return results.keySet();
083: }
084:
085: /**
086: * Returns a collection of the results.
087: *
088: * @return Collection of results.
089: */
090: public Collection getResults() {
091: return results.values();
092: }
093:
094: /**
095: * Returns a result of a specifed key.
096: *
097: * @param key Key of the result.
098: * @return Result of the profiling
099: */
100: public ProfilerResult getResult(Object key) {
101: return (ProfilerResult) results.get(key);
102: }
103:
104: /**
105: * Add a result for a request.
106: *
107: * @param uri URI of the request
108: * @param data Result of the profiling
109: */
110: public void addResult(String uri, ProfilerData data) {
111: Long key = new Long(data.getKey(uri));
112: ProfilerResult result = (ProfilerResult) results.get(key);
113: if (result == null) {
114: synchronized (results) {
115: if ((result = (ProfilerResult) results.get(key)) == null)
116: results.put(key, result = new ProfilerResult(uri,
117: results_count));
118: }
119: }
120:
121: result.addData(data);
122: }
123: }
|