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.mbean;
018    
019    import org.apache.camel.Component;
020    import org.apache.camel.ServiceStatus;
021    import org.apache.camel.impl.ServiceSupport;
022    import org.apache.camel.spi.ManagementStrategy;
023    import org.springframework.jmx.export.annotation.ManagedAttribute;
024    import org.springframework.jmx.export.annotation.ManagedResource;
025    
026    /**
027     * @version $Revision: 819963 $
028     */
029    @ManagedResource(description = "Managed Component")
030    public class ManagedComponent {
031    
032        private Component component;
033        private String name;
034    
035        public ManagedComponent(String name, Component component) {
036            this.name = name;
037            this.component = component;
038        }
039    
040        public void init(ManagementStrategy strategy) {
041            // do nothing
042        }
043    
044        public Component getComponent() {
045            return component;
046        }
047    
048        @ManagedAttribute(description = "Component Name")
049        public String getComponentName() {
050            return name;
051        }
052    
053        @ManagedAttribute(description = "Component State")
054        public String getState() {
055            // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
056            if (component instanceof ServiceSupport) {
057                ServiceStatus status = ((ServiceSupport) component).getStatus();
058                // if no status exists then its stopped
059                if (status == null) {
060                    status = ServiceStatus.Stopped;
061                }
062                return status.name();
063            }
064    
065            // assume started if not a ServiceSupport instance
066            return ServiceStatus.Started.name();
067        }
068    
069        @ManagedAttribute(description = "Camel id")
070        public String getCamelId() {
071            return component.getCamelContext().getName();
072        }
073    
074    }