01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2008-2017 the original author or authors.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.codehaus.griffon.runtime.core.artifact;
19
20 import griffon.core.ApplicationClassLoader;
21 import griffon.core.artifact.ArtifactHandler;
22 import griffon.core.artifact.GriffonArtifact;
23 import griffon.inject.Typed;
24 import griffon.util.ServiceLoaderUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.annotation.Nonnull;
29 import javax.inject.Inject;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import static java.util.Objects.requireNonNull;
36
37 /**
38 * Default implementation of {@code ArtifactManager}.
39 *
40 * @author Andres Almiray
41 * @since 2.0.0
42 */
43 public class DefaultArtifactManager extends AbstractArtifactManager {
44 private static final Logger LOG = LoggerFactory.getLogger(DefaultArtifactManager.class);
45
46 private final ApplicationClassLoader applicationClassLoader;
47
48 @Inject
49 public DefaultArtifactManager(@Nonnull ApplicationClassLoader applicationClassLoader) {
50 this.applicationClassLoader = requireNonNull(applicationClassLoader, "Argument 'applicationClassLoader' must not be null");
51 }
52
53 @Nonnull
54 @SuppressWarnings({"unchecked", "rawtypes"})
55 protected Map<String, List<Class<? extends GriffonArtifact>>> doLoadArtifactMetadata() {
56 final Map<String, List<Class<? extends GriffonArtifact>>> artifacts = new LinkedHashMap<>();
57
58 for (Map.Entry<String, ArtifactHandler> e : getArtifactHandlers().entrySet()) {
59 final String artifactType = e.getKey();
60 ArtifactHandler<?> artifactHandler = e.getValue();
61 Class<?> klass = artifactHandler.getClass().getAnnotation(Typed.class).value();
62 ServiceLoaderUtils.load(applicationClassLoader.get(), "META-INF/griffon/", klass, new ServiceLoaderUtils.LineProcessor() {
63 @Override
64 public void process(@Nonnull ClassLoader classLoader, @Nonnull Class<?> type, @Nonnull String line) {
65 List<Class<? extends GriffonArtifact>> list = artifacts.get(artifactType);
66 if (list == null) {
67 list = new ArrayList<>();
68 artifacts.put(artifactType, list);
69 }
70
71 try {
72 String className = line.trim();
73 Class<? extends GriffonArtifact> clazz = (Class<? extends GriffonArtifact>) applicationClassLoader.get().loadClass(className);
74 // if (Modifier.isAbstract(clazz.getModifiers())) return;
75 list.add(clazz);
76 } catch (ClassNotFoundException e) {
77 throw new IllegalArgumentException(e);
78 }
79 }
80 });
81 }
82
83 for (Map.Entry<String, List<Class<? extends GriffonArtifact>>> e : artifacts.entrySet()) {
84 LOG.debug("Artifacts of type '{}' = {}", e.getKey(), e.getValue().size());
85 }
86
87 return artifacts;
88 }
89 }
|