如何在我家附近找到更近的maven存储库

时间:2011-02-27 07:07:38

标签: java maven-2 maven

我想知道如何在家附近找到maven存储库?

2 个答案:

答案 0 :(得分: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. 对每个主机名重复一次(例如,如果您正在查看Maven本身的下载站点,则大约40次)

  4. 按GoogleMaps路线测量的距离或传输时间排序。

  5. 对于40位主持人,你可以在20个繁琐的时间内手动完成,但真正的专业人员宁可花几个小时编写可以在5分钟内完成的Selenium和java代码。

    在我大量的业余时间里,我只把一半的工作(即没有Selenium)扔在一起:

    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());
    }
    

    享受!

答案 1 :(得分:1)

Here有正式的中央存储库镜像列表(作为xml文件)和here的另一个中央存储库镜像列表。