@Override
 public void configure(AtmosphereConfig config) {
   String s = config.getInitParameter(ApplicationConfig.MESSAGE_DELIMITER);
   if (s != null) {
     wsDelimiter = s;
   }
 }
  @Override
  public void configure(AtmosphereConfig config) {
    try {

      String s = config.getInitParameter(ATMOSPHERE_SPRING_EXCLUDE_CLASSES);
      if (s != null) {
        String[] list = s.split(",");
        for (String clazz : list) {
          excludedFromInjection.add(IOUtils.loadClass(getClass(), clazz));
        }

        if (list.length > 0) {
          preventSpringInjection = true;
        }
      }

      context = new AnnotationConfigApplicationContext();
      context.setParent(
          WebApplicationContextUtils.getWebApplicationContext(
              config.framework().getServletContext()));

      context.refresh();

      // Hack to make it injectable
      context.register(AtmosphereConfig.class);
      ((AtmosphereConfig)
              context.getBean(AtmosphereConfig.class.getCanonicalName(), config.framework()))
          .populate(config);
    } catch (Exception ex) {
      logger.warn("Unable to configure injection", ex);
    }
  }
  protected Broadcaster getBroadcaster(boolean autoCreate) {
    if (broadcaster == null) {
      throw new IllegalStateException("No Broadcaster associated with this AtmosphereResource.");
    }

    String s = config.getInitParameter(ApplicationConfig.RECOVER_DEAD_BROADCASTER);
    if (s != null) {
      autoCreate = Boolean.parseBoolean(s);
    }

    if (autoCreate && broadcaster.isDestroyed() && config.getBroadcasterFactory() != null) {
      logger.debug(
          "Broadcaster {} has been destroyed and cannot be re-used. Recreating a new one with the same name. You can turn off this"
              + " mechanism by adding, in web.xml, {} set to false",
          broadcaster.getID(),
          ApplicationConfig.RECOVER_DEAD_BROADCASTER);

      Broadcaster.SCOPE scope = broadcaster.getScope();
      synchronized (this) {
        String id =
            scope != Broadcaster.SCOPE.REQUEST
                ? broadcaster.getID()
                : broadcaster.getID() + ".recovered" + UUID.randomUUID();

        // Another Thread may have added the Broadcaster.
        broadcaster = config.getBroadcasterFactory().lookup(id, true);
        broadcaster.setScope(scope);
        broadcaster.addAtmosphereResource(this);
      }
    }
    return broadcaster;
  }
 private static void keepAliveThreads(ThreadPoolExecutor e, AtmosphereConfig config) {
   int keepAlive = DEFAULT_KEEP_ALIVE;
   String s = config.getInitParameter(ApplicationConfig.EXECUTORFACTORY_KEEP_ALIVE);
   if (s != null) {
     keepAlive = Integer.parseInt(s);
   }
   e.setKeepAliveTime(keepAlive, TimeUnit.SECONDS);
 }
 public JBossWebCometSupport(AtmosphereConfig config) {
   super(config);
   Object b = config.getInitParameter(ApplicationConfig.TOMCAT_CLOSE_STREAM);
   closeConnectionOnInputStream = b == null ? true : Boolean.parseBoolean(b.toString());
   try {
     Class.forName(HttpEvent.class.getName());
   } catch (Throwable e) {
     throw new IllegalStateException(unableToDetectComet());
   }
 }
    /**
     * Checks in the {@link AtmosphereConfig} if the {@link #propertyName} is defined as an
     * init-param and instantiate the appropriate class.
     *
     * <p>If instantiation fails, the exception is logged and {@code null} is returned.
     *
     * @param desiredType the type to be returned
     * @param config the configuration that provides parameters
     * @param <T> the generic for modular call
     * @return the instance of the expected class, {@code null} if an error occurs
     */
    public <T> T retrieve(final Class<T> desiredType, final AtmosphereConfig config) {
      final String initParameter = config.getInitParameter(this.propertyName);
      final String className = (initParameter != null) ? initParameter : defaultClass;

      try {
        final AtmosphereFramework fwk = config.framework();
        final Object retval =
            fwk.newClassInstance(
                desiredType, desiredType.getClass().cast(Class.forName(className)));
        return desiredType.cast(retval);
      } catch (Exception e) {
        logger.error("Unable to initialize {}", getClass().getName(), e);
        return null;
      }
    }
  @Override
  public void init(ServletConfig sc) throws ServletException {

    String maxInactive =
        sc.getInitParameter(MAX_INACTIVE) != null
            ? sc.getInitParameter(MAX_INACTIVE)
            : config.getInitParameter(MAX_INACTIVE);

    if (maxInactive != null) {
      trackActiveRequest = true;
      final long maxInactiveTime = Long.parseLong(maxInactive);
      if (maxInactiveTime <= 0) return;

      closedDetector.scheduleAtFixedRate(
          new Runnable() {
            public void run() {
              for (AtmosphereRequest req : aliveRequests.keySet()) {
                long l = (Long) req.getAttribute(MAX_INACTIVE);
                if (l > 0 && System.currentTimeMillis() - l > maxInactiveTime) {
                  try {
                    logger.debug(
                        "Close detector disconnecting {}. Current size {}",
                        req,
                        aliveRequests.size());
                    AtmosphereResourceImpl r = (AtmosphereResourceImpl) aliveRequests.remove(req);
                    cancelled(req, r.getResponse(false));
                  } catch (Throwable e) {
                    logger.warn("closedDetector", e);
                  } finally {
                    try {
                      req.setAttribute(MAX_INACTIVE, (long) -1);
                    } catch (Throwable t) {
                      logger.trace("closedDetector", t);
                    }
                  }
                }
              }
            }
          },
          0,
          1,
          TimeUnit.SECONDS);
    }
  }
  public void configure(AtmosphereConfig config) {
    this.config = config;

    String maxInactive = config.getInitParameter(MAX_INACTIVE);
    if (maxInactive != null) {
      maxInactiveTime = Long.parseLong(maxInactive);
    }

    if (maxInactiveTime > 0) {
      ExecutorsFactory.getScheduler(config)
          .scheduleAtFixedRate(
              new Runnable() {
                public void run() {
                  idleResources();
                }
              },
              0,
              2,
              TimeUnit.SECONDS);
    }
  }
 public Tomcat7CometSupport(AtmosphereConfig config) {
   super(config);
   Object b = config.getInitParameter(ApplicationConfig.TOMCAT_CLOSE_STREAM);
   closeConnectionOnInputStream = b == null ? true : Boolean.parseBoolean(b.toString());
 }
  /**
   * Create an {@link ExecutorService} to be used for dispatching messages, not I/O events.
   *
   * @param config the {@link AtmosphereConfig}
   * @param name a name to use if shared is false.
   * @return {@link ExecutorService}
   */
  public static ExecutorService getMessageDispatcher(
      final AtmosphereConfig config, final String name) {
    final boolean shared = config.framework().isShareExecutorServices();

    boolean isExecutorShared = shared ? true : false;
    if (!shared || config.properties().get("executorService") == null) {
      int numberOfMessageProcessingThread = DEFAULT_MESSAGE_THREAD;
      String s =
          config.getInitParameter(
              ApplicationConfig.BROADCASTER_MESSAGE_PROCESSING_THREADPOOL_MAXSIZE);
      if (s != null) {
        numberOfMessageProcessingThread = Integer.parseInt(s);
      }

      if (isExecutorShared && numberOfMessageProcessingThread == 1) {
        logger.warn(
            "Not enough numberOfMessageProcessingThread for a shareable thread pool {}, "
                + "Setting it to a newCachedThreadPool",
            numberOfMessageProcessingThread);
        numberOfMessageProcessingThread = -1;
      }

      ThreadPoolExecutor messageService;
      if (numberOfMessageProcessingThread == -1) {
        messageService =
            (ThreadPoolExecutor)
                Executors.newCachedThreadPool(
                    new ThreadFactory() {

                      private final AtomicInteger count = new AtomicInteger();

                      @Override
                      public Thread newThread(final Runnable runnable) {
                        Thread t =
                            new Thread(
                                runnable,
                                (shared ? "Atmosphere-Shared" : name)
                                    + "-DispatchOp-"
                                    + count.getAndIncrement());
                        t.setDaemon(true);
                        return t;
                      }
                    });
      } else {
        messageService =
            (ThreadPoolExecutor)
                Executors.newFixedThreadPool(
                    numberOfMessageProcessingThread,
                    new ThreadFactory() {

                      private final AtomicInteger count = new AtomicInteger();

                      @Override
                      public Thread newThread(final Runnable runnable) {
                        Thread t =
                            new Thread(
                                runnable,
                                (shared ? "Atmosphere-Shared" : name)
                                    + "-DispatchOp-"
                                    + count.getAndIncrement());
                        t.setDaemon(true);
                        return t;
                      }
                    });
      }

      keepAliveThreads(messageService, config);

      if (shared) {
        config.properties().put("executorService", messageService);
      }
      return messageService;
    } else {
      return (ExecutorService) config.properties().get("executorService");
    }
  }