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.webservices.instrument;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020:
021: import org.apache.excalibur.instrument.InstrumentManager;
022: import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
023: import org.apache.excalibur.instrument.manager.InstrumentDescriptor;
024: import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
025: import org.apache.excalibur.instrument.manager.InstrumentableDescriptor;
026:
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: /**
031: * Implementation of {@link InstrumentationService} component. This component
032: * allows you to access sample information from the InstrumentManager.
033: *
034: * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
035: * @version $Id: InstrumentationServiceImpl.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public final class InstrumentationServiceImpl extends
038: AbstractLogEnabled implements InstrumentationService {
039:
040: private static final int[] EMPTY_INT_ARRAY = {};
041: private static final String[] EMPTY_STRING_ARRAY = {};
042:
043: // instrument manager reference
044: private DefaultInstrumentManager m_iManager;
045:
046: /**
047: * Sets the {@link InstrumentManager} for this service object.
048: *
049: * @param iManager an {@link InstrumentManager} instance
050: */
051: public void setInstrumentManager(final InstrumentManager iManager) {
052:
053: if (iManager == null) {
054: if (getLogger().isWarnEnabled())
055: getLogger()
056: .warn(
057: "No instrument manager available,"
058: + "please enable instrumentation in your web.xml");
059: }
060:
061: // we require a DefaultInstrumentManager, attempt a cast.
062: if (iManager instanceof DefaultInstrumentManager) {
063: m_iManager = (DefaultInstrumentManager) iManager;
064: } else {
065: throw new UnsupportedOperationException(
066: "InstrumentationService only supports DefaultInstrumentManager");
067: }
068: }
069:
070: /**
071: * Obtain an array of samples from a specified sample name.
072: *
073: * <p>
074: * The specified path parameter identifies a sample, hierarchically from
075: * Instrumentable name to Instrument name, to Instrument sample name
076: * (including any child Instrumentables) using the '.' character as a
077: * separator.
078: * </p>
079: *
080: * <pre>
081: * eg: instrument-manager.active-thread-count.maximum_1000_600
082: * </pre>
083: *
084: * <p>
085: * The above example identifies the sample 'maximum_1000_600' on instrument
086: * 'active-thread-count', on instrumentable 'instrument-manager'.
087: * </p>
088: *
089: * <p>
090: * The length of the returned array is dependant on the configuration of the
091: * sample being accessed. Check instrumentation.xconf for the length of pre-
092: * defined samples that operate constantly, when instrumentation is enabled.
093: * </p>
094: *
095: * @param path path value
096: * @return an <code>int[]</code> array of samples
097: * @exception Exception if an error occurs
098: */
099: public int[] getSampleSnapshot(final String path) throws Exception {
100:
101: // ensure we have an instrument manager available
102: if (!haveInstrumentManager()) {
103: getLogger()
104: .warn(
105: "No instrument manager available,"
106: + "please enable instrumentation in your web.xml");
107: return EMPTY_INT_ARRAY;
108: }
109:
110: // return the samples
111: return m_iManager.locateInstrumentSampleDescriptor(path)
112: .getSnapshot().getSamples();
113: }
114:
115: /**
116: * Obtain a list of available samples, useful for browsing
117: * available samples.
118: *
119: * @return an {@link String}[] array of sample names
120: */
121: public String[] getSampleNames() {
122:
123: // ensure we have an instrument manager available
124: if (!haveInstrumentManager()) {
125: getLogger()
126: .warn(
127: "No instrument manager available,"
128: + "please enable instrumentation in your web.xml");
129: return EMPTY_STRING_ARRAY;
130: }
131:
132: // list all instrumentables
133: final InstrumentableDescriptor[] descriptors = m_iManager
134: .getInstrumentableDescriptors();
135: final List names = new ArrayList();
136:
137: for (int i = 0; i < descriptors.length; ++i) {
138: // list all instruments
139: InstrumentDescriptor[] insts = descriptors[i]
140: .getInstrumentDescriptors();
141:
142: for (int k = 0; k < insts.length; ++k) {
143:
144: // list all samples
145: InstrumentSampleDescriptor[] samples = insts[k]
146: .getInstrumentSampleDescriptors();
147:
148: for (int j = 0; j < samples.length; ++j) {
149: names.add(samples[j].getName());
150: }
151: }
152: }
153:
154: return (String[]) names.toArray(new String[] {});
155: }
156:
157: /**
158: * Helper method to determine if a valid instrument manager is available
159: *
160: * @return true if an instrument manager is present, false otherwise
161: */
162: private boolean haveInstrumentManager() {
163: return (m_iManager != null);
164: }
165: }
|