001/*
002 * Copyright 2002-2016 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.weblogic;
018
019import java.lang.instrument.ClassFileTransformer;
020
021import org.springframework.core.OverridingClassLoader;
022import org.springframework.instrument.classloading.LoadTimeWeaver;
023import org.springframework.util.Assert;
024import org.springframework.util.ClassUtils;
025
026/**
027 * {@link LoadTimeWeaver} implementation for WebLogic's instrumentable
028 * ClassLoader.
029 *
030 * <p><b>NOTE:</b> Requires BEA WebLogic version 10 or higher.
031 *
032 * @author Costin Leau
033 * @author Juergen Hoeller
034 * @since 2.5
035 */
036public class WebLogicLoadTimeWeaver implements LoadTimeWeaver {
037
038        private final WebLogicClassLoaderAdapter classLoader;
039
040
041        /**
042         * Creates a new instance of the {@link WebLogicLoadTimeWeaver} class using
043         * the default {@link ClassLoader class loader}.
044         * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
045         */
046        public WebLogicLoadTimeWeaver() {
047                this(ClassUtils.getDefaultClassLoader());
048        }
049
050        /**
051         * Creates a new instance of the {@link WebLogicLoadTimeWeaver} class using
052         * the supplied {@link ClassLoader}.
053         * @param classLoader the {@code ClassLoader} to delegate to for
054         * weaving (must not be {@code null})
055         */
056        public WebLogicLoadTimeWeaver(ClassLoader classLoader) {
057                Assert.notNull(classLoader, "ClassLoader must not be null");
058                this.classLoader = new WebLogicClassLoaderAdapter(classLoader);
059        }
060
061
062        @Override
063        public void addTransformer(ClassFileTransformer transformer) {
064                this.classLoader.addTransformer(transformer);
065        }
066
067        @Override
068        public ClassLoader getInstrumentableClassLoader() {
069                return this.classLoader.getClassLoader();
070        }
071
072        @Override
073        public ClassLoader getThrowawayClassLoader() {
074                return new OverridingClassLoader(this.classLoader.getClassLoader(),
075                                this.classLoader.getThrowawayClassLoader());
076        }
077
078}