Beispiel #1
0
  void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event, final BeanManager beanManager) {

    // We are in provided mode
    if (this.context == null) {

      // This is not really clean : it couples the inject to the application notion
      // but avoiding that would mean to introduce more complex stuff and so
      // it's fine for the moment

      try {
        final ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream in = cl.getResourceAsStream("juzu/config.json");
        String serializedConfig = Tools.read(in);
        JSON config = (JSON) JSON.parse(serializedConfig);
        JSON applications = config.getJSON("application");
        if (applications.names().size() != 1) {
          throw new RuntimeException(
              "Was expecting application size to be 1 instead of " + applications);
        }
        String packageFQN = applications.names().iterator().next();
        ApplicationDescriptor descriptor = ApplicationDescriptor.create(cl, packageFQN);
        // For now we don't resolve anything...
        ResourceResolver resourceResolver =
            new ResourceResolver() {
              public URL resolve(String uri) {
                return null;
              }
            };

        //
        ProvidedCDIInjector injector =
            new ProvidedCDIInjector(cl, beanManager, descriptor, resourceResolver);

        // We start the application
        // it should:
        // - instantiate the plugins
        // - bind the beans from the plugins in the container
        // we rely on the lazy nature of the beans for not really starting...
        Application application = injector.getApplication();
        application.start();

        // At this point the application is not really started
        // we must go through the other CDI phases for effectively registering
        // the beans in the container
        this.context = (CDIContext) application.getInjectionContext();
      } catch (Exception e) {
        throw new UnsupportedOperationException(e);
      }
    }
  }
  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;
  }