~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/management/ManagementServerLoader.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  Copyright Terracotta, Inc.
 
3
 *
 
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
5
 *  you may not use this file except in compliance with the License.
 
6
 *  You may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 *  Unless required by applicable law or agreed to in writing, software
 
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 *  See the License for the specific language governing permissions and
 
14
 *  limitations under the License.
 
15
 */
 
16
 
 
17
package net.sf.ehcache.management;
 
18
 
 
19
import java.lang.reflect.Constructor;
 
20
import java.lang.reflect.Method;
 
21
import java.util.HashMap;
 
22
import java.util.Map;
 
23
 
 
24
import net.sf.ehcache.CacheManager;
 
25
import net.sf.ehcache.config.ManagementRESTServiceConfiguration;
 
26
 
 
27
import org.slf4j.Logger;
 
28
import org.slf4j.LoggerFactory;
 
29
 
 
30
/**
 
31
 *
 
32
 * ManagementServerLoader is a facility class to access the rest management interface
 
33
 * One would use it form EhCache or QuartzScheduler to start or stop a new rest management interface
 
34
 * or to simply register a cache or a scheduler to an already started management interface.
 
35
 *
 
36
 * It uses internally a ResourceClassLoader to load classes from a rest agent jar.
 
37
 *
 
38
 * @author Anthony Dahanne
 
39
 *
 
40
 */
 
41
public class ManagementServerLoader {
 
42
 
 
43
    private static final String PRIVATE_CLASSPATH = "rest-management-private-classpath";
 
44
    private static final Map<String, Object> MGMT_SVR_BY_BIND = new HashMap<String, Object>();
 
45
 
 
46
    private static final ResourceClassLoader RESOURCE_CLASS_LOADER;
 
47
    private static final Logger LOG = LoggerFactory.getLogger(ManagementServerLoader.class);
 
48
 
 
49
    static {
 
50
            RESOURCE_CLASS_LOADER = new ResourceClassLoader(PRIVATE_CLASSPATH, CacheManager.class.getClassLoader());
 
51
    }
 
52
 
 
53
    /**
 
54
     * Register a cacheManager to management rest server.
 
55
     * If the server does not exist, starts it.
 
56
     *
 
57
     * @param cacheManager
 
58
     * @param managementRESTServiceConfiguration
 
59
     */
 
60
    public static void register(CacheManager cacheManager, ManagementRESTServiceConfiguration managementRESTServiceConfiguration) {
 
61
 
 
62
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
 
63
        try {
 
64
            // because some code in Jersey is using the TCCL to resolve some classes
 
65
            Thread.currentThread().setContextClassLoader(RESOURCE_CLASS_LOADER);
 
66
 
 
67
            Class<?> managementServerImplClass = RESOURCE_CLASS_LOADER.loadClass("net.sf.ehcache.management.ManagementServerImpl");
 
68
            Object managementServerImpl = null;
 
69
            if (!MGMT_SVR_BY_BIND.containsKey(managementRESTServiceConfiguration.getBind())) {
 
70
                if (!MGMT_SVR_BY_BIND.isEmpty()) {
 
71
                    String alreadyBound = MGMT_SVR_BY_BIND.keySet().iterator().next();
 
72
                    managementRESTServiceConfiguration.setBind(alreadyBound);
 
73
                    LOG.warn("You can not have several Ehcache management rest agents running in the same ClassLoader; CacheManager "
 
74
                          + cacheManager.getName() + " will be registered to the already running Ehcache management rest agent listening on port "
 
75
                          + alreadyBound + ", the configuration will not be changed");
 
76
                } else {
 
77
                    startRestAgent(managementRESTServiceConfiguration, managementServerImplClass);
 
78
                }
 
79
            } else {
 
80
                LOG.warn("A previous CacheManager already instanciated the Ehcache Management rest agent, on port "
 
81
                        + managementRESTServiceConfiguration.getBind() + ", the configuration will not be changed for " + cacheManager.getName());
 
82
            }
 
83
            managementServerImpl = MGMT_SVR_BY_BIND.get(managementRESTServiceConfiguration.getBind());
 
84
            Method registerMethod = managementServerImplClass.getMethod("register", new Class[] {cacheManager.getClass()});
 
85
            registerMethod.invoke(managementServerImpl, cacheManager);
 
86
 
 
87
        } catch (Exception e) {
 
88
            if (e.getCause() instanceof ClassNotFoundException) {
 
89
                throw new RuntimeException(
 
90
                        "Failed to initialize the ManagementRESTService - Did you include ehcache-rest-agent on the classpath?", e);
 
91
            } else {
 
92
                throw new RuntimeException("Failed to instantiate ManagementServer.", e);
 
93
            }
 
94
        } finally {
 
95
            // setting back the appClassLoader as the TCCL
 
96
            Thread.currentThread().setContextClassLoader(contextClassLoader);
 
97
        }
 
98
    }
 
99
 
 
100
    private static void startRestAgent(ManagementRESTServiceConfiguration managementRESTServiceConfiguration,
 
101
            Class<?> managementServerImplClass) throws Exception {
 
102
        Object managementServerImpl;
 
103
        Constructor<?> managementServerImplClassConstructor = managementServerImplClass
 
104
                .getConstructor(new Class[] {managementRESTServiceConfiguration.getClass()});
 
105
        managementServerImpl = managementServerImplClassConstructor.newInstance(new Object[] {managementRESTServiceConfiguration});
 
106
        Method startMethod = managementServerImplClass.getMethod("start", new Class[] {});
 
107
        startMethod.invoke(managementServerImpl, new Object[] {});
 
108
        MGMT_SVR_BY_BIND.put(managementRESTServiceConfiguration.getBind(), managementServerImpl);
 
109
    }
 
110
 
 
111
    /**
 
112
     * Unregister a cache manager from a management rest server
 
113
     * If it is the last cache manager bound to this server, stops the server too.
 
114
     *
 
115
     * @param registeredMgmtSvrBind
 
116
     * @param cacheManager
 
117
     */
 
118
    public static void unregister(String registeredMgmtSvrBind, CacheManager cacheManager) {
 
119
        Object managementServerImpl = MGMT_SVR_BY_BIND.get(registeredMgmtSvrBind);
 
120
 
 
121
        Class<?> managementServerImplClass;
 
122
        boolean removeMgmtSvr = false;
 
123
        try {
 
124
            managementServerImplClass = RESOURCE_CLASS_LOADER.loadClass("net.sf.ehcache.management.ManagementServerImpl");
 
125
            Method registerMethod = managementServerImplClass.getMethod("unregister", new Class[] {cacheManager.getClass()});
 
126
            registerMethod.invoke(managementServerImpl, cacheManager);
 
127
 
 
128
            Method hasRegisteredMethod = managementServerImplClass.getMethod("hasRegistered", new Class[] {});
 
129
            Boolean hasRegistered = (Boolean) hasRegisteredMethod.invoke(managementServerImpl, new Object[] {});
 
130
 
 
131
            if (!hasRegistered) {
 
132
                removeMgmtSvr = true;
 
133
                Method stopMethod = managementServerImplClass.getMethod("stop", new Class[] {});
 
134
                stopMethod.invoke(managementServerImpl, new Object[] {});
 
135
            }
 
136
 
 
137
        } catch (Exception e) {
 
138
            LOG.warn("Failed to shutdown the ManagementRESTService", e);
 
139
        } finally {
 
140
            if (removeMgmtSvr) {
 
141
                MGMT_SVR_BY_BIND.remove(registeredMgmtSvrBind);
 
142
            }
 
143
        }
 
144
 
 
145
    }
 
146
 
 
147
}