예제 #1
0
  public int run() {
    try {
      Ice.Properties properties = _communicator.getProperties();

      //
      // Create an object adapter. Services probably should NOT share
      // this object adapter, as the endpoint(s) for this object adapter
      // will most likely need to be firewalled for security reasons.
      //
      Ice.ObjectAdapter adapter = null;
      if (!properties.getProperty("IceBox.ServiceManager.Endpoints").equals("")) {
        adapter = _communicator.createObjectAdapter("IceBox.ServiceManager");

        Ice.Identity identity = new Ice.Identity();
        identity.category = properties.getPropertyWithDefault("IceBox.InstanceName", "IceBox");
        identity.name = "ServiceManager";
        adapter.add(this, identity);
      }

      //
      // Parse the property set with the prefix "IceBox.Service.". These
      // properties should have the following format:
      //
      // IceBox.Service.Foo=Package.Foo [args]
      //
      // We parse the service properties specified in IceBox.LoadOrder
      // first, then the ones from remaining services.
      //
      final String prefix = "IceBox.Service.";
      java.util.Map<String, String> services = properties.getPropertiesForPrefix(prefix);
      String[] loadOrder = properties.getPropertyAsList("IceBox.LoadOrder");
      java.util.List<StartServiceInfo> servicesInfo = new java.util.ArrayList<StartServiceInfo>();
      for (String name : loadOrder) {
        if (name.length() > 0) {
          String key = prefix + name;
          String value = services.get(key);
          if (value == null) {
            FailureException ex = new FailureException();
            ex.reason = "ServiceManager: no service definition for `" + name + "'";
            throw ex;
          }
          servicesInfo.add(new StartServiceInfo(name, value, _argv));
          services.remove(key);
        }
      }
      for (java.util.Map.Entry<String, String> p : services.entrySet()) {
        String name = p.getKey().substring(prefix.length());
        String value = p.getValue();
        servicesInfo.add(new StartServiceInfo(name, value, _argv));
      }

      //
      // Check if some services are using the shared communicator in which
      // case we create the shared communicator now with a property set which
      // is the union of all the service properties (services which are using
      // the shared communicator).
      //
      if (properties.getPropertiesForPrefix("IceBox.UseSharedCommunicator.").size() > 0) {
        Ice.InitializationData initData = new Ice.InitializationData();
        initData.properties = createServiceProperties("SharedCommunicator");
        for (StartServiceInfo service : servicesInfo) {
          if (properties.getPropertyAsInt("IceBox.UseSharedCommunicator." + service.name) <= 0) {
            continue;
          }

          //
          // Load the service properties using the shared communicator properties as
          // the default properties.
          //
          Ice.StringSeqHolder serviceArgs = new Ice.StringSeqHolder(service.args);
          Ice.Properties svcProperties =
              Ice.Util.createProperties(serviceArgs, initData.properties);
          service.args = serviceArgs.value;

          //
          // Erase properties from the shared communicator which don't exist in the
          // service properties (which include the shared communicator properties
          // overriden by the service properties).
          //
          java.util.Map<String, String> allProps = initData.properties.getPropertiesForPrefix("");
          for (String key : allProps.keySet()) {
            if (svcProperties.getProperty(key).length() == 0) {
              initData.properties.setProperty(key, "");
            }
          }

          //
          // Add the service properties to the shared communicator properties.
          //
          for (java.util.Map.Entry<String, String> p :
              svcProperties.getPropertiesForPrefix("").entrySet()) {
            initData.properties.setProperty(p.getKey(), p.getValue());
          }

          //
          // Parse <service>.* command line options (the Ice command line options
          // were parsed by the createProperties above)
          //
          service.args = initData.properties.parseCommandLineOptions(service.name, service.args);
        }
        _sharedCommunicator = Ice.Util.initialize(initData);
      }

      for (StartServiceInfo s : servicesInfo) {
        start(s.name, s.className, s.args);
      }

      //
      // We may want to notify external scripts that the services
      // have started. This is done by defining the property:
      //
      // IceBox.PrintServicesReady=bundleName
      //
      // Where bundleName is whatever you choose to call this set of
      // services. It will be echoed back as "bundleName ready".
      //
      // This must be done after start() has been invoked on the
      // services.
      //
      String bundleName = properties.getProperty("IceBox.PrintServicesReady");
      if (bundleName.length() > 0) {
        System.out.println(bundleName + " ready");
      }

      //
      // Don't move after the adapter activation. This allows
      // applications to wait for the service manager to be
      // reachable before sending a signal to shutdown the
      // IceBox.
      //
      Ice.Application.shutdownOnInterrupt();

      //
      // Register "this" as a facet to the Admin object and
      // create Admin object
      //
      try {
        _communicator.addAdminFacet(this, "IceBox.ServiceManager");

        //
        // Add a Properties facet for each service
        //
        for (ServiceInfo info : _services) {
          Ice.Communicator communicator =
              info.communicator != null ? info.communicator : _sharedCommunicator;
          _communicator.addAdminFacet(
              new PropertiesAdminI(communicator.getProperties()),
              "IceBox.Service." + info.name + ".Properties");
        }

        _communicator.getAdmin();
      } catch (Ice.ObjectAdapterDeactivatedException ex) {
        //
        // Expected if the communicator has been shutdown.
        //
      }

      //
      // Start request dispatching after we've started the services.
      //
      if (adapter != null) {
        try {
          adapter.activate();
        } catch (Ice.ObjectAdapterDeactivatedException ex) {
          //
          // Expected if the communicator has been shutdown.
          //
        }
      }

      _communicator.waitForShutdown();
      Ice.Application.defaultInterrupt();

      //
      // Invoke stop() on the services.
      //
      stopAll();
    } catch (FailureException ex) {
      java.io.StringWriter sw = new java.io.StringWriter();
      java.io.PrintWriter pw = new java.io.PrintWriter(sw);
      pw.println(ex.reason);
      ex.printStackTrace(pw);
      pw.flush();
      _logger.error(sw.toString());
      stopAll();
      return 1;
    } catch (Ice.LocalException ex) {
      java.io.StringWriter sw = new java.io.StringWriter();
      java.io.PrintWriter pw = new java.io.PrintWriter(sw);
      ex.printStackTrace(pw);
      pw.flush();
      _logger.error("ServiceManager: " + ex + "\n" + sw.toString());
      stopAll();
      return 1;
    } catch (java.lang.Exception ex) {
      java.io.StringWriter sw = new java.io.StringWriter();
      java.io.PrintWriter pw = new java.io.PrintWriter(sw);
      ex.printStackTrace(pw);
      pw.flush();
      _logger.error("ServiceManager: unknown exception\n" + sw.toString());
      stopAll();
      return 1;
    }

    return 0;
  }