• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject.tools.jmx;
18 
19 import com.google.inject.Binding;
20 import com.google.inject.Guice;
21 import com.google.inject.Injector;
22 import com.google.inject.Key;
23 import com.google.inject.Module;
24 import java.lang.annotation.Annotation;
25 import java.lang.management.ManagementFactory;
26 import javax.management.MBeanServer;
27 import javax.management.MalformedObjectNameException;
28 import javax.management.ObjectName;
29 
30 /**
31  * Provides a JMX interface to Guice.
32  *
33  * @author crazybob@google.com (Bob Lee)
34  */
35 public class Manager {
36 
37   /**
38    * Registers all the bindings of an Injector with the platform MBean server. Consider using the
39    * name of your root {@link Module} class as the domain.
40    */
manage(String domain, Injector injector)41   public static void manage(String domain, Injector injector) {
42     manage(ManagementFactory.getPlatformMBeanServer(), domain, injector);
43   }
44 
45   /**
46    * Registers all the bindings of an Injector with the given MBean server. Consider using the name
47    * of your root {@link Module} class as the domain.
48    */
manage(MBeanServer server, String domain, Injector injector)49   public static void manage(MBeanServer server, String domain, Injector injector) {
50     // Register each binding independently.
51     for (Binding<?> binding : injector.getBindings().values()) {
52       // Construct the name manually so we can ensure proper ordering of the
53       // key/value pairs.
54       StringBuilder name = new StringBuilder();
55       name.append(domain).append(":");
56       Key<?> key = binding.getKey();
57       name.append("type=").append(quote(key.getTypeLiteral().toString()));
58       Annotation annotation = key.getAnnotation();
59       if (annotation != null) {
60         name.append(",annotation=").append(quote(annotation.toString()));
61       } else {
62         Class<? extends Annotation> annotationType = key.getAnnotationType();
63         if (annotationType != null) {
64           name.append(",annotation=").append(quote("@" + annotationType.getName()));
65         }
66       }
67 
68       try {
69         server.registerMBean(new ManagedBinding(binding), new ObjectName(name.toString()));
70       } catch (MalformedObjectNameException e) {
71         throw new RuntimeException("Bad object name: " + name, e);
72       } catch (Exception e) {
73         throw new RuntimeException(e);
74       }
75     }
76   }
77 
quote(String value)78   static String quote(String value) {
79     // JMX seems to have a comma bug.
80     return ObjectName.quote(value).replace(',', ';');
81   }
82 
83   /** Run with no arguments for usage instructions. */
main(String[] args)84   public static void main(String[] args) throws Exception {
85     if (args.length != 1) {
86       System.err.println(
87           "Usage: java -Dcom.sun.management.jmxremote "
88               + Manager.class.getName()
89               + " [module class name]");
90       System.err.println("Then run 'jconsole' to connect.");
91       System.exit(1);
92     }
93 
94     Module module = (Module) Class.forName(args[0]).newInstance();
95     Injector injector = Guice.createInjector(module);
96 
97     manage(args[0], injector);
98 
99     System.out.println("Press Ctrl+C to exit...");
100 
101     // Sleep forever.
102     Thread.sleep(Long.MAX_VALUE);
103   }
104 }
105