001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  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, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.system.resolver;
020    
021    import java.io.File;
022    import java.io.FileInputStream;
023    import java.io.IOException;
024    import java.util.Collection;
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.util.Map;
028    import java.util.Properties;
029    
030    import org.apache.geronimo.gbean.GBeanInfo;
031    import org.apache.geronimo.gbean.GBeanInfoBuilder;
032    import org.apache.geronimo.kernel.repository.Artifact;
033    import org.apache.geronimo.kernel.repository.ArtifactManager;
034    import org.apache.geronimo.kernel.repository.ArtifactResolver;
035    import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
036    import org.apache.geronimo.kernel.repository.ListableRepository;
037    import org.apache.geronimo.system.serverinfo.ServerInfo;
038    
039    /**
040     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
041     */
042    public class ExplicitDefaultArtifactResolver extends DefaultArtifactResolver {
043    
044        public ExplicitDefaultArtifactResolver(String versionMapLocation,
045                ArtifactManager artifactManager,
046                Collection repositories,
047                ServerInfo serverInfo ) throws IOException {
048            super(artifactManager, repositories, buildExplicitResolution(versionMapLocation, serverInfo));
049        }
050    
051        private static Map buildExplicitResolution(String versionMapLocation, ServerInfo serverInfo) throws IOException {
052            if (versionMapLocation == null) {
053                return null;
054            }
055            File location = serverInfo == null? new File(versionMapLocation): serverInfo.resolve(versionMapLocation);
056            FileInputStream in = new FileInputStream(location);
057            Properties properties = new Properties();
058            try {
059                properties.load(in);
060            } finally {
061                in.close();
062            }
063            return propertiesToArtifactMap(properties);
064        }
065    
066        private static Map propertiesToArtifactMap(Properties properties) {
067            Map explicitResolution = new HashMap();
068            for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) {
069                Map.Entry entry = (Map.Entry) iterator.next();
070                String key = (String) entry.getKey();
071                String resolvedString = (String) entry.getValue();
072                //split the string ourselves since we wish to allow blank artifactIds.
073                String[] parts = key.split("/", -1);
074                if (parts.length != 4) {
075                    throw new IllegalArgumentException("Invalid id: " + key);
076                }
077                Artifact source = new Artifact(parts[0], parts[1], (String)null, parts[3]);
078                Artifact resolved = Artifact.create(resolvedString);
079                explicitResolution.put(source,resolved);
080            }
081            return explicitResolution;
082        }
083    
084        public static final GBeanInfo GBEAN_INFO;
085    
086        static {
087            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ExplicitDefaultArtifactResolver.class, "ArtifactResolver");
088            infoFactory.addAttribute("versionMapLocation", String.class, true, true);
089            infoFactory.addReference("ArtifactManager", ArtifactManager.class, "ArtifactManager");
090            infoFactory.addReference("Repositories", ListableRepository.class, "Repository");
091            infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
092            infoFactory.addInterface(ArtifactResolver.class);
093    
094            infoFactory.setConstructor(new String[]{
095                    "versionMapLocation",
096                    "ArtifactManager",
097                    "Repositories",
098                    "ServerInfo"
099            });
100    
101    
102            GBEAN_INFO = infoFactory.getBeanInfo();
103    
104        }
105    
106        public static GBeanInfo getGBeanInfo() {
107            return GBEAN_INFO;
108        }
109    }