Esempio n. 1
0
  //
  // Only for use by Ice.CommunicatorI
  //
  public Instance(Ice.Communicator communicator, Ice.InitializationData initData) {
    _state = StateActive;
    _initData = initData;

    try {
      if (_initData.properties == null) {
        _initData.properties = Ice.Util.createProperties();
      }

      synchronized (Instance.class) {
        if (!_oneOfDone) {
          String stdOut = _initData.properties.getProperty("Ice.StdOut");
          String stdErr = _initData.properties.getProperty("Ice.StdErr");

          java.io.PrintStream outStream = null;

          if (stdOut.length() > 0) {
            //
            // We need to close the existing stdout for JVM thread dump to go
            // to the new file
            //
            System.out.close();

            try {
              outStream = new java.io.PrintStream(new java.io.FileOutputStream(stdOut, true));
            } catch (java.io.FileNotFoundException ex) {
              throw new Ice.FileException(0, stdOut, ex);
            }

            System.setOut(outStream);
          }
          if (stdErr.length() > 0) {
            //
            // close for consistency with stdout
            //
            System.err.close();

            if (stdErr.equals(stdOut)) {
              System.setErr(outStream);
            } else {
              try {
                System.setErr(new java.io.PrintStream(new java.io.FileOutputStream(stdErr, true)));
              } catch (java.io.FileNotFoundException ex) {
                throw new Ice.FileException(0, stdErr, ex);
              }
            }
          }
          _oneOfDone = true;
        }
      }

      if (_initData.logger == null) {
        String logfile = _initData.properties.getProperty("Ice.LogFile");
        if (_initData.properties.getPropertyAsInt("Ice.UseSyslog") > 0
            && !System.getProperty("os.name").startsWith("Windows")) {
          if (logfile.length() != 0) {
            throw new Ice.InitializationException("Both syslog and file logger cannot be enabled.");
          }
          _initData.logger =
              new Ice.SysLoggerI(
                  _initData.properties.getProperty("Ice.ProgramName"),
                  _initData.properties.getPropertyWithDefault("Ice.SyslogFacility", "LOG_USER"));
        } else if (logfile.length() != 0) {
          _initData.logger =
              new Ice.LoggerI(_initData.properties.getProperty("Ice.ProgramName"), logfile);
        } else {
          _initData.logger = Ice.Util.getProcessLogger();
        }
      }

      _packages = validatePackages();

      _useApplicationClassLoader =
          _initData.properties.getPropertyAsInt("Ice.UseApplicationClassLoader") > 0;

      _traceLevels = new TraceLevels(_initData.properties);

      _defaultsAndOverrides = new DefaultsAndOverrides(_initData.properties, _initData.logger);

      _clientACM =
          new ACMConfig(
              _initData.properties,
              _initData.logger,
              "Ice.ACM.Client",
              new ACMConfig(
                  _initData.properties, _initData.logger, "Ice.ACM", new ACMConfig(false)));

      _serverACM =
          new ACMConfig(
              _initData.properties,
              _initData.logger,
              "Ice.ACM.Server",
              new ACMConfig(
                  _initData.properties, _initData.logger, "Ice.ACM", new ACMConfig(true)));

      {
        final int defaultMessageSizeMax = 1024;
        int num =
            _initData.properties.getPropertyAsIntWithDefault(
                "Ice.MessageSizeMax", defaultMessageSizeMax);
        if (num < 1 || num > 0x7fffffff / 1024) {
          _messageSizeMax = 0x7fffffff;
        } else {
          _messageSizeMax = num * 1024; // Property is in kilobytes, _messageSizeMax in bytes
        }
      }

      if (_initData.properties.getProperty("Ice.BatchAutoFlushSize").isEmpty()
          && !_initData.properties.getProperty("Ice.BatchAutoFlush").isEmpty()) {
        if (_initData.properties.getPropertyAsInt("Ice.BatchAutoFlush") > 0) {
          _batchAutoFlushSize = _messageSizeMax;
        } else {
          _batchAutoFlushSize = 0;
        }
      } else {
        int num =
            _initData.properties.getPropertyAsIntWithDefault("Ice.BatchAutoFlushSize", 1024); // 1MB
        if (num < 1) {
          _batchAutoFlushSize = num;
        } else if (num > 0x7fffffff / 1024) {
          _batchAutoFlushSize = 0x7fffffff;
        } else {
          _batchAutoFlushSize =
              num * 1024; // Property is in kilobytes, _batchAutoFlushSize in bytes
        }
      }

      _implicitContext =
          Ice.ImplicitContextI.create(_initData.properties.getProperty("Ice.ImplicitContext"));

      _routerManager = new RouterManager();

      _locatorManager = new LocatorManager(_initData.properties);

      _referenceFactory = new ReferenceFactory(this, communicator);

      _requestHandlerFactory = new RequestHandlerFactory(this);

      _proxyFactory = new ProxyFactory(this);

      boolean isIPv6Supported = Network.isIPv6Supported();
      boolean ipv4 = _initData.properties.getPropertyAsIntWithDefault("Ice.IPv4", 1) > 0;
      boolean ipv6 =
          _initData.properties.getPropertyAsIntWithDefault("Ice.IPv6", isIPv6Supported ? 1 : 0) > 0;
      if (!ipv4 && !ipv6) {
        throw new Ice.InitializationException("Both IPV4 and IPv6 support cannot be disabled.");
      } else if (ipv4 && ipv6) {
        _protocolSupport = Network.EnableBoth;
      } else if (ipv4) {
        _protocolSupport = Network.EnableIPv4;
      } else {
        _protocolSupport = Network.EnableIPv6;
      }
      _preferIPv6 = _initData.properties.getPropertyAsInt("Ice.PreferIPv6Address") > 0;

      _networkProxy = createNetworkProxy(_initData.properties, _protocolSupport);

      _endpointFactoryManager = new EndpointFactoryManager(this);

      ProtocolInstance tcpProtocolInstance =
          new ProtocolInstance(this, Ice.TCPEndpointType.value, "tcp", false);
      _endpointFactoryManager.add(new TcpEndpointFactory(tcpProtocolInstance));

      ProtocolInstance udpProtocolInstance =
          new ProtocolInstance(this, Ice.UDPEndpointType.value, "udp", false);
      _endpointFactoryManager.add(new UdpEndpointFactory(udpProtocolInstance));

      _pluginManager = new Ice.PluginManagerI(communicator, this);

      _outgoingConnectionFactory = new OutgoingConnectionFactory(communicator, this);

      _servantFactoryManager = new ObjectFactoryManager();

      _objectAdapterFactory = new ObjectAdapterFactory(this, communicator);

      _retryQueue = new RetryQueue(this);

      //
      // If Ice.ThreadInterruptSafe is set or we're running on Android all
      // IO is done on the background thread. For Android we use the queue
      // executor as Android doesn't allow any network invocations on the main
      // thread even if the call is non-blocking.
      //
      if (_initData.properties.getPropertyAsInt("Ice.ThreadInterruptSafe") > 0
          || Util.isAndroid()) {
        _queueExecutor =
            new QueueExecutor(
                _initData.properties,
                Util.createThreadName(_initData.properties, "Ice.BackgroundIO"));
        _queueExecutorService = new QueueExecutorService(_queueExecutor);

        // Caching message buffers is not supported with background IO.
        _cacheMessageBuffers = 0;
      } else {
        _cacheMessageBuffers =
            _initData.properties.getPropertyAsIntWithDefault("Ice.CacheMessageBuffers", 2);
      }
    } catch (Ice.LocalException ex) {
      destroy();
      throw ex;
    }
  }