1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.commons.weaver;
20
21 import java.io.File;
22 import java.util.List;
23 import java.util.Properties;
24 import java.util.ServiceLoader;
25 import java.util.logging.Logger;
26
27 import org.apache.commons.weaver.lifecycle.WeaveLifecycle; //NOPMD used in javadoc
28 import org.apache.commons.weaver.model.WeaveEnvironment;
29 import org.apache.commons.weaver.spi.Weaver;
30
31 /**
32 * Implements {@link WeaveLifecycle#WEAVE}.
33 */
34 public class WeaveProcessor extends ProcessorBase<Weaver> {
35
36 /**
37 * Create a new {@link WeaveProcessor} instance using the {@link ServiceLoader} mechanism.
38 *
39 * @param classpath not {@code null}
40 * @param target not {@code null}
41 * @param configuration not {@code null}
42 */
43 public WeaveProcessor(final List<String> classpath, final File target, final Properties configuration) {
44 super(classpath, target, configuration, getServiceInstances(Weaver.class));
45 }
46
47 /**
48 * Create a new {@link WeaveProcessor} instance.
49 *
50 * @param classpath not {@code null}
51 * @param target not {@code null}
52 * @param configuration not {@code null}
53 * @param providers not (@code null}
54 */
55 public WeaveProcessor(final List<String> classpath, final File target, final Properties configuration,
56 final Iterable<Weaver> providers) {
57 super(classpath, target, configuration, providers);
58 }
59
60 /**
61 * Weave classes in target directory.
62 */
63 public void weave() {
64 if (!target.exists()) {
65 log.warning(() -> String.format("Target directory %s does not exist; nothing to do!", target));
66 }
67 for (final Weaver weaver : providers) {
68 final WeaveEnvironment env = new LocalWeaveEnvironment(target, classLoader, configuration,
69 Logger.getLogger(weaver.getClass().getName()));
70 weaver.process(env, finder);
71 }
72 }
73 }