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.servicemix.jbi.view;
018
019 import java.io.PrintWriter;
020 import java.io.StringWriter;
021
022 import javax.management.JMException;
023 import javax.management.MBeanOperationInfo;
024
025 import org.apache.servicemix.jbi.container.JBIContainer;
026 import org.apache.servicemix.jbi.management.BaseSystemService;
027 import org.apache.servicemix.jbi.management.OperationInfoHelper;
028 import org.springframework.beans.factory.InitializingBean;
029
030 /**
031 *
032 * @author gnodet
033 * @org.apache.xbean.XBean element="dotViewService"
034 */
035 public class DotViewService extends BaseSystemService implements InitializingBean, DotViewServiceMBean {
036
037 private JBIContainer container;
038 private boolean autoStart = true;
039 private DotViewEndpointListener endpointListener;
040 private DotViewFlowListener flowListener;
041
042 public boolean isAutoStart() {
043 return autoStart;
044 }
045
046 public void setAutoStart(boolean autoStart) {
047 this.autoStart = autoStart;
048 }
049
050 public JBIContainer getContainer() {
051 return container;
052 }
053
054 public void setContainer(JBIContainer container) {
055 this.container = container;
056 }
057
058 protected Class getServiceMBean() {
059 return DotViewServiceMBean.class;
060 }
061
062 public String getDescription() {
063 return "DotView service";
064 }
065
066 /* (non-Javadoc)
067 * @see javax.jbi.management.LifeCycleMBean#start()
068 */
069 public void start() throws javax.jbi.JBIException {
070 super.start();
071 this.container.addListener(endpointListener);
072 this.container.addListener(flowListener);
073 }
074
075 /* (non-Javadoc)
076 * @see javax.jbi.management.LifeCycleMBean#stop()
077 */
078 public void stop() throws javax.jbi.JBIException {
079 this.container.removeListener(endpointListener);
080 this.container.removeListener(flowListener);
081 super.stop();
082 }
083
084 public void afterPropertiesSet() throws Exception {
085 if (this.container == null) {
086 throw new IllegalArgumentException("container should not be null");
087 }
088 init(getContainer());
089 endpointListener = new DotViewEndpointListener();
090 endpointListener.setContainer(container);
091 endpointListener.setRerenderOnChange(false);
092 flowListener = new DotViewFlowListener();
093 flowListener.setContainer(container);
094 flowListener.setRerenderOnChange(false);
095 if (autoStart) {
096 start();
097 } else {
098 stop();
099 }
100 }
101
102 public String createEndpointGraph() throws Exception {
103 StringWriter sw = new StringWriter();
104 PrintWriter pw = new PrintWriter(sw);
105 endpointListener.generateFile(pw);
106 return sw.toString();
107 }
108
109 public String createFlowGraph() throws Exception {
110 StringWriter sw = new StringWriter();
111 PrintWriter pw = new PrintWriter(sw);
112 flowListener.generateFile(pw);
113 return sw.toString();
114 }
115
116 /**
117 * Get an array of MBeanOperationInfo
118 *
119 * @return array of OperationInfos
120 * @throws JMException
121 */
122 public MBeanOperationInfo[] getOperationInfos() throws JMException {
123 OperationInfoHelper helper = new OperationInfoHelper();
124 helper.addOperation(getObjectToManage(), "createEndpointGraph", 0, "create an endpoint dot graph");
125 helper.addOperation(getObjectToManage(), "createFlowGraph", 0, "create an flow dot graph");
126 return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos());
127 }
128
129 }