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