示例#1
0
  private static Map<Key<?>, Object> checkForInvalidSeedsAndCopy(
      Map<Key<?>, Object> seededObjects) {
    for (Map.Entry<Key<?>, Object> entry : seededObjects.entrySet()) {
      Key<?> key = entry.getKey();
      Object value = entry.getValue();
      if (value == null) {
        throw new IllegalArgumentException("Seeded objects contains null key. Key: " + key);
      }
      Class<?> rawType = key.getTypeLiteral().getRawType();
      if (!rawType.isInstance(value)) {
        if (!(value instanceof Key)) {
          throw new IllegalArgumentException(
              "Seeded object is not instance of key type. " + "Key: " + key + ". Value: " + value);
        }
        Class<?> valueRawType = ((Key) value).getTypeLiteral().getRawType();
        if (!rawType.isAssignableFrom(valueRawType)) {
          throw new IllegalArgumentException(
              "Chained value key type does not extend the key type. "
                  + "Key Key: "
                  + key
                  + ". Value Key: "
                  + value);
        }
      }
    }

    return seededObjects
        .entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  }
示例#2
0
文件: Qarrot.java 项目: viphe/qarrot
  private void scanBindings() throws IOException {
    for (Map.Entry<Key<?>, Binding<?>> e : globalInjector.getAllBindings().entrySet()) {
      Key<?> bindingKey = e.getKey();
      Binding<?> binding = e.getValue();
      TypeLiteral boundTypeLiteral = bindingKey.getTypeLiteral();
      Type boundType = boundTypeLiteral.getType();
      if (boundType instanceof Class) {
        final Class boundClass = (Class) boundType;
        for (Method method : boundClass.getMethods()) {
          if ((method.getModifiers() & Modifier.STATIC) == 0) {
            for (Annotation annotation : method.getAnnotations()) {
              if (annotation instanceof Path) {
                Path pathSpec = (Path) annotation;
                RouteSpec routeIn = Routes.parse(pathSpec.value(), true);
                endPointMethods.add(new EndPointMethod(routeIn, bindingKey, boundClass, method));
              }
            }
          }
        }

        if (MessageBodyReader.class.isAssignableFrom(boundClass)) {
          messageBodyReaders.add(0, (MessageBodyReader) globalInjector.getInstance(bindingKey));
        }
        if (MessageBodyWriter.class.isAssignableFrom(boundClass)) {
          messageBodyWriters.add(0, (MessageBodyWriter) globalInjector.getInstance(bindingKey));
        }
      }
    }
  }
示例#3
0
文件: Qarrot.java 项目: viphe/qarrot
  public <T> byte[] toBytes(MediaType mediaType, T o) throws IOException {
    if (o == null) {
      return null;
    }

    Class<T> clazz = (Class<T>) o.getClass();
    MessageBodyWriter<T> writer = findWriter(clazz, null, EMPTY_ANNOTATIONS, mediaType);
    if (writer == null) {
      throw new IllegalArgumentException(
          "cannot convert " + clazz.getName() + " into byte[] (" + mediaType + ")");
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
    writer.writeTo(o, clazz, clazz, EMPTY_ANNOTATIONS, mediaType, EMPTY_OBJECT_HEADERS, baos);
    return baos.toByteArray();
  }
示例#4
0
  @Override
  public void beforeStart(final Application application) {
    final Reflections reflections =
        new Reflections(
            new ConfigurationBuilder()
                .filterInputsBy(new FilterBuilder.Exclude(FilterBuilder.prefix("com.google")))
                .addUrls(ClasspathHelper.forClassLoader(application.classloader()))
                .addScanners(new SubTypesScanner()));

    // automatic Guice module detection
    Set<Class<? extends AbstractModule>> guiceModules =
        reflections.getSubTypesOf(AbstractModule.class);
    for (Class<? extends Module> moduleClass : guiceModules) {
      try {
        if (!moduleClass.isAnonymousClass()) {
          modules.add(moduleClass.newInstance());
        }
      } catch (InstantiationException e) {
        throw Throwables.propagate(e);
      } catch (IllegalAccessException e) {
        throw Throwables.propagate(e);
      }
    }

    modules.add(
        new AbstractModule() {
          @Override
          protected void configure() {
            bind(Application.class).toInstance(application);
            bind(Reflections.class).toInstance(reflections);

            Names.bindProperties(
                this.binder(),
                fromKeys(
                    application.configuration().keys(),
                    new Function<String, String>() {
                      @Override
                      public String apply(String key) {
                        // remove after https://play.lighthouseapp.com/projects/82401/tickets/372 is
                        // fixed
                        if (key.contains("akka")) return null;

                        return application.configuration().getString(key);
                      }
                    }));

            for (Class<? extends Controller> controllerClass :
                reflections.getSubTypesOf(Controller.class)) {
              requestStaticInjection(controllerClass);
            }

            // bind all services
            Multibinder<Service> serviceBinder = Multibinder.newSetBinder(binder(), Service.class);
            for (Class<? extends Service> serviceImplClass :
                reflections.getSubTypesOf(AbstractService.class)) {
              serviceBinder.addBinding().to(serviceImplClass).asEagerSingleton();
            }
            for (Class<? extends Service> serviceImplClass :
                reflections.getSubTypesOf(AbstractIdleService.class)) {
              serviceBinder.addBinding().to(serviceImplClass).asEagerSingleton();
            }
            for (Class<? extends Service> serviceImplClass :
                reflections.getSubTypesOf(AbstractExecutionThreadService.class)) {
              serviceBinder.addBinding().to(serviceImplClass).asEagerSingleton();
            }

            // bind actor - todo use reflections for this

            // start/stop services after injection and on shutdown of the Play app
            bindListener(
                MoreMatchers.subclassesOf(Service.class),
                new TypeListener() {
                  @Override
                  public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
                    typeEncounter.register(
                        new InjectionListener<I>() {
                          @Override
                          public void afterInjection(final I i) {
                            onStartListeners.add(
                                new OnStartListener() {
                                  @Override
                                  public void onApplicationStart(
                                      Application application, Injector injector) {
                                    Logger.info(String.format("Starting %s", i.toString()));
                                    ((Service) i).start();

                                    onStopListeners.add(
                                        new OnStopListener() {
                                          @Override
                                          public void onApplicationStop(Application application) {
                                            Logger.info(String.format("Stopping %s", i.toString()));
                                            ((Service) i).stop();
                                          }
                                        });
                                  }
                                });
                          }
                        });
                  }
                });
          }
        });
  }