comparison src/org/tmatesoft/hg/repo/HgLookup.java @ 295:981f9f50bb6c

Issue 11: Error log facility. SessionContext to share common facilities
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 16 Sep 2011 05:35:32 +0200
parents 41a778e3fd31
children dfb8405d996f
comparison
equal deleted inserted replaced
294:32890bab7209 295:981f9f50bb6c
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.net.MalformedURLException; 21 import java.net.MalformedURLException;
22 import java.net.URL; 22 import java.net.URL;
23 23
24 import org.tmatesoft.hg.core.HgBadArgumentException; 24 import org.tmatesoft.hg.core.HgBadArgumentException;
25 import org.tmatesoft.hg.core.HgException; 25 import org.tmatesoft.hg.core.HgInvalidFileException;
26 import org.tmatesoft.hg.core.SessionContext;
27 import org.tmatesoft.hg.internal.BasicSessionContext;
26 import org.tmatesoft.hg.internal.ConfigFile; 28 import org.tmatesoft.hg.internal.ConfigFile;
27 import org.tmatesoft.hg.internal.DataAccessProvider; 29 import org.tmatesoft.hg.internal.DataAccessProvider;
28 import org.tmatesoft.hg.internal.Internals;
29 30
30 /** 31 /**
31 * Utility methods to find Mercurial repository at a given location 32 * Utility methods to find Mercurial repository at a given location
32 * 33 *
33 * @author Artem Tikhomirov 34 * @author Artem Tikhomirov
34 * @author TMate Software Ltd. 35 * @author TMate Software Ltd.
35 */ 36 */
36 public class HgLookup { 37 public class HgLookup {
37 38
38 private ConfigFile globalCfg; 39 private ConfigFile globalCfg;
40 private SessionContext sessionContext;
41
42 public HgLookup() {
43 }
44
45 public HgLookup(SessionContext ctx) {
46 sessionContext = ctx;
47 }
39 48
40 public HgRepository detectFromWorkingDir() throws HgException { 49 public HgRepository detectFromWorkingDir() throws HgInvalidFileException {
41 return detect(System.getProperty("user.dir")); 50 return detect(System.getProperty("user.dir"));
42 } 51 }
43 52
44 public HgRepository detect(String location) throws HgException { 53 public HgRepository detect(String location) throws HgInvalidFileException {
45 return detect(new File(location)); 54 return detect(new File(location));
46 } 55 }
47 56
48 // look up in specified location and above 57 // look up in specified location and above
49 public HgRepository detect(File location) throws HgException { 58 public HgRepository detect(File location) throws HgInvalidFileException {
50 File dir = location.getAbsoluteFile(); 59 File dir = location.getAbsoluteFile();
51 File repository; 60 File repository;
52 do { 61 do {
53 repository = new File(dir, ".hg"); 62 repository = new File(dir, ".hg");
54 if (repository.exists() && repository.isDirectory()) { 63 if (repository.exists() && repository.isDirectory()) {
62 // return invalid repository 71 // return invalid repository
63 return new HgRepository(location.getPath()); 72 return new HgRepository(location.getPath());
64 } 73 }
65 try { 74 try {
66 String repoPath = repository.getParentFile().getCanonicalPath(); 75 String repoPath = repository.getParentFile().getCanonicalPath();
67 return new HgRepository(repoPath, repository); 76 return new HgRepository(getContext(), repoPath, repository);
68 } catch (IOException ex) { 77 } catch (IOException ex) {
69 throw new HgException(location.toString(), ex); 78 throw new HgInvalidFileException(location.toString(), ex, location);
70 } 79 }
71 } 80 }
72 81
73 public HgBundle loadBundle(File location) /*XXX perhaps, HgDataStreamException or anything like HgMalformedDataException? Or RuntimeEx is better?*/{ 82 public HgBundle loadBundle(File location) throws HgInvalidFileException {
74 if (location == null || !location.canRead()) { 83 if (location == null || !location.canRead()) {
75 throw new IllegalArgumentException(); 84 throw new HgInvalidFileException(String.format("Can't read file %s", location == null ? null : location.getPath()), null, location);
76 } 85 }
77 return new HgBundle(new DataAccessProvider(), location).link(); 86 return new HgBundle(new DataAccessProvider(getContext()), location).link();
78 } 87 }
79 88
80 /** 89 /**
81 * Try to instantiate remote server. 90 * Try to instantiate remote server.
82 * @param key either URL or a key from configuration file that points to remote server 91 * @param key either URL or a key from configuration file that points to remote server
103 url = new URL(server); 112 url = new URL(server);
104 } catch (MalformedURLException ex) { 113 } catch (MalformedURLException ex) {
105 throw new HgBadArgumentException(String.format("Found %s server spec in the config, but failed to initialize with it", key), ex); 114 throw new HgBadArgumentException(String.format("Found %s server spec in the config, but failed to initialize with it", key), ex);
106 } 115 }
107 } 116 }
108 return new HgRemoteRepository(url); 117 return new HgRemoteRepository(getContext(), url);
109 } 118 }
110 119
111 public HgRemoteRepository detect(URL url) throws HgException { 120 public HgRemoteRepository detect(URL url) throws HgBadArgumentException {
112 if (url == null) { 121 if (url == null) {
113 throw new IllegalArgumentException(); 122 throw new IllegalArgumentException();
114 } 123 }
115 if (Boolean.FALSE.booleanValue()) { 124 if (Boolean.FALSE.booleanValue()) {
116 throw HgRepository.notImplemented(); 125 throw HgRepository.notImplemented();
117 } 126 }
118 return new HgRemoteRepository(url); 127 return new HgRemoteRepository(getContext(), url);
119 } 128 }
120 129
121 private ConfigFile getGlobalConfig() { 130 private ConfigFile getGlobalConfig() {
122 if (globalCfg == null) { 131 if (globalCfg == null) {
123 globalCfg = new Internals().newConfigFile(); 132 globalCfg = new ConfigFile();
124 globalCfg.addLocation(new File(System.getProperty("user.home"), ".hgrc")); 133 try {
134 globalCfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
135 } catch (IOException ex) {
136 // XXX perhaps, makes sense to let caller/client know that we've failed to read global config?
137 getContext().getLog().warn(getClass(), ex, null);
138 }
125 } 139 }
126 return globalCfg; 140 return globalCfg;
127 } 141 }
142
143 private SessionContext getContext() {
144 if (sessionContext == null) {
145 sessionContext = new BasicSessionContext(null, null);
146 }
147 return sessionContext;
148 }
128 } 149 }