1
17 package org.apache.catalina.mbeans;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.management.MBeanException;
23 import javax.management.MalformedObjectNameException;
24 import javax.management.ObjectName;
25
26 import org.apache.catalina.Container;
27 import org.apache.catalina.ContainerListener;
28 import org.apache.catalina.JmxEnabled;
29 import org.apache.catalina.LifecycleException;
30 import org.apache.catalina.LifecycleListener;
31 import org.apache.catalina.Valve;
32 import org.apache.catalina.core.ContainerBase;
33 import org.apache.catalina.core.StandardContext;
34 import org.apache.catalina.core.StandardHost;
35 import org.apache.catalina.startup.ContextConfig;
36 import org.apache.catalina.startup.HostConfig;
37
38 public class ContainerMBean extends BaseCatalinaMBean<ContainerBase> {
39
40
50 public void addChild(String type, String name) throws MBeanException{
51
52 Container contained = (Container) newInstance(type);
53 contained.setName(name);
54
55 if(contained instanceof StandardHost){
56 HostConfig config = new HostConfig();
57 contained.addLifecycleListener(config);
58 } else if(contained instanceof StandardContext){
59 ContextConfig config = new ContextConfig();
60 contained.addLifecycleListener(config);
61 }
62
63 boolean oldValue= true;
64
65 ContainerBase container = doGetManagedResource();
66 try {
67 oldValue = container.getStartChildren();
68 container.setStartChildren(false);
69 container.addChild(contained);
70 contained.init();
71 } catch (LifecycleException e){
72 throw new MBeanException(e);
73 } finally {
74 if(container != null) {
75 container.setStartChildren(oldValue);
76 }
77 }
78 }
79
80
81
88 public void removeChild(String name) throws MBeanException{
89 if (name != null) {
90 Container container = doGetManagedResource();
91 Container contained = container.findChild(name);
92 container.removeChild(contained);
93 }
94 }
95
96
97
104 public String addValve(String valveType) throws MBeanException{
105 Valve valve = (Valve) newInstance(valveType);
106
107 Container container = doGetManagedResource();
108 container.getPipeline().addValve(valve);
109
110 if (valve instanceof JmxEnabled) {
111 return ((JmxEnabled)valve).getObjectName().toString();
112 } else {
113 return null;
114 }
115 }
116
117
118
125 public void removeValve(String valveName) throws MBeanException{
126 Container container = doGetManagedResource();
127
128 ObjectName oname;
129 try {
130 oname = new ObjectName(valveName);
131 } catch (MalformedObjectNameException e) {
132 throw new MBeanException(e);
133 } catch (NullPointerException e) {
134 throw new MBeanException(e);
135 }
136
137 if (container != null) {
138 Valve[] valves = container.getPipeline().getValves();
139 for (int i = 0; i < valves.length; i++) {
140 if (valves[i] instanceof JmxEnabled) {
141 ObjectName voname = ((JmxEnabled) valves[i]).getObjectName();
142 if (voname.equals(oname)) {
143 container.getPipeline().removeValve(valves[i]);
144 }
145 }
146 }
147 }
148 }
149
150
151
157 public void addLifecycleListener(String type) throws MBeanException{
158 LifecycleListener listener = (LifecycleListener) newInstance(type);
159 Container container = doGetManagedResource();
160 container.addLifecycleListener(listener);
161 }
162
163
164
171 public void removeLifecycleListeners(String type) throws MBeanException{
172 Container container = doGetManagedResource();
173
174 LifecycleListener[] listeners = container.findLifecycleListeners();
175 for (LifecycleListener listener : listeners){
176 if (listener.getClass().getName().equals(type)) {
177 container.removeLifecycleListener(listener);
178 }
179 }
180 }
181
182
183
189 public String[] findLifecycleListenerNames() throws MBeanException {
190 Container container = doGetManagedResource();
191 List<String> result = new ArrayList<>();
192
193 LifecycleListener[] listeners = container.findLifecycleListeners();
194 for(LifecycleListener listener: listeners){
195 result.add(listener.getClass().getName());
196 }
197
198 return result.toArray(new String[result.size()]);
199 }
200
201
202
208 public String[] findContainerListenerNames() throws MBeanException {
209 Container container = doGetManagedResource();
210 List<String> result = new ArrayList<>();
211
212 ContainerListener[] listeners = container.findContainerListeners();
213 for(ContainerListener listener: listeners){
214 result.add(listener.getClass().getName());
215 }
216
217 return result.toArray(new String[result.size()]);
218 }
219 }
220