KickJava   Java API By Example, From Geeks To Geeks.

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


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.cocoon.util.HashUtil;
19
20 import java.util.ArrayList JavaDoc;
21
22 /**
23  * Request-time profiler information.
24  *
25  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
26  * @author <a HREF="mailto:bruno@outerthought.org">Bruno Dumon</a>
27  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
28  * @version CVS $Id: ProfilerData.java 124685 2005-01-08 22:20:56Z antonio $
29  */

30 public class ProfilerData {
31
32     /**
33      * Entry, which stores the role and source of a component from a pipeline.
34      */

35     public static class Entry {
36
37         public String JavaDoc role;
38         public String JavaDoc source;
39         public long setup;
40         public long time;
41         public Object JavaDoc fragment;
42
43         protected Entry(String JavaDoc role, String JavaDoc source) {
44             this.role = role;
45             this.source = source;
46         }
47     }
48
49     // List of all entries
50
private ArrayList JavaDoc entries = null;
51
52     // Environment information
53
private EnvironmentInfo environmentinfo;
54
55     // Measured total time
56
private long totaltime = 0;
57
58     /**
59      * Create a new profiler dataset.
60      */

61     public ProfilerData() {
62         entries = new ArrayList JavaDoc();
63     }
64
65     /**
66      * Add new component from the pipeling, which should be measured.
67      *
68      * @param component Component of the pipeline.
69      * @param role Role of the component.
70      * @param source Source attribute of the component.
71      */

72     public void addComponent(Object JavaDoc component, String JavaDoc role, String JavaDoc source) {
73         entries.add(new Entry((role!=null)
74                               ? role
75                               : component.getClass().getName(), source));
76     }
77
78     /**
79      * Returns the count of components.
80      *
81      * @return Count of components.
82      */

83     public int getCount() {
84         return entries.size();
85     }
86
87     /**
88      * Set the environment information.
89      *
90      * @param environmentinfo Environment information.
91      */

92     public void setEnvironmentInfo(EnvironmentInfo environmentinfo) {
93         this.environmentinfo = environmentinfo;
94     }
95
96     /**
97      * Returns the environment information.
98      *
99      * @return Environment information.
100      */

101     public EnvironmentInfo getEnvironmentInfo() {
102         return this.environmentinfo;
103     }
104
105     /**
106      * Set measured time of precessing from the pipeline.
107      *
108      * @param time Total time of all components.
109      */

110     public void setTotalTime(long time) {
111         this.totaltime = time;
112     }
113
114     /**
115      * Return measured time of precessing from the pipeline.
116      *
117      * @return Total time of all components.
118      */

119     public long getTotalTime() {
120         return this.totaltime;
121     }
122
123     /**
124      * Set measured setup time of the i-th component of the pipeline.
125      *
126      * @param index Index of the component.
127      * @param time Measured setup time of the component.
128      */

129     public void setSetupTime(int index, long time) {
130         ((Entry) entries.get(index)).setup = time;
131     }
132
133     /**
134      * Get measured setup time of the i-th component of the pipeline.
135      *
136      * @param index Index of the component.
137      * @return Measured setup time of the component.
138      */

139     public long getSetupTime(int index) {
140         return ((Entry) entries.get(index)).setup;
141     }
142
143     /**
144      * Set measured processing time of the i-th component of the pipeline.
145      *
146      * @param index Index of the component.
147      * @param time Measured processing time of the component.
148      */

149     public void setProcessingTime(int index, long time) {
150         ((Entry) entries.get(index)).time = time;
151     }
152
153     /**
154      * Get measured processing time of the i-th component of the pipeline.
155      *
156      * @param index Index of the component.
157      * @return Measured processing time of the component.
158      */

159     public long getProcessingTime(int index) {
160         return ((Entry) entries.get(index)).time;
161     }
162
163     /**
164      * Set the SAX fragment for the i-th component of the pipeline.
165      *
166      * @param index Index of the component.
167      * @param fragment SAX fragment of the component.
168      */

169     public void setSAXFragment(int index, Object JavaDoc fragment) {
170         ((Entry) entries.get(index)).fragment = fragment;
171     }
172
173     /**
174      * Returns all measured times.
175      *
176      * @return Array of all entries.
177      */

178     public Entry[] getEntries() {
179         return (Entry[]) entries.toArray(new Entry[entries.size()]);
180     }
181
182     /**
183      * Generate a key for a given URI for this pipeline
184      *
185      * @param uri URI
186      * @return Hash key.
187      */

188     public long getKey(String JavaDoc uri) {
189         StringBuffer JavaDoc key = new StringBuffer JavaDoc(uri);
190
191         for (int i = 0; i<entries.size(); i++) {
192             Entry entry = (Entry) entries.get(i);
193
194             key.append(':');
195             key.append(entry.role);
196             key.append(':');
197             key.append(entry.source);
198         }
199         return HashUtil.hash(key);
200     }
201 }
202
Popular Tags