001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ConfigurationService.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.system;
030:
031: import com.sun.jbi.EnvironmentContext;
032: import com.sun.jbi.StringTranslator;
033: import com.sun.jbi.management.ConfigurationCategory;
034: import com.sun.jbi.management.LocalStringKeys;
035: import com.sun.jbi.management.MBeanNames;
036: import com.sun.jbi.management.config.RuntimeConfiguration;
037: import com.sun.jbi.management.config.ConfigurationBuilder;
038: import com.sun.jbi.management.config.ConfigurationFactory;
039: import com.sun.jbi.management.support.JbiNameInfo;
040:
041: import java.util.ArrayList;
042: import java.util.Map;
043: import java.util.Set;
044: import java.util.Iterator;
045: import java.util.Properties;
046: import java.util.logging.Logger;
047:
048: import javax.management.ObjectName;
049: import javax.management.MBeanServerConnection;
050: import javax.management.modelmbean.ModelMBeanInfo;
051: import javax.management.modelmbean.RequiredModelMBean;
052:
053: /**
054: * This is the JBI Framework Configuration Service, which registers the
055: * copnfiguration MBeans required to configure some of the sysytem services.
056: *
057: * @author Sun Microsystems, Inc.
058: */
059: public class ConfigurationService extends ModelSystemService implements
060: ConfigurationServiceMBean, com.sun.jbi.ServiceLifecycle {
061: /** our immutable name: */
062: private final JbiNameInfo mJbiNameInfo = new JbiNameInfo(
063: "ConfigurationService");
064:
065: /**
066: * Management context
067: */
068: private EnvironmentContext mEnv = null;
069:
070: /** The default configuration file name */
071: private static final String DEFAULT_CONFIG_FILE = "runtime-config.defaults";
072:
073: /** Registered Configuration MBean service types */
074: private java.util.Set<MBeanNames.ServiceType> mConfigSvcTypes;
075:
076: /** local model init - called by constructor - create custom mbeans. */
077: protected void initModelSystemService(
078: com.sun.jbi.EnvironmentContext anEnv)
079: throws javax.jbi.JBIException {
080: String loggerName = com.sun.jbi.management.config.LoggerConfigurationFactory.CONFIGURATION_LOGGER;
081:
082: Logger logger = Logger.getLogger(loggerName);
083:
084: //initialize the super.
085: super .initModelSystemService(anEnv, logger, mJbiNameInfo);
086:
087: //add ConfigurationService MBean to START/STOP mbean set:
088: mStartMBeans
089: .add(
090: mConfigServiceMBeanName,
091: com.sun.jbi.management.system.ConfigurationServiceMBean.class,
092: this );
093: }
094:
095: /*-----------------------------------------------------------------------------------*\
096: * com.sun.jbi.ServiceLifeCycle operations *
097: \*-----------------------------------------------------------------------------------*/
098:
099: /**
100: * Initialize a service. This performs any initialization tasks
101: * required by the service but does not make the service ready
102: * to process requests.
103: * @param aContext the JBI environment context created
104: * by the JBI framework
105: * @throws javax.jbi.JBIException if an error occurs
106: */
107: public void initService(EnvironmentContext aContext)
108: throws javax.jbi.JBIException {
109: mEnv = aContext;
110: /*
111: * Local initialization of this service.
112: * Local routine is responsible for calling super.initModelSystemService(..).
113: */
114: initModelSystemService(mEnv);
115:
116: // Register the instance configuration MBeans these are registered here
117: // and not in the LifeCycle MBean start() since these MBeans are
118: // required before any of the other system services initialize in
119: // initService()
120: try {
121: registerGlobalRuntimeConfigMBeans();
122: registerRuntimeConfigMBeans();
123: } catch (Exception ex) {
124: throw new javax.jbi.JBIException(ex);
125: }
126:
127: // Register the life cycle MBean for this servie
128: bootstrap();
129:
130: }
131:
132: /**
133: * Start a service. This makes the service ready to process requests.
134: * @throws javax.jbi.JBIException if an error occurs
135: */
136: public void startService() throws javax.jbi.JBIException {
137: // Nothing to be done here
138: }
139:
140: /**
141: * Stop a service. This makes the service stop processing requests.
142: * @throws javax.jbi.JBIException if an error occurs
143: */
144: public void stopService() throws javax.jbi.JBIException {
145: try {
146: // Unregister the config MBeans now
147: unregisterRuntimeConfigMBeans();
148: unregisterGlobalRuntimeConfigMBeans();
149: } catch (Exception ex) {
150: throw new javax.jbi.JBIException(ex);
151: }
152: }
153:
154: /*-----------------------------------------------------------------------------------*\
155: * ConfigurationServiceMBEan operations *
156: \*-----------------------------------------------------------------------------------*/
157:
158: /**
159: * Lookup a system Configuration MBean by system service type. For each service type
160: * there are two instance MBeans the target specific instance configuration MBean
161: * and the global configuration instance MBean.
162: *
163: * @param aSvcName is the name of the system service
164: * @return the JMX object name array of the service Configuration MBean names.
165: */
166: public ObjectName[] getSystemConfigMBean(String aSvcType) {
167: MBeanNames mbn = mEnv.getMBeanNames();
168: String tmp = mbn.getJmxDomainName();
169:
170: tmp += ":" + mbn.SERVICE_NAME_KEY + "="
171: + mbn.SERVICE_NAME_CONFIG_SERVICE;
172: tmp += "," + mbn.CONTROL_TYPE_KEY + "=" + aSvcType;
173: tmp += "," + mbn.COMPONENT_TYPE_KEY + "="
174: + mbn.COMPONENT_TYPE_SYSTEM;
175: tmp += ",*";
176:
177: ObjectName[] names = null;
178: try {
179: ObjectName configMBeanFilter = new ObjectName(tmp);
180: java.util.Set<ObjectName> nameSet = mMBeanServer
181: .queryNames(configMBeanFilter, null);
182: names = new ObjectName[nameSet.size()];
183: nameSet.toArray(names);
184: } catch (javax.management.MalformedObjectNameException mbex) {
185: mLogger.warning(mbex.toString());
186: }
187:
188: return names;
189: }
190:
191: /**
192: * Looks up all the instance ConfigurationMBeans registered for the JBI system services.
193: *
194: * This returns all the instance configuration MBeans : instance configuration
195: * and global instance configuration MBeans
196: * </br>
197: * ObjectName pattern is :
198: * </br>
199: * com.sun.jbi:ComponentType=System, ServiceName=ConfigurationService
200: *
201: *
202: * @return array of object names for all system service ConfigurationMBeans.
203: * @return zero-length array if no services registered.
204: */
205: public ObjectName[] getSystemConfigMBeans() {
206: MBeanNames mbn = mEnv.getMBeanNames();
207: String tmp = mbn.getJmxDomainName();
208:
209: // ObjectName pattern is : com.sun.jbi:ComponentType=System, ServiceName=ConfigurationService
210: tmp += ":" + mbn.COMPONENT_TYPE_KEY + "="
211: + mbn.COMPONENT_TYPE_SYSTEM;
212: tmp += "," + mbn.SERVICE_NAME_KEY + "="
213: + mbn.SERVICE_NAME_CONFIG_SERVICE;
214: //wildcard goes at the end:
215: tmp += ",*";
216:
217: ObjectName[] names = null;
218: try {
219: ObjectName configMBeanFilter = new ObjectName(tmp);
220: java.util.Set<ObjectName> nameSet = mMBeanServer
221: .queryNames(configMBeanFilter, null);
222: names = new ObjectName[nameSet.size()];
223: nameSet.toArray(names);
224: } catch (javax.management.MalformedObjectNameException mbex) {
225: mLogger.warning(mbex.toString());
226: }
227:
228: //if no configuration mbeans found for any system service...
229: if (names.length <= 1) {
230: String statusMsg = mTranslator
231: .getString(LocalStringKeys.CS_GETSYSTEMCONFIGMBEANS_NO_SERVICES);
232: mLogger.severe(statusMsg);
233: }
234:
235: return names;
236: }
237:
238: /////////
239: //methods private to ConfigurationService
240: /////////
241:
242: /**
243: * Register the runtime configuration dynamic MBeans for this instance.
244: */
245: private void registerRuntimeConfigMBeans() throws Exception {
246: Map<MBeanNames.ServiceType, ConfigurationFactory> configs = ConfigurationBuilder
247: .createConfigurations(readDefaultProperties());
248: mConfigSvcTypes = configs.keySet();
249:
250: for (MBeanNames.ServiceType serviceType : mConfigSvcTypes) {
251: ObjectName name = mEnv.getMBeanNames()
252: .getSystemServiceMBeanName(
253: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
254: ConfigurationBuilder
255: .getControlType(serviceType));
256:
257: if (!mMBeanServer.isRegistered(name)) {
258: // Create and register the facade config MBean
259: ModelMBeanInfo mbeanInfo = RuntimeConfiguration
260: .createMBeanInfo(configs.get(serviceType)
261: .createMBeanAttributeInfo());
262: RequiredModelMBean modelConfigMBean = new com.sun.jbi.management.config.InstanceConfiguration(
263: mbeanInfo, ConfigurationCategory
264: .valueOf(serviceType.toString()));
265: modelConfigMBean.setManagedResource(modelConfigMBean,
266: "ObjectReference");
267: mMBeanServer.registerMBean(modelConfigMBean, name);
268: }
269: }
270: }
271:
272: /**
273: * Unregister the runtime configuration dynamic MBeans.
274: *
275: * @param target - target instance / cluster name
276: */
277: private void unregisterRuntimeConfigMBeans() throws Exception {
278: for (MBeanNames.ServiceType serviceType : mConfigSvcTypes) {
279: ObjectName name = mEnv.getMBeanNames()
280: .getSystemServiceMBeanName(
281: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
282: ConfigurationBuilder
283: .getControlType(serviceType));
284:
285: // Unregister the config MBean
286: if (mMBeanServer.isRegistered(name)) {
287: mMBeanServer.unregisterMBean(name);
288: }
289: }
290: }
291:
292: /**
293: * Register the runtime configuration dynamic MBeans for this instance.
294: */
295: private void registerGlobalRuntimeConfigMBeans() throws Exception {
296: Map<MBeanNames.ServiceType, ConfigurationFactory> configs = ConfigurationBuilder
297: .createConfigurations(readDefaultProperties());
298: mConfigSvcTypes = configs.keySet();
299:
300: ManagementContext mgtCtx = new ManagementContext(mEnv);
301: for (MBeanNames.ServiceType serviceType : mConfigSvcTypes) {
302: ObjectName name = mgtCtx.getMBeanNames("domain")
303: .getSystemServiceMBeanName(
304: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
305: ConfigurationBuilder
306: .getControlType(serviceType));
307:
308: if (!mMBeanServer.isRegistered(name)) {
309: // Create and register the facade config MBean
310: ModelMBeanInfo mbeanInfo = RuntimeConfiguration
311: .createMBeanInfo(configs.get(serviceType)
312: .createMBeanAttributeInfo());
313: RequiredModelMBean modelConfigMBean = new com.sun.jbi.management.config.GlobalConfiguration(
314: mbeanInfo, ConfigurationCategory
315: .valueOf(serviceType.toString()));
316: modelConfigMBean.setManagedResource(modelConfigMBean,
317: "ObjectReference");
318: mMBeanServer.registerMBean(modelConfigMBean, name);
319: }
320: }
321: }
322:
323: /**
324: * Unregister the runtime configuration dynamic MBeans.
325: *
326: * @param target - target instance / cluster name
327: */
328: private void unregisterGlobalRuntimeConfigMBeans() throws Exception {
329: ManagementContext mgtCtx = new ManagementContext(mEnv);
330: for (MBeanNames.ServiceType serviceType : mConfigSvcTypes) {
331: ObjectName name = mgtCtx.getMBeanNames("domain")
332: .getSystemServiceMBeanName(
333: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
334: ConfigurationBuilder
335: .getControlType(serviceType));
336:
337: // Unregister the config MBean
338: if (mMBeanServer.isRegistered(name)) {
339: mMBeanServer.unregisterMBean(name);
340: }
341: }
342: }
343:
344: /**
345: * Read the default configuration file from $JBI_INSTALL_ROOT/lib/templates
346: *
347: * @return the loaded properties
348: * @exception java.io.IOException on file read errors
349: */
350: private Properties readDefaultProperties()
351: throws java.io.IOException {
352: Properties defaults = new Properties();
353: StringBuffer pathToConfig = new StringBuffer(mEnv
354: .getJbiInstallRoot());
355: pathToConfig.append(java.io.File.separatorChar);
356: pathToConfig.append("lib");
357: pathToConfig.append(java.io.File.separatorChar);
358: pathToConfig.append("install");
359: pathToConfig.append(java.io.File.separatorChar);
360: pathToConfig.append("templates");
361: pathToConfig.append(java.io.File.separatorChar);
362:
363: java.io.File defConfig = new java.io.File(pathToConfig
364: .toString(), DEFAULT_CONFIG_FILE);
365: if (defConfig.exists() && defConfig.length() > 0) {
366: java.io.InputStream ipStr = new java.io.FileInputStream(
367: defConfig);
368: if (ipStr != null) {
369: defaults.load(ipStr);
370: ipStr.close();
371: }
372: }
373:
374: return defaults;
375: }
376: }
|