01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.rice.resourceloader.management;
17:
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import org.apache.commons.lang.StringUtils;
22: import org.kuali.rice.resourceloader.GlobalResourceLoader;
23: import org.kuali.rice.resourceloader.ResourceLoader;
24: import org.springframework.beans.BeansException;
25: import org.springframework.beans.factory.BeanFactory;
26: import org.springframework.beans.factory.BeanFactoryAware;
27: import org.springframework.beans.factory.DisposableBean;
28: import org.springframework.beans.factory.InitializingBean;
29: import org.springframework.jmx.export.MBeanExporter;
30:
31: public class ResourceLoaderMBeanExporter implements BeanFactoryAware,
32: InitializingBean, DisposableBean {
33:
34: private String objectNamePrefix;
35: private MBeanExporter exporter = new MBeanExporter();
36:
37: public void afterPropertiesSet() throws Exception {
38: if (StringUtils.isEmpty(this .objectNamePrefix)) {
39: throw new IllegalArgumentException(
40: "The objectNamePrefix must be set.");
41: }
42: this .exporter.setAutodetect(false);
43: ResourceLoader rootResourceLoader = GlobalResourceLoader
44: .getResourceLoader();
45: Map<String, Object> beans = new HashMap<String, Object>();
46:
47: String loaderNamePrefix = getObjectNamePrefix()
48: + ",type=ResourceLoaders,name=";
49: addResourceLoader(loaderNamePrefix, rootResourceLoader, beans);
50: this .exporter.setBeans(beans);
51: this .exporter.afterPropertiesSet();
52: }
53:
54: protected void addResourceLoader(String namePrefix,
55: ResourceLoader resourceLoader, Map<String, Object> beans) {
56: String beanName = namePrefix
57: + resourceLoader.getName().toString();
58: beans.put(fixBeanName(beanName, beanName, beans, 0),
59: new ResourceLoaderWrapper(resourceLoader));
60: for (ResourceLoader childLoader : resourceLoader
61: .getResourceLoaders()) {
62: addResourceLoader(namePrefix, childLoader, beans);
63: }
64: }
65:
66: public void setBeanFactory(BeanFactory beanFactory)
67: throws BeansException {
68: this .exporter.setBeanFactory(beanFactory);
69: }
70:
71: public void destroy() throws Exception {
72: this .exporter.destroy();
73: }
74:
75: public String getObjectNamePrefix() {
76: return this .objectNamePrefix;
77: }
78:
79: public void setObjectNamePrefix(String objectNamePrefix) {
80: this .objectNamePrefix = objectNamePrefix;
81: }
82:
83: protected String fixBeanName(String generatedBeanName,
84: String actualBeanName, Map beans, int counter) {
85: if (!(beans.containsKey(actualBeanName))) {
86: return actualBeanName;
87: }
88: counter++;
89: return fixBeanName(generatedBeanName, generatedBeanName + "("
90: + counter + ")", beans, counter);
91: }
92:
93: }
|