public ModelController createController(final ModelNode model, final Setup registration)
      throws InterruptedException {
    final ServiceController<?> existingController =
        serviceContainer.getService(ServiceName.of("ModelController"));
    if (existingController != null) {
      final CountDownLatch latch = new CountDownLatch(1);
      existingController.addListener(
          new AbstractServiceListener<Object>() {
            public void listenerAdded(ServiceController<?> serviceController) {
              serviceController.setMode(ServiceController.Mode.REMOVE);
            }

            public void transition(
                ServiceController<?> serviceController, ServiceController.Transition transition) {
              if (transition.equals(ServiceController.Transition.REMOVING_to_REMOVED)) {
                latch.countDown();
              }
            }
          });
      latch.await();
    }

    ServiceTarget target = serviceContainer.subTarget();
    ControlledProcessState processState = new ControlledProcessState(true);
    ModelControllerService svc = new ModelControllerService(processState, registration, model);
    ServiceBuilder<ModelController> builder =
        target.addService(ServiceName.of("ModelController"), svc);
    builder.install();
    svc.latch.await();
    ModelController controller = svc.getValue();
    ModelNode setup = Util.getEmptyOperation("setup", new ModelNode());
    controller.execute(setup, null, null, null);
    processState.setRunning();
    return controller;
  }
Пример #2
0
  private void doRemove(final ServiceController<?> controller) {
    final Step removalStep = activeStep;
    controller.addListener(
        new AbstractServiceListener<Object>() {
          public void listenerAdded(final ServiceController<?> controller) {
            final Map<ServiceName, ServiceController<?>> map = realRemovingControllers;
            synchronized (map) {
              map.put(controller.getName(), controller);
              controller.setMode(ServiceController.Mode.REMOVE);
            }
          }

          public void transition(
              final ServiceController<?> controller,
              final ServiceController.Transition transition) {
            switch (transition) {
              case REMOVING_to_REMOVED:
              case REMOVING_to_DOWN:
                {
                  final Map<ServiceName, ServiceController<?>> map = realRemovingControllers;
                  synchronized (map) {
                    ServiceName name = controller.getName();
                    if (map.get(name) == controller) {
                      map.remove(controller.getName());
                      removalSteps.put(name, removalStep);
                      map.notifyAll();
                    }
                  }
                  break;
                }
            }
          }
        });
  }
  @SuppressWarnings("unchecked")
  @Override
  public final Object getObjectInstance(
      Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    final Reference reference = asReference(obj);
    final ServiceNameRefAdr nameAdr = (ServiceNameRefAdr) reference.get("srof");
    if (nameAdr == null) {
      throw new NamingException("Invalid context reference.  Not a 'srof' reference.");
    }
    final ServiceName serviceName = (ServiceName) nameAdr.getContent();
    final ServiceController<?> controller;
    try {
      controller = serviceRegistry.getRequiredService(serviceName);
    } catch (ServiceNotFoundException e) {
      throw new NamingException("Could not resolve service " + serviceName);
    }

    ServiceReferenceListener listener = new ServiceReferenceListener();
    controller.addListener(listener);
    synchronized (listener) {
      // if we are interrupted just let the exception propagate for now
      while (!listener.finished) {
        try {
          listener.wait();
        } catch (InterruptedException e) {
          throw new NamingException(
              "Thread interrupted while retrieving service reference for service " + serviceName);
        }
      }
    }
    switch (listener.getState()) {
      case UP:
        return getObjectInstance(listener.getValue(), obj, name, nameCtx, environment);
      case START_FAILED:
        throw new NamingException(
            "Could not resolve service reference to "
                + serviceName
                + " in factory "
                + getClass().getName()
                + ". Service was in state START_FAILED.");
      case REMOVED:
        throw new NamingException(
            "Could not resolve service reference to "
                + serviceName
                + " in factory "
                + getClass().getName()
                + ". Service was in state START_FAILED.");
    }
    // we should never get here, as the listener should not notify unless the state was one of the
    // above
    throw new NamingException(
        "Could not resolve service reference to "
            + serviceName
            + " in factory "
            + getClass().getName()
            + ". This is a bug in ServiceReferenceObjectFactory. State was"
            + listener.getState());
  }
  public void unbind(final Name name) throws NamingException {
    requireOwner();
    final ServiceName bindName = buildServiceName(name);

    final ServiceController<?> controller = getServiceRegistry().getService(bindName);
    if (controller == null) {
      throw MESSAGES.cannotResolveService(bindName);
    }

    final UnbindListener listener = new UnbindListener();
    controller.addListener(listener);
    try {
      listener.await();
    } catch (Exception e) {
      throw namingException("Failed to unbind [" + bindName + "]", e);
    }
  }