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