Esempio n. 1
0
  public boolean doGet(String path, ServletContext ctx, HttpServletResponse resp)
      throws ServletException, IOException {
    if (path != null && path.length() > 0) {
      for (Application runtime : runtimes) {
        Iterable<ResourceResolver> resolvers = runtime.resolveBeans(ResourceResolver.class);
        for (ResourceResolver resolver : resolvers) {
          // For now we only have resource of URL type ...
          URL content = resolver.resolve(path);
          InputStream in;
          if (content != null) {
            in = content.openStream();
          } else {
            // It could be a server resource like an image
            in = ctx.getResourceAsStream(path);
          }

          if (in != null) {
            int pos = path.lastIndexOf('/');
            String name = pos == -1 ? path : path.substring(pos + 1);
            String contentType = ctx.getMimeType(name);
            if (contentType != null) {
              resp.setContentType(contentType);
            }
            Tools.copy(in, resp.getOutputStream());
            return true;
          }
        }
      }
    }
    return false;
  }
Esempio n. 2
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);
      }
    }
  }