Permalink
Browse files

monitors are always inheritable

  • Loading branch information...
1 parent 90efcf1 commit 3e7abdd1edc8c5835750c8d6db37e087ad57fa7b @ropalka ropalka committed Jan 4, 2013
@@ -135,6 +135,18 @@ public DelegatingServiceBuilder(final ServiceBuilder<T> delegate) {
return this;
}
+ @Override
+ public ServiceBuilder<T> addMonitor(final StabilityMonitor monitor) {
+ delegate.addMonitor(monitor);
+ return this;
+ }
+
+ @Override
+ public ServiceBuilder<T> addMonitors(final StabilityMonitor... monitors) {
+ delegate.addMonitors(monitors);
+ return this;
+ }
+
/** {@inheritDoc} */
public ServiceBuilder<T> addListener(final ServiceListener<? super T> listener) {
delegate.addListener(listener);
@@ -36,6 +36,7 @@
* @param <T> the service type
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
+ * @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public interface ServiceBuilder<T> {
@@ -188,6 +189,22 @@
* @return this builder
*/
ServiceBuilder<T> addInjection(Injector<? super T> target);
+
+ /**
+ * Add a service stability monitor that will be added to this service.
+ *
+ * @param monitor the stability monitor to add to the service
+ * @return this builder
+ */
+ ServiceBuilder<T> addMonitor(final StabilityMonitor monitor);
+
+ /**
+ * Add service stability monitors that will be added to this service.
+ *
+ * @param monitors a list of stability monitors to add to the service
+ * @return this builder
+ */
+ ServiceBuilder<T> addMonitors(final StabilityMonitor... monitors);
/**
* Add a service listener that will be added to this service.
@@ -51,6 +51,7 @@
private final ServiceName serviceName;
private ServiceController.Mode initialMode = ServiceController.Mode.ACTIVE;
private final Set<ServiceName> aliases = new HashSet<ServiceName>(0);
+ private final IdentityHashSet<StabilityMonitor> monitors = new IdentityHashSet<StabilityMonitor>();
private final Map<ServiceName, Dependency> dependencies = new HashMap<ServiceName, Dependency>(0);
private final Map<ServiceListener<? super T>, ServiceListener.Inheritance> listeners = new IdentityHashMap<ServiceListener<? super T>, ServiceListener.Inheritance>(0);
private final List<ValueInjection<?>> valueInjections = new ArrayList<ValueInjection<?>>(0);
@@ -236,6 +237,22 @@ private Dependency doAddDependency(final ServiceName name, final DependencyType
outInjections.add(target);
return this;
}
+
+ @Override
+ public ServiceBuilder<T> addMonitor(final StabilityMonitor monitor) {
+ checkAlreadyInstalled();
+ monitors.add(monitor);
+ return this;
+ }
+
+ @Override
+ public ServiceBuilder<T> addMonitors(final StabilityMonitor... monitors) {
+ checkAlreadyInstalled();
+ for (StabilityMonitor monitor : monitors) {
+ this.monitors.add(monitor);
+ }
+ return this;
+ }
@Override
public ServiceBuilderImpl<T> addListener(final ServiceListener<? super T> listener) {
@@ -281,6 +298,11 @@ private Dependency doAddDependency(final ServiceName name, final DependencyType
return addListenerNoCheck(inheritance, serviceListeners);
}
+ ServiceBuilderImpl<T> addMonitorNoCheck(final StabilityMonitor monitor) {
+ monitors.add(monitor);
+ return this;
+ }
+
ServiceBuilderImpl<T> addListenerNoCheck(final ServiceListener.Inheritance inheritance, final Collection<? extends ServiceListener<? super T>> serviceListeners) {
for (ServiceListener<? super T> listener : serviceListeners) {
listeners.put(listener, inheritance);
@@ -326,10 +348,14 @@ ServiceName getName() {
return dependencies;
}
+ Set<StabilityMonitor> getMonitors() {
+ return monitors;
+ }
+
Map<ServiceListener<? super T>,ServiceListener.Inheritance> getListeners() {
return listeners;
}
-
+
List<ValueInjection<?>> getValueInjections() {
return valueInjections;
}
@@ -582,6 +582,10 @@ void apply(ServiceBuilderImpl<?> builder, ServiceControllerImpl<?> parent, boole
}
if (first) builder.addListenerNoCheck(ServiceListener.Inheritance.NONE, once);
builder.addListenerNoCheck(ServiceListener.Inheritance.ALL, inherited);
+ Set<StabilityMonitor> monitors = parent.getMonitors();
+ for (final StabilityMonitor monitor : monitors) {
+ builder.addMonitorNoCheck(monitor);
+ }
parentParent = parent.getParent();
}
if (parentParent != null) {
@@ -650,7 +654,7 @@ void apply(ServiceBuilderImpl<?> builder) {
// Next create the actual controller
final ServiceControllerImpl<T> instance = new ServiceControllerImpl<T>(serviceBuilder.getServiceValue(),
dependencies, valueInjectionArray, outInjectionArray, primaryRegistration, aliasRegistrations,
- serviceBuilder.getListeners(), serviceBuilder.getParent());
+ serviceBuilder.getMonitors(), serviceBuilder.getListeners(), serviceBuilder.getParent());
boolean ok = false;
try {
serviceValue.setValue(instance);
@@ -183,7 +183,7 @@
static final int MAX_DEPENDENCIES = (1 << 14) - 1;
- ServiceControllerImpl(final Value<? extends Service<S>> serviceValue, final Dependency[] dependencies, final ValueInjection<?>[] injections, final ValueInjection<?>[] outInjections, final ServiceRegistrationImpl primaryRegistration, final ServiceRegistrationImpl[] aliasRegistrations, final Map<? extends ServiceListener<? super S>, ServiceListener.Inheritance> listeners, final ServiceControllerImpl<?> parent) {
+ ServiceControllerImpl(final Value<? extends Service<S>> serviceValue, final Dependency[] dependencies, final ValueInjection<?>[] injections, final ValueInjection<?>[] outInjections, final ServiceRegistrationImpl primaryRegistration, final ServiceRegistrationImpl[] aliasRegistrations, final Set<StabilityMonitor> monitors, final Map<? extends ServiceListener<? super S>, ServiceListener.Inheritance> listeners, final ServiceControllerImpl<?> parent) {
assert dependencies.length <= MAX_DEPENDENCIES;
this.serviceValue = serviceValue;
this.dependencies = dependencies;
@@ -192,7 +192,7 @@
this.primaryRegistration = primaryRegistration;
this.aliasRegistrations = aliasRegistrations;
this.listeners = new IdentityHashMap<ServiceListener<? super S>, ServiceListener.Inheritance>(listeners);
- this.monitors = new IdentityHashSet<StabilityMonitor>();
+ this.monitors = new IdentityHashSet<StabilityMonitor>(monitors);
this.parent = parent;
int depCount = dependencies.length;
unstartedDependencies = 0;
@@ -1498,6 +1498,11 @@ void removeMonitor(final StabilityMonitor stabilityMonitor) {
monitors.remove(stabilityMonitor);
}
}
+
+ Set<StabilityMonitor> getMonitors() {
+ assert holdsLock(this);
+ return monitors;
+ }
private enum ListenerNotification {
/** Notify the listener that is has been added. */
@@ -37,11 +37,11 @@
public final class StabilityMonitor {
private final Object lock = new Object();
- private int unstableServices;
private final Set<ServiceController<?>> problems = new IdentityHashSet<ServiceController<?>>();
private final Set<ServiceController<?>> failed = new IdentityHashSet<ServiceController<?>>();
private final ArrayList<ServiceController<?>> controllers = new ArrayList<ServiceController<?>>();
-
+ private int unstableServices;
+
public void addController(ServiceController<?> controller) {
final ServiceControllerImpl<?> serviceController = (ServiceControllerImpl<?>) controller;
serviceController.addMonitor(this);
@@ -188,6 +188,6 @@ public Object getValue() throws IllegalStateException, IllegalArgumentException
assertController(controller1.getName(), controller1);
assertController(controller2.getName(), controller2);
assertTrue(problem.isEmpty());
- assertTrue(failed.isEmpty());
+ assertTrue(failed.size() == 1);
}
}

0 comments on commit 3e7abdd

Please sign in to comment.