001 /*
002 * Copyright 2009-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.codehaus.griffon.runtime.core;
018
019 import griffon.core.ArtifactInfo;
020 import griffon.core.GriffonApplication;
021 import groovy.util.ConfigObject;
022 import groovy.util.ConfigSlurper;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025
026 import java.io.IOException;
027 import java.lang.reflect.Modifier;
028 import java.net.URL;
029 import java.util.*;
030
031 import static org.codehaus.griffon.runtime.util.GriffonApplicationHelper.loadClass;
032
033 /**
034 * Default implementation of {@code ArtifactManager}.
035 *
036 * @author Andres Almiray
037 * @since 0.9.2
038 */
039 public class DefaultArtifactManager extends AbstractArtifactManager {
040 private static final Logger LOG = LoggerFactory.getLogger(DefaultArtifactManager.class);
041
042 public DefaultArtifactManager(GriffonApplication app) {
043 super(app);
044 }
045
046 protected Map<String, List<ArtifactInfo>> doLoadArtifactMetadata() {
047 Map<String, List<ArtifactInfo>> artifacts = new LinkedHashMap<String, List<ArtifactInfo>>();
048 try {
049 Enumeration<URL> urls = getApp().getClass().getClassLoader().getResources("META-INF/griffon-artifacts.properties");
050 ConfigSlurper slurper = new ConfigSlurper();
051 while (urls.hasMoreElements()) {
052 processURL(urls.nextElement(), slurper, artifacts);
053 }
054 } catch (IOException e) {
055 throw new IllegalArgumentException(e);
056 }
057
058 return artifacts;
059 }
060
061 private void processURL(URL url, ConfigSlurper slurper, Map<String, List<ArtifactInfo>> artifacts) {
062 Properties p = new Properties();
063 try {
064 p.load(url.openStream());
065 } catch (IOException e) {
066 return;
067 }
068
069 ConfigObject config = slurper.parse(p);
070 if (LOG.isDebugEnabled()) {
071 LOG.debug("Loading artifact definitions from " + url);
072 }
073
074 for (Object key : config.keySet()) {
075 String type = key.toString();
076 String classes = (String) config.get(type);
077 if (classes.startsWith("'") && classes.endsWith("'")) {
078 classes = classes.substring(1, classes.length() - 1);
079 }
080 String[] classNames = classes.split(",");
081 if (LOG.isDebugEnabled()) {
082 LOG.debug("Artifacts of type '" + type + "' = " + classNames.length);
083 }
084
085 List<ArtifactInfo> list = artifacts.get(type);
086 if (list == null) {
087 list = new ArrayList<ArtifactInfo>();
088 artifacts.put(type, list);
089 }
090
091 for (String className : classNames) {
092 try {
093 Class clazz = loadClass(className);
094 if (Modifier.isAbstract(clazz.getModifiers())) continue;
095 ArtifactInfo info = new ArtifactInfo(clazz, type);
096 if (!list.contains(info)) list.add(info);
097 } catch (ClassNotFoundException e) {
098 throw new IllegalArgumentException(e);
099 }
100 }
101 }
102 }
103 }
|