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