001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.weaver;
020
021import java.io.File;
022import java.net.URLClassLoader;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.LinkedHashSet;
026import java.util.List;
027import java.util.Properties;
028import java.util.ServiceLoader;
029import java.util.Set;
030import java.util.logging.Logger;
031
032import org.apache.commons.lang3.Validate;
033import org.apache.commons.weaver.model.WeaveEnvironment;
034import org.apache.commons.weaver.spi.Cleaner;
035import org.apache.commons.weaver.utils.URLArray;
036import org.apache.xbean.finder.archive.FileArchive;
037
038/**
039 * This class discovers and invokes available {@link Cleaner} plugins.
040 */
041public class CleanProcessor {
042    private static final Logger LOG = Logger.getLogger(CleanProcessor.class.getName());
043
044    /**
045     * List of picked up cleaner plugins.
046     */
047    private static final List<Cleaner> CLEANERS;
048
049    static {
050        final List<Cleaner> cleaners = new ArrayList<Cleaner>();
051        for (final Cleaner cleaner : ServiceLoader.load(Cleaner.class)) {
052            cleaners.add(cleaner);
053        }
054        CLEANERS = Collections.unmodifiableList(cleaners);
055    }
056
057    /**
058     * The classpath which will be used to look up cross references during cleaning.
059     */
060    private final List<String> classpath;
061
062    /**
063     * The actual path to be woven, replacing any affected classes.
064     */
065    private final File target;
066
067    /**
068     * Properties for configuring discovered plugin modules.
069     */
070    private final Properties configuration;
071
072    /**
073     * Create a new {@link CleanProcessor} instance.
074     *
075     * @param classpath not {@code null}
076     * @param target not {@code null}
077     * @param configuration not {@code null}
078     */
079    public CleanProcessor(final List<String> classpath, final File target, final Properties configuration) {
080        super();
081        this.classpath = Validate.notNull(classpath, "classpath");
082        this.target = Validate.notNull(target, "target");
083        Validate.isTrue(!target.exists() || target.isDirectory(), "%s is not a directory", target);
084        this.configuration = Validate.notNull(configuration, "configuration");
085    }
086
087    /**
088     * Clean specified targets.
089     */
090    public void clean() {
091        if (!target.exists()) {
092            LOG.warning("Target directory " + target + " does not exist; nothing to do!");
093        }
094        final Set<String> finderClasspath = new LinkedHashSet<String>();
095        finderClasspath.add(target.getAbsolutePath());
096        finderClasspath.addAll(classpath);
097        final ClassLoader classLoader = new URLClassLoader(URLArray.fromPaths(finderClasspath));
098        final Finder finder = new Finder(new FileArchive(classLoader, target));
099        for (final Cleaner cleaner : CLEANERS) {
100            final WeaveEnvironment env =
101                new LocalWeaveEnvironment(target, classLoader, configuration, Logger.getLogger(cleaner.getClass()
102                    .getName()));
103            cleaner.clean(env, finder);
104        }
105    }
106}