Esempio n. 1
0
 public static void main(String[] args) {
   int status = 0;
   Ice.Communicator ic = null;
   try {
     // 初使化连接,args可以传一些初使化参数,如连接超时时间,初使化客户连接池的数量等
     ic = Ice.Util.initialize(args);
     // 创建名为SimplePrinterAdapter的适配器,并要求适配器使 用缺省的协议(TCP/IP侦听端口为10000的请求)
     Ice.ObjectAdapter adapter =
         ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
     // 实例化一个PrinterI对象,为Printer接口创建一个服务对象
     Ice.Object object = new PrinterI();
     // 将服务单元增加到适配器中,并给服务对象指定名称为 SimplePrinter,该名称用于唯一确定一个服务单元
     adapter.add(object, Ice.Util.stringToIdentity("SimplePrinter"));
     // 激活适配器,这样做的好处是可以等到所有资源就位后再触发
     adapter.activate();
     // 让服务在退出之前,一直持续对请求的监听
     ic.waitForShutdown();
   } catch (Ice.LocalException e) {
     e.printStackTrace();
     status = 1;
   } catch (Exception e) {
     System.err.println(e.getMessage());
     status = 1;
   } finally {
     if (ic != null) ic.destroy();
   }
   System.exit(status);
 }
Esempio n. 2
0
 public void run() {
   try {
     communicator().destroy();
   } catch (Ice.LocalException ex) {
     ex.printStackTrace();
   }
 }
Esempio n. 3
0
  @Override
  public int run(String[] args) {
    if (args.length > 0) {
      System.err.println(appName() + ": too many arguments");
      return 1;
    }

    //
    // Since this is an interactive demo we want to clear the
    // Application installed interrupt callback and install our
    // own shutdown hook.
    //
    setInterruptHook(new ShutdownHook());

    //
    // First we try to connect to the object with the `hello'
    // identity. If it's not registered with the registry, we
    // search for an object with the ::Demo::Hello type.
    //
    HelloPrx hello = null;
    try {
      hello = HelloPrxHelper.checkedCast(communicator().stringToProxy("hello"));
    } catch (Ice.NotRegisteredException ex) {
      IceGrid.QueryPrx query =
          IceGrid.QueryPrxHelper.checkedCast(communicator().stringToProxy("DemoIceGrid/Query"));
      hello = HelloPrxHelper.checkedCast(query.findObjectByType("::Demo::Hello"));
    }
    if (hello == null) {
      System.err.println("couldn't find a `::Demo::Hello' object");
      return 1;
    }

    menu();

    java.io.BufferedReader in =
        new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

    String line = null;
    do {
      try {
        System.out.print("==> ");
        System.out.flush();
        line = in.readLine();
        if (line == null) {
          break;
        }
        if (line.equals("t")) {
          hello.sayHello();
        } else if (line.equals("s")) {
          hello.shutdown();
        } else if (line.equals("x")) {
          // Nothing to do
        } else if (line.equals("?")) {
          menu();
        } else {
          System.out.println("unknown command `" + line + "'");
          menu();
        }
      } catch (java.io.IOException ex) {
        ex.printStackTrace();
      } catch (Ice.LocalException ex) {
        ex.printStackTrace();
      }
    } while (!line.equals("x"));

    return 0;
  }
Esempio n. 4
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;
  }
Esempio n. 5
0
  public int run(String[] args) {
    if (args.length > 0) {
      System.err.println(appName() + ": too many arguments");
      return 1;
    }

    //
    // Since this is an interactive demo we want to clear the
    // Application installed interrupt callback and install our
    // own shutdown hook.
    //
    setInterruptHook(new ShutdownHook());

    HelloPrx twoway =
        HelloPrxHelper.checkedCast(
            communicator()
                .propertyToProxy("Hello.Proxy")
                .ice_twoway()
                .ice_timeout(-1)
                .ice_secure(false));
    if (twoway == null) {
      System.err.println("invalid proxy");
      return 1;
    }
    HelloPrx oneway = (HelloPrx) twoway.ice_oneway();
    HelloPrx batchOneway = (HelloPrx) twoway.ice_batchOneway();
    HelloPrx datagram = (HelloPrx) twoway.ice_datagram();
    HelloPrx batchDatagram = (HelloPrx) twoway.ice_batchDatagram();

    boolean secure = false;
    int timeout = -1;
    int delay = 0;

    menu();

    java.io.BufferedReader in =
        new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

    String line = null;
    do {
      try {
        System.out.print("==> ");
        System.out.flush();
        line = in.readLine();
        if (line == null) {
          break;
        }
        if (line.equals("t")) {
          twoway.sayHello(delay);
        } else if (line.equals("o")) {
          oneway.sayHello(delay);
        } else if (line.equals("O")) {
          batchOneway.sayHello(delay);
        } else if (line.equals("d")) {
          if (secure) {
            System.out.println("secure datagrams are not supported");
          } else {
            datagram.sayHello(delay);
          }
        } else if (line.equals("D")) {
          if (secure) {
            System.out.println("secure datagrams are not supported");
          } else {
            batchDatagram.sayHello(delay);
          }
        } else if (line.equals("f")) {
          communicator().flushBatchRequests();
        } else if (line.equals("T")) {
          if (timeout == -1) {
            timeout = 2000;
          } else {
            timeout = -1;
          }

          twoway = (HelloPrx) twoway.ice_timeout(timeout);
          oneway = (HelloPrx) oneway.ice_timeout(timeout);
          batchOneway = (HelloPrx) batchOneway.ice_timeout(timeout);

          if (timeout == -1) {
            System.out.println("timeout is now switched off");
          } else {
            System.out.println("timeout is now set to 2000ms");
          }
        } else if (line.equals("P")) {
          if (delay == 0) {
            delay = 2500;
          } else {
            delay = 0;
          }

          if (delay == 0) {
            System.out.println("server delay is now deactivated");
          } else {
            System.out.println("server delay is now set to 2500ms");
          }
        } else if (line.equals("S")) {
          secure = !secure;

          twoway = (HelloPrx) twoway.ice_secure(secure);
          oneway = (HelloPrx) oneway.ice_secure(secure);
          batchOneway = (HelloPrx) batchOneway.ice_secure(secure);
          datagram = (HelloPrx) datagram.ice_secure(secure);
          batchDatagram = (HelloPrx) batchDatagram.ice_secure(secure);

          if (secure) {
            System.out.println("secure mode is now on");
          } else {
            System.out.println("secure mode is now off");
          }
        } else if (line.equals("s")) {
          twoway.shutdown();
        } else if (line.equals("x")) {
          // Nothing to do
        } else if (line.equals("?")) {
          menu();
        } else {
          System.out.println("unknown command `" + line + "'");
          menu();
        }
      } catch (java.io.IOException ex) {
        ex.printStackTrace();
      } catch (Ice.LocalException ex) {
        ex.printStackTrace();
      }
    } while (!line.equals("x"));

    return 0;
  }