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.lang.Nullable;
024import org.springframework.util.Assert;
025import org.springframework.util.ClassUtils;
026
027/**
028 * {@link LoadTimeWeaver} implementation for WebLogic's instrumentable
029 * ClassLoader.
030 *
031 * <p><b>NOTE:</b> Requires BEA WebLogic version 10 or higher.
032 *
033 * @author Costin Leau
034 * @author Juergen Hoeller
035 * @since 2.5
036 */
037public class WebLogicLoadTimeWeaver implements LoadTimeWeaver {
038
039        private final WebLogicClassLoaderAdapter classLoader;
040
041
042        /**
043         * Creates a new instance of the {@link WebLogicLoadTimeWeaver} class using
044         * the default {@link ClassLoader class loader}.
045         * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
046         */
047        public WebLogicLoadTimeWeaver() {
048                this(ClassUtils.getDefaultClassLoader());
049        }
050
051        /**
052         * Creates a new instance of the {@link WebLogicLoadTimeWeaver} class using
053         * the supplied {@link ClassLoader}.
054         * @param classLoader the {@code ClassLoader} to delegate to for weaving
055         */
056        public WebLogicLoadTimeWeaver(@Nullable 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}