All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.geronimo.persistence.TransformerWrapper Maven / Gradle / Ivy

The newest version!
/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.geronimo.persistence;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

import javax.persistence.spi.ClassTransformer;

import org.apache.xbean.osgi.bundle.util.BundleUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleReference;

/**
 * @version $Rev: 1162632 $ $Date: 2011-08-29 10:14:06 +0800 (Mon, 29 Aug 2011) $
 */
public class TransformerWrapper implements ClassFileTransformer {

    private final ClassTransformer classTransformer;
    private final Bundle bundle;

    public TransformerWrapper(ClassTransformer classTransformer, Bundle bundle) {
        this.classTransformer = classTransformer;
        this.bundle = BundleUtils.unwrapBundle(bundle);
    }

    public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        if (loader instanceof BundleReference && ((BundleReference) loader).getBundle() == bundle) {
            try {
                return classTransformer.transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
            } catch (IllegalClassFormatException e) {
                throw e;
            } catch (RuntimeException e) {
                return null;
            }
        }
        return null;

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy