1 /*
2 ~ Copyright 2006-2007 Nicolas De Loof.
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.jmonit.features;
17
18 import org.jmonit.Monitor;
19 import org.jmonit.events.MonitoringEvent;
20 import org.jmonit.events.MonitoringEventBus;
21 import org.jmonit.events.MonitoringEventListener;
22 import org.jmonit.spi.Plugin;
23
24 /**
25 * Abstract support class for Plugins. Stores the {@link MonitoringEventBus} and
26 * provides a delegate method to publish events. Automatically register the
27 * plugin as a {@link MonitoringEventListener}.
28 *
29 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
30 */
31 public abstract class AbstractFeature<F>
32 implements Plugin<F>, MonitoringEventListener
33 {
34
35 private MonitoringEventBus bus;
36
37 private Monitor monitor;
38
39 /**
40 * {@inheritDoc}
41 *
42 * @see org.jmonit.spi.Plugin#setMonitoringEventBus(org.jmonit.events.MonitoringEventBus)
43 */
44 public void setMonitoringEventBus( MonitoringEventBus bus )
45 {
46 this.bus = bus;
47 bus.addListener( this );
48 }
49
50 /**
51 * {@inheritDoc}
52 *
53 * @see org.jmonit.spi.Plugin#setMonitor(org.jmonit.Monitor)
54 */
55 public void setMonitor( Monitor monitor )
56 {
57 this.monitor = monitor;
58 }
59
60 public void fireMonitoringEvent( MonitoringEvent event )
61 {
62 bus.fireMonitoringEvent( event );
63 }
64
65 public <T> T getFeature( Class<T> feature )
66 {
67 return monitor.getFeature( feature );
68 }
69
70 }