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