KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > profiler > ProfilerImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.profiler;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * Profiler component implementation. Stores profiler data for
30  * all pipelines.
31  *
32  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
33  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
34  * @version CVS $Id: ProfilerImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
35  */

36 public class ProfilerImpl extends AbstractLogEnabled
37     implements Profiler, ThreadSafe, Configurable {
38
39     // Maximal count of entries, which should be stored.
40
private int results_count = 10;
41
42     private Map JavaDoc results;
43
44     public ProfilerImpl()
45     {
46         results = new HashMap JavaDoc();
47     }
48
49     /**
50      * Pass the Configuration to the Configurable class. This method must
51      * always be called after the constructor and before any other method.
52      *
53      * @param configuration the class configurations.
54      */

55     public void configure(Configuration configuration)
56         throws ConfigurationException {
57
58         this.results_count = configuration.getAttributeAsInteger("results", 10);
59     }
60
61     /**
62      * Clear the results.
63      */

64     public void clearResults()
65     {
66         results.clear();
67     }
68
69     /**
70      * Remove the specified result.
71      */

72     public void clearResult(Object JavaDoc key)
73     {
74         results.remove(key);
75     }
76
77     /**
78      * Returns a collection of all keys
79      *
80      * @return Keys of all results.
81      */

82     public Collection JavaDoc getResultKeys()
83     {
84         return results.keySet();
85     }
86
87     /**
88      * Returns a collection of the results.
89      *
90      * @return Collection of results.
91      */

92     public Collection JavaDoc getResults()
93     {
94         return results.values();
95     }
96
97     /**
98      * Returns a result of a specifed key.
99      *
100      * @param key Key of the result.
101      * @return Result of the profiling
102      */

103     public ProfilerResult getResult(Object JavaDoc key)
104     {
105         return (ProfilerResult)results.get(key);
106     }
107
108     /**
109      * Add a result for a request.
110      *
111      * @param uri URI of the request
112      * @param data Result of the profiling
113      */

114     public void addResult(String JavaDoc uri, ProfilerData data)
115     {
116         Long JavaDoc key = new Long JavaDoc(data.getKey(uri));
117         ProfilerResult result = (ProfilerResult)results.get(key);
118         if(result == null){
119             synchronized(results){
120                 if((result = (ProfilerResult)results.get(key)) == null)
121                     results.put(key, result = new ProfilerResult(uri, results_count));
122             }
123         }
124
125         result.addData(data);
126     }
127 }
128
Popular Tags