001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.management.counters;
019:
020: import java.util.Map;
021: import java.util.concurrent.ConcurrentHashMap;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import javax.annotation.PostConstruct;
026: import javax.annotation.Resource;
027: import javax.management.JMException;
028: import javax.management.ObjectName;
029:
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.common.logging.LogUtils;
032: import org.apache.cxf.interceptor.Interceptor;
033: import org.apache.cxf.management.InstrumentationManager;
034: import org.apache.cxf.management.interceptor.ResponseTimeMessageInInterceptor;
035: import org.apache.cxf.management.interceptor.ResponseTimeMessageInvokerInterceptor;
036: import org.apache.cxf.management.interceptor.ResponseTimeMessageOutInterceptor;
037:
038: /* Counters are created and managed by CounterRepository
039: * If a counter which is queried form the counterRepository is not exist,
040: * the CounterRepository will create a counter and expose it to the JMX
041: * Because we can get the instance of the counter object,
042: * it is not need to qurey the counter object from JMX
043: * */
044: public class CounterRepository {
045: private static final Logger LOG = LogUtils
046: .getL7dLogger(CounterRepository.class);
047:
048: private Map<ObjectName, Counter> counters;
049: private Bus bus;
050:
051: public CounterRepository() {
052: counters = new ConcurrentHashMap<ObjectName, Counter>();
053: }
054:
055: @Resource(name="bus")
056: public void setBus(Bus b) {
057: bus = b;
058: }
059:
060: public Bus getBus() {
061: return bus;
062: }
063:
064: public Map<ObjectName, Counter> getCounters() {
065: return counters;
066: }
067:
068: @PostConstruct
069: void registerInterceptorsToBus() {
070: Interceptor in = new ResponseTimeMessageInInterceptor();
071: Interceptor invoker = new ResponseTimeMessageInvokerInterceptor();
072: Interceptor out = new ResponseTimeMessageOutInterceptor();
073:
074: bus.getInInterceptors().add(in);
075: bus.getInInterceptors().add(invoker);
076: bus.getOutInterceptors().add(out);
077: bus.setExtension(this , CounterRepository.class);
078:
079: //create CounterRepositroyMoniter to writer the counter log
080:
081: //if the service is stopped or removed, the counters should remove itself
082: }
083:
084: public void increaseCounter(ObjectName on,
085: MessageHandlingTimeRecorder mhtr) {
086: Counter counter = getCounter(on);
087: if (null == counter) {
088: counter = createCounter(on, mhtr);
089: counters.put(on, counter);
090: }
091: counter.increase(mhtr);
092: }
093:
094: //find a counter
095: public Counter getCounter(ObjectName on) {
096: return counters.get(on);
097: }
098:
099: public Counter createCounter(ObjectName on,
100: MessageHandlingTimeRecorder mhtr) {
101: Counter counter = null;
102: counter = new ResponseTimeCounter(on);
103: InstrumentationManager im = bus
104: .getExtension(InstrumentationManager.class);
105: if (null != im) {
106: try {
107: im.register(counter);
108: } catch (JMException e) {
109: LOG.log(Level.WARNING,
110: "INSTRUMENTATION_REGISTER_FAULT_MSG",
111: new Object[] { e });
112: }
113: }
114: return counter;
115: }
116:
117: }
|