1 19 package org.netbeans.modules.subversion.config; 20 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.net.InetSocketAddress ; 26 import java.net.Proxy ; 27 import java.net.ProxySelector ; 28 import java.net.SocketAddress ; 29 import java.net.URISyntaxException ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.StringTokenizer ; 34 import java.util.prefs.Preferences ; 35 import org.ini4j.Ini; 36 import org.netbeans.modules.subversion.util.FileUtils; 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.Utilities; 40 41 53 public class SvnConfigFiles { 54 55 56 private static SvnConfigFiles instance; 57 58 60 private Ini svnServers = null; 61 62 64 private Ini config = null; 65 66 private static final String UNIX_CONFIG_DIR = ".subversion/"; private static final String GROUPS_SECTION = "groups"; private static final String GLOBAL_SECTION = "global"; private static final String WINDOWS_USER_APPDATA = getAPPDATA(); 70 private static final String WINDOWS_CONFIG_DIR = WINDOWS_USER_APPDATA + "\\Subversion"; private static final String WINDOWS_GLOBAL_CONFIG_DIR = getGlobalAPPDATA() + "\\Subversion"; private static final List <String > DEFAULT_GLOBAL_IGNORES = 73 parseGlobalIgnores("*.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store"); 75 private interface IniFilePatcher { 76 void patch(Ini file); 77 } 78 79 85 private class ConfigIniFilePatcher implements IniFilePatcher { 86 public void patch(Ini file) { 87 Ini.Section auth = (Ini.Section) file.get("auth"); if(auth == null) { 90 auth = file.add("auth"); } 92 auth.put("store-auth-creds", "no"); } 94 } 95 96 99 private SvnConfigFiles() { 100 config = copyConfigFileToIDEConfigDir("config", new ConfigIniFilePatcher()); svnServers = loadSystemIniFile("servers"); 104 } 105 106 111 public static SvnConfigFiles getInstance() { 112 if(instance==null) { 113 instance = new SvnConfigFiles(); 114 } 115 return instance; 116 } 117 118 125 public void setProxy(String host) { 126 127 assert host != null && !host.trim().equals(""): "can\'t do anything for a null host"; 129 if(host.startsWith("file:///")) { 130 return; 132 } 133 134 ProxySelector ps = ProxySelector.getDefault(); 135 List <Proxy > proxies = null; 136 try { 137 proxies = ps.select(new java.net.URI (host)); 138 } catch (URISyntaxException ex) { 139 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 140 } 141 142 Proxy proxy = null; 143 for(Proxy p : proxies) { 144 if( p.type().equals(Proxy.Type.HTTP) || 145 p.type().equals(Proxy.Type.DIRECT) ) 146 { 147 proxy = p; 148 if (proxy.type().equals(Proxy.Type.DIRECT)) { 149 break; 150 } 151 } 152 } 153 154 Ini nbServers = new Ini(); 155 Ini.Section nbGlobalSection = nbServers.add(GLOBAL_SECTION); 156 Ini.Section svnGlobalSection = svnServers.get(GLOBAL_SECTION); 157 if(proxy.type().equals(Proxy.Type.DIRECT)) { 158 if(svnGlobalSection != null) { 160 mergeNonProxyKeys(svnGlobalSection, nbGlobalSection); 162 } 163 } else { 164 SocketAddress sa = proxy.address(); 166 InetSocketAddress proxyAddress = (InetSocketAddress ) sa; 167 168 nbGlobalSection.put("http-proxy-host", proxyAddress.getHostName()); nbGlobalSection.put("http-proxy-port", Integer.toString(proxyAddress.getPort())); 171 Preferences prefs = org.openide.util.NbPreferences.root ().node ("org/netbeans/core"); boolean useAuth = prefs.getBoolean ("useProxyAuthentication", false); if(useAuth) { 175 String username = prefs.get ("proxyAuthenticationUsername", ""); String password = prefs.get ("proxyAuthenticationPassword", ""); 178 nbGlobalSection.put("http-proxy-username", username); nbGlobalSection.put("http-proxy-password", password); } 181 182 Ini.Section svnHostGroup = getServerGroup(host); 186 if(svnGlobalSection != null) { 187 mergeNonProxyKeys(svnGlobalSection, nbGlobalSection); 189 } 190 if(svnHostGroup != null) { 191 mergeNonProxyKeys(svnHostGroup, nbGlobalSection); 192 } 193 } 194 storeIni(nbServers, "servers"); } 196 197 private void mergeNonProxyKeys(Ini.Section source, Ini.Section target) { 198 for (String key : source.keySet()) { 199 if(!isProxyConfigurationKey(key)) { 200 target.put(key, source.get(key)); 201 } 202 } 203 } 204 205 public void setExternalCommand(String tunnelName, String command) { 206 Ini.Section tunnels = getSection(config, "tunnels", true); 207 tunnels.put(tunnelName, command); 208 storeIni(config, "config"); } 210 211 public String getExternalCommand(String tunnelName) { 212 Ini.Section tunnels = getSection(config, "tunnels", true); 213 String cmd = tunnels.get(tunnelName); 214 return cmd != null ? cmd : ""; 215 } 216 217 private Ini.Section getSection(Ini ini, String key, boolean create) { 218 Ini.Section section = ini.get(key); 219 if(section == null) { 220 return ini.add(key); 221 } 222 return section; 223 } 224 225 private void storeIni(Ini ini, String iniFile) { 226 try { 227 File file = FileUtil.normalizeFile(new File (getNBConfigPath() + "/" + iniFile)); file.getParentFile().mkdirs(); 229 ini.store(FileUtils.createOutputStream(file)); 230 } catch (IOException ex) { 231 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 232 } 233 } 234 235 241 public List <String > getGlobalIgnores() { 242 Ini.Section miscellany = config.get("miscellany"); if (miscellany != null) { 244 String ignores = miscellany.get("global-ignores"); if (ignores != null && ignores.trim().length() > 0) { 246 return parseGlobalIgnores(ignores); 247 } 248 } 249 return DEFAULT_GLOBAL_IGNORES; 250 } 251 252 private static List <String > parseGlobalIgnores(String ignores) { 253 StringTokenizer st = new StringTokenizer (ignores, " "); List <String > ret = new ArrayList <String >(10); 255 while (st.hasMoreTokens()) { 256 String entry = st.nextToken(); 257 if (!entry.equals("")) ret.add(entry); 259 } 260 return ret; 261 } 262 263 270 public static String getUserConfigPath() { 271 if(Utilities.isUnix()) { 272 String path = System.getProperty("user.home") ; return path + "/" + UNIX_CONFIG_DIR; } else if (Utilities.isWindows()){ 275 return WINDOWS_CONFIG_DIR; 276 } 277 return ""; } 279 280 287 public static String getNBConfigPath() { 288 String nbHome = System.getProperty("netbeans.user"); return nbHome + "/config/svn/config/"; } 291 292 299 private Ini.Section getServerGroup(String host) { 300 if(host == null || host.equals("")) { return null; 302 } 303 Ini.Section groups = svnServers.get(GROUPS_SECTION); 304 if(groups != null) { 305 for (Iterator <String > it = groups.keySet().iterator(); it.hasNext();) { 306 String key = it.next(); 307 String value = groups.get(key); 308 if(value != null) { 309 value = value.trim(); 311 if(value != null && match(value, host)) { 312 return svnServers.get(key); 313 } 314 } 315 } 316 } 317 return null; 318 } 319 320 328 private boolean match(String value, String host) { 329 String [] values = value.split(","); for (int i = 0; i < values.length; i++) { 331 value = values[i].trim(); 332 333 if(value.equals("*") || value.equals(host) ) { return true; 335 } 336 337 int idx = value.indexOf("*"); if(idx > -1 && matchSegments(value, host) ) { 339 return true; 340 } 341 } 342 return false; 343 } 344 345 353 private boolean matchSegments(String value, String host) { 354 String [] valueSegments = value.split("."); String [] hostSegments = host.split("."); 357 int idx = 0; 358 for (int i = 0; i < hostSegments.length; i++) { 359 if( !valueSegments[idx].equals("*") && !valueSegments[idx].equals(hostSegments[i]) ) 361 { 362 return false; 363 } 364 if( !valueSegments[idx].equals("*") ) { idx++; 366 } 367 } 368 return false; 369 } 370 371 375 private Ini copyConfigFileToIDEConfigDir(String fileName, IniFilePatcher patcher) { 376 Ini systemIniFile = loadSystemIniFile(fileName); 377 378 patcher.patch(systemIniFile); 379 380 File file = FileUtil.normalizeFile(new File (getNBConfigPath() + "/" + fileName)); try { 382 file.getParentFile().mkdirs(); 383 systemIniFile.store(FileUtils.createOutputStream(file)); 384 } catch (IOException ex) { 385 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); } 387 return systemIniFile; 388 } 389 390 402 private Ini loadSystemIniFile(String fileName) { 403 String filePath = getUserConfigPath() + "/" + fileName; File file = FileUtil.normalizeFile(new File (filePath)); 406 Ini system = null; 407 try { 408 system = new Ini(new FileReader (file)); 409 } catch (FileNotFoundException ex) { 410 system = new Ini(); 411 } catch (IOException ex) { 412 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 413 } 414 415 Ini global = null; 416 try { 417 global = new Ini(new FileReader (getGlobalConfigPath() + "/" + fileName)); } catch (FileNotFoundException ex) { 419 } catch (IOException ex) { 421 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 422 } 423 424 if(global != null) { 425 merge(global, system); 426 } 427 428 if(system.size() < 1) { 429 ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not load the file " + filePath + ". Falling back on svn defaults."); } 431 return system; 432 } 433 434 440 private void merge(Ini source, Ini target) { 441 for (Iterator <String > itSections = source.keySet().iterator(); itSections.hasNext();) { 442 String sectionName = itSections.next(); 443 Ini.Section sourceSection = source.get( sectionName ); 444 Ini.Section targetSection = target.get( sectionName ); 445 446 if(targetSection == null) { 447 targetSection = target.add(sectionName); 448 } 449 450 for (Iterator <String > itVariables = sourceSection.keySet().iterator(); itVariables.hasNext();) { 451 String key = itVariables.next(); 452 453 if(!targetSection.containsKey(key)) { 454 targetSection.put(key, sourceSection.get(key)); 455 } 456 } 457 } 458 } 459 460 466 private boolean isProxyConfigurationKey(String key) { 467 return key.equals("http-proxy-host") || key.equals("http-proxy-port") || key.equals("http-proxy-username") || key.equals("http-proxy-password"); } 472 473 476 private static String getGlobalConfigPath () { 477 if(Utilities.isUnix()) { 478 return "/etc/subversion"; } else if (Utilities.isWindows()){ 480 return WINDOWS_GLOBAL_CONFIG_DIR; 481 } 482 return ""; } 484 485 489 private static String getAPPDATA() { 490 String appdata = ""; 491 if(Utilities.isWindows()) { 492 appdata = System.getenv("APPDATA"); } 494 return appdata!= null? appdata: ""; 495 } 496 497 501 private static String getGlobalAPPDATA() { 502 if(Utilities.isWindows()) { 503 String globalProfile = System.getenv("ALLUSERSPROFILE"); if(globalProfile == null || globalProfile.trim().equals("")) { globalProfile = ""; 506 } 507 String appdataPath = WINDOWS_USER_APPDATA; 508 if(appdataPath == null || appdataPath.equals("")) { return ""; } 511 String appdata = ""; int idx = appdataPath.lastIndexOf("\\"); if(idx > -1) { 514 appdata = appdataPath.substring(idx + 1); 515 if(appdata.trim().equals("")) { int previdx = appdataPath.lastIndexOf("\\", idx); if(idx > -1) { 518 appdata = appdataPath.substring(previdx + 1, idx); 519 } 520 } 521 } else { 522 return ""; } 524 return globalProfile + "/" + appdata; } 526 return ""; } 528 529 } 530 | Popular Tags |