KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > DomainMBean


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: DomainMBean.java,v 1.3 2005/12/25 03:42:16 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 //jdk imports
31
import java.lang.reflect.Method JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 //JMX imports
38
import javax.management.AttributeList JavaDoc;
39
40 // commons imports
41
import com.sun.enterprise.util.SystemPropertyConstants;
42 import com.sun.enterprise.util.OS;
43 import com.sun.enterprise.util.i18n.StringManager;
44
45 //config imports
46
import com.sun.enterprise.admin.config.BaseConfigMBean;
47 import com.sun.enterprise.admin.config.ConfigMBeanHelper;
48 import com.sun.enterprise.admin.config.MBeanConfigException;
49 import com.sun.enterprise.admin.meta.MBeanRegistryEntry;
50 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor;
51
52 //core imports
53
import com.sun.enterprise.instance.InstanceEnvironment;
54 import com.sun.enterprise.server.ApplicationServer;
55
56 import com.sun.enterprise.admin.mbeanapi.IDomainMBean;
57 import com.sun.enterprise.config.serverbeans.PropertyResolver;
58
59 // Logging
60
import java.util.logging.Level JavaDoc;
61 import com.sun.enterprise.config.ConfigException;
62 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
63
64
65 public class DomainMBean extends BaseConfigMBean
66     implements IDomainMBean
67 {
68     private static final StringManager localStrings =
69             StringManager.getManager(DomainMBean.class);
70     
71     /**
72      * Resolve tokens of the form ${..} in value.
73      * @param value string value to resolve
74      * @param instanceName - instance name for obtaining token values. If null, then DAS is used for resolution
75      *
76      * @returns resolved string
77      *
78      */

79     public String JavaDoc resolveTokens(String JavaDoc value, String JavaDoc instanceName) throws ConfigException
80     {
81         return resolveTokens(value, instanceName, false);
82     }
83     
84     /**
85      * Resolve tokens of the form ${..} in value.
86      * @param value string value to resolve
87      * @param instanceName - instance name for obtaining token values. If null, then DAS is used for resolution
88      * @param bResolvePathesAsWell - if true, resolves pathes as well (currently only for DAS!)
89      *
90      * @returns resolved string
91      *
92      */

93     public String JavaDoc resolveTokens(String JavaDoc value, String JavaDoc instanceName, boolean bResolvePathesAsWell) throws ConfigException
94     {
95         //FIXME: bResolvePathesAsWell=true is working correct only for DAS
96
if(instanceName==null)
97         {
98             instanceName=MBeanRegistryFactory.getAdminContext().getServerName();
99         }
100         PropertyResolver resolver = new PropertyResolver(getConfigContext(), instanceName);
101         return resolver.resolve(value, bResolvePathesAsWell);
102     }
103     /*
104      * This operation allows to obtain third part software properties set such as jdbc-resources, connectors etc.
105      * Since this set depend on what kind (brand) of software it is for, attributeList parameter defines attributes
106      * for undesrstanding what kind of software used.
107      * This operation suggests that correspondent MBeans' classes override static getDefaultCustomProperties(attributeList)
108      * "mbeanTypeName" - parameter is typeName from mbeans descriptors file.
109      */

110     public AttributeList JavaDoc getDefaultCustomProperties(String JavaDoc mbeanTypeName, AttributeList JavaDoc attributeList)
111     {
112         if(mbeanTypeName==null)
113             return null;
114         try {
115             MBeanRegistryEntry entry = m_registry.findMBeanRegistryEntryByType(mbeanTypeName);
116             MBeanNamingDescriptor descr = entry.getNamingDescriptor();
117             String JavaDoc className = descr.getMBeanClassName();
118             Class JavaDoc cl = Class.forName(className);
119             Method JavaDoc method = cl.getDeclaredMethod("getDefaultCustomProperties", new Class JavaDoc[]{Class.forName("javax.management.AttributeList")});
120             return (AttributeList JavaDoc)method.invoke(null, new Object JavaDoc[]{attributeList});
121         } catch (Exception JavaDoc e)
122         {
123             _sLogger.fine("getDefaultCustomProperties(): Exception for mbeanTypeName:"+mbeanTypeName);
124             return null;
125         }
126             
127     }
128     
129     /*
130      * Returns default values for attributes for mbean.
131      * If Custom MBean class implents public static getDefaultAttributeValues(String[]) then it will be called,
132      * otherwise returns DTD defined default values (from ConfigBeans)
133      *
134      * if attrNames is null - all default attributes values are returning
135      */

136     public AttributeList JavaDoc getDefaultAttributeValues(String JavaDoc mbeanTypeName, String JavaDoc attrNames[])
137     {
138         if(mbeanTypeName==null)
139             return null;
140         try {
141             MBeanRegistryEntry entry = m_registry.findMBeanRegistryEntryByType(mbeanTypeName);
142             if(attrNames==null)
143             {
144                 attrNames = entry.getAttributeNames();
145                 if(attrNames==null || attrNames.length<1)
146                     return null;
147             }
148             MBeanNamingDescriptor descr = entry.getNamingDescriptor();
149             String JavaDoc className = descr.getMBeanClassName();
150             Class JavaDoc cl = Class.forName(className);
151             Method JavaDoc method = null;
152             try {
153                 method = cl.getDeclaredMethod("getDefaultAttributeValues", new Class JavaDoc[]{(new String JavaDoc[0]).getClass()});
154                 return (AttributeList JavaDoc)method.invoke(null, new Object JavaDoc[]{attrNames});
155             } catch (Exception JavaDoc e)
156             {
157                 //no custom implementation - just ignore
158
}
159             // standard
160
return ConfigMBeanHelper.getDefaultAttributeValues(descr, attrNames);
161         } catch (Exception JavaDoc e)
162         {
163             _sLogger.fine("getDefaultAttributeValues(): Exception for mbeanTypeName:"+mbeanTypeName);
164             return null;
165         }
166     }
167
168      /**
169      * Returns the absolute path of the config directory.
170      *
171      * @returns the absolute path of the config directory.
172      */

173     public String JavaDoc getConfigDir() {
174         InstanceEnvironment env =
175                 ApplicationServer.getServerContext().getInstanceEnvironment();
176         return env.getConfigDirPath();
177     }
178   
179     private static final String JavaDoc BUNDLED_DOMAINS_ROOT = "/var/appserver/domains";
180
181     private static final String JavaDoc AUTOSTART_FILENAME = "autostart";
182
183     /**
184      * Is autostart feature supported for this domain. Enabling autostart will
185      * result in a domain being started up at the time of machine startup
186      * (boot). The autostart feature is supported only on domains in default
187      * domain directory of Solaris bundled release.
188      * @returns true if autostart feature is supported for this domain.
189      */

190     public boolean isAutoStartSupported() {
191         if (OS.isUnix()) {
192             if (getConfigDir().startsWith(BUNDLED_DOMAINS_ROOT)) {
193                 return true;
194             }
195         }
196         return false;
197     }
198
199     /**
200      * Is auto start enabled for this domain.
201      * @throw MBeanConfigException if autostart is not supported for this domain
202      */

203     public boolean isAutoStartEnabled() throws MBeanConfigException {
204         checkAutoStartSupported();
205         File JavaDoc autoStartFile = getAutoStartFile();
206         return autoStartFile.exists();
207     }
208
209     /**
210      * Set autostart enabled state.
211      * @param state if true enables autostart, otherwise disables autostart
212      * @throw MBeanConfigException if autostart is not supported or if there
213      * is an error in setting autostart state.
214      */

215     public void setAutoStartEnabled(boolean state) throws MBeanConfigException {
216         checkAutoStartSupported();
217         boolean success = (state ? enableAutoStart() : disableAutoStart());
218         if (!success) {
219             String JavaDoc msg = localStrings.getString(
220                     "admin.mbeans.domain.set_autostart_failed");
221             throw new MBeanConfigException(msg);
222         }
223     }
224
225     /**
226      * Returns the name of this domain.
227      *
228      * @return domain name
229      */

230     public String JavaDoc getName() throws MBeanConfigException {
231
232         String JavaDoc name = null;
233
234         try {
235             name = System.getProperty(SystemPropertyConstants.DOMAIN_NAME);
236         } catch (Exception JavaDoc e) {
237             String JavaDoc msg = localStrings.getString(
238                     "admin.mbeans.domain.get_name_failed")
239                     + " " + e.getLocalizedMessage();
240             throw new MBeanConfigException(msg);
241         }
242
243         if (name == null) {
244             String JavaDoc msg = localStrings.getString(
245                     "admin.mbeans.domain.get_name_failed");
246             throw new MBeanConfigException(msg);
247         }
248
249         return name;
250     }
251
252     private boolean enableAutoStart() {
253         File JavaDoc autoStartFile = getAutoStartFile();
254         boolean success = false;
255         try {
256             if (!autoStartFile.exists()) {
257                 success = autoStartFile.createNewFile();
258             } else {
259                 success = true;
260             }
261         } catch (IOException JavaDoc ioe) {
262             _sLogger.log(Level.FINE, "mbean.autostart_ioexception", ioe);
263             _sLogger.log(Level.WARNING, "mbean.autostart_enable_error",
264                     new Object JavaDoc[] {autoStartFile, ioe.getMessage()});
265         }
266         return success;
267     }
268
269     private boolean disableAutoStart() {
270         File JavaDoc autoStartFile = getAutoStartFile();
271         boolean success = true;
272         if (autoStartFile.exists()) {
273             success = autoStartFile.delete();
274         }
275         return success;
276     }
277
278     private void checkAutoStartSupported() throws MBeanConfigException {
279         if (!isAutoStartSupported()) {
280             String JavaDoc msg = localStrings.getString(
281                     "admin.mbeans.domain.autostart_not_supported");
282             throw new MBeanConfigException(msg);
283         }
284     }
285
286     private File JavaDoc getAutoStartFile() {
287         return new File JavaDoc(getConfigDir(), AUTOSTART_FILENAME);
288     }
289 }
290
Popular Tags