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 ManagementAgent managementAgent;
062    private CamelContext camelContext;
063
064    public DefaultManagementStrategy() {
065    }
066
067    public DefaultManagementStrategy(CamelContext camelContext) {
068        this.camelContext = camelContext;
069    }
070
071    public List<EventNotifier> getEventNotifiers() {
072        return eventNotifiers;
073    }
074
075    public void addEventNotifier(EventNotifier eventNotifier) {
076        this.eventNotifiers.add(eventNotifier);
077    }
078
079    public boolean removeEventNotifier(EventNotifier eventNotifier) {
080        return eventNotifiers.remove(eventNotifier);
081    }
082
083    public void setEventNotifiers(List<EventNotifier> eventNotifiers) {
084        this.eventNotifiers = eventNotifiers;
085    }
086
087    public EventFactory getEventFactory() {
088        return eventFactory;
089    }
090
091    public void setEventFactory(EventFactory eventFactory) {
092        this.eventFactory = eventFactory;
093    }
094
095    public ManagementNamingStrategy getManagementNamingStrategy() {
096        if (managementNamingStrategy == null) {
097            managementNamingStrategy = new DefaultManagementNamingStrategy();
098        }
099        return managementNamingStrategy;
100    }
101
102    public void setManagementNamingStrategy(ManagementNamingStrategy managementNamingStrategy) {
103        this.managementNamingStrategy = managementNamingStrategy;
104    }
105
106    public ManagementObjectStrategy getManagementObjectStrategy() {
107        if (managementObjectStrategy == null) {
108            managementObjectStrategy = new DefaultManagementObjectStrategy();
109        }
110        return managementObjectStrategy;
111    }
112
113    public void setManagementObjectStrategy(ManagementObjectStrategy managementObjectStrategy) {
114        this.managementObjectStrategy = managementObjectStrategy;
115    }
116
117    public ManagementAgent getManagementAgent() {
118        return managementAgent;
119    }
120
121    public void setManagementAgent(ManagementAgent managementAgent) {
122        this.managementAgent = managementAgent;
123    }
124
125    @Deprecated
126    public void onlyManageProcessorWithCustomId(boolean flag) {
127        LOG.warn("Using @deprecated option onlyManageProcessorWithCustomId on ManagementStrategy. Configure this on ManagementAgent instead.");
128        if (managementAgent != null) {
129            getManagementAgent().setOnlyRegisterProcessorWithCustomId(flag);
130        } else {
131            throw new IllegalStateException("Not started");
132        }
133    }
134
135    @Deprecated
136    public boolean isOnlyManageProcessorWithCustomId() {
137        if (managementAgent != null) {
138            boolean only = getManagementAgent().getOnlyRegisterProcessorWithCustomId() != null && getManagementAgent().getOnlyRegisterProcessorWithCustomId();
139            return only;
140        } else {
141            throw new IllegalStateException("Not started");
142        }
143    }
144
145    public boolean manageProcessor(ProcessorDefinition<?> definition) {
146        return false;
147    }
148
149    public void manageObject(Object managedObject) throws Exception {
150        // noop
151    }
152
153    public void manageNamedObject(Object managedObject, Object preferredName) throws Exception {
154        // noop
155    }
156
157    public <T> T getManagedObjectName(Object managedObject, String customName, Class<T> nameType) throws Exception {
158        // noop
159        return null;
160    }
161
162    public void unmanageObject(Object managedObject) throws Exception {
163        // noop
164    }
165
166    public void unmanageNamedObject(Object name) throws Exception {
167        // noop
168    }
169
170    public boolean isManaged(Object managedObject, Object name) {
171        // noop
172        return false;
173    }
174
175    public CamelContext getCamelContext() {
176        return camelContext;
177    }
178
179    public void setCamelContext(CamelContext camelContext) {
180        this.camelContext = camelContext;
181    }
182
183    public void notify(EventObject event) throws Exception {
184        if (eventNotifiers != null && !eventNotifiers.isEmpty()) {
185            for (EventNotifier notifier : eventNotifiers) {
186                if (notifier.isEnabled(event)) {
187                    notifier.notify(event);
188                }
189            }
190        }
191    }
192
193    public Statistic createStatistic(String name, Object owner, Statistic.UpdateMode updateMode) {
194        // noop
195        return null;
196    }
197
198    @Deprecated
199    public void setStatisticsLevel(ManagementStatisticsLevel level) {
200        LOG.warn("Using @deprecated option statisticsLevel on ManagementStrategy. Configure this on ManagementAgent instead.");
201        if (managementAgent != null) {
202            getManagementAgent().setStatisticsLevel(level);
203        } else {
204            throw new IllegalStateException("Not started");
205        }
206    }
207
208    @Deprecated
209    public ManagementStatisticsLevel getStatisticsLevel() {
210        if (managementAgent != null) {
211            return getManagementAgent().getStatisticsLevel();
212        } else {
213            throw new IllegalStateException("Not started");
214        }
215    }
216
217    @Deprecated
218    public boolean isLoadStatisticsEnabled() {
219        if (managementAgent != null) {
220            boolean load = getManagementAgent().getLoadStatisticsEnabled() != null && getManagementAgent().getLoadStatisticsEnabled();
221            return load;
222        } else {
223            throw new IllegalStateException("Not started");
224        }
225    }
226
227    @Deprecated
228    public void setLoadStatisticsEnabled(boolean loadStatisticsEnabled) {
229        LOG.warn("Using @deprecated option loadStatisticsEnabled on ManagementStrategy. Configure this on ManagementAgent instead.");
230        if (managementAgent != null) {
231            getManagementAgent().setLoadStatisticsEnabled(loadStatisticsEnabled);
232        } else {
233            throw new IllegalStateException("Not started");
234        }
235    }
236
237    protected void doStart() throws Exception {
238        LOG.info("JMX is disabled");
239        doStartManagementStrategy();
240    }
241
242    protected void doStartManagementStrategy() throws Exception {
243        ObjectHelper.notNull(camelContext, "CamelContext");
244
245        if (eventNotifiers != null) {
246            for (EventNotifier notifier : eventNotifiers) {
247
248                // inject CamelContext if the service is aware
249                if (notifier instanceof CamelContextAware) {
250                    CamelContextAware aware = (CamelContextAware) notifier;
251                    aware.setCamelContext(camelContext);
252                }
253
254                ServiceHelper.startService(notifier);
255            }
256        }
257
258        if (managementAgent != null) {
259            ServiceHelper.startService(managementAgent);
260            // set the naming strategy using the domain name from the agent
261            if (managementNamingStrategy == null) {
262                setManagementNamingStrategy(new DefaultManagementNamingStrategy(managementAgent.getMBeanObjectDomainName()));
263            }
264        }
265        if (managementNamingStrategy instanceof CamelContextAware) {
266            ((CamelContextAware) managementNamingStrategy).setCamelContext(getCamelContext());
267        }
268    }
269
270    protected void doStop() throws Exception {
271        ServiceHelper.stopServices(managementAgent, eventNotifiers);
272    }
273
274}