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     */
017    package org.apache.camel.management;
018    
019    import java.util.ArrayList;
020    import java.util.EventObject;
021    import java.util.List;
022    
023    import org.apache.camel.ManagementStatisticsLevel;
024    import org.apache.camel.model.ProcessorDefinition;
025    import org.apache.camel.spi.EventFactory;
026    import org.apache.camel.spi.EventNotifier;
027    import org.apache.camel.spi.ManagementAgent;
028    import org.apache.camel.spi.ManagementNamingStrategy;
029    import org.apache.camel.spi.ManagementStrategy;
030    import org.apache.camel.util.ServiceHelper;
031    import org.fusesource.commons.management.Statistic;
032    
033    /**
034     * A default management strategy that does <b>not</b> manage.
035     * <p/>
036     * This is default only used if Camel detects that it cannot use the JMX capable
037     * {@link org.apache.camel.management.ManagedManagementStrategy} strategy. Then Camel will
038     * fallback to use this instead that is basically a simple and <tt>noop</tt> strategy.
039     * <p/>
040     * This class can also be used to extend your custom management implement. In fact the JMX capable
041     * provided by Camel extends this class as well.
042     *
043     * @see ManagedManagementStrategy
044     * @version $Revision: 898746 $
045     */
046    public class DefaultManagementStrategy implements ManagementStrategy {
047    
048        private List<EventNotifier> eventNotifiers = new ArrayList<EventNotifier>();
049        private EventFactory eventFactory = new DefaultEventFactory();
050        private ManagementNamingStrategy managementNamingStrategy;
051        private boolean onlyManageProcessorWithCustomId;
052        private ManagementAgent managementAgent;
053        private ManagementStatisticsLevel statisticsLevel = ManagementStatisticsLevel.All;
054    
055        public List<EventNotifier> getEventNotifiers() {
056            return eventNotifiers;
057        }
058    
059        public void addEventNotifier(EventNotifier eventNotifier) {
060            this.eventNotifiers.add(eventNotifier);
061        }
062    
063        public void setEventNotifiers(List<EventNotifier> eventNotifiers) {
064            this.eventNotifiers = eventNotifiers;
065        }
066    
067        public EventFactory getEventFactory() {
068            return eventFactory;
069        }
070    
071        public void setEventFactory(EventFactory eventFactory) {
072            this.eventFactory = eventFactory;
073        }
074    
075        public ManagementNamingStrategy getManagementNamingStrategy() {
076            if (managementNamingStrategy == null) {
077                managementNamingStrategy = new DefaultManagementNamingStrategy();
078            }
079            return managementNamingStrategy;
080        }
081    
082        public void setManagementNamingStrategy(ManagementNamingStrategy managementNamingStrategy) {
083            this.managementNamingStrategy = managementNamingStrategy;
084        }
085    
086        public ManagementAgent getManagementAgent() {
087            return managementAgent;
088        }
089    
090        public void setManagementAgent(ManagementAgent managementAgent) {
091            this.managementAgent = managementAgent;
092        }
093    
094        public void onlyManageProcessorWithCustomId(boolean flag) {
095            onlyManageProcessorWithCustomId = flag;
096        }
097    
098        public boolean isOnlyManageProcessorWithCustomId() {
099            return onlyManageProcessorWithCustomId;
100        }
101    
102        public boolean manageProcessor(ProcessorDefinition<?> definition) {
103            return false;
104        }
105    
106        public void manageObject(Object managedObject) throws Exception {
107            // noop
108        }
109    
110        public void manageNamedObject(Object managedObject, Object preferedName) throws Exception {
111            // noop
112        }
113    
114        public <T> T getManagedObjectName(Object managedObject, String customName, Class<T> nameType) throws Exception {
115            // noop
116            return null;
117        }
118    
119        public void unmanageObject(Object managedObject) throws Exception {
120            // noop
121        }
122    
123        public void unmanageNamedObject(Object name) throws Exception {
124            // noop
125        }
126    
127        public boolean isManaged(Object managedObject, Object name) {
128            // noop
129            return false;
130        }
131    
132        public void notify(EventObject event) throws Exception {
133            if (eventNotifiers != null && !eventNotifiers.isEmpty()) {
134                for (EventNotifier notifier : eventNotifiers) {
135                    if (notifier.isEnabled(event)) {
136                        notifier.notify(event);
137                    }
138                }
139            }
140        }
141    
142        public Statistic createStatistic(String name, Object owner, Statistic.UpdateMode updateMode) {
143            // noop
144            return null;
145        }
146    
147        public void setStatisticsLevel(ManagementStatisticsLevel level) {
148            this.statisticsLevel = level;
149        }
150    
151        public ManagementStatisticsLevel getStatisticsLevel() {
152            return statisticsLevel;
153        }
154    
155        public void start() throws Exception {
156            if (eventNotifiers != null) {
157                ServiceHelper.startServices(eventNotifiers);
158            }
159            if (managementAgent != null) {
160                managementAgent.start();
161                // set the naming strategy using the domain name from the agent
162                if (managementNamingStrategy == null) {
163                    setManagementNamingStrategy(new DefaultManagementNamingStrategy(managementAgent.getMBeanObjectDomainName()));
164                }
165            }
166        }
167    
168        public void stop() throws Exception {
169            if (managementAgent != null) {
170                managementAgent.stop();
171            }
172            if (eventNotifiers != null) {
173                ServiceHelper.stopServices(eventNotifiers);
174            }
175        }
176    
177    }