1 23 24 27 28 package com.sun.enterprise.admin.mbeans; 29 30 import java.lang.reflect.Method ; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 import javax.management.AttributeList ; 39 40 import com.sun.enterprise.util.SystemPropertyConstants; 42 import com.sun.enterprise.util.OS; 43 import com.sun.enterprise.util.i18n.StringManager; 44 45 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 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 import java.util.logging.Level ; 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 79 public String resolveTokens(String value, String instanceName) throws ConfigException 80 { 81 return resolveTokens(value, instanceName, false); 82 } 83 84 93 public String resolveTokens(String value, String instanceName, boolean bResolvePathesAsWell) throws ConfigException 94 { 95 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 110 public AttributeList getDefaultCustomProperties(String mbeanTypeName, AttributeList 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 className = descr.getMBeanClassName(); 118 Class cl = Class.forName(className); 119 Method method = cl.getDeclaredMethod("getDefaultCustomProperties", new Class []{Class.forName("javax.management.AttributeList")}); 120 return (AttributeList )method.invoke(null, new Object []{attributeList}); 121 } catch (Exception e) 122 { 123 _sLogger.fine("getDefaultCustomProperties(): Exception for mbeanTypeName:"+mbeanTypeName); 124 return null; 125 } 126 127 } 128 129 136 public AttributeList getDefaultAttributeValues(String mbeanTypeName, String 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 className = descr.getMBeanClassName(); 150 Class cl = Class.forName(className); 151 Method method = null; 152 try { 153 method = cl.getDeclaredMethod("getDefaultAttributeValues", new Class []{(new String [0]).getClass()}); 154 return (AttributeList )method.invoke(null, new Object []{attrNames}); 155 } catch (Exception e) 156 { 157 } 159 return ConfigMBeanHelper.getDefaultAttributeValues(descr, attrNames); 161 } catch (Exception e) 162 { 163 _sLogger.fine("getDefaultAttributeValues(): Exception for mbeanTypeName:"+mbeanTypeName); 164 return null; 165 } 166 } 167 168 173 public String getConfigDir() { 174 InstanceEnvironment env = 175 ApplicationServer.getServerContext().getInstanceEnvironment(); 176 return env.getConfigDirPath(); 177 } 178 179 private static final String BUNDLED_DOMAINS_ROOT = "/var/appserver/domains"; 180 181 private static final String AUTOSTART_FILENAME = "autostart"; 182 183 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 203 public boolean isAutoStartEnabled() throws MBeanConfigException { 204 checkAutoStartSupported(); 205 File autoStartFile = getAutoStartFile(); 206 return autoStartFile.exists(); 207 } 208 209 215 public void setAutoStartEnabled(boolean state) throws MBeanConfigException { 216 checkAutoStartSupported(); 217 boolean success = (state ? enableAutoStart() : disableAutoStart()); 218 if (!success) { 219 String msg = localStrings.getString( 220 "admin.mbeans.domain.set_autostart_failed"); 221 throw new MBeanConfigException(msg); 222 } 223 } 224 225 230 public String getName() throws MBeanConfigException { 231 232 String name = null; 233 234 try { 235 name = System.getProperty(SystemPropertyConstants.DOMAIN_NAME); 236 } catch (Exception e) { 237 String 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 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 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 ioe) { 262 _sLogger.log(Level.FINE, "mbean.autostart_ioexception", ioe); 263 _sLogger.log(Level.WARNING, "mbean.autostart_enable_error", 264 new Object [] {autoStartFile, ioe.getMessage()}); 265 } 266 return success; 267 } 268 269 private boolean disableAutoStart() { 270 File 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 msg = localStrings.getString( 281 "admin.mbeans.domain.autostart_not_supported"); 282 throw new MBeanConfigException(msg); 283 } 284 } 285 286 private File getAutoStartFile() { 287 return new File (getConfigDir(), AUTOSTART_FILENAME); 288 } 289 } 290 | Popular Tags |