01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.web.model;
18:
19: import java.lang.ref.Reference;
20: import java.lang.ref.SoftReference;
21: import java.util.Map;
22: import java.util.concurrent.ConcurrentHashMap;
23:
24: import javax.management.MBeanServerConnection;
25: import javax.management.MalformedObjectNameException;
26: import javax.management.ObjectName;
27:
28: import org.springframework.jmx.access.MBeanProxyFactoryBean;
29:
30: public class ProxyManager {
31:
32: private final MBeanServerConnection server;
33: private final Map<ObjectName, Reference<Object>> proxies = new ConcurrentHashMap<ObjectName, Reference<Object>>();
34:
35: public ProxyManager(MBeanServerConnection server) {
36: this .server = server;
37: }
38:
39: @SuppressWarnings("unchecked")
40: public <T> T getProxy(ObjectName name, Class<T> type) {
41: Reference r = proxies.get(name);
42: T proxy = (r != null) ? (T) r.get() : null;
43: if (proxy == null) {
44: MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
45: factory.setServer(server);
46: try {
47: factory.setObjectName(name);
48: } catch (MalformedObjectNameException e) {
49: throw new IllegalStateException(e);
50: }
51: factory.setProxyInterface(type);
52: factory.setUseStrictCasing(false);
53: factory.afterPropertiesSet();
54: proxy = (T) factory.getObject();
55: proxies.put(name, new SoftReference<Object>(proxy));
56: }
57: return proxy;
58: }
59:
60: }
|