1package org.robolectric.internal.dependency;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.MalformedURLException;
7import java.net.URL;
8import java.util.Properties;
9import org.robolectric.res.FsFile;
10
11public class PropertiesDependencyResolver implements DependencyResolver {
12  private final Properties properties;
13  private final FsFile baseDir;
14  private DependencyResolver delegate;
15
16  public PropertiesDependencyResolver(FsFile propertiesFile, DependencyResolver delegate) throws IOException {
17    this.properties = loadProperties(propertiesFile);
18    this.baseDir = propertiesFile.getParent();
19    this.delegate = delegate;
20  }
21
22  private Properties loadProperties(FsFile propertiesFile) throws IOException {
23    final Properties properties = new Properties();
24    InputStream stream = propertiesFile.getInputStream();
25    properties.load(stream);
26    stream.close();
27    return properties;
28  }
29
30  @Override
31  public URL getLocalArtifactUrl(DependencyJar dependency) {
32    String depShortName = dependency.getShortName();
33    String path = properties.getProperty(depShortName);
34    if (path != null) {
35      File pathFile = new File(path);
36      if (!pathFile.isAbsolute()) {
37        pathFile = new File(baseDir.getPath(), path);
38      }
39      try {
40        return pathFile.toURI().toURL();
41      } catch (MalformedURLException e) {
42        throw new RuntimeException(e);
43      }
44    } else {
45      if (delegate != null) {
46        return delegate.getLocalArtifactUrl(dependency);
47      }
48    }
49
50    throw new RuntimeException("no artifacts found for " + dependency);
51  }
52}
53