Пример #1
0
 public void run() {
   try {
     communicator().destroy();
   } catch (Ice.LocalException ex) {
     ex.printStackTrace();
   }
 }
Пример #2
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);
 }
Пример #3
0
  private void setServerProcessProxy(Ice.ObjectAdapter adminAdapter, Ice.Identity adminIdentity) {
    Ice.ObjectPrx admin = adminAdapter.createProxy(adminIdentity);
    Ice.LocatorPrx locator = adminAdapter.getLocator();
    String serverId = _initData.properties.getProperty("Ice.Admin.ServerId");

    if (locator != null && !serverId.isEmpty()) {
      Ice.ProcessPrx process = Ice.ProcessPrxHelper.uncheckedCast(admin.ice_facet("Process"));
      try {
        //
        // Note that as soon as the process proxy is registered, the communicator might be
        // shutdown by a remote client and admin facets might start receiving calls.
        //
        locator.getRegistry().setServerProcessProxy(serverId, process);
      } catch (Ice.ServerNotFoundException ex) {
        if (_traceLevels.location >= 1) {
          StringBuilder s = new StringBuilder(128);
          s.append("couldn't register server `");
          s.append(serverId);
          s.append("' with the locator registry:\n");
          s.append("the server is not known to the locator registry");
          _initData.logger.trace(_traceLevels.locationCat, s.toString());
        }

        throw new Ice.InitializationException(
            "Locator knows nothing about server `" + serverId + "'");
      } catch (Ice.LocalException ex) {
        if (_traceLevels.location >= 1) {
          StringBuilder s = new StringBuilder(128);
          s.append("couldn't register server `");
          s.append(serverId);
          s.append("' with the locator registry:\n");
          s.append(ex.toString());
          _initData.logger.trace(_traceLevels.locationCat, s.toString());
        }
        throw ex;
      }

      if (_traceLevels.location >= 1) {
        StringBuilder s = new StringBuilder(128);
        s.append("registered server `");
        s.append(serverId);
        s.append("' with the locator registry");
        _initData.logger.trace(_traceLevels.locationCat, s.toString());
      }
    }
  }
Пример #4
0
 public void end_modifyTransport(Ice.AsyncResult __result) throws GenericError {
   Ice.AsyncResult.__check(__result, this, __modifyTransport_name);
   boolean __ok = __result.__wait();
   try {
     if (!__ok) {
       try {
         __result.__throwUserException();
       } catch (GenericError __ex) {
         throw __ex;
       } catch (Ice.UserException __ex) {
         throw new Ice.UnknownUserException(__ex.ice_name(), __ex);
       }
     }
     __result.__readEmptyParams();
   } catch (Ice.LocalException ex) {
     Ice.Instrumentation.InvocationObserver __obsv = __result.__getObserver();
     if (__obsv != null) {
       __obsv.failed(ex.ice_name());
     }
     throw ex;
   }
 }
Пример #5
0
 public byte[] end_abortStream(Ice.AsyncResult __result) {
   Ice.AsyncResult.__check(__result, this, __abortStream_name);
   boolean __ok = __result.__wait();
   try {
     if (!__ok) {
       try {
         __result.__throwUserException();
       } catch (Ice.UserException __ex) {
         throw new Ice.UnknownUserException(__ex.ice_name(), __ex);
       }
     }
     IceInternal.BasicStream __is = __result.__startReadParams();
     byte[] __ret;
     __ret = MarkerHelper.read(__is);
     __result.__endReadParams();
     return __ret;
   } catch (Ice.LocalException ex) {
     Ice.Instrumentation.InvocationObserver __obsv = __result.__getObserver();
     if (__obsv != null) {
       __obsv.failed(ex.ice_name());
     }
     throw ex;
   }
 }
Пример #6
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;
  }
Пример #7
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;
  }
Пример #8
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;
  }