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