1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.system.resolver;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import org.apache.geronimo.gbean.GBeanInfo;
31 import org.apache.geronimo.gbean.GBeanInfoBuilder;
32 import org.apache.geronimo.kernel.repository.Artifact;
33 import org.apache.geronimo.kernel.repository.ArtifactManager;
34 import org.apache.geronimo.kernel.repository.ArtifactResolver;
35 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
36 import org.apache.geronimo.kernel.repository.ListableRepository;
37 import org.apache.geronimo.system.serverinfo.ServerInfo;
38
39 /**
40 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
41 */
42 public class ExplicitDefaultArtifactResolver extends DefaultArtifactResolver {
43
44 public ExplicitDefaultArtifactResolver(String versionMapLocation,
45 ArtifactManager artifactManager,
46 Collection repositories,
47 ServerInfo serverInfo ) throws IOException {
48 super(artifactManager, repositories, buildExplicitResolution(versionMapLocation, serverInfo));
49 }
50
51 private static Map buildExplicitResolution(String versionMapLocation, ServerInfo serverInfo) throws IOException {
52 if (versionMapLocation == null) {
53 return null;
54 }
55 File location = serverInfo == null? new File(versionMapLocation): serverInfo.resolve(versionMapLocation);
56 FileInputStream in = new FileInputStream(location);
57 Properties properties = new Properties();
58 try {
59 properties.load(in);
60 } finally {
61 in.close();
62 }
63 return propertiesToArtifactMap(properties);
64 }
65
66 private static Map propertiesToArtifactMap(Properties properties) {
67 Map explicitResolution = new HashMap();
68 for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) {
69 Map.Entry entry = (Map.Entry) iterator.next();
70 String key = (String) entry.getKey();
71 String resolvedString = (String) entry.getValue();
72
73 String[] parts = key.split("/", -1);
74 if (parts.length != 4) {
75 throw new IllegalArgumentException("Invalid id: " + key);
76 }
77 Artifact source = new Artifact(parts[0], parts[1], (String)null, parts[3]);
78 Artifact resolved = Artifact.create(resolvedString);
79 explicitResolution.put(source,resolved);
80 }
81 return explicitResolution;
82 }
83
84 public static final GBeanInfo GBEAN_INFO;
85
86 static {
87 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ExplicitDefaultArtifactResolver.class, "ArtifactResolver");
88 infoFactory.addAttribute("versionMapLocation", String.class, true, true);
89 infoFactory.addReference("ArtifactManager", ArtifactManager.class, "ArtifactManager");
90 infoFactory.addReference("Repositories", ListableRepository.class, "Repository");
91 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
92 infoFactory.addInterface(ArtifactResolver.class);
93
94 infoFactory.setConstructor(new String[]{
95 "versionMapLocation",
96 "ArtifactManager",
97 "Repositories",
98 "ServerInfo"
99 });
100
101
102 GBEAN_INFO = infoFactory.getBeanInfo();
103
104 }
105
106 public static GBeanInfo getGBeanInfo() {
107 return GBEAN_INFO;
108 }
109 }