13e81c867eccd437b1c10232196a52c1efb7199eeChristian Williamspackage org.robolectric.internal.dependency;
23e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams
33e81c867eccd437b1c10232196a52c1efb7199eeChristian Williamsimport java.io.File;
4096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williamsimport java.io.IOException;
5096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williamsimport java.io.InputStream;
63e81c867eccd437b1c10232196a52c1efb7199eeChristian Williamsimport java.net.MalformedURLException;
73e81c867eccd437b1c10232196a52c1efb7199eeChristian Williamsimport java.net.URL;
83e81c867eccd437b1c10232196a52c1efb7199eeChristian Williamsimport java.util.Properties;
9851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williamsimport org.robolectric.res.FsFile;
103e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams
1187bfaca94071897beadb00f476d4a7b3b0ff2b12Christian Williamspublic class PropertiesDependencyResolver implements DependencyResolver {
123e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams  private final Properties properties;
13096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams  private final FsFile baseDir;
143e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams  private DependencyResolver delegate;
153e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams
16096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams  public PropertiesDependencyResolver(FsFile propertiesFile, DependencyResolver delegate) throws IOException {
17096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    this.properties = loadProperties(propertiesFile);
18096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    this.baseDir = propertiesFile.getParent();
19096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    this.delegate = delegate;
20096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams  }
21096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams
22096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams  private Properties loadProperties(FsFile propertiesFile) throws IOException {
23096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    final Properties properties = new Properties();
24096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    InputStream stream = propertiesFile.getInputStream();
25096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    properties.load(stream);
26096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    stream.close();
27096baf3e8b49f813c96b7d21bb3ceac4d219605cChristian Williams    return properties;
283e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams  }
293e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams
303e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams  @Override
313e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams  public URL getLocalArtifactUrl(DependencyJar dependency) {
323e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams    String depShortName = dependency.getShortName();
333e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams    String path = properties.getProperty(depShortName);
343e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams    if (path != null) {
35e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish      File pathFile = new File(path);
36e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish      if (!pathFile.isAbsolute()) {
37e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish        pathFile = new File(baseDir.getPath(), path);
38e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish      }
39e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish      try {
40e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish        return pathFile.toURI().toURL();
41e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish      } catch (MalformedURLException e) {
42e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish        throw new RuntimeException(e);
433e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams      }
443e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams    } else {
45f1552b3bb889237fbbc16c961418700162d26d51Christian Williams      if (delegate != null) {
46e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish        return delegate.getLocalArtifactUrl(dependency);
47f1552b3bb889237fbbc16c961418700162d26d51Christian Williams      }
48f1552b3bb889237fbbc16c961418700162d26d51Christian Williams    }
49f1552b3bb889237fbbc16c961418700162d26d51Christian Williams
50e2a15962ef31efd550f0c4df6c3e637f6097ee43Jonathan Gerrish    throw new RuntimeException("no artifacts found for " + dependency);
513e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams  }
523e81c867eccd437b1c10232196a52c1efb7199eeChristian Williams}
53