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.mbean;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Route;
021import org.apache.camel.Service;
022import org.apache.camel.ServiceStatus;
023import org.apache.camel.StatefulService;
024import org.apache.camel.StaticService;
025import org.apache.camel.Suspendable;
026import org.apache.camel.SuspendableService;
027import org.apache.camel.api.management.ManagedInstance;
028import org.apache.camel.api.management.ManagedResource;
029import org.apache.camel.api.management.mbean.ManagedServiceMBean;
030import org.apache.camel.spi.ManagementStrategy;
031
032@ManagedResource(description = "Managed Service")
033public class ManagedService implements ManagedInstance, ManagedServiceMBean {
034    private final CamelContext context;
035    private final Service service;
036    private Route route;
037
038    public ManagedService(CamelContext context, Service service) {
039        this.context = context;
040        this.service = service;
041    }
042
043    public void init(ManagementStrategy strategy) {
044        // do nothing
045    }
046
047    public boolean isStaticService() {
048        return service instanceof StaticService;
049    }
050
051    public Service getService() {
052        return service;
053    }
054
055    public CamelContext getContext() {
056        return context;
057    }
058
059    public Route getRoute() {
060        return route;
061    }
062
063    public void setRoute(Route route) {
064        this.route = route;
065    }
066
067    public String getState() {
068        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
069        if (service instanceof StatefulService) {
070            ServiceStatus status = ((StatefulService) service).getStatus();
071            return status.name();
072        }
073
074        // assume started if not a ServiceSupport instance
075        return ServiceStatus.Started.name();
076    }
077
078    public String getCamelId() {
079        return context.getName();
080    }
081
082    public String getCamelManagementName() {
083        return context.getManagementName();
084    }
085
086    public String getRouteId() {
087        if (route != null) {
088            return route.getId();
089        }
090        return null;
091    }
092
093    public String getServiceType() {
094        if (service != null) {
095            return service.getClass().getSimpleName();
096        }
097        return null;
098    }
099
100    public void start() throws Exception {
101        if (!context.getStatus().isStarted()) {
102            throw new IllegalArgumentException("CamelContext is not started");
103        }
104        service.start();
105    }
106
107    public void stop() throws Exception {
108        if (!context.getStatus().isStarted()) {
109            throw new IllegalArgumentException("CamelContext is not started");
110        }
111        service.stop();
112    }
113
114    public boolean isSupportSuspension() {
115        return service instanceof Suspendable && service instanceof SuspendableService;
116    }
117
118    public boolean isSuspended() {
119        if (service instanceof SuspendableService) {
120            SuspendableService ss = (SuspendableService) service;
121            return ss.isSuspended();
122        } else {
123            return false;
124        }
125    }
126
127    public void suspend() throws Exception {
128        if (!context.getStatus().isStarted()) {
129            throw new IllegalArgumentException("CamelContext is not started");
130        }
131        if (service instanceof Suspendable && service instanceof SuspendableService) {
132            SuspendableService ss = (SuspendableService) service;
133            ss.suspend();
134        } else {
135            throw new UnsupportedOperationException("suspend() is not a supported operation");
136        }
137    }
138
139    public void resume() throws Exception {
140        if (!context.getStatus().isStarted()) {
141            throw new IllegalArgumentException("CamelContext is not started");
142        }
143        if (service instanceof SuspendableService) {
144            SuspendableService ss = (SuspendableService) service;
145            ss.resume();
146        } else {
147            throw new UnsupportedOperationException("resume() is not a supported operation");
148        }
149    }
150
151    public Object getInstance() {
152        return service;
153    }
154}