@GET
 public String get() {
   assertTrue(application != null);
   assertTrue(application.getClasses().contains(ResourceB.class));
   assertTrue(application.getSingletons().size() > 0);
   assertTrue(
       application.getSingletons().iterator().next().getClass().equals(ResourceAReader.class));
   return "get!";
 }
  @Test
  public void addsSingletonsFromNewApplicationToTheBeginningOfSetOfSingletons() {
    Application applicationOne = mock(Application.class);
    Application applicationTwo = mock(Application.class);
    ResourceOne resourceOne = new ResourceOne();
    ResourceTwo resourceTwo = new ResourceTwo();
    when(applicationOne.getSingletons()).thenReturn(newHashSet(resourceOne));
    when(applicationTwo.getSingletons()).thenReturn(newHashSet(resourceTwo));

    everrestApplication.addApplication(applicationTwo);
    assertEquals(newHashSet(resourceTwo), everrestApplication.getSingletons());
    everrestApplication.addApplication(applicationOne);
    assertEquals(newHashSet(resourceOne, resourceTwo), everrestApplication.getSingletons());
  }
  /**
   * Attaches a JAX-RS {@link Application} to this JaxRsApplication.<br>
   * The providers are available for all root resource classes provided to this JaxRsApplication. If
   * you won't mix them, instantiate another JaxRsApplication.
   *
   * @param appConfig Contains the classes to load as root resource classes and as providers.
   *     Invalid root resource classes and provider classes are ignored, according to JAX-RS
   *     specification.
   * @return true, if all resource classes and providers could be added, or false at least one could
   *     not be added. Exceptions were logged.
   * @throws IllegalArgumentException if the given appConfig is null.
   */
  public boolean add(Application appConfig) throws IllegalArgumentException {
    if (appConfig == null) {
      throw new IllegalArgumentException("The ApplicationConfig must not be null");
    }

    JaxRsRestlet jaxRsRestlet = this.jaxRsRestlet;
    boolean everythingFine = true;

    if (jaxRsRestlet == null) {
      everythingFine = false;
      getLogger().warning("No JAX-RS to Restlet adapter available to handle to calls.");
    } else {
      Set<Class<?>> classes = appConfig.getClasses();
      Set<Object> singletons = appConfig.getSingletons();

      if (singletons != null) {
        for (Object singleton : singletons) {
          // LATER test: check, if a singleton is also available in
          // the
          // classes -> ignore or whatever
          if (singleton != null && !classes.contains(singleton.getClass())) {
            everythingFine &= jaxRsRestlet.addSingleton(singleton);
          }
        }
      }

      if (classes != null) {
        for (Class<?> clazz : classes) {
          everythingFine &= jaxRsRestlet.addClass(clazz);
        }
      }
    }

    return everythingFine;
  }
예제 #4
0
 public void publish(Application application) {
   Set<Class<?>> classes = new LinkedHashSet<>();
   Set<Class<?>> appClasses = application.getClasses();
   if (appClasses != null) {
     classes.addAll(appClasses);
   }
   if (application instanceof EverrestApplication) {
     EverrestApplication everrest = (EverrestApplication) application;
     for (Map.Entry<String, Class<?>> e : everrest.getResourceClasses().entrySet()) {
       Class<?> clazz = e.getValue();
       addResource(e.getKey(), clazz);
       classes.remove(clazz);
     }
     for (Map.Entry<String, Object> e : everrest.getResourceSingletons().entrySet()) {
       addResource(e.getKey(), e.getValue());
     }
     for (ObjectFactory<? extends ObjectModel> factory : everrest.getFactories()) {
       addFactory(factory);
       classes.remove(factory.getObjectModel().getObjectClass());
     }
   }
   for (Class<?> clazz : classes) {
     addPerRequest(clazz);
   }
   Set<Object> singletons = application.getSingletons();
   if (singletons != null) {
     for (Object instance : singletons) {
       addSingleton(instance);
     }
   }
 }
예제 #5
0
  @Test
  public void callsPreDestroyMethodsForSingletonComponentsOnStop() throws Exception {
    EchoResource resource = new EchoResource();
    Application application = mock(Application.class);
    when(application.getSingletons()).thenReturn(newHashSet(resource));
    when(requestHandler.getResources()).thenReturn(mock(ResourceBinder.class));

    everrestProcessor.addApplication(application);
    everrestProcessor.stop();

    assertEquals(1, resource.preDestroyActionInvocationsCounter.get());
  }
예제 #6
0
 private RSDL getRSDL(Application application) throws ClassNotFoundException, IOException {
   RSDL rsdl = null;
   for (Object obj : application.getSingletons()) {
     if (obj instanceof BackendApiResource) {
       BackendApiResource resource = (BackendApiResource) obj;
       rsdl = resource.getRSDL();
       break;
     }
   }
   assert (rsdl != null)
       : "Resource that generates RSDL, BackendApiResource, not found (this should never happen)";
   return rsdl;
 }
예제 #7
0
  public static List<String> getPaths(Application application) {
    List<String> paths = new ArrayList<String>();

    Set<Class<?>> clazzes = application.getClasses();
    for (Class<?> clazz : clazzes) {
      paths.addAll(getPaths(clazz));
    }

    Set<Object> singletons = application.getSingletons();
    for (Object object : singletons) {
      paths.addAll(getPaths(object.getClass()));
    }

    return paths;
  }
예제 #8
0
  @SuppressWarnings("unchecked")
  public static JAXRSServerFactoryBean createApplication(
      Application app, boolean ignoreAppPath, boolean staticSubresourceResolution) {

    Set<Object> singletons = app.getSingletons();
    verifySingletons(singletons);

    List<Class<?>> resourceClasses = new ArrayList<Class<?>>();
    List<Object> providers = new ArrayList<Object>();
    List<Feature> features = new ArrayList<Feature>();
    Map<Class<?>, ResourceProvider> map = new HashMap<Class<?>, ResourceProvider>();

    // Note, app.getClasses() returns a list of per-request classes
    // or singleton provider classes
    for (Class<?> cls : app.getClasses()) {
      if (isValidApplicationClass(cls, singletons)) {
        if (isValidProvider(cls)) {
          providers.add(createProviderInstance(cls));
        } else if (Feature.class.isAssignableFrom(cls)) {
          features.add(createFeatureInstance((Class<? extends Feature>) cls));
        } else {
          resourceClasses.add(cls);
          map.put(cls, new PerRequestResourceProvider(cls));
        }
      }
    }

    // we can get either a provider or resource class here
    for (Object o : singletons) {
      if (isValidProvider(o.getClass())) {
        providers.add(o);
      } else if (o instanceof Feature) {
        features.add((Feature) o);
      } else {
        resourceClasses.add(o.getClass());
        map.put(o.getClass(), new SingletonResourceProvider(o));
      }
    }

    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    String address = "/";
    if (!ignoreAppPath) {
      ApplicationPath appPath = app.getClass().getAnnotation(ApplicationPath.class);
      if (appPath != null) {
        address = appPath.value();
      }
    }
    if (!address.startsWith("/")) {
      address = "/" + address;
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(staticSubresourceResolution);
    bean.setResourceClasses(resourceClasses);
    bean.setProviders(providers);
    bean.setFeatures(features);
    for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
      bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    Map<String, Object> appProps = app.getProperties();
    if (appProps != null) {
      bean.getProperties(true).putAll(appProps);
    }
    bean.setApplication(app);

    return bean;
  }