1 package org.kuali.common.jute.runtime;
2
3 import static java.lang.management.ManagementFactory.getOperatingSystemMXBean;
4 import static org.kuali.common.jute.base.Optionals.fromNegativeToAbsent;
5
6 import java.util.List;
7
8 import javax.inject.Provider;
9
10 import org.kuali.common.jute.runtime.annotation.GarbageCollectionEvents;
11 import org.kuali.common.jute.runtime.annotation.ProcessId;
12 import org.kuali.common.jute.runtime.annotation.Processors;
13 import org.kuali.common.jute.runtime.annotation.SystemLoadAverage;
14
15 import com.google.common.base.Optional;
16 import com.google.inject.AbstractModule;
17 import com.google.inject.TypeLiteral;
18
19 public class RuntimeModule extends AbstractModule {
20
21 @Override
22 protected void configure() {
23 bindConstant().annotatedWith(Processors.class).to(Runtime.getRuntime().availableProcessors());
24 bind(Memory.class).toProvider(MemoryProvider.INSTANCE);
25 bind(Uptime.class).toProvider(UptimeProvider.INSTANCE);
26 bind(new TypeLiteral<Optional<Integer>>() {}).annotatedWith(ProcessId.class).toProvider(ProcessIdProvider.INSTANCE).asEagerSingleton();
27 bind(ClassLoading.class).toProvider(ClassLoadingProvider.INSTANCE);
28 bind(Threads.class).toProvider(ThreadsProvider.INSTANCE);
29 bind(new TypeLiteral<List<GarbageCollectionEvent>>() {}).annotatedWith(GarbageCollectionEvents.class).toProvider(GarbageCollectionEventsProvider.INSTANCE);
30 bind(new TypeLiteral<Optional<Double>>() {}).annotatedWith(SystemLoadAverage.class).toProvider(SystemLoadAverageProvider.INSTANCE);
31 bind(VirtualRuntime.class).toProvider(VirtualRuntime.Builder.class);
32 }
33
34 private enum MemoryProvider implements Provider<Memory> {
35 INSTANCE;
36 @Override
37 public Memory get() {
38 return Memory.build();
39 }
40 }
41
42 private enum SystemLoadAverageProvider implements Provider<Optional<Double>> {
43 INSTANCE;
44 @Override
45 public Optional<Double> get() {
46 return fromNegativeToAbsent(getOperatingSystemMXBean().getSystemLoadAverage());
47 }
48 }
49
50 private enum UptimeProvider implements Provider<Uptime> {
51 INSTANCE;
52 @Override
53 public Uptime get() {
54 return Uptime.build();
55 }
56 }
57
58 private enum ClassLoadingProvider implements Provider<ClassLoading> {
59 INSTANCE;
60 @Override
61 public ClassLoading get() {
62 return ClassLoading.build();
63 }
64 }
65
66 private enum GarbageCollectionEventsProvider implements Provider<List<GarbageCollectionEvent>> {
67 INSTANCE;
68 @Override
69 public List<GarbageCollectionEvent> get() {
70 return GarbageCollectionEvent.buildAll();
71 }
72 }
73
74 private enum ThreadsProvider implements Provider<Threads> {
75 INSTANCE;
76 @Override
77 public Threads get() {
78 return Threads.build();
79 }
80 }
81
82 }