1 /*
2 * Copyright 2004-2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.apache.catalina.storeconfig;
17
18 import java.io.InputStream;
19
20 import javax.management.MBeanServer;
21 import javax.management.ObjectName;
22 import javax.management.modelmbean.ModelMBean;
23
24 import org.apache.catalina.Lifecycle;
25 import org.apache.catalina.LifecycleEvent;
26 import org.apache.catalina.LifecycleListener;
27 import org.apache.catalina.core.StandardServer;
28 import org.apache.catalina.mbeans.MBeanUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.modeler.ManagedBean;
32 import org.apache.commons.modeler.Registry;
33
34 /**
35 * Load and Register StoreConfig MBean <i>Catalina:type=StoreConfig,resource="url"</i>
36 *
37 * @author Peter Rossbach
38 */
39 public class StoreConfigLifecycleListener implements LifecycleListener {
40 private static Log log = LogFactory
41 .getLog(StoreConfigLifecycleListener.class);
42
43 IStoreConfig storeConfig;
44
45 private String storeConfigClass = "org.apache.catalina.storeconfig.StoreConfig";
46
47 private String storeRegistry = null;
48
49 /*
50 * register StoreRegistry after Start the complete Server
51 *
52 * @see org.apache.catalina.LifecycleListener#lifecycleEvent(org.apache.catalina.LifecycleEvent)
53 */
54 public void lifecycleEvent(LifecycleEvent event) {
55 if (Lifecycle.AFTER_START_EVENT.equals(event.getType())) {
56 if (event.getSource() instanceof StandardServer) {
57 createMBean();
58 }
59 }
60 }
61
62 /**
63 * create StoreConfig MBean and load StoreRgistry MBeans name is
64 * <i>Catalina:type=StoreConfig </i>
65 */
66 protected void createMBean() {
67 StoreLoader loader = new StoreLoader();
68 try {
69 Class clazz = Class.forName(getStoreConfigClass(), true, this
70 .getClass().getClassLoader());
71 storeConfig = (IStoreConfig) clazz.newInstance();
72 if (null == getStoreRegistry())
73 // default Loading
74 loader.load();
75 else
76 // load a spezial file registry (url)
77 loader.load(getStoreRegistry());
78 // use the loader Registry
79 storeConfig.setRegistry(loader.getRegistry());
80 } catch (Exception e) {
81 log.error("createMBean load", e);
82 return;
83 }
84 MBeanServer mserver = MBeanUtils.createServer();
85 InputStream descriptor = null;
86 try {
87 ObjectName objectName = new ObjectName("Catalina:type=StoreConfig" );
88 if (!mserver.isRegistered(objectName)) {
89 descriptor = this.getClass().getResourceAsStream(
90 "mbeans-descriptors.xml");
91 Registry registry = MBeanUtils.createRegistry();
92 registry.loadMetadata(descriptor);
93 mserver.registerMBean(getManagedBean(storeConfig), objectName);
94 }
95 } catch (Exception ex) {
96 log.error("createMBean register MBean", ex);
97
98 } finally {
99 if (descriptor != null) {
100 try {
101 descriptor.close();
102 descriptor = null;
103 } catch (Exception ex) {
104 log.error("createMBean register MBean", ex);
105 }
106 }
107 }
108 }
109
110 /**
111 * Create a ManagedBean (StoreConfig)
112 *
113 * @param object
114 * @return The bean
115 * @throws Exception
116 */
117 protected ModelMBean getManagedBean(Object object) throws Exception {
118 ManagedBean managedBean = Registry.getRegistry(null, null)
119 .findManagedBean(object.getClass().getName());
120 return managedBean.createMBean(object);
121 }
122
123 /**
124 * @return Returns the storeConfig.
125 */
126 public IStoreConfig getStoreConfig() {
127 return storeConfig;
128 }
129
130 /**
131 * @param storeConfig
132 * The storeConfig to set.
133 */
134 public void setStoreConfig(IStoreConfig storeConfig) {
135 this.storeConfig = storeConfig;
136 }
137
138 /**
139 * @return Returns the storeConfigClass.
140 */
141 public String getStoreConfigClass() {
142 return storeConfigClass;
143 }
144
145 /**
146 * @param storeConfigClass
147 * The storeConfigClass to set.
148 */
149 public void setStoreConfigClass(String storeConfigClass) {
150 this.storeConfigClass = storeConfigClass;
151 }
152
153 /**
154 * @return Returns the storeRegistry.
155 */
156 public String getStoreRegistry() {
157 return storeRegistry;
158 }
159
160 /**
161 * @param storeRegistry
162 * The storeRegistry to set.
163 */
164 public void setStoreRegistry(String storeRegistry) {
165 this.storeRegistry = storeRegistry;
166 }
167 }