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 */
017package org.apache.camel.management;
018
019import java.util.EventObject;
020import java.util.List;
021import java.util.concurrent.CopyOnWriteArrayList;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.CamelContextAware;
025import org.apache.camel.ManagementStatisticsLevel;
026import org.apache.camel.management.event.DefaultEventFactory;
027import org.apache.camel.management.mbean.Statistic;
028import org.apache.camel.model.ProcessorDefinition;
029import org.apache.camel.spi.EventFactory;
030import org.apache.camel.spi.EventNotifier;
031import org.apache.camel.spi.ManagementAgent;
032import org.apache.camel.spi.ManagementNamingStrategy;
033import org.apache.camel.spi.ManagementObjectStrategy;
034import org.apache.camel.spi.ManagementStrategy;
035import org.apache.camel.support.ServiceSupport;
036import org.apache.camel.util.ObjectHelper;
037import org.apache.camel.util.ServiceHelper;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041/**
042 * A default management strategy that does <b>not</b> manage.
043 * <p/>
044 * This is default only used if Camel detects that it cannot use the JMX capable
045 * {@link org.apache.camel.management.ManagedManagementStrategy} strategy. Then Camel will
046 * fallback to use this instead that is basically a simple and <tt>noop</tt> strategy.
047 * <p/>
048 * This class can also be used to extend your custom management implement. In fact the JMX capable
049 * provided by Camel extends this class as well.
050 *
051 * @see ManagedManagementStrategy
052 * @version 
053 */
054public class DefaultManagementStrategy extends ServiceSupport implements ManagementStrategy, CamelContextAware {
055
056    private static final Logger LOG = LoggerFactory.getLogger(DefaultManagementStrategy.class);
057    private List<EventNotifier> eventNotifiers = new CopyOnWriteArrayList<EventNotifier>();
058    private EventFactory eventFactory = new DefaultEventFactory();
059    private ManagementNamingStrategy managementNamingStrategy;
060    private ManagementObjectStrategy managementObjectStrategy;
061    private boolean onlyManageProcessorWithCustomId;
062    private ManagementAgent managementAgent;
063    private ManagementStatisticsLevel statisticsLevel = ManagementStatisticsLevel.All;
064    private boolean loadStatisticsEnabled;
065    private CamelContext camelContext;
066
067    public DefaultManagementStrategy() {
068    }
069
070    public DefaultManagementStrategy(CamelContext camelContext) {
071        this.camelContext = camelContext;
072    }
073
074    public List<EventNotifier> getEventNotifiers() {
075        return eventNotifiers;
076    }
077
078    public void addEventNotifier(EventNotifier eventNotifier) {
079        this.eventNotifiers.add(eventNotifier);
080    }
081
082    public boolean removeEventNotifier(EventNotifier eventNotifier) {
083        return eventNotifiers.remove(eventNotifier);
084    }
085
086    public void setEventNotifiers(List<EventNotifier> eventNotifiers) {
087        this.eventNotifiers = eventNotifiers;
088    }
089
090    public EventFactory getEventFactory() {
091        return eventFactory;
092    }
093
094    public void setEventFactory(EventFactory eventFactory) {
095        this.eventFactory = eventFactory;
096    }
097
098    public ManagementNamingStrategy getManagementNamingStrategy() {
099        if (managementNamingStrategy == null) {
100            managementNamingStrategy = new DefaultManagementNamingStrategy();
101        }
102        return managementNamingStrategy;
103    }
104
105    public void setManagementNamingStrategy(ManagementNamingStrategy managementNamingStrategy) {
106        this.managementNamingStrategy = managementNamingStrategy;
107    }
108
109    public ManagementObjectStrategy getManagementObjectStrategy() {
110        if (managementObjectStrategy == null) {
111            managementObjectStrategy = new DefaultManagementObjectStrategy();
112        }
113        return managementObjectStrategy;
114    }
115
116    public void setManagementObjectStrategy(ManagementObjectStrategy managementObjectStrategy) {
117        this.managementObjectStrategy = managementObjectStrategy;
118    }
119
120    public ManagementAgent getManagementAgent() {
121        return managementAgent;
122    }
123
124    public void setManagementAgent(ManagementAgent managementAgent) {
125        this.managementAgent = managementAgent;
126    }
127
128    public void onlyManageProcessorWithCustomId(boolean flag) {
129        onlyManageProcessorWithCustomId = flag;
130    }
131
132    public boolean isOnlyManageProcessorWithCustomId() {
133        return onlyManageProcessorWithCustomId;
134    }
135
136    public boolean manageProcessor(ProcessorDefinition<?> definition) {
137        return false;
138    }
139
140    public void manageObject(Object managedObject) throws Exception {
141        // noop
142    }
143
144    public void manageNamedObject(Object managedObject, Object preferredName) throws Exception {
145        // noop
146    }
147
148    public <T> T getManagedObjectName(Object managedObject, String customName, Class<T> nameType) throws Exception {
149        // noop
150        return null;
151    }
152
153    public void unmanageObject(Object managedObject) throws Exception {
154        // noop
155    }
156
157    public void unmanageNamedObject(Object name) throws Exception {
158        // noop
159    }
160
161    public boolean isManaged(Object managedObject, Object name) {
162        // noop
163        return false;
164    }
165
166    public CamelContext getCamelContext() {
167        return camelContext;
168    }
169
170    public void setCamelContext(CamelContext camelContext) {
171        this.camelContext = camelContext;
172    }
173
174    public void notify(EventObject event) throws Exception {
175        if (eventNotifiers != null && !eventNotifiers.isEmpty()) {
176            for (EventNotifier notifier : eventNotifiers) {
177                if (notifier.isEnabled(event)) {
178                    notifier.notify(event);
179                }
180            }
181        }
182    }
183
184    public Statistic createStatistic(String name, Object owner, Statistic.UpdateMode updateMode) {
185        // noop
186        return null;
187    }
188
189    public void setStatisticsLevel(ManagementStatisticsLevel level) {
190        this.statisticsLevel = level;
191    }
192
193    public ManagementStatisticsLevel getStatisticsLevel() {
194        return statisticsLevel;
195    }
196
197    public boolean isLoadStatisticsEnabled() {
198        return loadStatisticsEnabled;
199    }
200
201    public void setLoadStatisticsEnabled(boolean loadStatisticsEnabled) {
202        this.loadStatisticsEnabled = loadStatisticsEnabled;
203    }
204
205    protected void doStart() throws Exception {
206        LOG.info("JMX is disabled");
207        doStartManagementStrategy();
208    }
209
210    protected void doStartManagementStrategy() throws Exception {
211        ObjectHelper.notNull(camelContext, "CamelContext");
212
213        if (eventNotifiers != null) {
214            for (EventNotifier notifier : eventNotifiers) {
215
216                // inject CamelContext if the service is aware
217                if (notifier instanceof CamelContextAware) {
218                    CamelContextAware aware = (CamelContextAware) notifier;
219                    aware.setCamelContext(camelContext);
220                }
221
222                ServiceHelper.startService(notifier);
223            }
224        }
225
226        if (managementAgent != null) {
227            ServiceHelper.startService(managementAgent);
228            // set the naming strategy using the domain name from the agent
229            if (managementNamingStrategy == null) {
230                setManagementNamingStrategy(new DefaultManagementNamingStrategy(managementAgent.getMBeanObjectDomainName()));
231            }
232        }
233        if (managementNamingStrategy instanceof CamelContextAware) {
234            ((CamelContextAware) managementNamingStrategy).setCamelContext(getCamelContext());
235        }
236    }
237
238    protected void doStop() throws Exception {
239        ServiceHelper.stopServices(managementAgent, eventNotifiers);
240    }
241
242}