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