Clover Coverage Report - guice
Coverage timestamp: Wed Jan 7 2009 19:09:55 CST
../../../../../img/srcFileCovDistChart0.png 84% of files have more coverage
26   113   10   6.5
6   65   0.38   4
4     2.5  
1    
 
  Manager       Line # 35 26 10 36 0% 0.0
 
No Tests
 
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.
39    * Consider using the name of your root {@link Module} class as the domain.
40    */
 
41  0 toggle public static void manage(
42    String domain,
43    Injector injector) {
44  0 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  0 toggle public static void manage(MBeanServer server, String domain,
52    Injector injector) {
53    // Register each binding independently.
54  0 for (Binding<?> binding : injector.getBindings().values()) {
55    // Construct the name manually so we can ensure proper ordering of the
56    // key/value pairs.
57  0 StringBuilder name = new StringBuilder();
58  0 name.append(domain).append(":");
59  0 Key<?> key = binding.getKey();
60  0 name.append("type=").append(quote(key.getTypeLiteral().toString()));
61  0 Annotation annotation = key.getAnnotation();
62  0 if (annotation != null) {
63  0 name.append(",annotation=").append(quote(annotation.toString()));
64    }
65    else {
66  0 Class<? extends Annotation> annotationType = key.getAnnotationType();
67  0 if (annotationType != null) {
68  0 name.append(",annotation=")
69    .append(quote("@" + annotationType.getName()));
70    }
71    }
72   
73  0 try {
74  0 server.registerMBean(new ManagedBinding(binding),
75    new ObjectName(name.toString()));
76    }
77    catch (MalformedObjectNameException e) {
78  0 throw new RuntimeException("Bad object name: "
79    + name.toString(), e);
80    }
81    catch (Exception e) {
82  0 throw new RuntimeException(e);
83    }
84    }
85    }
86   
 
87  0 toggle static String quote(String value) {
88    // JMX seems to have a comma bug.
89  0 return ObjectName.quote(value).replace(',', ';');
90    }
91   
92    /**
93    * Run with no arguments for usage instructions.
94    */
 
95  0 toggle public static void main(String[] args) throws Exception {
96  0 if (args.length != 1) {
97  0 System.err.println("Usage: java -Dcom.sun.management.jmxremote "
98    + Manager.class.getName() + " [module class name]");
99  0 System.err.println("Then run 'jconsole' to connect.");
100  0 System.exit(1);
101    }
102   
103  0 Module module = (Module) Class.forName(args[0]).newInstance();
104  0 Injector injector = Guice.createInjector(module);
105   
106  0 manage(args[0], injector);
107   
108  0 System.out.println("Press Ctrl+C to exit...");
109   
110    // Sleep forever.
111  0 Thread.sleep(Long.MAX_VALUE);
112    }
113    }