| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer; |
| 17 | |
|
| 18 | |
import org.dozer.config.BeanContainer; |
| 19 | |
import org.dozer.config.GlobalSettings; |
| 20 | |
import org.dozer.jmx.DozerAdminController; |
| 21 | |
import org.dozer.jmx.DozerStatisticsController; |
| 22 | |
import org.dozer.jmx.JMXPlatform; |
| 23 | |
import org.dozer.jmx.JMXPlatformImpl; |
| 24 | |
import org.dozer.util.DefaultClassLoader; |
| 25 | |
import org.dozer.util.DozerClassLoader; |
| 26 | |
import org.dozer.util.DozerConstants; |
| 27 | |
import org.dozer.util.DozerProxyResolver; |
| 28 | |
import org.dozer.util.MappingUtils; |
| 29 | |
import org.dozer.util.ReflectionUtils; |
| 30 | |
import org.dozer.loader.xml.ExpressionElementReader; |
| 31 | |
import org.dozer.loader.xml.ELEngine; |
| 32 | |
import org.slf4j.Logger; |
| 33 | |
import org.slf4j.LoggerFactory; |
| 34 | |
|
| 35 | |
import javax.management.InstanceAlreadyExistsException; |
| 36 | |
import javax.management.InstanceNotFoundException; |
| 37 | |
import javax.management.MBeanRegistrationException; |
| 38 | |
import javax.management.MalformedObjectNameException; |
| 39 | |
import javax.management.NotCompliantMBeanException; |
| 40 | |
import java.util.ServiceLoader; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public final class DozerInitializer { |
| 50 | |
|
| 51 | 1 | private final Logger log = LoggerFactory.getLogger(DozerInitializer.class); |
| 52 | |
|
| 53 | |
private static final String DOZER_STATISTICS_CONTROLLER = "org.dozer.jmx:type=DozerStatisticsController"; |
| 54 | |
private static final String DOZER_ADMIN_CONTROLLER = "org.dozer.jmx:type=DozerAdminController"; |
| 55 | |
|
| 56 | 1 | private static final DozerInitializer instance = new DozerInitializer(); |
| 57 | |
|
| 58 | 1 | private volatile boolean isInitialized = false; |
| 59 | |
|
| 60 | 1 | private DozerInitializer() { |
| 61 | 1 | } |
| 62 | |
|
| 63 | |
public void init() { |
| 64 | 1137 | init(getClass().getClassLoader()); |
| 65 | 1137 | } |
| 66 | |
|
| 67 | |
public void init(ClassLoader classLoader) { |
| 68 | |
|
| 69 | 1137 | synchronized (this) { |
| 70 | 1137 | if (isInitialized) { |
| 71 | 1132 | log.debug("Tried to perform initialization when Dozer was already started."); |
| 72 | 1132 | return; |
| 73 | |
} |
| 74 | |
|
| 75 | 5 | log.info("Initializing Dozer. Version: {}, Thread Name: {}", |
| 76 | |
DozerConstants.CURRENT_VERSION, Thread.currentThread().getName()); |
| 77 | |
|
| 78 | 5 | GlobalSettings globalSettings = GlobalSettings.getInstance(); |
| 79 | 5 | initialize(globalSettings, classLoader); |
| 80 | |
|
| 81 | 5 | isInitialized = true; |
| 82 | 5 | } |
| 83 | 5 | } |
| 84 | |
|
| 85 | |
void initialize(GlobalSettings globalSettings, ClassLoader classLoader) { |
| 86 | 7 | if (globalSettings.isAutoregisterJMXBeans()) { |
| 87 | |
|
| 88 | |
try { |
| 89 | 5 | registerJMXBeans(new JMXPlatformImpl()); |
| 90 | 0 | } catch (Throwable t) { |
| 91 | 0 | log.warn("Unable to register Dozer JMX MBeans with the PlatformMBeanServer. Dozer will still function " |
| 92 | |
+ "normally, but management via JMX may not be available", t); |
| 93 | 5 | } |
| 94 | |
} |
| 95 | |
|
| 96 | 7 | BeanContainer beanContainer = BeanContainer.getInstance(); |
| 97 | |
|
| 98 | 7 | registerClassLoader(globalSettings, classLoader, beanContainer); |
| 99 | 6 | registerProxyResolver(globalSettings, beanContainer); |
| 100 | |
|
| 101 | 5 | if (globalSettings.isElEnabled()) { |
| 102 | 5 | ELEngine engine = new ELEngine(); |
| 103 | 5 | engine.init(); |
| 104 | 5 | beanContainer.setElEngine(engine); |
| 105 | 5 | beanContainer.setElementReader(new ExpressionElementReader(engine)); |
| 106 | |
} |
| 107 | |
|
| 108 | 5 | for (DozerModule module : ServiceLoader.load(DozerModule.class)) { |
| 109 | 0 | module.init(); |
| 110 | 0 | } |
| 111 | 5 | } |
| 112 | |
|
| 113 | |
private void registerClassLoader(GlobalSettings globalSettings, ClassLoader classLoader, BeanContainer beanContainer) { |
| 114 | 7 | String classLoaderName = globalSettings.getClassLoaderName(); |
| 115 | 7 | if (!DozerConstants.DEFAULT_CLASS_LOADER_BEAN.equals(classLoaderName)) { |
| 116 | 1 | DefaultClassLoader defaultClassLoader = new DefaultClassLoader(classLoader); |
| 117 | 1 | Class<? extends DozerClassLoader> classLoaderType = loadBeanType(classLoaderName, defaultClassLoader, DozerClassLoader.class); |
| 118 | 0 | DozerClassLoader classLoaderBean = ReflectionUtils.newInstance(classLoaderType); |
| 119 | 0 | beanContainer.setClassLoader(classLoaderBean); |
| 120 | |
} |
| 121 | 6 | } |
| 122 | |
|
| 123 | |
private void registerProxyResolver(GlobalSettings globalSettings, BeanContainer beanContainer) { |
| 124 | 6 | String proxyResolverName = globalSettings.getProxyResolverName(); |
| 125 | 6 | if (!DozerConstants.DEFAULT_PROXY_RESOLVER_BEAN.equals(proxyResolverName)) { |
| 126 | 1 | DozerClassLoader initializedClassLoader = beanContainer.getClassLoader(); |
| 127 | 1 | Class<? extends DozerProxyResolver> proxyResolverType = loadBeanType(proxyResolverName, initializedClassLoader, DozerProxyResolver.class); |
| 128 | 0 | DozerProxyResolver proxyResolverBean = ReflectionUtils.newInstance(proxyResolverType); |
| 129 | 0 | beanContainer.setProxyResolver(proxyResolverBean); |
| 130 | |
} |
| 131 | 5 | } |
| 132 | |
|
| 133 | |
private <T> Class<? extends T> loadBeanType(String classLoaderName, DozerClassLoader classLoader, Class<T> iface) { |
| 134 | 2 | Class<?> beanType = classLoader.loadClass(classLoaderName); |
| 135 | 1 | if (beanType != null && !iface.isAssignableFrom(beanType)) { |
| 136 | 1 | MappingUtils.throwMappingException("Incompatible types: " + iface.getName() + " and " + classLoaderName); |
| 137 | |
} |
| 138 | 0 | return (Class<? extends T>) beanType; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public void destroy() { |
| 145 | 13 | synchronized (this) { |
| 146 | 13 | if (!isInitialized) { |
| 147 | 9 | log.debug("Tried to destroy when no Dozer instance started."); |
| 148 | 9 | return; |
| 149 | |
} |
| 150 | |
|
| 151 | |
try { |
| 152 | 4 | unregisterJMXBeans(new JMXPlatformImpl()); |
| 153 | 0 | } catch (Throwable e) { |
| 154 | 0 | log.warn("Exception caught while disposing Dozer JMX MBeans.", e); |
| 155 | 4 | } |
| 156 | 4 | isInitialized = false; |
| 157 | 4 | } |
| 158 | 4 | } |
| 159 | |
|
| 160 | |
public boolean isInitialized() { |
| 161 | 8 | return isInitialized; |
| 162 | |
} |
| 163 | |
|
| 164 | |
private void registerJMXBeans(JMXPlatform platform) throws MalformedObjectNameException, InstanceNotFoundException, |
| 165 | |
MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException { |
| 166 | 5 | if (platform.isAvailable()) { |
| 167 | 5 | platform.registerMBean(DOZER_STATISTICS_CONTROLLER, new DozerStatisticsController()); |
| 168 | 5 | platform.registerMBean(DOZER_ADMIN_CONTROLLER, new DozerAdminController()); |
| 169 | |
} else { |
| 170 | 0 | log.warn("jdk1.5 jmx management classes unavailable. Dozer JMX MBeans will not be auto registered."); |
| 171 | |
} |
| 172 | 5 | } |
| 173 | |
|
| 174 | |
private void unregisterJMXBeans(JMXPlatform platform) throws MBeanRegistrationException, MalformedObjectNameException { |
| 175 | 4 | if (platform.isAvailable()) { |
| 176 | 4 | platform.unregisterMBean(DOZER_ADMIN_CONTROLLER); |
| 177 | 4 | platform.unregisterMBean(DOZER_STATISTICS_CONTROLLER); |
| 178 | |
} else { |
| 179 | 0 | log.warn("jdk1.5 jmx management classes unavailable."); |
| 180 | |
} |
| 181 | 4 | } |
| 182 | |
|
| 183 | |
public static DozerInitializer getInstance() { |
| 184 | 1141 | return instance; |
| 185 | |
} |
| 186 | |
|
| 187 | |
} |