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.io.IOException;
020import java.lang.management.ManagementFactory;
021import java.net.InetAddress;
022import java.net.UnknownHostException;
023import java.rmi.NoSuchObjectException;
024import java.rmi.RemoteException;
025import java.rmi.registry.LocateRegistry;
026import java.rmi.registry.Registry;
027import java.rmi.server.UnicastRemoteObject;
028import java.util.LinkedHashMap;
029import java.util.List;
030import java.util.Map;
031import java.util.concurrent.ConcurrentHashMap;
032import java.util.concurrent.ConcurrentMap;
033
034import javax.management.JMException;
035import javax.management.MBeanServer;
036import javax.management.MBeanServerFactory;
037import javax.management.MBeanServerInvocationHandler;
038import javax.management.NotCompliantMBeanException;
039import javax.management.ObjectInstance;
040import javax.management.ObjectName;
041import javax.management.remote.JMXConnectorServer;
042import javax.management.remote.JMXConnectorServerFactory;
043import javax.management.remote.JMXServiceURL;
044
045import org.apache.camel.CamelContext;
046import org.apache.camel.CamelContextAware;
047import org.apache.camel.ExtendedCamelContext;
048import org.apache.camel.ManagementStatisticsLevel;
049import org.apache.camel.api.management.JmxSystemPropertyKeys;
050import org.apache.camel.spi.ManagementAgent;
051import org.apache.camel.spi.ManagementMBeanAssembler;
052import org.apache.camel.support.management.DefaultManagementMBeanAssembler;
053import org.apache.camel.support.service.ServiceHelper;
054import org.apache.camel.support.service.ServiceSupport;
055import org.apache.camel.util.InetAddressUtil;
056import org.apache.camel.util.ObjectHelper;
057import org.apache.camel.util.StringHelper;
058import org.slf4j.Logger;
059import org.slf4j.LoggerFactory;
060
061/**
062 * Default implementation of the Camel JMX service agent
063 */
064public class DefaultManagementAgent extends ServiceSupport implements ManagementAgent, CamelContextAware {
065
066    public static final String DEFAULT_DOMAIN = "org.apache.camel";
067    public static final String DEFAULT_HOST = "localhost";
068    public static final int DEFAULT_REGISTRY_PORT = 1099;
069    public static final int DEFAULT_CONNECTION_PORT = -1;
070    public static final String DEFAULT_SERVICE_URL_PATH = "/jmxrmi/camel";
071    private static final Logger LOG = LoggerFactory.getLogger(DefaultManagementAgent.class);
072
073    private CamelContext camelContext;
074    private MBeanServer server;
075    private ManagementMBeanAssembler assembler;
076
077    // need a name -> actual name mapping as some servers changes the names (such as WebSphere)
078    private final ConcurrentMap<ObjectName, ObjectName> mbeansRegistered = new ConcurrentHashMap<>();
079    private JMXConnectorServer cs;
080    private Registry registry;
081
082    private Integer registryPort = DEFAULT_REGISTRY_PORT;
083    private Integer connectorPort = DEFAULT_CONNECTION_PORT;
084    private String mBeanServerDefaultDomain = DEFAULT_DOMAIN;
085    private String mBeanObjectDomainName = DEFAULT_DOMAIN;
086    private String serviceUrlPath = DEFAULT_SERVICE_URL_PATH;
087    private Boolean usePlatformMBeanServer = true;
088    private Boolean createConnector = false;
089    private Boolean onlyRegisterProcessorWithCustomId = false;
090    private Boolean loadStatisticsEnabled = false;
091    private Boolean endpointRuntimeStatisticsEnabled;
092    private Boolean registerAlways = false;
093    private Boolean registerNewRoutes = true;
094    private Boolean mask = true;
095    private Boolean includeHostName = false;
096    private Boolean useHostIPAddress = false;
097    private String managementNamePattern = "#name#";
098    private ManagementStatisticsLevel statisticsLevel = ManagementStatisticsLevel.Default;
099
100    public DefaultManagementAgent() {
101    }
102
103    public DefaultManagementAgent(CamelContext camelContext) {
104        this.camelContext = camelContext;
105    }
106
107    protected void finalizeSettings() throws Exception {
108        // JVM system properties take precedence over any configuration
109        Map<String, Object> values = new LinkedHashMap<>();
110
111        if (System.getProperty(JmxSystemPropertyKeys.REGISTRY_PORT) != null) {
112            registryPort = Integer.getInteger(JmxSystemPropertyKeys.REGISTRY_PORT);
113            values.put(JmxSystemPropertyKeys.REGISTRY_PORT, registryPort);
114        }
115        if (System.getProperty(JmxSystemPropertyKeys.CONNECTOR_PORT) != null) {
116            connectorPort = Integer.getInteger(JmxSystemPropertyKeys.CONNECTOR_PORT);
117            values.put(JmxSystemPropertyKeys.CONNECTOR_PORT, connectorPort);
118        }
119        if (System.getProperty(JmxSystemPropertyKeys.DOMAIN) != null) {
120            mBeanServerDefaultDomain = System.getProperty(JmxSystemPropertyKeys.DOMAIN);
121            values.put(JmxSystemPropertyKeys.DOMAIN, mBeanServerDefaultDomain);
122        }
123        if (System.getProperty(JmxSystemPropertyKeys.MBEAN_DOMAIN) != null) {
124            mBeanObjectDomainName = System.getProperty(JmxSystemPropertyKeys.MBEAN_DOMAIN);
125            values.put(JmxSystemPropertyKeys.MBEAN_DOMAIN, mBeanObjectDomainName);
126        }
127        if (System.getProperty(JmxSystemPropertyKeys.SERVICE_URL_PATH) != null) {
128            serviceUrlPath = System.getProperty(JmxSystemPropertyKeys.SERVICE_URL_PATH);
129            values.put(JmxSystemPropertyKeys.SERVICE_URL_PATH, serviceUrlPath);
130        }
131        if (System.getProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR) != null) {
132            createConnector = Boolean.getBoolean(JmxSystemPropertyKeys.CREATE_CONNECTOR);
133            values.put(JmxSystemPropertyKeys.CREATE_CONNECTOR, createConnector);
134        }
135        if (System.getProperty(JmxSystemPropertyKeys.ONLY_REGISTER_PROCESSOR_WITH_CUSTOM_ID) != null) {
136            onlyRegisterProcessorWithCustomId = Boolean.getBoolean(JmxSystemPropertyKeys.ONLY_REGISTER_PROCESSOR_WITH_CUSTOM_ID);
137            values.put(JmxSystemPropertyKeys.ONLY_REGISTER_PROCESSOR_WITH_CUSTOM_ID, onlyRegisterProcessorWithCustomId);
138        }
139        if (System.getProperty(JmxSystemPropertyKeys.USE_PLATFORM_MBS) != null) {
140            usePlatformMBeanServer = Boolean.getBoolean(JmxSystemPropertyKeys.USE_PLATFORM_MBS);
141            values.put(JmxSystemPropertyKeys.USE_PLATFORM_MBS, usePlatformMBeanServer);
142        }
143        if (System.getProperty(JmxSystemPropertyKeys.REGISTER_ALWAYS) != null) {
144            registerAlways = Boolean.getBoolean(JmxSystemPropertyKeys.REGISTER_ALWAYS);
145            values.put(JmxSystemPropertyKeys.REGISTER_ALWAYS, registerAlways);
146        }
147        if (System.getProperty(JmxSystemPropertyKeys.REGISTER_NEW_ROUTES) != null) {
148            registerNewRoutes = Boolean.getBoolean(JmxSystemPropertyKeys.REGISTER_NEW_ROUTES);
149            values.put(JmxSystemPropertyKeys.REGISTER_NEW_ROUTES, registerNewRoutes);
150        }
151        if (System.getProperty(JmxSystemPropertyKeys.MASK) != null) {
152            mask = Boolean.getBoolean(JmxSystemPropertyKeys.MASK);
153            values.put(JmxSystemPropertyKeys.MASK, mask);
154        }
155        if (System.getProperty(JmxSystemPropertyKeys.INCLUDE_HOST_NAME) != null) {
156            includeHostName = Boolean.getBoolean(JmxSystemPropertyKeys.INCLUDE_HOST_NAME);
157            values.put(JmxSystemPropertyKeys.INCLUDE_HOST_NAME, includeHostName);
158        }
159        if (System.getProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR) != null) {
160            createConnector = Boolean.getBoolean(JmxSystemPropertyKeys.CREATE_CONNECTOR);
161            values.put(JmxSystemPropertyKeys.CREATE_CONNECTOR, createConnector);
162        }
163        if (System.getProperty(JmxSystemPropertyKeys.LOAD_STATISTICS_ENABLED) != null) {
164            loadStatisticsEnabled = Boolean.getBoolean(JmxSystemPropertyKeys.LOAD_STATISTICS_ENABLED);
165            values.put(JmxSystemPropertyKeys.LOAD_STATISTICS_ENABLED, loadStatisticsEnabled);
166        }
167        if (System.getProperty(JmxSystemPropertyKeys.ENDPOINT_RUNTIME_STATISTICS_ENABLED) != null) {
168            endpointRuntimeStatisticsEnabled = Boolean.getBoolean(JmxSystemPropertyKeys.ENDPOINT_RUNTIME_STATISTICS_ENABLED);
169            values.put(JmxSystemPropertyKeys.ENDPOINT_RUNTIME_STATISTICS_ENABLED, endpointRuntimeStatisticsEnabled);
170        }
171        if (System.getProperty(JmxSystemPropertyKeys.STATISTICS_LEVEL) != null) {
172            statisticsLevel = camelContext.getTypeConverter().mandatoryConvertTo(ManagementStatisticsLevel.class, System.getProperty(JmxSystemPropertyKeys.STATISTICS_LEVEL));
173            values.put(JmxSystemPropertyKeys.STATISTICS_LEVEL, statisticsLevel);
174        }
175        if (System.getProperty(JmxSystemPropertyKeys.MANAGEMENT_NAME_PATTERN) != null) {
176            managementNamePattern = System.getProperty(JmxSystemPropertyKeys.MANAGEMENT_NAME_PATTERN);
177            values.put(JmxSystemPropertyKeys.MANAGEMENT_NAME_PATTERN, managementNamePattern);
178        }
179        if (System.getProperty(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS) != null) {
180            useHostIPAddress = Boolean.getBoolean(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS);
181            values.put(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS, useHostIPAddress);
182        }
183
184        if (!values.isEmpty()) {
185            LOG.info("ManagementAgent detected JVM system properties: {}", values);
186        }
187    }
188
189    public void setRegistryPort(Integer port) {
190        registryPort = port;
191    }
192
193    public Integer getRegistryPort() {
194        return registryPort;
195    }
196
197    public void setConnectorPort(Integer port) {
198        connectorPort = port;
199    }
200
201    public Integer getConnectorPort() {
202        return connectorPort;
203    }
204
205    public void setMBeanServerDefaultDomain(String domain) {
206        mBeanServerDefaultDomain = domain;
207    }
208
209    public String getMBeanServerDefaultDomain() {
210        return mBeanServerDefaultDomain;
211    }
212
213    public void setMBeanObjectDomainName(String domainName) {
214        mBeanObjectDomainName = domainName;
215    }
216
217    public String getMBeanObjectDomainName() {
218        return mBeanObjectDomainName;
219    }
220
221    public void setServiceUrlPath(String url) {
222        serviceUrlPath = url;
223    }
224
225    public String getServiceUrlPath() {
226        return serviceUrlPath;
227    }
228
229    public void setCreateConnector(Boolean flag) {
230        createConnector = flag;
231    }
232
233    public Boolean getCreateConnector() {
234        return createConnector;
235    }
236
237    public void setUsePlatformMBeanServer(Boolean flag) {
238        usePlatformMBeanServer = flag;
239    }
240
241    public Boolean getUsePlatformMBeanServer() {
242        return usePlatformMBeanServer;
243    }
244
245    public Boolean getOnlyRegisterProcessorWithCustomId() {
246        return onlyRegisterProcessorWithCustomId;
247    }
248
249    public void setOnlyRegisterProcessorWithCustomId(Boolean onlyRegisterProcessorWithCustomId) {
250        this.onlyRegisterProcessorWithCustomId = onlyRegisterProcessorWithCustomId;
251    }
252
253    public void setMBeanServer(MBeanServer mbeanServer) {
254        server = mbeanServer;
255    }
256
257    public MBeanServer getMBeanServer() {
258        return server;
259    }
260
261    public Boolean getRegisterAlways() {
262        return registerAlways != null && registerAlways;
263    }
264
265    public void setRegisterAlways(Boolean registerAlways) {
266        this.registerAlways = registerAlways;
267    }
268
269    public Boolean getRegisterNewRoutes() {
270        return registerNewRoutes != null && registerNewRoutes;
271    }
272
273    public void setRegisterNewRoutes(Boolean registerNewRoutes) {
274        this.registerNewRoutes = registerNewRoutes;
275    }
276
277    public Boolean getMask() {
278        return mask != null && mask;
279    }
280
281    public void setMask(Boolean mask) {
282        this.mask = mask;
283    }
284
285    public Boolean getIncludeHostName() {
286        return includeHostName != null && includeHostName;
287    }
288
289    public void setIncludeHostName(Boolean includeHostName) {
290        this.includeHostName = includeHostName;
291    }
292
293    public Boolean getUseHostIPAddress() {
294        return useHostIPAddress != null && useHostIPAddress;
295    }
296
297    public void setUseHostIPAddress(Boolean useHostIPAddress) {
298        this.useHostIPAddress = useHostIPAddress;
299    }
300
301    public String getManagementNamePattern() {
302        return managementNamePattern;
303    }
304
305    public void setManagementNamePattern(String managementNamePattern) {
306        this.managementNamePattern = managementNamePattern;
307    }
308
309    public Boolean getLoadStatisticsEnabled() {
310        return loadStatisticsEnabled;
311    }
312
313    public void setLoadStatisticsEnabled(Boolean loadStatisticsEnabled) {
314        this.loadStatisticsEnabled = loadStatisticsEnabled;
315    }
316
317    public Boolean getEndpointRuntimeStatisticsEnabled() {
318        return endpointRuntimeStatisticsEnabled;
319    }
320
321    public void setEndpointRuntimeStatisticsEnabled(Boolean endpointRuntimeStatisticsEnabled) {
322        this.endpointRuntimeStatisticsEnabled = endpointRuntimeStatisticsEnabled;
323    }
324
325    public ManagementStatisticsLevel getStatisticsLevel() {
326        return statisticsLevel;
327    }
328
329    public void setStatisticsLevel(ManagementStatisticsLevel statisticsLevel) {
330        this.statisticsLevel = statisticsLevel;
331    }
332
333    public CamelContext getCamelContext() {
334        return camelContext;
335    }
336
337    public void setCamelContext(CamelContext camelContext) {
338        this.camelContext = camelContext;
339    }
340
341    public void register(Object obj, ObjectName name) throws JMException {
342        register(obj, name, false);
343    }
344
345    public void register(Object obj, ObjectName name, boolean forceRegistration) throws JMException {
346        try {
347            registerMBeanWithServer(obj, name, forceRegistration);
348        } catch (NotCompliantMBeanException e) {
349            // If this is not a "normal" MBean, then try to deploy it using JMX annotations
350            ObjectHelper.notNull(assembler, "ManagementMBeanAssembler", camelContext);
351            Object mbean = assembler.assemble(server, obj, name);
352            if (mbean != null) {
353                // and register the mbean
354                registerMBeanWithServer(mbean, name, forceRegistration);
355            }
356        }
357    }
358
359    public void unregister(ObjectName name) throws JMException {
360        if (isRegistered(name)) {
361            ObjectName on = mbeansRegistered.remove(name);
362            server.unregisterMBean(on);
363            LOG.debug("Unregistered MBean with ObjectName: {}", name);
364        } else {
365            mbeansRegistered.remove(name);
366        }
367    }
368
369    public boolean isRegistered(ObjectName name) {
370        if (server == null) {
371            return false;
372        }
373        ObjectName on = mbeansRegistered.get(name);
374        return (on != null && server.isRegistered(on))
375                || server.isRegistered(name);
376    }
377
378    public <T> T newProxyClient(ObjectName name, Class<T> mbean) {
379        if (isRegistered(name)) {
380            ObjectName on = mbeansRegistered.get(name);
381            return MBeanServerInvocationHandler.newProxyInstance(server, on != null ? on : name, mbean, false);
382        } else {
383            return null;
384        }
385    }
386
387    protected void doStart() throws Exception {
388        ObjectHelper.notNull(camelContext, "CamelContext");
389
390        // create mbean server if is has not be injected.
391        if (server == null) {
392            finalizeSettings();
393            createMBeanServer();
394        }
395
396        // ensure assembler is started
397        assembler = camelContext.adapt(ExtendedCamelContext.class).getManagementMBeanAssembler();
398        if (assembler == null) {
399            assembler = new DefaultManagementMBeanAssembler(camelContext);
400        }
401        ServiceHelper.startService(assembler);
402
403        LOG.debug("Starting JMX agent on server: {}", getMBeanServer());
404    }
405
406    protected void doStop() throws Exception {
407        // close JMX Connector, if it was created
408        if (cs != null) {
409            try {
410                cs.stop();
411                LOG.debug("Stopped JMX Connector");
412            } catch (IOException e) {
413                LOG.debug("Error occurred during stopping JMXConnectorService: "
414                        + cs + ". This exception will be ignored.");
415            }
416            cs = null;
417        }
418
419        // Unexport JMX RMI registry, if it was created
420        if (registry != null) {
421            try {
422                UnicastRemoteObject.unexportObject(registry, true);
423                LOG.debug("Unexported JMX RMI Registry");
424            } catch (NoSuchObjectException e) {
425                LOG.debug("Error occurred while unexporting JMX RMI registry. This exception will be ignored.");
426            }
427        }
428
429        if (mbeansRegistered.isEmpty()) {
430            return;
431        }
432
433        // Using the array to hold the busMBeans to avoid the CurrentModificationException
434        ObjectName[] mBeans = mbeansRegistered.keySet().toArray(new ObjectName[mbeansRegistered.size()]);
435        int caught = 0;
436        for (ObjectName name : mBeans) {
437            try {
438                unregister(name);
439            } catch (Exception e) {
440                LOG.info("Exception unregistering MBean with name {}", name, e);
441                caught++;
442            }
443        }
444        if (caught > 0) {
445            LOG.warn("A number of " + caught
446                     + " exceptions caught while unregistering MBeans during stop operation."
447                     + " See INFO log for details.");
448        }
449
450        ServiceHelper.stopService(assembler);
451    }
452
453    private void registerMBeanWithServer(Object obj, ObjectName name, boolean forceRegistration)
454        throws JMException {
455
456        // have we already registered the bean, there can be shared instances in the camel routes
457        boolean exists = isRegistered(name);
458        if (exists) {
459            if (forceRegistration) {
460                LOG.info("ForceRegistration enabled, unregistering existing MBean with ObjectName: {}", name);
461                server.unregisterMBean(name);
462            } else {
463                // okay ignore we do not want to force it and it could be a shared instance
464                LOG.debug("MBean already registered with ObjectName: {}", name);
465            }
466        }
467
468        // register bean if by force or not exists
469        ObjectInstance instance = null;
470        if (forceRegistration || !exists) {
471            LOG.trace("Registering MBean with ObjectName: {}", name);
472            instance = server.registerMBean(obj, name);
473        }
474
475        // need to use the name returned from the server as some JEE servers may modify the name
476        if (instance != null) {
477            ObjectName registeredName = instance.getObjectName();
478            LOG.debug("Registered MBean with ObjectName: {}", registeredName);
479            mbeansRegistered.put(name, registeredName);
480        }
481    }
482
483    protected void createMBeanServer() {
484        String hostName;
485        boolean canAccessSystemProps = true;
486        try {
487            // we'll do it this way mostly to determine if we should lookup the hostName
488            SecurityManager sm = System.getSecurityManager();
489            if (sm != null) {
490                sm.checkPropertiesAccess();
491            }
492        } catch (SecurityException se) {
493            canAccessSystemProps = false;
494        }
495
496        if (canAccessSystemProps) {
497            try {
498                if (useHostIPAddress) {
499                    hostName = InetAddress.getLocalHost().getHostAddress();
500                } else {
501                    hostName = InetAddressUtil.getLocalHostName();
502                }
503            } catch (UnknownHostException uhe) {
504                LOG.info("Cannot determine localhost name or address. Using default: {}", DEFAULT_REGISTRY_PORT, uhe);
505                hostName = DEFAULT_HOST;
506            }
507        } else {
508            hostName = DEFAULT_HOST;
509        }
510
511        server = findOrCreateMBeanServer();
512
513        try {
514            // Create the connector if we need
515            if (createConnector) {
516                createJmxConnector(hostName);
517            }
518        } catch (IOException ioe) {
519            LOG.warn("Could not create and start JMX connector.", ioe);
520        }
521    }
522    
523    protected MBeanServer findOrCreateMBeanServer() {
524
525        // return platform mbean server if the option is specified.
526        if (usePlatformMBeanServer) {
527            return ManagementFactory.getPlatformMBeanServer();
528        }
529
530        // look for the first mbean server that has match default domain name
531        List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
532
533        for (MBeanServer server : servers) {
534            LOG.debug("Found MBeanServer with default domain {}", server.getDefaultDomain());
535
536            if (mBeanServerDefaultDomain.equals(server.getDefaultDomain())) {
537                return server;
538            }
539        }
540
541        // create a mbean server with the given default domain name
542        return MBeanServerFactory.createMBeanServer(mBeanServerDefaultDomain);
543    }
544
545    protected void createJmxConnector(String host) throws IOException {
546        StringHelper.notEmpty(serviceUrlPath, "serviceUrlPath");
547        ObjectHelper.notNull(registryPort, "registryPort");
548
549        try {
550            registry = LocateRegistry.createRegistry(registryPort);
551            LOG.debug("Created JMXConnector RMI registry on port {}", registryPort);
552        } catch (RemoteException ex) {
553            // The registry may had been created, we could get the registry instead
554        }
555
556        // must start with leading slash
557        String path = serviceUrlPath.startsWith("/") ? serviceUrlPath : "/" + serviceUrlPath;
558        // Create an RMI connector and start it
559        final JMXServiceURL url;
560        if (connectorPort > 0) {
561            url = new JMXServiceURL("service:jmx:rmi://" + host + ":" + connectorPort + "/jndi/rmi://" + host
562                                    + ":" + registryPort + path);
563        } else {
564            url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + registryPort + path);
565        }
566
567        cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
568
569        // use async thread for starting the JMX Connector
570        // (no need to use a thread pool or enlist in JMX as this thread is terminated when the JMX connector has been started)
571        String threadName = camelContext.getExecutorServiceManager().resolveThreadName("JMXConnector: " + url);
572        Thread thread = getCamelContext().getExecutorServiceManager().newThread(threadName, () -> {
573            try {
574                LOG.debug("Staring JMX Connector thread to listen at: {}", url);
575                cs.start();
576                LOG.info("JMX Connector thread started and listening at: {}", url);
577            } catch (IOException ioe) {
578                LOG.warn("Could not start JMXConnector thread at: " + url + ". JMX Connector not in use.", ioe);
579            }
580        });
581        thread.start();
582    }
583
584}