Exemplo n.º 1
0
  /**
   * Unregister a cache manager from a management rest server If it is the last cache manager bound
   * to this server, stops the server too.
   *
   * @param registeredMgmtSvrBind the bind identifying what to un-register from
   * @param cacheManager the cacheManager to un-register
   */
  public static void unregister(String registeredMgmtSvrBind, CacheManager cacheManager) {
    Object managementServerImpl = MGMT_SVR_BY_BIND.get(registeredMgmtSvrBind);

    Class<?> managementServerImplClass;
    boolean removeMgmtSvr = false;
    try {
      managementServerImplClass =
          RESOURCE_CLASS_LOADER.loadClass("net.sf.ehcache.management.ManagementServerImpl");
      Method registerMethod =
          managementServerImplClass.getMethod("unregister", new Class[] {cacheManager.getClass()});
      registerMethod.invoke(managementServerImpl, cacheManager);

      Method hasRegisteredMethod =
          managementServerImplClass.getMethod("hasRegistered", new Class[] {});
      Boolean hasRegistered =
          (Boolean) hasRegisteredMethod.invoke(managementServerImpl, new Object[] {});

      // there are no more cacheManagers registered to the rest agent, we can now stop it
      if (!hasRegistered) {
        removeMgmtSvr = true;
        Method stopMethod = managementServerImplClass.getMethod("stop", new Class[] {});
        stopMethod.invoke(managementServerImpl, new Object[] {});
      }

    } catch (Exception e) {
      LOG.warn("Failed to shutdown the ManagementRESTService", e);
    } finally {
      if (removeMgmtSvr) {
        MGMT_SVR_BY_BIND.remove(registeredMgmtSvrBind);
      }
    }
  }
Exemplo n.º 2
0
  /**
   * Register a cacheManager to management rest server. If the server does not exist, starts it.
   *
   * @param cacheManager the cacheManager to register
   * @param clientUUID the client UUID
   * @param managementRESTServiceConfiguration the management configuration
   */
  public static void register(
      CacheManager cacheManager,
      String clientUUID,
      ManagementRESTServiceConfiguration managementRESTServiceConfiguration) {

    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      // because some code in Jersey is using the TCCL to resolve some classes
      Thread.currentThread().setContextClassLoader(RESOURCE_CLASS_LOADER);

      Class<?> managementServerImplClass =
          RESOURCE_CLASS_LOADER.loadClass("net.sf.ehcache.management.ManagementServerImpl");
      Object managementServerImpl = null;
      if (!MGMT_SVR_BY_BIND.containsKey(managementRESTServiceConfiguration.getBind())) {
        if (!MGMT_SVR_BY_BIND.isEmpty()) {
          String alreadyBound = MGMT_SVR_BY_BIND.keySet().iterator().next();
          managementRESTServiceConfiguration.setBind(alreadyBound);
          LOG.warn(
              "You can not have several Ehcache management rest agents running in the same ClassLoader; CacheManager "
                  + cacheManager.getName()
                  + " will be registered to the already running Ehcache management rest agent listening on port "
                  + alreadyBound
                  + ", the configuration will not be changed");
        } else {
          startRestAgent(managementRESTServiceConfiguration, managementServerImplClass, clientUUID);
        }
      } else {
        LOG.warn(
            "A previous CacheManager already instantiated the Ehcache Management rest agent"
                + (ManagementRESTServiceConfiguration.NO_BIND.equals(
                        managementRESTServiceConfiguration.getBind())
                    ? ", reachable only through the TSA agent"
                    : ", on port " + managementRESTServiceConfiguration.getBind())
                + ", the configuration will not be changed for "
                + cacheManager.getName());
      }
      managementServerImpl = MGMT_SVR_BY_BIND.get(managementRESTServiceConfiguration.getBind());
      Method registerMethod =
          managementServerImplClass.getMethod("register", new Class[] {cacheManager.getClass()});
      registerMethod.invoke(managementServerImpl, cacheManager);

    } catch (Exception e) {
      if (e.getCause() instanceof ClassNotFoundException) {
        throw new RuntimeException(
            "Failed to initialize the ManagementRESTService - Did you include ehcache-rest-agent on the classpath?",
            e);
      } else {
        throw new RuntimeException("Failed to instantiate ManagementServer.", e);
      }
    } finally {
      // setting back the appClassLoader as the TCCL
      Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
  }