예제 #1
0
  @Override
  public synchronized void initializePlugins() {
    if (_initialized) {
      InitializationException ex = new InitializationException();
      ex.reason = "plug-ins already initialized";
      throw ex;
    }

    //
    // Invoke initialize() on the plug-ins, in the order they were loaded.
    //
    java.util.List<Plugin> initializedPlugins = new java.util.ArrayList<>();
    try {
      for (PluginInfo p : _plugins) {
        try {
          p.plugin.initialize();
        } catch (PluginInitializationException ex) {
          throw ex;
        } catch (RuntimeException ex) {
          PluginInitializationException e = new PluginInitializationException();
          e.reason = "plugin `" + p.name + "' initialization failed";
          e.initCause(ex);
          throw e;
        }
        initializedPlugins.add(p.plugin);
      }
    } catch (RuntimeException ex) {
      //
      // Destroy the plug-ins that have been successfully initialized, in the
      // reverse order.
      //
      java.util.ListIterator<Plugin> i = initializedPlugins.listIterator(initializedPlugins.size());
      while (i.hasPrevious()) {
        Plugin p = i.previous();
        try {
          p.destroy();
        } catch (RuntimeException e) {
          // Ignore.
        }
      }
      throw ex;
    }

    _initialized = true;
  }
예제 #2
0
  public String[] loadPlugins(String[] cmdArgs) {
    assert (_communicator != null);

    //
    // Load and initialize the plug-ins defined in the property set
    // with the prefix "Ice.Plugin.". These properties should
    // have the following format:
    //
    // Ice.Plugin.name[.<language>]=entry_point [args]
    //
    // If the Ice.PluginLoadOrder property is defined, load the
    // specified plug-ins in the specified order, then load any
    // remaining plug-ins.
    //
    final String prefix = "Ice.Plugin.";
    Properties properties = _communicator.getProperties();
    java.util.Map<String, String> plugins = properties.getPropertiesForPrefix(prefix);

    final String[] loadOrder = properties.getPropertyAsList("Ice.PluginLoadOrder");
    for (String name : loadOrder) {
      if (findPlugin(name) != null) {
        PluginInitializationException ex = new PluginInitializationException();
        ex.reason = "plug-in `" + name + "' already loaded";
        throw ex;
      }

      String key = "Ice.Plugin." + name + ".java";
      boolean hasKey = plugins.containsKey(key);
      if (hasKey) {
        plugins.remove("Ice.Plugin." + name);
      } else {
        key = "Ice.Plugin." + name;
        hasKey = plugins.containsKey(key);
      }

      if (hasKey) {
        final String value = plugins.get(key);
        cmdArgs = loadPlugin(name, value, cmdArgs);
        plugins.remove(key);
      } else {
        PluginInitializationException ex = new PluginInitializationException();
        ex.reason = "plug-in `" + name + "' not defined";
        throw ex;
      }
    }

    //
    // Load any remaining plug-ins that weren't specified in PluginLoadOrder.
    //
    while (!plugins.isEmpty()) {
      java.util.Iterator<java.util.Map.Entry<String, String>> p = plugins.entrySet().iterator();
      java.util.Map.Entry<String, String> entry = p.next();

      String name = entry.getKey().substring(prefix.length());

      int dotPos = name.lastIndexOf('.');
      if (dotPos != -1) {
        String suffix = name.substring(dotPos + 1);
        if (suffix.equals("cpp") || suffix.equals("clr")) {
          //
          // Ignored
          //
          p.remove();
        } else if (suffix.equals("java")) {
          name = name.substring(0, dotPos);
          cmdArgs = loadPlugin(name, entry.getValue(), cmdArgs);
          p.remove();

          //
          // Don't want to load this one if it's there!
          //
          plugins.remove("Ice.Plugin." + name);
        } else {
          //
          // Name is just a regular name that happens to contain a dot
          //
          dotPos = -1;
        }
      }

      if (dotPos == -1) {
        //
        // Is there a .java entry?
        //
        String value = entry.getValue();
        p.remove();

        String javaValue = plugins.remove("Ice.Plugin." + name + ".java");
        if (javaValue != null) {
          value = javaValue;
        }

        cmdArgs = loadPlugin(name, value, cmdArgs);
      }
    }

    return cmdArgs;
  }