1
17 package org.apache.catalina.mbeans;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.management.MalformedObjectNameException;
23 import javax.management.ObjectName;
24
25 import org.apache.catalina.deploy.NamingResourcesImpl;
26 import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
27 import org.apache.tomcat.util.descriptor.web.ContextResource;
28 import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
29 import org.apache.tomcat.util.modeler.BaseModelMBean;
30 import org.apache.tomcat.util.modeler.ManagedBean;
31 import org.apache.tomcat.util.modeler.Registry;
32 import org.apache.tomcat.util.res.StringManager;
33
34
40 public class NamingResourcesMBean extends BaseModelMBean {
41
42 private static final StringManager sm = StringManager.getManager(NamingResourcesMBean.class);
43
44
45
46
49 protected final Registry registry = MBeanUtils.createRegistry();
50
51
52
55 protected final ManagedBean managed = registry.findManagedBean("NamingResources");
56
57
58
59
60
65 public String[] getEnvironments() {
66 ContextEnvironment[] envs = ((NamingResourcesImpl)this.resource).findEnvironments();
67 List<String> results = new ArrayList<>();
68 for (int i = 0; i < envs.length; i++) {
69 try {
70 ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), envs[i]);
71 results.add(oname.toString());
72 } catch (MalformedObjectNameException e) {
73 IllegalArgumentException iae = new IllegalArgumentException(
74 sm.getString("namingResourcesMBean.createObjectNameError.environment", envs[i]));
75 iae.initCause(e);
76 throw iae;
77 }
78 }
79 return results.toArray(new String[results.size()]);
80 }
81
82
83
88 public String[] getResources() {
89 ContextResource[] resources = ((NamingResourcesImpl)this.resource).findResources();
90 List<String> results = new ArrayList<>();
91 for (int i = 0; i < resources.length; i++) {
92 try {
93 ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resources[i]);
94 results.add(oname.toString());
95 } catch (MalformedObjectNameException e) {
96 IllegalArgumentException iae = new IllegalArgumentException(
97 sm.getString("namingResourcesMBean.createObjectNameError.resource", resources[i]));
98 iae.initCause(e);
99 throw iae;
100 }
101 }
102 return results.toArray(new String[results.size()]);
103 }
104
105
106
111 public String[] getResourceLinks() {
112 ContextResourceLink[] resourceLinks =
113 ((NamingResourcesImpl)this.resource).findResourceLinks();
114 List<String> results = new ArrayList<>();
115 for (int i = 0; i < resourceLinks.length; i++) {
116 try {
117 ObjectName oname =
118 MBeanUtils.createObjectName(managed.getDomain(), resourceLinks[i]);
119 results.add(oname.toString());
120 } catch (MalformedObjectNameException e) {
121 IllegalArgumentException iae = new IllegalArgumentException(
122 sm.getString("namingResourcesMBean.createObjectNameError.resourceLink", resourceLinks[i]));
123 iae.initCause(e);
124 throw iae;
125 }
126 }
127 return results.toArray(new String[results.size()]);
128 }
129
130
131
132
133
142 public String addEnvironment(String envName, String type, String value)
143 throws MalformedObjectNameException {
144
145 NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
146 if (nresources == null) {
147 return null;
148 }
149 ContextEnvironment env = nresources.findEnvironment(envName);
150 if (env != null) {
151 throw new IllegalArgumentException(sm.getString("namingResourcesMBean.addAlreadyExists.environment", envName));
152 }
153 env = new ContextEnvironment();
154 env.setName(envName);
155 env.setType(type);
156 env.setValue(value);
157 nresources.addEnvironment(env);
158
159
160 ManagedBean managed = registry.findManagedBean("ContextEnvironment");
161 ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), env);
162 return oname.toString();
163 }
164
165
166
174 public String addResource(String resourceName, String type)
175 throws MalformedObjectNameException {
176
177 NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
178 if (nresources == null) {
179 return null;
180 }
181 ContextResource resource = nresources.findResource(resourceName);
182 if (resource != null) {
183 throw new IllegalArgumentException(sm.getString("namingResourcesMBean.addAlreadyExists.resource", resourceName));
184 }
185 resource = new ContextResource();
186 resource.setName(resourceName);
187 resource.setType(type);
188 nresources.addResource(resource);
189
190
191 ManagedBean managed = registry.findManagedBean("ContextResource");
192 ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);
193 return oname.toString();
194 }
195
196
197
205 public String addResourceLink(String resourceLinkName, String type)
206 throws MalformedObjectNameException {
207
208 NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
209 if (nresources == null) {
210 return null;
211 }
212 ContextResourceLink resourceLink =
213 nresources.findResourceLink(resourceLinkName);
214 if (resourceLink != null) {
215 throw new IllegalArgumentException(sm.getString("namingResourcesMBean.addAlreadyExists.resourceLink", resourceLinkName));
216 }
217 resourceLink = new ContextResourceLink();
218 resourceLink.setName(resourceLinkName);
219 resourceLink.setType(type);
220 nresources.addResourceLink(resourceLink);
221
222
223 ManagedBean managed = registry.findManagedBean("ContextResourceLink");
224 ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resourceLink);
225 return oname.toString();
226 }
227
228
229
234 public void removeEnvironment(String envName) {
235 NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
236 if (nresources == null) {
237 return;
238 }
239 ContextEnvironment env = nresources.findEnvironment(envName);
240 if (env == null) {
241 throw new IllegalArgumentException(sm.getString("namingResourcesMBean.removeNotFound.environment", envName));
242 }
243 nresources.removeEnvironment(envName);
244 }
245
246
247
252 public void removeResource(String resourceName) {
253 resourceName = ObjectName.unquote(resourceName);
254 NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
255 if (nresources == null) {
256 return;
257 }
258 ContextResource resource = nresources.findResource(resourceName);
259 if (resource == null) {
260 throw new IllegalArgumentException(sm.getString("namingResourcesMBean.removeNotFound.resource", resourceName));
261 }
262 nresources.removeResource(resourceName);
263 }
264
265
266
271 public void removeResourceLink(String resourceLinkName) {
272 resourceLinkName = ObjectName.unquote(resourceLinkName);
273 NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
274 if (nresources == null) {
275 return;
276 }
277 ContextResourceLink resourceLink = nresources.findResourceLink(resourceLinkName);
278 if (resourceLink == null) {
279 throw new IllegalArgumentException(sm.getString("namingResourcesMBean.removeNotFound.resourceLink", resourceLinkName));
280 }
281 nresources.removeResourceLink(resourceLinkName);
282 }
283 }
284