Exemplo n.º 1
0
  private static void testPotato(
      Class<? extends Collection> implClazz, Class<? extends List> argClazz) throws Throwable {
    try {
      System.out.printf("implClazz=%s, argClazz=%s\n", implClazz.getName(), argClazz.getName());
      final int iterations = 100000;
      final List<Integer> list = (List<Integer>) argClazz.newInstance();
      final Integer one = Integer.valueOf(1);
      final List<Integer> oneElementList = Collections.singletonList(one);
      final Constructor<? extends Collection> constr = implClazz.getConstructor(Collection.class);
      final Thread t =
          new CheckedThread() {
            public void realRun() {
              for (int i = 0; i < iterations; i++) {
                list.add(one);
                list.remove(one);
              }
            }
          };
      t.setDaemon(true);
      t.start();

      for (int i = 0; i < iterations; i++) {
        Collection<?> coll = constr.newInstance(list);
        Object[] elts = coll.toArray();
        check(elts.length == 0 || (elts.length == 1 && elts[0] == one));
      }
    } catch (Throwable t) {
      unexpected(t);
    }
  }
Exemplo n.º 2
0
  @Override
  public void onApplicationStop() {

    List<Class> jobs = Play.classloader.getAssignableClasses(Job.class);

    for (final Class clazz : jobs) {
      // @OnApplicationStop
      if (clazz.isAnnotationPresent(OnApplicationStop.class)) {
        try {
          Job<?> job = ((Job<?>) clazz.newInstance());
          scheduledJobs.add(job);
          job.run();
          if (job.wasError) {
            if (job.lastException != null) {
              throw job.lastException;
            }
            throw new RuntimeException("@OnApplicationStop Job has failed");
          }
        } catch (InstantiationException e) {
          throw new UnexpectedException("Job could not be instantiated", e);
        } catch (IllegalAccessException e) {
          throw new UnexpectedException("Job could not be instantiated", e);
        } catch (Throwable ex) {
          if (ex instanceof PlayException) {
            throw (PlayException) ex;
          }
          throw new UnexpectedException(ex);
        }
      }
    }

    executor.shutdownNow();
    executor.getQueue().clear();
  }
Exemplo n.º 3
0
  public JxpSource getJxpServlet(String name) {
    JxpSource source = _httpServlets.get(name);
    if (source != null) return source;

    try {
      Class c = Class.forName(name);
      Object n = c.newInstance();
      if (!(n instanceof HttpServlet))
        throw new RuntimeException("class [" + name + "] is not a HttpServlet");

      HttpServlet servlet = (HttpServlet) n;
      servlet.init(createServletConfig(name));
      source = new ServletSource(servlet);
      _httpServlets.put(name, source);
      return source;
    } catch (Exception e) {
      throw new RuntimeException("can't load [" + name + "]", e);
    }
  }
Exemplo n.º 4
0
  @Override
  public void afterApplicationStart() {
    List<Class<?>> jobs = new ArrayList<Class<?>>();
    for (Class clazz : Play.classloader.getAllClasses()) {
      if (Job.class.isAssignableFrom(clazz)) {
        jobs.add(clazz);
      }
    }
    scheduledJobs = new ArrayList<Job>();
    for (final Class<?> clazz : jobs) {
      // @OnApplicationStart
      if (clazz.isAnnotationPresent(OnApplicationStart.class)) {
        // check if we're going to run the job sync or async
        OnApplicationStart appStartAnnotation = clazz.getAnnotation(OnApplicationStart.class);
        if (!appStartAnnotation.async()) {
          // run job sync
          try {
            Job<?> job = ((Job<?>) clazz.newInstance());
            scheduledJobs.add(job);
            job.run();
            if (job.wasError) {
              if (job.lastException != null) {
                throw job.lastException;
              }
              throw new RuntimeException("@OnApplicationStart Job has failed");
            }
          } catch (InstantiationException e) {
            throw new UnexpectedException("Job could not be instantiated", e);
          } catch (IllegalAccessException e) {
            throw new UnexpectedException("Job could not be instantiated", e);
          } catch (Throwable ex) {
            if (ex instanceof PlayException) {
              throw (PlayException) ex;
            }
            throw new UnexpectedException(ex);
          }
        } else {
          // run job async
          try {
            Job<?> job = ((Job<?>) clazz.newInstance());
            scheduledJobs.add(job);
            // start running job now in the background
            @SuppressWarnings("unchecked")
            Callable<Job> callable = (Callable<Job>) job;
            executor.submit(callable);
          } catch (InstantiationException ex) {
            throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
          } catch (IllegalAccessException ex) {
            throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
          }
        }
      }

      // @On
      if (clazz.isAnnotationPresent(On.class)) {
        try {
          Job<?> job = ((Job<?>) clazz.newInstance());
          scheduledJobs.add(job);
          scheduleForCRON(job);
        } catch (InstantiationException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        } catch (IllegalAccessException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        }
      }
      // @Every
      if (clazz.isAnnotationPresent(Every.class)) {
        try {
          Job job = (Job) clazz.newInstance();
          scheduledJobs.add(job);
          String value = job.getClass().getAnnotation(Every.class).value();
          if (value.startsWith("cron.")) {
            value = Play.configuration.getProperty(value);
          }
          value = Expression.evaluate(value, value).toString();
          if (!"never".equalsIgnoreCase(value)) {
            executor.scheduleWithFixedDelay(
                job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS);
          }
        } catch (InstantiationException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        } catch (IllegalAccessException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        }
      }
    }
  }