001: /**
002: * Copyright (C) 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package com.google.inject.tools.jmx;
016:
017: import com.google.inject.Binding;
018: import com.google.inject.Guice;
019: import com.google.inject.Injector;
020: import com.google.inject.Key;
021: import com.google.inject.Module;
022: import java.lang.annotation.Annotation;
023: import java.lang.management.ManagementFactory;
024: import javax.management.MBeanServer;
025: import javax.management.MalformedObjectNameException;
026: import javax.management.ObjectName;
027:
028: /**
029: * Provides a JMX interface to Guice.
030: *
031: * @author crazybob@google.com (Bob Lee)
032: */
033: public class Manager {
034:
035: /**
036: * Registers all the bindings of an Injector with the platform MBean server.
037: * Consider using the name of your root {@link Module} class as the domain.
038: */
039: public static void manage(String domain, Injector injector) {
040: manage(ManagementFactory.getPlatformMBeanServer(), domain,
041: injector);
042: }
043:
044: /**
045: * Registers all the bindings of an Injector with the given MBean server.
046: * Consider using the name of your root {@link Module} class as the domain.
047: */
048: public static void manage(MBeanServer server, String domain,
049: Injector injector) {
050: // Register each binding independently.
051: for (Binding<?> binding : injector.getBindings().values()) {
052: // Construct the name manually so we can ensure proper ordering of the
053: // key/value pairs.
054: StringBuilder name = new StringBuilder();
055: name.append(domain).append(":");
056: Key<?> key = binding.getKey();
057: name.append("type=").append(
058: quote(key.getTypeLiteral().toString()));
059: Annotation annotation = key.getAnnotation();
060: if (annotation != null) {
061: name.append(",annotation=").append(
062: quote(annotation.toString()));
063: } else {
064: Class<? extends Annotation> annotationType = key
065: .getAnnotationType();
066: if (annotationType != null) {
067: name.append(",annotation=").append(
068: quote("@" + annotationType.getName()));
069: }
070: }
071:
072: try {
073: server.registerMBean(new ManagedBinding(binding),
074: new ObjectName(name.toString()));
075: } catch (MalformedObjectNameException e) {
076: throw new RuntimeException("Bad object name: "
077: + name.toString(), e);
078: } catch (Exception e) {
079: throw new RuntimeException(e);
080: }
081: }
082: }
083:
084: static String quote(String value) {
085: // JMX seems to have a comma bug.
086: return ObjectName.quote(value).replace(',', ';');
087: }
088:
089: /**
090: * Run with no arguments for usage instructions.
091: */
092: public static void main(String[] args) throws Exception {
093: if (args.length != 1) {
094: System.err
095: .println("Usage: java -Dcom.sun.management.jmxremote "
096: + Manager.class.getName()
097: + " [module class name]");
098: System.err.println("Then run 'jconsole' to connect.");
099: System.exit(1);
100: }
101:
102: Module module = (Module) Class.forName(args[0]).newInstance();
103: Injector injector = Guice.createInjector(module);
104:
105: manage(args[0], injector);
106:
107: System.out.println("Press Ctrl+C to exit...");
108:
109: // Sleep forever.
110: Thread.sleep(Long.MAX_VALUE);
111: }
112: }
|