예제 #1
0
  private synchronized void start(String service, String className, String[] args)
      throws FailureException {
    //
    // Instantiate the class.
    //
    ServiceInfo info = new ServiceInfo();
    info.name = service;
    info.status = StatusStopped;
    info.args = args;
    try {
      Class<?> c = IceInternal.Util.findClass(className, null);
      if (c == null) {
        FailureException e = new FailureException();
        e.reason = "ServiceManager: class " + className + " not found";
        throw e;
      }
      java.lang.Object obj = c.newInstance();
      try {
        info.service = (Service) obj;
      } catch (ClassCastException ex) {
        FailureException e = new FailureException();
        e.reason = "ServiceManager: class " + className + " does not implement IceBox.Service";
        throw e;
      }
    } catch (IllegalAccessException ex) {
      FailureException e = new FailureException();
      e.reason = "ServiceManager: unable to access default constructor in class " + className;
      e.initCause(ex);
      throw e;
    } catch (InstantiationException ex) {
      FailureException e = new FailureException();
      e.reason = "ServiceManager: unable to instantiate class " + className;
      e.initCause(ex);
      throw e;
    }

    //
    // Invoke Service::start().
    //
    try {
      //
      // If Ice.UseSharedCommunicator.<name> is defined, create a
      // communicator for the service. The communicator inherits
      // from the shared communicator properties. If it's not
      // defined, add the service properties to the shared
      // commnunicator property set.
      //
      Ice.Communicator communicator;
      if (_communicator.getProperties().getPropertyAsInt("IceBox.UseSharedCommunicator." + service)
          > 0) {
        assert (_sharedCommunicator != null);
        communicator = _sharedCommunicator;
      } else {
        //
        // Create the service properties. We use the communicator properties as the default
        // properties if IceBox.InheritProperties is set.
        //
        Ice.InitializationData initData = new Ice.InitializationData();
        initData.properties = createServiceProperties(service);
        Ice.StringSeqHolder serviceArgs = new Ice.StringSeqHolder(info.args);
        if (serviceArgs.value.length > 0) {
          //
          // Create the service properties with the given service arguments. This should
          // read the service config file if it's specified with --Ice.Config.
          //
          initData.properties = Ice.Util.createProperties(serviceArgs, initData.properties);

          //
          // Next, parse the service "<service>.*" command line options (the Ice command
          // line options were parsed by the createProperties above)
          //
          serviceArgs.value =
              initData.properties.parseCommandLineOptions(service, serviceArgs.value);
        }

        //
        // Clone the logger to assign a new prefix.
        //
        initData.logger =
            _logger.cloneWithPrefix(initData.properties.getProperty("Ice.ProgramName"));

        //
        // Remaining command line options are passed to the communicator. This is
        // necessary for Ice plug-in properties (e.g.: IceSSL).
        //
        info.communicator = Ice.Util.initialize(serviceArgs, initData);
        info.args = serviceArgs.value;
        communicator = info.communicator;
      }

      try {
        info.service.start(service, communicator, info.args);
        info.status = StatusStarted;

        //
        // There is no need to notify the observers since the 'start all'
        // (that indirectly calls this method) occurs before the creation of
        // the Server Admin object, and before the activation of the main
        // object adapter (so before any observer can be registered)
        //
      } catch (Throwable ex) {
        if (info.communicator != null) {
          try {
            info.communicator.shutdown();
            info.communicator.waitForShutdown();
          } catch (Ice.CommunicatorDestroyedException e) {
            //
            // Ignore, the service might have already destroyed
            // the communicator for its own reasons.
            //
          } catch (java.lang.Exception e) {
            java.io.StringWriter sw = new java.io.StringWriter();
            java.io.PrintWriter pw = new java.io.PrintWriter(sw);
            e.printStackTrace(pw);
            pw.flush();
            _logger.warning(
                "ServiceManager: exception in shutting down communicator for service "
                    + service
                    + "\n"
                    + sw.toString());
          }

          try {
            info.communicator.destroy();
          } catch (java.lang.Exception e) {
            java.io.StringWriter sw = new java.io.StringWriter();
            java.io.PrintWriter pw = new java.io.PrintWriter(sw);
            e.printStackTrace(pw);
            pw.flush();
            _logger.warning(
                "ServiceManager: exception in destroying communciator for service"
                    + service
                    + "\n"
                    + sw.toString());
          }
        }
        throw ex;
      }

      _services.add(info);
    } catch (FailureException ex) {
      throw ex;
    } catch (Throwable ex) {
      FailureException e = new FailureException();
      e.reason = "ServiceManager: exception while starting service " + service + ": " + ex;
      e.initCause(ex);
      throw e;
    }
  }