001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.client.builder;
022    
023    import java.io.IOException;
024    import java.util.Collection;
025    import java.util.LinkedHashSet;
026    
027    import org.apache.geronimo.kernel.repository.ArtifactManager;
028    import org.apache.geronimo.kernel.repository.Repository;
029    import org.apache.geronimo.kernel.repository.ListableRepository;
030    import org.apache.geronimo.kernel.repository.ArtifactResolver;
031    import org.apache.geronimo.kernel.repository.Artifact;
032    import org.apache.geronimo.kernel.repository.MissingDependencyException;
033    import org.apache.geronimo.kernel.repository.MultipleMatchesException;
034    import org.apache.geronimo.system.resolver.ExplicitDefaultArtifactResolver;
035    import org.apache.geronimo.system.serverinfo.ServerInfo;
036    import org.apache.geronimo.gbean.GBeanInfo;
037    import org.apache.geronimo.gbean.GBeanInfoBuilder;
038    
039    /**
040     * This class is intended to get around some problems using the normal ExplicitDefaultArtifactResolver for client building.
041     * We really want it to refer to the server's client_artifact_aliases.properties file, but that isn't available when run
042     * from the car-maven-plugin.  Also the ServerInfo is missing and the ArtifactManager is not in a parent configuration
043     * (the car-maven-plugin starts a configuration that isn't a parent of anything.  We might be able to fix that by a
044     * use of artifact_aliases.properties itself, but that might be for later).
045     *
046     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
047     */
048    public class OptionalExplicitDefaultArtifactResolver implements ArtifactResolver {
049    
050        private final ArtifactResolver delegate;
051    
052        public OptionalExplicitDefaultArtifactResolver(String versionMapLocation, Collection<ArtifactManager> artifactManagers, Collection<ListableRepository> repositories, Collection<ServerInfo> serverInfos, Collection<ArtifactResolver> fallbackResolver) throws IOException {
053            ServerInfo serverInfo = getServerInfo(serverInfos);
054            if (serverInfo != null) {
055                delegate = new ExplicitDefaultArtifactResolver(versionMapLocation, getArtifactManager(artifactManagers), repositories, serverInfo);
056            } else {
057                if (fallbackResolver == null || fallbackResolver.isEmpty()) {
058                    throw new IllegalStateException("No ServerInfo and no delegate ArtifactResolver supplied");
059                }
060                delegate = fallbackResolver.iterator().next();
061            }
062        }
063    
064        private static ServerInfo getServerInfo(Collection<ServerInfo> serverInfo) {
065            if (serverInfo == null || serverInfo.isEmpty()) {
066                return null;
067            } else {
068                return serverInfo.iterator().next();
069            }
070        }
071    
072        private static ArtifactManager getArtifactManager(Collection<ArtifactManager> artifactManagers) {
073            if (artifactManagers == null || artifactManagers.isEmpty()) {
074                throw new IllegalStateException("No ArtifactManager found");
075            }
076            return artifactManagers.iterator().next();
077        }
078    
079        public Artifact generateArtifact(Artifact source, String defaultType) {
080            return delegate.generateArtifact(source, defaultType);
081        }
082    
083        public Artifact resolveInClassLoader(Artifact source) throws MissingDependencyException {
084            return delegate.resolveInClassLoader(source);
085        }
086    
087        public Artifact resolveInClassLoader(Artifact source, Collection parentConfigurations) throws MissingDependencyException {
088            return delegate.resolveInClassLoader(source, parentConfigurations);
089        }
090    
091        public LinkedHashSet resolveInClassLoader(Collection artifacts) throws MissingDependencyException {
092            return delegate.resolveInClassLoader(artifacts);
093        }
094    
095        public LinkedHashSet resolveInClassLoader(Collection artifacts, Collection parentConfigurations) throws MissingDependencyException {
096            return delegate.resolveInClassLoader(artifacts, parentConfigurations);
097        }
098    
099        public Artifact queryArtifact(Artifact artifact) throws MultipleMatchesException {
100            return delegate.queryArtifact(artifact);
101        }
102    
103        public Artifact[] queryArtifacts(Artifact artifact) {
104            return delegate.queryArtifacts(artifact);
105        }
106    
107        public static final GBeanInfo GBEAN_INFO;
108    
109        static {
110            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(OptionalExplicitDefaultArtifactResolver.class, "ArtifactResolver");
111            infoFactory.addAttribute("versionMapLocation", String.class, true, true);
112            infoFactory.addReference("ArtifactManager", ArtifactManager.class, "ArtifactManager");
113            infoFactory.addReference("Repositories", ListableRepository.class, "Repository");
114            infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
115            infoFactory.addReference("FallbackArtifactResolver", ArtifactResolver.class, "ArtifactResolver");
116            infoFactory.addInterface(ArtifactResolver.class);
117    
118            infoFactory.setConstructor(new String[]{
119                    "versionMapLocation",
120                    "ArtifactManager",
121                    "Repositories",
122                    "ServerInfo",
123                    "FallbackArtifactResolver"
124            });
125    
126    
127            GBEAN_INFO = infoFactory.getBeanInfo();
128    
129        }
130    
131        public static GBeanInfo getGBeanInfo() {
132            return GBEAN_INFO;
133        }
134    
135    }