Announcement

Collapse
No announcement yet.

java applikation pluginfähig machen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • java applikation pluginfähig machen

    hy

    bin grad am planen einer applikation, würd die jedoch gern so programmiern, dass andere plugins machen können und somit die applikation erweitern können - a la eclipse.

    gibts da nen guide dazu? worauf muss ich aufpassen?

    tia
    ciao david

  • #2
    hallo,

    warum nutzt du dann nicht gleich plattformen für deine anwendung, wie halt eclipse oder netbeans?

    da ist schon alles dabei, incl. api und so.

    mf

    Comment


    • #3
      Es gab da mal in einer Ausgabe des Java Magazins einen tollen Artikel zu
      diesem Thema. Ich nutze heute folgenden simplen Code für solche
      Aufgaben:
      ---------------

      public interface Plugin
      {
      public abstract String getUniqueName();

      public abstract String getDisplayName();

      public abstract String getVendor();
      }

      public interface PluginFactory<T extends Plugin>
      {
      public abstract T[] createPlugins();
      }


      import java.io.File;
      import java.io.FileFilter;
      import java.io.InputStream;
      import java.net.URL;
      import java.net.URLClassLoader;
      import java.util.Collection;
      import java.util.Enumeration;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Properties;
      import java.util.jar.JarFile;
      import java.util.zip.ZipEntry;

      public class PluginLoader<P extends Plugin> {

      private Map<String, P> loadedPlugins;

      private File pluginDirectory;

      private String pluginFactory;

      private String pluginConfigFile;

      public PluginLoader(String pluginConfigFile, String pluginFactory,
      File pluginDirectory) {
      this.pluginFactory = pluginFactory;
      this.pluginConfigFile = pluginConfigFile;
      this.pluginDirectory = pluginDirectory;
      }

      public Collection<P> getPlugins() {
      if (loadedPlugins == null) {
      loadedPlugins = new HashMap<String, P>();
      loadPlugins();
      }

      return loadedPlugins.values();
      }

      private void loadPlugins() {
      try {
      // try to find all plugins in the classpath...
      Enumeration e = getClass().getClassLoader().getResources(
      pluginConfigFile);
      while (e.hasMoreElements()) {
      Properties properties = new Properties();
      URL url = (URL) e.nextElement();
      InputStream is = url.openStream();
      try {
      properties.load(is);
      String factoryName =
      properties.getProperty(pluginFactory);
      if (factoryName != null) {
      P[] plugins =
      loadFactoryPlugins(factoryName);
      if (plugins != null)
      addPlugins(plugins);
      }
      } finally {
      is.close();
      }
      }

      // try to find the plugins from the jars in the plugin path
      File pluginPath = pluginDirectory;

      File[] jars = pluginPath.listFiles(new FileFilter() {
      public boolean accept(File file) {
      String fileName = file.getName().toLowerCase();
      return (file.isFile() && fileName.endsWith(".jar"));
      }
      });
      for (int i = 0; i < jars.length; i++) {
      JarFile jarFile = new JarFile(jars[i]);
      try {
      ZipEntry entry = jarFile.getEntry(pluginConfigFile);
      if (entry != null) {
      Properties properties = new Properties();
      InputStream is = jarFile.getInputStream(entry);
      try {
      properties.load(is);
      String factoryName = properties
      .getProperty(pluginFactory);
      if (factoryName != null) {
      P[] plugins =
      loadFactoryPlugins(factoryName,

      createJarClassLoader(jars[i]));
      if (plugins != null)
      addPlugins(plugins);
      }
      } finally {
      is.close();
      }
      }
      } finally {
      jarFile.close();
      }
      }
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      }

      private P[] loadFactoryPlugins(String factoryName) {
      return loadFactoryPlugins(factoryName,
      getClass().getClassLoader());
      }

      private P[] loadFactoryPlugins(String factoryName, ClassLoader loader)
      {
      try {
      Class factoryClass = loader.loadClass(factoryName);
      PluginFactory<P> factory = (PluginFactory<P>) factoryClass
      .newInstance();
      return factory.createPlugins();
      } catch (Exception ex) {
      ex.printStackTrace();
      return null;
      }
      }

      private void addPlugins(P[] plugins) {
      for (int i = 0; i < plugins.length; i++) {
      P plugin = plugins[i];
      String name = plugin.getUniqueName();
      if (!loadedPlugins.containsKey(name))
      loadedPlugins.put(name, plugin);
      }
      }

      private ClassLoader createJarClassLoader(File jar) {
      try {
      return new URLClassLoader(new URL[] { jar.toURL() });
      } catch (Exception ex) {
      return null;
      }
      }
      }
      -------------------------------------

      Und der Treiber sieht dann etwa so aus:

      -------------------------------------

      import java.io.File;
      import java.util.Collection;

      public class PluginTest {

      /**
      * @param args
      */
      public static void main(String[] args) {

      PluginLoader<TestPlugin> pluginLoader = new
      PluginLoader<TestPlugin>(
      "test-plugin.properties", "plugin-factory", new File(
      "plugins/"));

      Collection<TestPlugin> plugins = pluginLoader.getPlugins();

      for(TestPlugin plugin : plugins)
      {
      plugin.doSomething();
      }
      }
      }

      ---------------------------------------

      Im Verzeichnis "plugins/" werden JAR Dateien untersucht, ob sie eine Datei
      namens "test-plugin.properties" enthalten. Diese Datei enthält dann einen
      Eintrag "plugin-factory=<PlugIn Factory implementierende Klasse>".

      Die Plugin-Schreiber müssen also PluginFactory und Plugin implementieren
      und diese zusammen mit der Properties Datei in einem JAR bundeln. Dieses
      JAR wird dann in das Plugin Verzeichnis kopiert.

      Comment


      • #4
        wow, danke für die schnellen antworten.
        werd auch mal meine java-magzine durchforsten. war mir nicht sicher wo ichs gelesen hatte.

        verwende zur zeit noch jbuilder, steig jedoch auf eclipse um.

        danke für die ausführliche beschreibung und erklärung. ich hoffe ich bekomme das hin!

        ist sonst etwas zu beachten? bei der GUI vielleicht?

        danke nochmal.

        ciao davi

        Comment

        Working...
        X