001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.instrument.classloading.jboss;
018
019import java.lang.instrument.ClassFileTransformer;
020
021import org.springframework.instrument.classloading.LoadTimeWeaver;
022import org.springframework.instrument.classloading.SimpleThrowawayClassLoader;
023import org.springframework.util.Assert;
024import org.springframework.util.ClassUtils;
025
026/**
027 * {@link LoadTimeWeaver} implementation for JBoss's instrumentable ClassLoader.
028 * Autodetects the specific JBoss version at runtime: currently supports
029 * JBoss AS 6 and 7, as well as WildFly 8 and 9 (as of Spring 4.2).
030 *
031 * <p><b>NOTE:</b> On JBoss 6, to avoid the container loading the classes before the
032 * application actually starts, one needs to add a <tt>WEB-INF/jboss-scanning.xml</tt>
033 * file to the application archive - with the following content:
034 * <pre class="code">&lt;scanning xmlns="urn:jboss:scanning:1.0"/&gt;</pre>
035 *
036 * <p>Thanks to Ales Justin and Marius Bogoevici for the initial prototype.
037 *
038 * @author Costin Leau
039 * @author Juergen Hoeller
040 * @since 3.0
041 */
042public class JBossLoadTimeWeaver implements LoadTimeWeaver {
043
044        private final JBossClassLoaderAdapter adapter;
045
046
047        /**
048         * Create a new instance of the {@link JBossLoadTimeWeaver} class using
049         * the default {@link ClassLoader class loader}.
050         * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
051         */
052        public JBossLoadTimeWeaver() {
053                this(ClassUtils.getDefaultClassLoader());
054        }
055
056        /**
057         * Create a new instance of the {@link JBossLoadTimeWeaver} class using
058         * the supplied {@link ClassLoader}.
059         * @param classLoader the {@code ClassLoader} to delegate to for weaving
060         * (must not be {@code null})
061         */
062        public JBossLoadTimeWeaver(ClassLoader classLoader) {
063                Assert.notNull(classLoader, "ClassLoader must not be null");
064                if (classLoader.getClass().getName().startsWith("org.jboss.modules")) {
065                        // JBoss AS 7 or WildFly
066                        this.adapter = new JBossModulesAdapter(classLoader);
067                }
068                else {
069                        // JBoss AS 6
070                        this.adapter = new JBossMCAdapter(classLoader);
071                }
072        }
073
074
075        @Override
076        public void addTransformer(ClassFileTransformer transformer) {
077                this.adapter.addTransformer(transformer);
078        }
079
080        @Override
081        public ClassLoader getInstrumentableClassLoader() {
082                return this.adapter.getInstrumentableClassLoader();
083        }
084
085        @Override
086        public ClassLoader getThrowawayClassLoader() {
087                return new SimpleThrowawayClassLoader(getInstrumentableClassLoader());
088        }
089
090}