@Override
 public JSON getDescriptor(ModuleMetaModel metaModel) {
   JSON json = new JSON();
   for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) {
     json.set(application.getHandle().getPackage().toString(), new JSON());
   }
   return json;
 }
Beispiel #2
0
 public TreeMap<String, Object> build() {
   TreeMap<String, Object> ret = new TreeMap<String, Object>(entries);
   for (java.util.Map.Entry<String, Object> entry : ret.entrySet()) {
     Object value = entry.getValue();
     if (value instanceof JSON) {
       JSON json = (JSON) value;
       entry.setValue(json.build());
     }
   }
   return ret;
 }
  private void emitConfig(ModuleMetaModel metaModel) {
    JSON descriptor = new JSON();

    // Application configs
    for (ApplicationMetaModel application : metaModel.getChildren(ApplicationMetaModel.class)) {

      //
      metaModel.processingContext.log(
          "Emitting application " + application.getHandle() + " config");

      // Recycle
      descriptor.clear();

      // Emit config
      for (ApplicationMetaModelPlugin plugin : context.getPlugins()) {
        JSON pluginDescriptor = plugin.getDescriptor(application);
        if (pluginDescriptor != null) {
          descriptor.set(plugin.getName(), pluginDescriptor);
        }
      }

      //
      Writer writer = null;
      try {
        FileObject fo =
            metaModel.processingContext.createResource(
                StandardLocation.CLASS_OUTPUT, application.getName(), "config.json");
        writer = fo.openWriter();
        descriptor.toString(writer, 2);
      } catch (IOException e) {
        throw ApplicationMetaModel.CANNOT_WRITE_APPLICATION_CONFIG.failure(
            e, metaModel.processingContext.get(application.getHandle()), application.getName());
      } finally {
        Tools.safeClose(writer);
      }
    }
  }
Beispiel #4
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 JSON toJSON() {
   JSON json = new JSON();
   json.set("handle", handle);
   json.map("methods", getMethods());
   return json;
 }
  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;
  }