001/*
002 * Copyright 2002-2017 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.websphere;
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 WebSphere's instrumentable ClassLoader.
029 * Compatible with WebSphere 7 as well as 8 and 9.
030 *
031 * @author Costin Leau
032 * @since 3.1
033 */
034public class WebSphereLoadTimeWeaver implements LoadTimeWeaver {
035
036        private final WebSphereClassLoaderAdapter classLoader;
037
038
039        /**
040         * Create a new instance of the {@link WebSphereLoadTimeWeaver} class using
041         * the default {@link ClassLoader class loader}.
042         * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
043         */
044        public WebSphereLoadTimeWeaver() {
045                this(ClassUtils.getDefaultClassLoader());
046        }
047
048        /**
049         * Create a new instance of the {@link WebSphereLoadTimeWeaver} class using
050         * the supplied {@link ClassLoader}.
051         * @param classLoader the {@code ClassLoader} to delegate to for weaving
052         */
053        public WebSphereLoadTimeWeaver(@Nullable ClassLoader classLoader) {
054                Assert.notNull(classLoader, "ClassLoader must not be null");
055                this.classLoader = new WebSphereClassLoaderAdapter(classLoader);
056        }
057
058
059        @Override
060        public void addTransformer(ClassFileTransformer transformer) {
061                this.classLoader.addTransformer(transformer);
062        }
063
064        @Override
065        public ClassLoader getInstrumentableClassLoader() {
066                return this.classLoader.getClassLoader();
067        }
068
069        @Override
070        public ClassLoader getThrowawayClassLoader() {
071                return new OverridingClassLoader(this.classLoader.getClassLoader(),
072                                this.classLoader.getThrowawayClassLoader());
073        }
074
075}