001    /*
002     * Copyright (C) 2006-2007 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.gmaven.runtime.loader.artifact;
018    
019    import org.apache.maven.artifact.Artifact;
020    import org.apache.maven.artifact.DefaultArtifact;
021    import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
022    import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
023    import org.codehaus.gmaven.feature.Provider;
024    import org.codehaus.gmaven.feature.ProviderLoader;
025    import org.codehaus.gmaven.runtime.loader.realm.RealmManager;
026    import org.codehaus.plexus.classworlds.realm.ClassRealm;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    import java.io.File;
031    import java.net.URL;
032    import java.util.ArrayList;
033    import java.util.HashMap;
034    import java.util.Iterator;
035    import java.util.List;
036    import java.util.Map;
037    
038    /**
039     * Loads a provider based on a configured {@link ArtifactHandler}.
040     *
041     * @plexus.component role="org.codehaus.gmaven.feature.ProviderLoader" role-hint="artifact"
042     *
043     * @version $Id: ArtifactProviderLoader.java 21 2009-07-16 09:42:35Z user57 $
044     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
045    */
046    public class ArtifactProviderLoader
047        implements ProviderLoader
048    {
049        private final Logger log = LoggerFactory.getLogger(getClass());
050    
051        /**
052         * @plexus.requirement
053         * 
054         * @noinspection UnusedDeclaration
055         */
056        private RealmManager realmManager;
057    
058        private ArtifactHandler handler;
059    
060        public ArtifactProviderLoader() {}
061        
062        public ArtifactHandler getHandler() {
063            return handler;
064        }
065    
066        public void setHandler(final ArtifactHandler handler) {
067            this.handler = handler;
068        }
069    
070        public Map load(final String key) throws Exception {
071            assert key != null;
072    
073            if (handler == null) {
074                log.error("Artifact handler has not been configured; unable to load anything");
075                return null;
076            }
077    
078            Provider provider = loadProvider(key);
079    
080            Map providers = new HashMap();
081            
082            providers.put(provider.key(), provider);
083    
084            return providers;
085        }
086    
087        private URL[] buildClassPath(final Artifact query) throws Exception {
088            assert query != null;
089    
090            Artifact artifact = handler.createDependency(query);
091            ArtifactResolutionResult result = handler.resolve(artifact, new ScopeArtifactFilter(DefaultArtifact.SCOPE_RUNTIME));
092    
093            List classPath = new ArrayList();
094    
095            // Add runtime dependency classpath
096            for (Iterator iter = result.getArtifacts().iterator(); iter.hasNext();) {
097                Artifact element = (Artifact) iter.next();
098    
099                File file = element.getFile();
100                URL url = file.toURI().toURL();
101    
102                classPath.add(url);
103            }
104    
105            return (URL[]) classPath.toArray(new URL[classPath.size()]);
106        }
107    
108        private Provider loadProvider(final String key) throws Exception {
109            assert key != null;
110    
111            log.debug("Loading providers: {}", key);
112    
113            Artifact query = handler.createQuery(key);
114            URL[] classPath = buildClassPath(query);
115            ClassLoader parent = getClass().getClassLoader();
116            ClassRealm realm = realmManager.createProviderRealm(key, classPath, parent);
117            
118            Class type = realm.loadClass("org.codehaus.gmaven.runtime.v" + key.replace('.', '_').replace('-', '_') + ".ProviderImpl");
119    
120            return (Provider) type.newInstance();
121        }
122    }