Esempio n. 1
0
  public ApplicationDescriptor(Class<?> applicationClass) throws Exception {
    // Load config
    JSON config;
    InputStream in = null;
    try {
      in = applicationClass.getResourceAsStream("config.json");
      String s = Tools.read(in);
      config = (JSON) JSON.parse(s);
    } catch (IOException e) {
      throw new AssertionError(e);
    } finally {
      Tools.safeClose(in);
    }

    //
    Class<?> packageClass;
    try {
      packageClass =
          applicationClass
              .getClassLoader()
              .loadClass(applicationClass.getPackage().getName() + ".package-info");
    } catch (ClassNotFoundException e) {
      AssertionError ae = new AssertionError("Cannot load package class");
      ae.initCause(e);
      throw ae;
    }

    //
    HashMap<String, ApplicationPlugin> pluginMap = new HashMap<String, ApplicationPlugin>();
    for (ApplicationPlugin plugin : ServiceLoader.load(ApplicationPlugin.class)) {
      pluginMap.put(plugin.getName(), plugin);
    }

    //
    HashMap<String, Descriptor> pluginDescriptors = new HashMap<String, Descriptor>();
    for (String name : config.names()) {
      ApplicationPlugin plugin = pluginMap.get(name);
      if (plugin == null) {
        throw new UnsupportedOperationException("Handle me gracefully : missing plugin " + name);
      }
      JSON pluginConfig = config.getJSON(name);
      Descriptor pluginDescriptor = plugin.init(applicationClass.getClassLoader(), pluginConfig);
      pluginDescriptors.put(name, pluginDescriptor);
    }

    //
    for (Iterator<String> i = pluginMap.keySet().iterator(); i.hasNext(); ) {
      String name = i.next();
      if (!pluginDescriptors.containsKey(name)) {
        i.remove();
      }
    }

    //
    this.applicationClass = applicationClass;
    this.name = applicationClass.getSimpleName();
    this.packageName = applicationClass.getPackage().getName();
    this.templates = (TemplatesDescriptor) pluginDescriptors.get("template");
    this.packageClass = packageClass;
    this.controllers = (ControllersDescriptor) pluginDescriptors.get("controller");
    this.pluginDescriptors = pluginDescriptors;
    this.plugins = pluginMap;
  }
Esempio n. 2
0
 public void addPlugin(ApplicationPlugin plugin) throws Exception {
   plugins.put(plugin.getName(), plugin);
   pluginDescriptors.put(name, plugin.init(applicationClass.getClassLoader(), new JSON()));
 }