Beispiel #1
0
  void associate(
      ObjectStore store, com.sleepycat.db.Transaction txn, boolean createDb, boolean populateIndex)
      throws com.sleepycat.db.DatabaseException, java.io.FileNotFoundException {
    assert (txn != null);
    _store = store;

    _dbName = EvictorI.indexPrefix + store.dbName() + "." + _name;

    com.sleepycat.db.SecondaryConfig config = new com.sleepycat.db.SecondaryConfig();
    config.setAllowCreate(createDb);
    config.setAllowPopulate(populateIndex);
    config.setSortedDuplicates(true);
    config.setType(com.sleepycat.db.DatabaseType.BTREE);
    config.setKeyCreator(this);

    Ice.Properties properties = store.evictor().communicator().getProperties();
    String propPrefix = "Freeze.Evictor." + store.evictor().filename() + ".";

    int btreeMinKey = properties.getPropertyAsInt(propPrefix + _dbName + ".BtreeMinKey");
    if (btreeMinKey > 2) {
      if (store.evictor().trace() >= 1) {
        store
            .evictor()
            .communicator()
            .getLogger()
            .trace(
                "Freeze.Evictor",
                "Setting \""
                    + store.evictor().filename()
                    + "."
                    + _dbName
                    + "\"'s btree minkey to "
                    + btreeMinKey);
      }
      config.setBtreeMinKey(btreeMinKey);
    }

    boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0;
    if (checksum) {
      //
      // No tracing
      //
      config.setChecksum(true);
    }

    //
    // Can't change page size
    //

    _db =
        _store
            .evictor()
            .dbEnv()
            .getEnv()
            .openSecondaryDatabase(txn, _store.evictor().filename(), _dbName, _store.db(), config);
  }
Beispiel #2
0
  public int run(String[] args) {
    if (args.length > 0) {
      System.err.println(appName() + ": too many arguments");
      return 1;
    }

    Ice.Properties properties = communicator().getProperties();

    //
    // Create an object adapter
    //
    Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Library");

    //
    // Create an evictor for books.
    //
    Freeze.Evictor evictor =
        Freeze.Util.createBackgroundSaveEvictor(adapter, _envName, "books", null, null, true);
    int evictorSize = properties.getPropertyAsInt("EvictorSize");
    if (evictorSize > 0) {
      evictor.setSize(evictorSize);
    }

    adapter.addServantLocator(evictor, "book");

    //
    // Create the library, and add it to the object adapter.
    //
    LibraryI library = new LibraryI(communicator(), _envName, "authors", evictor);
    adapter.add(library, communicator().stringToIdentity("library"));

    //
    // Create and install a factory for books.
    //
    Ice.ObjectFactory bookFactory = new BookFactory(library);
    communicator().addObjectFactory(bookFactory, Demo.Book.ice_staticId());

    //
    // Everything ok, let's go.
    //
    adapter.activate();

    shutdownOnInterrupt();
    communicator().waitForShutdown();
    defaultInterrupt();

    library.close();
    return 0;
  }
Beispiel #3
0
  private NetworkProxy createNetworkProxy(Ice.Properties properties, int protocolSupport) {
    String proxyHost;

    proxyHost = properties.getProperty("Ice.SOCKSProxyHost");
    if (!proxyHost.isEmpty()) {
      if (protocolSupport == Network.EnableIPv6) {
        throw new Ice.InitializationException("IPv6 only is not supported with SOCKS4 proxies");
      }
      int proxyPort = properties.getPropertyAsIntWithDefault("Ice.SOCKSProxyPort", 1080);
      return new SOCKSNetworkProxy(proxyHost, proxyPort);
    }

    proxyHost = properties.getProperty("Ice.HTTPProxyHost");
    if (!proxyHost.isEmpty()) {
      return new HTTPNetworkProxy(
          proxyHost, properties.getPropertyAsIntWithDefault("Ice.HTTPProxyPort", 1080));
    }

    return null;
  }
  private Ice.Properties createServiceProperties(String service) {
    Ice.Properties properties;
    Ice.Properties communicatorProperties = _communicator.getProperties();
    if (communicatorProperties.getPropertyAsInt("IceBox.InheritProperties") > 0) {
      properties = communicatorProperties._clone();
      properties.setProperty("Ice.Admin.Endpoints", ""); // Inherit all except Ice.Admin.Endpoints!
    } else {
      properties = Ice.Util.createProperties();
    }

    String programName = communicatorProperties.getProperty("Ice.ProgramName");
    if (programName.length() == 0) {
      properties.setProperty("Ice.ProgramName", service);
    } else {
      properties.setProperty("Ice.ProgramName", programName + "-" + service);
    }
    return properties;
  }
Beispiel #5
0
  TraceLevels(Ice.Properties properties) {
    networkCat = "Network";
    protocolCat = "Protocol";
    retryCat = "Retry";
    locationCat = "Locator";
    slicingCat = "Slicing";
    threadPoolCat = "ThreadPool";

    final String keyBase = "Ice.Trace.";

    network = properties.getPropertyAsInt(keyBase + networkCat);
    protocol = properties.getPropertyAsInt(keyBase + protocolCat);
    retry = properties.getPropertyAsInt(keyBase + retryCat);
    location = properties.getPropertyAsInt(keyBase + locationCat);
    slicing = properties.getPropertyAsInt(keyBase + slicingCat);
    threadPool = properties.getPropertyAsInt(keyBase + threadPoolCat);
  }
Beispiel #6
0
  ObjectStore(
      String facet,
      String facetType,
      boolean createDb,
      EvictorI evictor,
      java.util.List<Index> indices,
      boolean populateEmptyIndices) {
    _cache = new Cache(this);

    _facet = facet;

    _evictor = evictor;
    _indices = indices;
    _communicator = evictor.communicator();
    _encoding = evictor.encoding();
    _keepStats = false;

    if (facet.equals("")) {
      _dbName = EvictorI.defaultDb;
    } else {
      _dbName = facet;
    }

    if (facetType != null) {
      //
      // Create a sample servant with this type
      //
      Ice.ValueFactory factory = _communicator.getValueFactoryManager().find(facetType);
      if (factory == null) {
        throw new DatabaseException(
            _evictor.errorPrefix() + "No value factory registered for type-id '" + facetType + "'");
      }

      _sampleServant = factory.create(facetType);
    }

    Connection connection = Util.createConnection(_communicator, evictor.dbEnv().getEnvName());

    try {
      Catalog catalog = new Catalog(connection, Util.catalogName(), true);
      CatalogData catalogData = catalog.get(evictor.filename());

      if (catalogData != null) {
        if (catalogData.evictor) {
          _keepStats = catalogData.value.isEmpty();
        } else {
          DatabaseException ex = new DatabaseException();
          ex.message = _evictor.errorPrefix() + evictor.filename() + " is not an evictor database";
          throw ex;
        }
      }

      com.sleepycat.db.Environment dbEnv = evictor.dbEnv().getEnv();

      //
      // TODO: FREEZE_DB_MODE
      //
      com.sleepycat.db.DatabaseConfig config = new com.sleepycat.db.DatabaseConfig();
      config.setType(com.sleepycat.db.DatabaseType.BTREE);
      config.setAllowCreate(createDb);

      Ice.Properties properties = _evictor.communicator().getProperties();
      String propPrefix = "Freeze.Evictor." + _evictor.filename() + ".";

      int btreeMinKey = properties.getPropertyAsInt(propPrefix + _dbName + ".BtreeMinKey");
      if (btreeMinKey > 2) {
        if (_evictor.trace() >= 1) {
          _evictor
              .communicator()
              .getLogger()
              .trace(
                  "Freeze.Evictor",
                  "Setting \""
                      + _evictor.filename()
                      + "."
                      + _dbName
                      + "\"'s btree minkey to "
                      + btreeMinKey);
        }
        config.setBtreeMinKey(btreeMinKey);
      }

      boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0;
      if (checksum) {
        if (_evictor.trace() >= 1) {
          _evictor
              .communicator()
              .getLogger()
              .trace("Freeze.Evictor", "Turning checksum on for \"" + _evictor.filename() + "\"");
        }

        config.setChecksum(true);
      }

      int pageSize = properties.getPropertyAsInt(propPrefix + "PageSize");
      if (pageSize > 0) {
        if (_evictor.trace() >= 1) {
          _evictor
              .communicator()
              .getLogger()
              .trace(
                  "Freeze.Evictor",
                  "Setting \"" + _evictor.filename() + "\"'s pagesize to " + pageSize);
        }
        config.setPageSize(pageSize);
      }

      try {
        Transaction tx = connection.beginTransaction();
        com.sleepycat.db.Transaction txn = Util.getTxn(tx);

        _db = dbEnv.openDatabase(txn, evictor.filename(), _dbName, config);

        for (Index index : _indices) {
          index.associate(this, txn, createDb, populateEmptyIndices);
        }

        if (catalogData == null) {
          catalogData = new CatalogData(true, "::Ice::Identity", "Object");
          catalog.put(evictor.filename(), catalogData);
        }

        tx.commit();
      } catch (java.io.FileNotFoundException dx) {
        throw new NotFoundException(_evictor.errorPrefix() + "Db.open: " + dx.getMessage(), dx);
      } catch (com.sleepycat.db.DatabaseException dx) {
        throw new DatabaseException(_evictor.errorPrefix() + "Db.open: " + dx.getMessage(), dx);
      } finally {
        Transaction tx = connection.currentTransaction();
        if (tx != null) {
          try {
            tx.rollback();
          } catch (DatabaseException de) {
          }
        }
      }
    } finally {
      connection.close();
    }
  }
  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;
  }