KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webservices > instrument > InstrumentationServiceImpl


1 /*
2  * Copyright 1999-2005 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.webservices.instrument;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19
20 import org.apache.excalibur.instrument.InstrumentManager;
21 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
22 import org.apache.excalibur.instrument.manager.InstrumentDescriptor;
23 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
24 import org.apache.excalibur.instrument.manager.InstrumentableDescriptor;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Implementation of {@link InstrumentationService} component. This component
31  * allows you to access sample information from the InstrumentManager.
32  *
33  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
34  * @version $Id: InstrumentationServiceImpl.java 306633 2005-10-06 13:19:23Z vgritsenko $
35  */

36 public final class InstrumentationServiceImpl extends AbstractLogEnabled
37                                               implements InstrumentationService {
38
39     private static final int[] EMPTY_INT_ARRAY = {};
40     private static final String JavaDoc[] EMPTY_STRING_ARRAY = {};
41
42     // instrument manager reference
43
private DefaultInstrumentManager m_iManager;
44
45     /**
46      * Sets the {@link InstrumentManager} for this service object.
47      *
48      * @param iManager an {@link InstrumentManager} instance
49      */

50     public void setInstrumentManager(final InstrumentManager iManager) {
51
52         if (iManager == null) {
53             if (getLogger().isWarnEnabled())
54                 getLogger().warn(
55                     "No instrument manager available," +
56                     "please enable instrumentation in your web.xml"
57                 );
58         }
59
60         // we require a DefaultInstrumentManager, attempt a cast.
61
if (iManager instanceof DefaultInstrumentManager) {
62             m_iManager = (DefaultInstrumentManager) iManager;
63         } else {
64             throw new UnsupportedOperationException JavaDoc(
65                 "InstrumentationService only supports DefaultInstrumentManager"
66             );
67         }
68     }
69
70     /**
71      * Obtain an array of samples from a specified sample name.
72      *
73      * <p>
74      * The specified path parameter identifies a sample, hierarchically from
75      * Instrumentable name to Instrument name, to Instrument sample name
76      * (including any child Instrumentables) using the '.' character as a
77      * separator.
78      * </p>
79      *
80      * <pre>
81      * eg: instrument-manager.active-thread-count.maximum_1000_600
82      * </pre>
83      *
84      * <p>
85      * The above example identifies the sample 'maximum_1000_600' on instrument
86      * 'active-thread-count', on instrumentable 'instrument-manager'.
87      * </p>
88      *
89      * <p>
90      * The length of the returned array is dependant on the configuration of the
91      * sample being accessed. Check instrumentation.xconf for the length of pre-
92      * defined samples that operate constantly, when instrumentation is enabled.
93      * </p>
94      *
95      * @param path path value
96      * @return an <code>int[]</code> array of samples
97      * @exception Exception if an error occurs
98      */

99     public int[] getSampleSnapshot(final String JavaDoc path)
100         throws Exception JavaDoc {
101
102         // ensure we have an instrument manager available
103
if (!haveInstrumentManager()) {
104             getLogger().warn(
105                "No instrument manager available," +
106                "please enable instrumentation in your web.xml"
107             );
108             return EMPTY_INT_ARRAY;
109         }
110
111         // return the samples
112
return m_iManager.locateInstrumentSampleDescriptor(path)
113             .getSnapshot().getSamples();
114     }
115
116     /**
117      * Obtain a list of available samples, useful for browsing
118      * available samples.
119      *
120      * @return an {@link String}[] array of sample names
121      */

122     public String JavaDoc[] getSampleNames() {
123
124         // ensure we have an instrument manager available
125
if (!haveInstrumentManager()) {
126             getLogger().warn(
127                 "No instrument manager available," +
128                 "please enable instrumentation in your web.xml"
129             );
130             return EMPTY_STRING_ARRAY;
131         }
132
133         // list all instrumentables
134
final InstrumentableDescriptor[] descriptors =
135             m_iManager.getInstrumentableDescriptors();
136         final List JavaDoc names = new ArrayList JavaDoc();
137
138         for (int i = 0; i < descriptors.length; ++i) {
139             // list all instruments
140
InstrumentDescriptor[] insts =
141                 descriptors[i].getInstrumentDescriptors();
142
143             for (int k = 0; k < insts.length; ++k) {
144
145                 // list all samples
146
InstrumentSampleDescriptor[] samples =
147                     insts[k].getInstrumentSampleDescriptors();
148
149                 for (int j = 0; j < samples.length; ++j) {
150                     names.add(samples[j].getName());
151                 }
152             }
153         }
154
155         return (String JavaDoc[])names.toArray(new String JavaDoc[]{});
156     }
157
158     /**
159      * Helper method to determine if a valid instrument manager is available
160      *
161      * @return true if an instrument manager is present, false otherwise
162      */

163     private boolean haveInstrumentManager() {
164         return (m_iManager != null);
165     }
166 }
167
Popular Tags