1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.mbeans;
18
19 import javax.management.MBeanException;
20
21 import org.apache.catalina.Executor;
22 import org.apache.catalina.Service;
23 import org.apache.catalina.connector.Connector;
24
25 public class ServiceMBean extends BaseCatalinaMBean<Service> {
26
27     /**
28      * Add a new Connector to the set of defined Connectors, and associate it
29      * with this Service's Container.
30      *
31      * @param address The IP address on which to bind
32      * @param port TCP port number to listen on
33      * @param isAjp Create a AJP/1.3 Connector
34      * @param isSSL Create a secure Connector
35      *
36      * @throws MBeanException error creating the connector
37      */

38     public void addConnector(String address, int port, boolean isAjp, boolean isSSL) throws MBeanException {
39
40         Service service = doGetManagedResource();
41         String protocol = isAjp ? "AJP/1.3" : "HTTP/1.1";
42         Connector connector = new Connector(protocol);
43         if ((address!=null) && (address.length()>0)) {
44             connector.setProperty("address", address);
45         }
46         connector.setPort(port);
47         connector.setSecure(isSSL);
48         connector.setScheme(isSSL ? "https" : "http");
49
50         service.addConnector(connector);
51     }
52
53
54     /**
55      * Adds a named executor to the service
56      * @param type Classname of the Executor to be added
57      * @throws MBeanException error creating the executor
58      */

59     public void addExecutor(String type) throws MBeanException {
60         Service service = doGetManagedResource();
61         Executor executor = (Executor) newInstance(type);
62         service.addExecutor(executor);
63     }
64
65
66     /**
67      * Find and return the set of Connectors associated with this Service.
68      * @return an array of string representations of the connectors
69      * @throws MBeanException error accessing the associated service
70      */

71     public String[] findConnectors() throws MBeanException {
72
73         Service service = doGetManagedResource();
74
75         Connector[] connectors = service.findConnectors();
76         String[] str = new String[connectors.length];
77
78         for(int i = 0; i < connectors.length; i++) {
79             str[i] = connectors[i].toString();
80         }
81
82         return str;
83     }
84
85
86     /**
87      * Retrieves all executors.
88      * @return an array of string representations of the executors
89      * @throws MBeanException error accessing the associated service
90      */

91     public String[] findExecutors() throws MBeanException {
92
93         Service service = doGetManagedResource();
94
95         Executor[] executors = service.findExecutors();
96         String[] str = new String[executors.length];
97
98         for(int i = 0; i < executors.length; i++){
99             str[i] = executors[i].toString();
100         }
101
102         return str;
103     }
104
105
106     /**
107      * Retrieves executor by name
108      * @param name Name of the executor to be retrieved
109      * @return a string representation of the executor
110      * @throws MBeanException error accessing the associated service
111      */

112     public String getExecutor(String name) throws MBeanException{
113         Service service = doGetManagedResource();
114         Executor executor = service.getExecutor(name);
115         return executor.toString();
116     }
117 }
118