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 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 /**
35  * <p>A <strong>ModelMBean</strong> implementation for the
36  * <code>org.apache.catalina.deploy.NamingResourcesImpl</code> component.</p>
37  *
38  * @author Amy Roh
39  */

40 public class NamingResourcesMBean extends BaseModelMBean {
41
42     private static final StringManager sm = StringManager.getManager(NamingResourcesMBean.class);
43
44     // ----------------------------------------------------- Instance Variables
45
46     /**
47      * The configuration information registry for our managed beans.
48      */

49     protected final Registry registry = MBeanUtils.createRegistry();
50
51
52     /**
53      * The <code>ManagedBean</code> information describing this MBean.
54      */

55     protected final ManagedBean managed = registry.findManagedBean("NamingResources");
56
57
58     // ------------------------------------------------------------- Attributes
59
60     /**
61      * Return the MBean Names of the set of defined environment entries for
62      * this web application
63      * @return an array of object names as strings
64      */

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     /**
84      * Return the MBean Names of all the defined resource references for this
85      * application.
86      * @return an array of object names as strings
87      */

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     /**
107      * Return the MBean Names of all the defined resource link references for
108      * this application.
109      * @return an array of object names as strings
110      */

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     // ------------------------------------------------------------- Operations
132
133     /**
134      * Add an environment entry for this web application.
135      *
136      * @param envName New environment entry name
137      * @param type The type of the new environment entry
138      * @param value The value of the new environment entry
139      * @return the object name of the new environment entry
140      * @throws MalformedObjectNameException if the object name was invalid
141      */

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         // Return the corresponding MBean name
160         ManagedBean managed = registry.findManagedBean("ContextEnvironment");
161         ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), env);
162         return oname.toString();
163     }
164
165
166     /**
167      * Add a resource reference for this web application.
168      *
169      * @param resourceName New resource reference name
170      * @param type New resource reference type
171      * @return the object name of the new resource
172      * @throws MalformedObjectNameException if the object name was invalid
173      */

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         // Return the corresponding MBean name
191         ManagedBean managed = registry.findManagedBean("ContextResource");
192         ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);
193         return oname.toString();
194     }
195
196
197     /**
198      * Add a resource link reference for this web application.
199      *
200      * @param resourceLinkName New resource link reference name
201      * @param type New resource link reference type
202      * @return the object name of the new resource link
203      * @throws MalformedObjectNameException if the object name was invalid
204      */

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         // Return the corresponding MBean name
223         ManagedBean managed = registry.findManagedBean("ContextResourceLink");
224         ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resourceLink);
225         return oname.toString();
226     }
227
228
229     /**
230      * Remove any environment entry with the specified name.
231      *
232      * @param envName Name of the environment entry to remove
233      */

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     /**
248      * Remove any resource reference with the specified name.
249      *
250      * @param resourceName Name of the resource reference to remove
251      */

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     /**
267      * Remove any resource link reference with the specified name.
268      *
269      * @param resourceLinkName Name of the resource link reference to remove
270      */

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