1 package org.sape.carbon.services.carbonmanagement; 2 3 import java.io.IOException ; 4 import java.io.StringWriter ; 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 import java.util.Arrays ; 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 13 import org.sape.carbon.core.bootstrap.BootStrapper; 14 import org.sape.carbon.core.component.ComponentConfiguration; 15 import org.sape.carbon.core.component.Lookup; 16 import org.sape.carbon.core.component.lifecycle.Configurable; 17 import org.sape.carbon.core.component.lifecycle.InvalidStateException; 18 import org.sape.carbon.core.component.lifecycle.LifecycleException; 19 import org.sape.carbon.core.config.Config; 20 import org.sape.carbon.core.config.Configuration; 21 import org.sape.carbon.core.config.InvalidConfigurationException; 22 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor; 23 import org.sape.carbon.core.config.node.Node; 24 import org.sape.carbon.core.config.node.NodeNotFoundException; 25 import org.sape.carbon.core.exception.ExceptionUtility; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.jdom.output.XMLOutputter; 30 31 39 public class DefaultCarbonManagementServiceImpl 40 implements CarbonManagementService, Configurable { 41 42 43 private Log log = LogFactory.getLog(this.getClass()); 44 45 46 private CarbonManagementServiceConfiguration config; 47 48 55 public void configure(ComponentConfiguration configuration) 56 throws InvalidConfigurationException { 57 58 try { 59 this.config = (CarbonManagementServiceConfiguration) configuration; 60 } catch (ClassCastException cce) { 61 throw new InvalidConfigurationException( 62 this.getClass(), 63 configuration.getConfigurationName(), 64 null, 65 "Configuration does not implement " 66 + CarbonManagementServiceConfiguration.class.getName(), 67 cce); 68 } 69 } 70 71 79 public String viewConfigurationXML(String configuationPath) 80 throws ConfigurationNotFoundException { 81 82 try { 83 Configuration config = 84 Config.getInstance().fetchConfiguration(configuationPath); 85 86 StringWriter writer = new StringWriter (); 87 XMLOutputter outputter = new XMLOutputter(" ", true); 88 outputter.output(config.getDataStructure(), writer); 89 return writer.toString(); 90 91 } catch (org.sape.carbon.core.config.ConfigurationNotFoundException 92 cnfe) { 93 94 throw new ConfigurationNotFoundException( 95 this.getClass(), 96 "[" + configuationPath + "] was not found", 97 cnfe); 98 } catch (IOException ioe) { 99 throw new InvalidStateException( 100 this.getClass(), 101 "[" + configuationPath + "] could not be converted to XML", 102 ioe); 103 } 104 } 105 106 111 public String viewConfigurationTree() { 112 StringBuffer buffer = new StringBuffer (); 113 114 buffer.append("<html><body><h2>Configuration Tree</h2>\n"); 115 buffer.append("<pre><font face='Courier New'>"); 116 117 try { 118 appendSubtreeToBuffer( 119 buffer, 120 Config.getInstance().fetchNode(Config.ROOT_NODE_NAME), 121 0); 122 123 } catch (NodeNotFoundException nnfe) { 124 } 126 127 buffer.append("</font></pre></body></html>"); 128 129 return buffer.toString(); 130 } 131 132 135 public void refreshAllConfigurations() { 136 try { 137 Config.getInstance().fetchNode(Config.ROOT_NODE_NAME).refresh(); 138 } catch (NodeNotFoundException nnfe) { 139 } 141 } 142 143 150 public void refreshConfiguration(String configuationPath) 151 throws ConfigurationNotFoundException { 152 153 try { 154 Config.getInstance().fetchNode(configuationPath).refresh(); 155 } catch (org.sape.carbon.core.config.node.NodeNotFoundException nnfe) { 156 throw new ConfigurationNotFoundException( 157 this.getClass(), 158 "[" + configuationPath + "] was not found", 159 nnfe); 160 } 161 } 162 163 169 public void refreshAllComponentConfigurations() { 170 Collection componentNames = 171 Lookup.getInstance().getComponentKeeper().getComponentNames(); 172 173 Collection dontReconfigure = 174 Arrays.asList(this.config.getDontReconfigure()); 175 176 Iterator componentIterator = componentNames.iterator(); 177 while (componentIterator.hasNext()) { 178 String componentName = (String ) componentIterator.next(); 179 180 if (!dontReconfigure.contains(componentName)) { 181 try { 182 ConfigurationInterceptor assistantView = 183 (ConfigurationInterceptor) Lookup 184 .getInstance() 185 .fetchComponent( 186 componentName); 187 188 assistantView.refreshConfiguration(); 189 } catch (LifecycleException le) { 190 if (log.isDebugEnabled()) { 191 log.debug("Could not reconfigure component [" 192 + componentName 193 + "] due to exception: " 194 + ExceptionUtility.printStackTracesToString(le)); 195 } 196 } 197 } 198 } 199 } 200 201 204 public void loadComponent(String componentName) 205 throws ComponentNotFoundException { 206 207 try { 208 Lookup.getInstance().fetchComponent(componentName); 209 } catch (org.sape.carbon.core.component.ComponentNotFoundException 210 cnfe) { 211 212 throw new ComponentNotFoundException( 213 this.getClass(), 214 "[" + componentName + "] was not found", 215 cnfe); 216 } 217 } 218 219 225 public String getDeploymentProperties() { 226 String [] propertyNames = this.config.getDeploymentPropertyNames(); 227 228 StringBuffer buf = new StringBuffer (256); 229 buf.append("<html><body><h2>Deployment Properties</h2>\n"); 230 231 buf.append("<table border='2'><tr> <td><b>Name</b></td> "); 232 buf.append("<td><b>Value</b></td> </tr>\n"); 233 234 for (int index = 0; index < propertyNames.length; index++) { 235 buf.append("<tr> <td>"); 236 buf.append(propertyNames[index]); 237 buf.append("</td> <td>"); 238 buf.append( 239 BootStrapper.getInstance().getDeploymentProperty( 240 propertyNames[index])); 241 buf.append("</td> </tr>\n"); 242 } 243 buf.append("</table></body></html>"); 244 245 return buf.toString(); 246 } 247 248 249 256 public Map getDeploymentPropertyMap() { 257 String [] propertyNames = this.config.getDeploymentPropertyNames(); 258 Map map = new HashMap (); 259 260 for (int index = 0; index < propertyNames.length; index++) { 261 map.put( 262 propertyNames[index], 263 BootStrapper.getInstance().getDeploymentProperty( 264 propertyNames[index])); 265 } 266 267 return map; 268 } 269 270 273 public String getDeploymentProperty(String propertyName) { 274 return BootStrapper.getInstance().getDeploymentProperty(propertyName); 275 } 276 277 278 private static final String TAB = "| "; 279 280 281 private static final String TAB_WITH_CHILD = "+--"; 282 283 284 private static final String NEW_LINE = "\n"; 285 286 287 private static final String ARROW = " --> "; 288 289 298 private void appendSubtreeToBuffer( 299 StringBuffer buffer, 300 Node subtree, 301 int depth) { 302 303 304 Node[] children = subtree.fetchChildren(); 305 String nodeName = subtree.getName(); 306 307 if ("".equals(nodeName)) { 308 nodeName = Config.ROOT_NODE_NAME; 309 } 310 311 for (int count = 0; count < depth; count++) { 312 buffer.append(TAB); 313 } 314 315 if (children.length == 0) { 316 buffer.append(TAB); 317 buffer.append(nodeName); 318 buffer.append(NEW_LINE); 319 320 } else { 321 buffer.append(TAB_WITH_CHILD); 322 buffer.append(nodeName); 323 buffer.append(NEW_LINE); 324 325 for (int index = 0; index < children.length; index++) { 326 if (!children[index] 327 .getAbsoluteName() 328 .startsWith(subtree.getAbsoluteName())) { 329 for (int count = 0; count < depth; count++) { 331 buffer.append(TAB); 332 } 333 334 buffer.append(TAB); 335 buffer.append(children[index].getName()); 336 buffer.append(ARROW); 337 buffer.append(children[index].getAbsoluteName()); 338 buffer.append(NEW_LINE); 339 340 } else { 341 appendSubtreeToBuffer(buffer, children[index], depth + 1); 342 } 343 } 344 } 345 } 346 347 348 353 public long getFreeMemory() { 354 return Runtime.getRuntime().freeMemory(); 355 } 356 357 362 public long getTotalMemory() { 363 return Runtime.getRuntime().totalMemory(); 364 } 365 366 371 public long getMaximumMemory() { 372 long maxMemory = -1; 373 try { 374 Runtime rt = Runtime.getRuntime(); 375 Method m = Runtime .class.getMethod("maxMemory", new Class [0]); 376 maxMemory = ((Long ) m.invoke(rt, new Object [0])).longValue(); 377 } catch (NoSuchMethodException nsme) { 378 } catch (IllegalAccessException iae) { 380 } catch (InvocationTargetException ite) { 382 } 384 return maxMemory; 385 } 386 387 392 public int getAvailableProcessors() { 393 int processors = -1; 394 try { 395 Runtime rt = Runtime.getRuntime(); 396 Method m = 397 Runtime .class.getMethod("availableProcessors", new Class [0]); 398 399 processors = ((Integer ) m.invoke(rt, new Object [0])).intValue(); 400 } catch (NoSuchMethodException nsme) { 401 } catch (IllegalAccessException iae) { 403 } catch (InvocationTargetException ite) { 405 } 407 return processors; 408 } 409 410 415 private ThreadGroup getRootThreadGroup() { 416 ThreadGroup group = Thread.currentThread().getThreadGroup(); 417 while (group.getParent() != null) { 418 group = group.getParent(); 419 } 420 421 return group; 422 } 423 424 429 public int getActiveThreadCount() { 430 return getRootThreadGroup().activeCount(); 431 } 432 433 438 public int getActiveThreadGroupCount() { 439 return getRootThreadGroup().activeGroupCount(); 440 } 441 } 442 | Popular Tags |