KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > tools > jmx > Manager


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 JavaDoc;
25 import java.lang.management.ManagementFactory JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
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.
39    * Consider using the name of your root {@link Module} class as the domain.
40    */

41   public static void manage(
42       String JavaDoc domain,
43       Injector injector) {
44     manage(ManagementFactory.getPlatformMBeanServer(), domain, injector);
45   }
46
47   /**
48    * Registers all the bindings of an Injector with the given MBean server.
49    * Consider using the name of your root {@link Module} class as the domain.
50    */

51   public static void manage(MBeanServer JavaDoc server, String JavaDoc domain,
52       Injector injector) {
53     // Register each binding independently.
54
for (Binding<?> binding : injector.getBindings().values()) {
55       // Construct the name manually so we can ensure proper ordering of the
56
// key/value pairs.
57
StringBuilder JavaDoc name = new StringBuilder JavaDoc();
58       name.append(domain).append(":");
59       Key<?> key = binding.getKey();
60       name.append("type=").append(quote(key.getTypeLiteral().toString()));
61       Annotation JavaDoc annotation = key.getAnnotation();
62       if (annotation != null) {
63         name.append(",annotation=").append(quote(annotation.toString()));
64       }
65       else {
66         Class JavaDoc<? extends Annotation JavaDoc> annotationType = key.getAnnotationType();
67         if (annotationType != null) {
68           name.append(",annotation=")
69               .append(quote("@" + annotationType.getName()));
70         }
71       }
72
73       try {
74         server.registerMBean(new ManagedBinding(binding),
75             new ObjectName JavaDoc(name.toString()));
76       }
77       catch (MalformedObjectNameException JavaDoc e) {
78         throw new RuntimeException JavaDoc("Bad object name: "
79             + name.toString(), e);
80       }
81       catch (Exception JavaDoc e) {
82         throw new RuntimeException JavaDoc(e);
83       }
84     }
85   }
86
87   static String JavaDoc quote(String JavaDoc value) {
88     // JMX seems to have a comma bug.
89
return ObjectName.quote(value).replace(',', ';');
90   }
91
92   /**
93    * Run with no arguments for usage instructions.
94    */

95   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
96     if (args.length != 1) {
97       System.err.println("Usage: java -Dcom.sun.management.jmxremote "
98           + Manager.class.getName() + " [module class name]");
99       System.err.println("Then run 'jconsole' to connect.");
100       System.exit(1);
101     }
102
103     Module module = (Module) Class.forName(args[0]).newInstance();
104     Injector injector = Guice.createInjector(module);
105
106     manage(args[0], injector);
107
108     System.out.println("Press Ctrl+C to exit...");
109
110     // Sleep forever.
111
Thread.sleep(Long.MAX_VALUE);
112   }
113 }
114
Popular Tags