2011-02-27 80 views

回答

1

Here的官方中央存儲庫鏡像列表(如xml文件)和here的另一箇中央存儲庫鏡像列表。

2

實際上,在http://docs.codehaus.org/display/MAVENUSER/Mirrors+Repositories上市存在問題。在加利福尼亞州的舊金山還是在密蘇里州的聖路易斯? (我想這是關於爲什麼代碼,甚至xml,不應該被評論的無意的教訓)。爲了使事情更加混亂,域名的所有者(根據http://whois.net/whois/maven.org)位於馬里蘭州富爾頓市。

也許問題不是「存儲庫在哪裏?」 (這esaj回答),但真的: 「如何我可以找到最接近到我家的maven存儲庫?」

這個問題也給我帶來了困擾。

另一方面,鑑於數據包在世界各地的傳輸速度如何,它真的很重要嗎?

但它仍然竊聽我。

有兩種方法,找出哪些主機是最接近:

  1. 打開瀏覽器,進入http://freegeoip.net,並輸入主機名。如果你知道你在哪裏,那麼你可以去GoogleMaps,獲取路線,並檢查距離。

  2. 打開終端窗口,並ping主機名以查找以毫秒爲單位的平均往返傳輸時間。這會給你電子距離,這對於文件傳輸時間來說真的更重要。

  3. 重複每個主機名(即約40倍,如果你正在尋找的下載網站Maven的本身)

  4. 排序由Google地圖方向測量的距離,或在途時間。

40臺主機,你也許可以做手工,在20繁瑣分鐘,但真正的專業人士寧願花幾個小時寫的硒和java代碼,能做到這一點在5分鐘內。

在我的豐富大量的空餘時間,我扔在一起,只有一半的工作(即無硒):

public class ClosestUrlFinder { 

    /** 
    * Find out where a hostname is. 
    * Adapted from http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ 
    * Send HTTP GET requests to: freegeoip.net/{format}/{ip_or_hostname} 
    * The API supports both HTTP and HTTPS. 
    * Supported formats are csv, xml or json. 
    * E.G. http://freegeoip.net/xml/www.poolsaboveground.com 
    * returns <Response><Ip>207.58.184.93</Ip><CountryCode>US</CountryCode> 
    * <CountryName>United States</CountryName><RegionCode>VA</RegionCode><RegionName>Virginia</RegionName> 
    * <City>Mclean</City><ZipCode>22101</ZipCode> 
    * <Latitude>38.9358</Latitude><Longitude>-77.1621</Longitude> 
    * <MetroCode>511</MetroCode><AreaCode>703</AreaCode></Response> 
    * @param urlOrHostname 
    * @return 
    */ 
    public String getUrlLocation(String urlOrHostname) { 
     String USER_AGENT = "Mozilla/5.0"; 
     BufferedReader in = null; 
     URL urlObject; 
     StringBuilder response = null; 
     try { 
      urlObject = new URL("http://freegeoip.net/xml/" + urlOrHostname); 
      HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("User-Agent", USER_AGENT); 
      int responseCode = connection.getResponseCode(); 
      System.out.println("\nSending 'GET' request to URL : " + urlOrHostname); 
      System.out.println("Response Code : " + responseCode); 
      in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String inputLine; 
      response = new StringBuilder(); 
      while ((inputLine = in.readLine()) != null) { 
       response.append(inputLine); 
      } 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (in != null) { 
       try { in.close(); } 
       catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     //System.out.println(response.toString()); 
     return response.toString(); 
    } 


    /* 
    * Fixed version of code from http://stackoverflow.com/questions/11506321/java-code-to-ping-an-ip-address 
    */ 
    public String runDosCommand(List<String> command) 
    { 
     String string = "", result = ""; 
     ProcessBuilder pBuilder = new ProcessBuilder(command); 
     Process process; 
     try { 
      process = pBuilder.start(); 
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
      //System.out.println("Standard output of the command:"); 
      while ((string = stdInput.readLine()) != null) 
      { 
       System.out.println(string); 
       result = result + "\n" + string; 
      } 
      while ((string = stdError.readLine()) != null) 
      { 
       System.out.println("stdError: "+ string); 
       result = result + "\nstdError: " + string; 
      } 
     } catch (IOException e) { 
      System.out.println("Error with command "+command); 
      e.printStackTrace(); 
     } 
     return result; 
    } 

    public String pingUrl(String url) { 
     List<String> command = new ArrayList<String>(); 
     String averagePingTime = "0", geoText = "", loss = ""; 
     command.add("ping"); 
     command.add("-n"); 
     command.add("2"); 
     url = url.replace("ftp://", ""); 
     url = Utils.first(url.replace("/", " ")); 
     url = Utils.first(url.replace(":", " ")); 
     command.add(url); 
     System.out.println("command is: "+command); 
     String pingResult = runDosCommand(command); 
     String timeoutString = "Request timed out"; 
     String timeout = Utils.grep(timeoutString, pingResult); 
     String noHostString = "Ping request could not find host"; 
     String noHost = Utils.grep(noHostString, pingResult); 
     String unreachableString = "Destination net unreachable"; 
     String unreachable = Utils.grep(unreachableString, pingResult); 
     if (Utils.isNullOrEmptyString(timeout) && Utils.isNullOrEmptyString(noHost) 
       && Utils.isNullOrEmptyString(unreachable)) { 
      String lostString = "Lost ="; 
      loss = Utils.grep(lostString, pingResult); 
      int index = loss.indexOf(lostString); 
      loss = loss.substring(index); 
      if (!loss.equals("Lost = 0 (0% loss),")) { 
       System.out.println("Non-zero loss for " + url); 
       averagePingTime = "0"; 
      } else { 
       String replyString = "Reply from"; 
       String replyFrom = Utils.grep(replyString, pingResult); 
       String ipAddress = Utils.nth(replyFrom, 3); 
       System.out.println("reply Ip="+ipAddress.replace(":", "")); 
       String averageString = "Average ="; 
       averagePingTime = Utils.grep(averageString, pingResult); 
       index = averagePingTime.indexOf(averageString); 
       averagePingTime = averagePingTime.substring(index); 
       averagePingTime = Utils.last(averagePingTime).replace("ms", ""); 

       String xml = getUrlLocation(url);   
       //System.out.println("xml="+xml); 
       geoText = extractTextFromXML(xml); 
       System.out.println("geoText="+geoText); 
      } 
     } else { 
      averagePingTime = "0"; 
      System.out.println("Error. Either could not find host, Request timed out, or unreachable network for " + url);; 
     } 
     System.out.println("Results for " + url + " are: " + loss + " " + averagePingTime + " miliseconds."); 
     return url + " " + averagePingTime + " " + geoText ; 
    } 


    public ArrayList<Entry<String,Integer>> pingManyUrls() { 
     ArrayList<Entry<String,Integer>> mirrorTimeList = new ArrayList<>(); 
     String[] urls = Utils.readTextFile("resources/MavenUrls.txt").split("\\s+"); 
     System.out.println("Start pingManyUrls with "+urls.length + " urls."); 
     for (String url : urls) { 
      url = url.trim(); 
      System.out.println("************************************\npingManyUrls: Check Url " + url); 
      if (arrayListContainsKey(url, mirrorTimeList)) { 
       System.out.println("Key " + url + " already in array."); 
      } else { 
       String pingInfo = pingUrl(url); 
       String averagePingString = Utils.nth(pingInfo, 2); 
       if (!averagePingString.equals("0")) { 
        int averagePingMilliseconds = Integer.parseInt(averagePingString); 
        pingInfo = Utils.rest(pingInfo); //chop the first term (the url) 
        pingInfo = Utils.rest(pingInfo); // chop the 2nd term (the time) 
        url = url + " " + pingInfo; 
        System.out.println(" Adding Key " + url + " " + averagePingMilliseconds + " to array."); 
        Entry<String,Integer> urlTimePair = new java.util.AbstractMap.SimpleEntry<>(url, averagePingMilliseconds); 
        mirrorTimeList.add(urlTimePair); 
       } else { System.out.println("Url " + url + " has a problem and therefore will be not be included."); } 
      } 
     } 
     Collections.sort(mirrorTimeList, new Comparator<Entry<String,Integer>>() { 
      @Override 
      public int compare (Entry<String,Integer> pair1, Entry<String,Integer> pair2) { 
       return pair1.getValue().compareTo(pair2.getValue()); 
      } 
     }); 
     return mirrorTimeList; 
    } 


    public boolean arrayListContainsKey(String key, ArrayList<Entry<String,Integer>> arrayList) { 
     for (Entry<String,Integer> keyValuePair : arrayList) { 
      //System.out.println(keyValuePair.getKey() + " =? " + key); 
      if (key.equalsIgnoreCase(keyValuePair.getKey())) { return true; } 
     } 
     return false; 
    } 

    public String extractFirstTagContents(Element parentElement, String tagname) { 
     NodeList list = parentElement.getElementsByTagName(tagname); 
     if (list != null && list.getLength()>0) { 
      String contents = list.item(0).getTextContent(); 
      System.out.println("Tagname=" + tagname + " contents="+contents); 
      return contents; 
     } 
     return ""; 
    } 

    public String extractTextFromXML(String xml) { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     String content = ""; 
     try { 
      builder = factory.newDocumentBuilder(); 
      Document document = builder.parse(new InputSource(new StringReader(xml))); 
      Element rootElement = document.getDocumentElement(); 
      content = rootElement.getTextContent(); 
      //System.out.println("content="+content); 
//   String city = extractFirstTagContents(rootElement, "City"); 
//   String state = extractFirstTagContents(rootElement, "RegionName"); 
//   String zipCode = extractFirstTagContents(rootElement, "ZipCode"); 
//   String country = extractFirstTagContents(rootElement, "CountryName"); 
//   String latitude = extractFirstTagContents(rootElement, "Latitude"); 
//   String longitude = extractFirstTagContents(rootElement, "Longitude"); 
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException saxex) { 
      // TODO Auto-generated catch block 
      saxex.printStackTrace(); 
     } catch (IOException ioex) { 
      // TODO Auto-generated catch block 
      ioex.printStackTrace(); 
     } 
     return content; 
    } 


    public static void main(String[] args) { 
     System.out.println("Starting ClosestUrlFinder"); 
     ClosestUrlFinder ping = new ClosestUrlFinder(); 
     ArrayList<Entry<String,Integer>> mirrorList = ping.pingManyUrls(); 
     System.out.println("Final Results, sorted by trip travel time (out of "+mirrorList.size()+" successful pings)."); 
     for (Entry<String,Integer> urlTime : mirrorList) { 
      System.out.println(urlTime.getKey() + " " + urlTime.getValue()); 
     } 
    } 

} // End of Class 

凡MavenUrls.txt的數據是:

apache.claz.org 
apache.cs.utah.edu 
apache.mesi.com.ar 
apache.mirrors.hoobly.com 
apache.mirrors.lucidnetworks.net 
apache.mirrors.pair.com 
apache.mirrors.tds.net 
apache.osuosl.org 
apache.petsads.us 
apache.spinellicreations.com 
apache.tradebit.com 
download.nextag.com 
ftp.osuosl.org 
ftp://mirror.reverse.net/pub/apache/ 
mirror.cc.columbia.edu/pub/software/apache/ 
mirror.cogentco.com/pub/apache/ 
mirror.metrocast.net/apache/ 
mirror.nexcess.net/apache/ 
mirror.olnevhost.net/pub/apache/ 
mirror.reverse.net/pub/apache/ 
mirror.sdunix.com/apache/ 
mirror.symnds.com/software/Apache/ 
mirror.tcpdiag.net/apache/ 
mirrors.gigenet.com/apache/ 
mirrors.ibiblio.org/apache/ 
mirrors.sonic.net/apache/ 
psg.mtu.edu/pub/apache/ 
supergsego.com/apache/ 
www.bizdirusa.com 
www.bizdirusa.com/mirrors/apache/ 
www.carfab.com/apachesoftware/ 
www.dsgnwrld.com/am/ 
www.eng.lsu.edu/mirrors/apache/ 
www.gtlib.gatech.edu/pub/apache/ 
www.interior-dsgn.com/apache/ 
www.motorlogy.com/apache/ 
www.picotopia.org/apache/ 
www.poolsaboveground.com/apache/ 
www.trieuvan.com/apache/ 
www.webhostingjams.com/mirror/apache/ 

和我Utils類包括以下內容:

/** 
* Given a string of words separated by spaces, returns the first word. 
* @param string 
* @return 
*/ 
public static String first(String string) { 
    if (isNullOrEmptyString(string)) { return ""; } 
    string = string.trim(); 
    int index = string.indexOf(" "); //TODO: shouldn't this be "\\s+" to handle double spaces and tabs? That means regexp version of indexOf 
    if (index<0) { return string; } 
    return string.substring(0, index); 
} 

/** 
* Given a string of words separated by spaces, returns the rest of the string after the first word. 
* @param string 
* @return 
*/ 
public static String rest(String string) { 
    if (isNullOrEmptyString(string)) { return ""; } 
    string = string.trim(); 
    int index = string.indexOf(" "); //TODO: shouldn't this be "\\s+" to handle double spaces and tabs? That means regexp version of indexOf 
    if (index<0) { return ""; } 
    return string.substring(index+1); 
} 

public static String grep(String regexp, String multiLineStringToSearch) { 
    String result = ""; 
    //System.out.println("grep for '"+ regexp + "'"); 
    String[] lines = multiLineStringToSearch.split("\\n"); 
    //System.out.println("grep input string contains "+ lines.length + " lines."); 
    Pattern pattern = Pattern.compile(regexp); 
    for (String line : lines) { 
     Matcher matcher = pattern.matcher(line); 
     if (matcher.find()) { 
      //System.out.println("grep found match="+ line); 
      result = result + "\n" + line; 
     } 
    } 
    return result.trim(); 
} 

/** 
* Given the path and name of a plain text (ascii) file, 
* reads it and returns the contents as a string. 
* @param filePath 
* @return 
*/ 
public static String readTextFile(String filePath) { 
    BufferedReader reader; 
    String result = ""; 
    int counter = 0; 
    try { 
     reader = new BufferedReader(new FileReader(filePath)); 
     String line = ""; 
     StringBuilder stringBuilder = new StringBuilder(); 
     String lineSeparator = System.getProperty("line.separator"); 
     while ((line = reader.readLine()) != null) { 
      counter = counter + 1; 
      stringBuilder.append(line); 
      stringBuilder.append(lineSeparator); 
     } 
     reader.close(); 
     result = stringBuilder.toString(); 
     logger.info("readTextFile: Read "+filePath+" with "+ counter + " lines, "+result.length()+" characters."); 
     return result; 
    } catch (FileNotFoundException e) { 
     logger.fatal("readTextFile: Could not find file "+filePath+"."); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     logger.fatal("readTextFile: Could not read file "+filePath+"."); 
     e.printStackTrace(); 
    } 
    return ""; 
} 

public static boolean isNullOrEmptyString(String s) { 
    return (s==null || s.equals("")); 
} 

/** 
* Given a string of words separated by one or more whitespaces, returns the Nth word. 
* @param string 
* @return 
*/ 
public static String nth(String string, int n) { 
    string = string.trim(); 
    String[] splitstring = string.split("\\s+"); 
    String result = ""; 
    if (splitstring.length<n) { return ""; } 
    else { 
     result = splitstring[n - 1]; 
    } 
    return result.trim(); 
} 

/** 
* Given a string of words separated by spaces, returns the last word. 
* @param string 
* @return 
*/ 
public static String last(String string) { 
    return string.substring(string.trim().lastIndexOf(" ")+1, string.trim().length()); 
} 

享受!