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 @Override 048 public boolean isStaticService() { 049 return service instanceof StaticService; 050 } 051 052 public Service getService() { 053 return service; 054 } 055 056 public CamelContext getContext() { 057 return context; 058 } 059 060 public Route getRoute() { 061 return route; 062 } 063 064 public void setRoute(Route route) { 065 this.route = route; 066 } 067 068 @Override 069 public String getState() { 070 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. 071 if (service instanceof StatefulService) { 072 ServiceStatus status = ((StatefulService) service).getStatus(); 073 return status.name(); 074 } 075 076 // assume started if not a ServiceSupport instance 077 return ServiceStatus.Started.name(); 078 } 079 080 @Override 081 public String getCamelId() { 082 return context.getName(); 083 } 084 085 @Override 086 public String getCamelManagementName() { 087 return context.getManagementName(); 088 } 089 090 @Override 091 public String getRouteId() { 092 if (route != null) { 093 return route.getId(); 094 } 095 return null; 096 } 097 098 @Override 099 public String getServiceType() { 100 if (service != null) { 101 return service.getClass().getSimpleName(); 102 } 103 return null; 104 } 105 106 @Override 107 public void start() throws Exception { 108 if (!context.getStatus().isStarted()) { 109 throw new IllegalArgumentException("CamelContext is not started"); 110 } 111 service.start(); 112 } 113 114 @Override 115 public void stop() throws Exception { 116 if (!context.getStatus().isStarted()) { 117 throw new IllegalArgumentException("CamelContext is not started"); 118 } 119 service.stop(); 120 } 121 122 @Override 123 public boolean isSupportSuspension() { 124 return service instanceof Suspendable && service instanceof SuspendableService; 125 } 126 127 @Override 128 public boolean isSuspended() { 129 if (service instanceof SuspendableService) { 130 SuspendableService ss = (SuspendableService) service; 131 return ss.isSuspended(); 132 } else { 133 return false; 134 } 135 } 136 137 @Override 138 public void suspend() throws Exception { 139 if (!context.getStatus().isStarted()) { 140 throw new IllegalArgumentException("CamelContext is not started"); 141 } 142 if (service instanceof Suspendable && service instanceof SuspendableService) { 143 SuspendableService ss = (SuspendableService) service; 144 ss.suspend(); 145 } else { 146 throw new UnsupportedOperationException("suspend() is not a supported operation"); 147 } 148 } 149 150 @Override 151 public void resume() throws Exception { 152 if (!context.getStatus().isStarted()) { 153 throw new IllegalArgumentException("CamelContext is not started"); 154 } 155 if (service instanceof SuspendableService) { 156 SuspendableService ss = (SuspendableService) service; 157 ss.resume(); 158 } else { 159 throw new UnsupportedOperationException("resume() is not a supported operation"); 160 } 161 } 162 163 @Override 164 public Object getInstance() { 165 return service; 166 } 167}