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