maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdca...@apache.org
Subject svn commit: r1126850 - in /maven/maven-3/branches/mirror-group-routing: maven-auto-mirror-model/ maven-auto-mirror-model/src/main/java/org/apache/maven/repository/automirror/ maven-auto-mirror-model/src/test/java/org/apache/maven/repository/automirror/...
Date Tue, 24 May 2011 03:29:42 GMT
Author: jdcasey
Date: Tue May 24 03:29:41 2011
New Revision: 1126850

URL: http://svn.apache.org/viewvc?rev=1126850&view=rev
Log:
git branch import script I wrote fell down on the job. These were added in previous patch(es).

Added:
    maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRouterLoader.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRouterLoader.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/pom.xml   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRouteTest.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRoutingTableTest.java   (with props)
    maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java   (with props)
Removed:
    maven/maven-3/branches/mirror-group-routing/maven-auto-mirror-model/pom.xml
    maven/maven-3/branches/mirror-group-routing/maven-auto-mirror-model/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java
    maven/maven-3/branches/mirror-group-routing/maven-auto-mirror-model/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java
    maven/maven-3/branches/mirror-group-routing/maven-auto-mirror-model/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java
    maven/maven-3/branches/mirror-group-routing/maven-auto-mirror-model/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java
    maven/maven-3/branches/mirror-group-routing/maven-auto-mirror-model/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java
    maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/DefaultMirrorRouter.java
    maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRoutingTableLoader.java
    maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRoutingTableLoader.java

Added: maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRouterLoader.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRouterLoader.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRouterLoader.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRouterLoader.java Tue May 24 03:29:41 2011
@@ -0,0 +1,401 @@
+package org.apache.maven.repository.mirror.loader;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.codehaus.plexus.util.IOUtil.close;
+
+import org.apache.http.Header;
+import org.apache.http.HeaderElement;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.maven.repository.automirror.MirrorRoute;
+import org.apache.maven.repository.automirror.MirrorRouteSerializer;
+import org.apache.maven.repository.automirror.MirrorRouterModelException;
+import org.apache.maven.repository.automirror.MirrorRoutingTable;
+import org.apache.maven.repository.mirror.MirrorRouter;
+import org.apache.maven.repository.mirror.MirrorRouterException;
+import org.apache.maven.repository.mirror.configuration.MirrorRouterConfiguration;
+import org.apache.maven.repository.mirror.discovery.RouterDiscoveryStrategy;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.util.IOUtil;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+@Component( role = MirrorRouterLoader.class )
+public class DefaultMirrorRouterLoader
+    implements MirrorRouterLoader
+{
+
+    @Requirement( role = RouterDiscoveryStrategy.class )
+    private Map<String, RouterDiscoveryStrategy> strategies;
+
+    @Requirement
+    private Logger logger;
+
+    public MirrorRouter load( final MirrorRouterConfiguration config )
+        throws MirrorRouterException
+    {
+        if ( config == null )
+        {
+            return new MirrorRouter();
+        }
+        
+        MirrorRoutingTable routingTable = loadRoutingTable( config );
+        Map<String, MirrorRoute> selectedRoutes = loadSelectedRoutes( config );
+
+        return new MirrorRouter( routingTable, selectedRoutes );
+    }
+
+    public void saveSelectedMirrors( final MirrorRouter router, final MirrorRouterConfiguration config )
+        throws MirrorRouterException
+    {
+        if ( config == null )
+        {
+            return;
+        }
+        
+        Map<String, MirrorRoute> selectedRoutes = router.getSelectedRoutes();
+        File selectedRoutesFile = config.getSelectedRoutesFile();
+        if ( selectedRoutesFile != null )
+        {
+            FileWriter writer = null;
+            try
+            {
+                File dir = selectedRoutesFile.getParentFile();
+                if ( dir != null && !dir.exists() )
+                {
+                    dir.mkdirs();
+                }
+                
+                writer = new FileWriter( selectedRoutesFile );
+                MirrorRouteSerializer.serializeLoose( new LinkedHashSet<MirrorRoute>( selectedRoutes.values() ), writer );
+            }
+            catch ( IOException e )
+            {
+                throw new MirrorRouterException( "Cannot write selected mirrors to: " + selectedRoutesFile, e );
+            }
+            catch ( MirrorRouterModelException e )
+            {
+                throw new MirrorRouterException( "Cannot write selected mirrors to: " + selectedRoutesFile, e );
+            }
+            finally
+            {
+                close( writer );
+            }
+        }
+    }
+
+    protected Map<String, MirrorRoute> loadSelectedRoutes( MirrorRouterConfiguration config )
+        throws MirrorRouterException
+    {
+        File selectedRoutesFile = config.getSelectedRoutesFile();
+        if ( selectedRoutesFile != null && selectedRoutesFile.exists() && selectedRoutesFile.canRead() )
+        {
+            FileReader reader = null;
+            try
+            {
+                reader = new FileReader( selectedRoutesFile );
+                Set<MirrorRoute> routes = MirrorRouteSerializer.deserializeLoose( reader );
+                
+                Map<String, MirrorRoute> result = new LinkedHashMap<String, MirrorRoute>();
+                for ( MirrorRoute route : routes )
+                {
+                    for ( String mirrorOf : route.getMirrorOfUrls() )
+                    {
+                        result.put( mirrorOf, route );
+                    }
+                }
+                
+                return result;
+            }
+            catch ( IOException e )
+            {
+                throw new MirrorRouterException( "Cannot read selected mirrors from: " + selectedRoutesFile, e );
+            }
+            catch ( MirrorRouterModelException e )
+            {
+                throw new MirrorRouterException( "Cannot read selected mirrors from: " + selectedRoutesFile, e );
+            }
+            finally
+            {
+                close( reader );
+            }
+        }
+        
+        return Collections.emptyMap();
+    }
+
+    protected MirrorRoutingTable loadRoutingTable( final MirrorRouterConfiguration config )
+        throws MirrorRouterException
+    {
+        MirrorRoutingTable routingTable = null;
+
+        if ( !config.isDisabled() )
+        {
+            final DefaultHttpClient client = new DefaultHttpClient();
+            if ( config.getRouterCredentials() != null )
+            {
+                client.setCredentialsProvider( new CredentialsProvider()
+                {
+                    public void setCredentials( final AuthScope authscope, final Credentials credentials )
+                    {
+                    }
+
+                    public synchronized Credentials getCredentials( final AuthScope authscope )
+                    {
+                        final UsernamePasswordCredentials creds = config.getRouterCredentials();
+                        return creds;
+                    }
+
+                    public void clear()
+                    {
+                    }
+                } );
+            }
+
+            try
+            {
+                if ( config.getRouterUrl() != null )
+                {
+                    routingTable = getMirrorMapping( config.getRouterUrl(), config, client );
+                }
+                else
+                {
+                    final String[] discoStrategies = config.getDiscoveryStrategies();
+                    final List<RouterDiscoveryStrategy> strats = new ArrayList<RouterDiscoveryStrategy>();
+                    if ( discoStrategies.length == 1 )
+                    {
+                        final String key = discoStrategies[0];
+                        if ( MirrorRouterConfiguration.NO_DISCOVERY_STRATEGIES.equalsIgnoreCase( key ) )
+                        {
+                            // NOP
+                        }
+                        else if ( MirrorRouterConfiguration.ALL_DISCOVERY_STRATEGIES.equalsIgnoreCase( key ) )
+                        {
+                            strats.addAll( strategies.values() );
+                        }
+                        else
+                        {
+                            final RouterDiscoveryStrategy strat = getDiscoveryStrategy( key );
+                            if ( strat != null )
+                            {
+                                strats.add( strat );
+                            }
+                        }
+                    }
+                    else
+                    {
+                        for ( final String key : discoStrategies )
+                        {
+                            final RouterDiscoveryStrategy strat = getDiscoveryStrategy( key );
+                            if ( strat != null )
+                            {
+                                strats.add( strat );
+                            }
+                        }
+                    }
+
+                    String routerUrl = null;
+                    for ( final RouterDiscoveryStrategy strategy : strats )
+                    {
+                        routerUrl = strategy.findRouter();
+                        if ( routerUrl != null && routerUrl.trim().length() > 0 )
+                        {
+                            routingTable = getMirrorMapping( routerUrl, config, client );
+                            if ( routingTable != null )
+                            {
+                                break;
+                            }
+                        }
+                    }
+                }
+
+                final String centralRouterUrl = config.getCanonicalRouterUrl();
+                if ( routingTable == null && centralRouterUrl != null && centralRouterUrl.trim().length() > 0 )
+                {
+                    routingTable = getMirrorMapping( centralRouterUrl, config, client );
+                }
+            }
+            catch ( final MirrorRouterException e )
+            {
+                if ( logger.isDebugEnabled() )
+                {
+                    logger.error( "Failed to auto-detect mirrors: " + e.getMessage(), e );
+                }
+            }
+        }
+
+        if ( routingTable == null )
+        {
+            routingTable = new MirrorRoutingTable();
+        }
+
+        return routingTable;
+    }
+
+    private RouterDiscoveryStrategy getDiscoveryStrategy( final String key )
+    {
+        RouterDiscoveryStrategy strat = strategies.get( key );
+        if ( strat == null )
+        {
+            strat = strategies.get( key.toLowerCase() );
+        }
+
+        if ( strat == null )
+        {
+            logger.warn( "Cannot find RouterDiscoveryStrategy with hint: '" + key + "'" );
+        }
+
+        return strat;
+    }
+
+    private MirrorRoutingTable getMirrorMapping( final String routerUrl, final MirrorRouterConfiguration config,
+                                                 final HttpClient client )
+    {
+        if ( config.isDisabled() )
+        {
+            return new MirrorRoutingTable();
+        }
+
+        if ( routerUrl != null && routerUrl.trim().length() > 0 )
+        {
+            if ( logger.isDebugEnabled() )
+            {
+                logger.debug( "Grabbing mirror mappings from: " + routerUrl.toString() );
+            }
+            System.out.println( "Grabbing mirror mappings from: " + routerUrl );
+
+            final HttpGet get = new HttpGet( routerUrl );
+            get.addHeader( "Accept", "application/json;q=0.9,*/*;q=0.8" );
+
+            try
+            {
+                return client.execute( get, new ResponseHandler<MirrorRoutingTable>()
+                {
+                    public MirrorRoutingTable handleResponse( final HttpResponse response )
+                        throws /* ClientProtocolException, */IOException
+                    {
+                        final int statusCode = response.getStatusLine().getStatusCode();
+                        if ( statusCode == 200 )
+                        {
+                            InputStream stream = null;
+                            try
+                            {
+                                stream = response.getEntity().getContent();
+                                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                                IOUtil.copy( stream, baos );
+
+                                String content = null;
+                                final Header contentType = response.getFirstHeader( "Content-Type" );
+                                if ( contentType != null )
+                                {
+                                    final HeaderElement[] contentTypeElts = contentType.getElements();
+
+                                    if ( contentTypeElts != null )
+                                    {
+                                        for ( final HeaderElement elt : contentTypeElts )
+                                        {
+                                            final NameValuePair nv = elt.getParameterByName( "charset" );
+                                            if ( nv != null )
+                                            {
+                                                content = new String( baos.toByteArray(), nv.getValue() );
+                                            }
+                                        }
+                                    }
+                                }
+
+                                if ( content == null )
+                                {
+                                    content = new String( baos.toByteArray() );
+                                }
+
+                                if ( logger.isDebugEnabled() )
+                                {
+                                    logger.debug( "Response code/message: '" + response.getStatusLine().getStatusCode()
+                                        + " " + response.getStatusLine().getReasonPhrase() + "'\nContent is:\n\n"
+                                        + content );
+                                }
+
+                                return MirrorRouteSerializer.deserialize( content );
+                            }
+                            catch ( final MirrorRouterModelException e )
+                            {
+                                logger.error( "Failed to retrieve mirror mapping from: " + routerUrl, e );
+                            }
+                            finally
+                            {
+                                close( stream );
+                            }
+                        }
+                        else if ( logger.isDebugEnabled() )
+                        {
+                            logger.debug( "Response: " + response.getStatusLine().getStatusCode() + " "
+                                + response.getStatusLine().getReasonPhrase() );
+                        }
+
+                        return null;
+                    }
+                } );
+            }
+            catch ( final ClientProtocolException e )
+            {
+                if ( logger.isDebugEnabled() )
+                {
+                    logger.debug( "Failed to read proxied repositories from: '" + routerUrl + "'. Reason: "
+                                      + e.getMessage(), e );
+                }
+            }
+            catch ( final IOException e )
+            {
+                if ( logger.isDebugEnabled() )
+                {
+                    logger.debug( "Failed to read proxied repositories from: '" + routerUrl + "'. Reason: "
+                                      + e.getMessage(), e );
+                }
+            }
+        }
+
+        return null;
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/DefaultMirrorRouterLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRouterLoader.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRouterLoader.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRouterLoader.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRouterLoader.java Tue May 24 03:29:41 2011
@@ -0,0 +1,35 @@
+package org.apache.maven.repository.mirror.loader;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.repository.mirror.MirrorRouter;
+import org.apache.maven.repository.mirror.MirrorRouterException;
+import org.apache.maven.repository.mirror.configuration.MirrorRouterConfiguration;
+
+public interface MirrorRouterLoader
+{
+
+    MirrorRouter load( MirrorRouterConfiguration config )
+        throws MirrorRouterException;
+    
+    void saveSelectedMirrors( MirrorRouter router, MirrorRouterConfiguration config )
+        throws MirrorRouterException;
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-core/src/main/java/org/apache/maven/repository/mirror/loader/MirrorRouterLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/pom.xml
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/pom.xml?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/pom.xml (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/pom.xml Tue May 24 03:29:41 2011
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+  <!--
+    Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+    file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+    the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+    applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
+    governing permissions and limitations under the License.
+  -->
+
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <artifactId>maven</artifactId>
+    <groupId>org.apache.maven</groupId>
+    <version>3.0.3-SNAPSHOT</version>
+  </parent>
+  
+  <artifactId>maven-routem-api</artifactId>
+  
+  <name>Maven Route-M API</name>
+  <description>Maven Route-M auto-mirroring data API</description>
+  
+  <dependencies>
+  	<dependency>
+  		<groupId>com.google.code.gson</groupId>
+  		<artifactId>gson</artifactId>
+  	</dependency>
+  	<dependency>
+  		<groupId>org.codehaus.plexus</groupId>
+  		<artifactId>plexus-utils</artifactId>
+  	</dependency>
+  </dependencies>
+</project>

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java Tue May 24 03:29:41 2011
@@ -0,0 +1,188 @@
+package org.apache.maven.repository.automirror;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.codehaus.plexus.util.StringUtils.join;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+public class MirrorRoute
+    implements Comparable<MirrorRoute>
+{
+
+    private final String id;
+
+    private final String routeUrl;
+
+    private final int weight;
+
+    private final boolean enabled;
+
+    private final Set<String> mirrorOfUrls;
+
+    // NOTE: ONLY used during deserialization!
+    MirrorRoute()
+    {
+        id = null;
+        mirrorOfUrls = Collections.emptySet();
+        routeUrl = null;
+        weight = 0;
+        enabled = false;
+    }
+
+    public MirrorRoute( final String id, final String routeUrl, final int weight,
+                        final boolean enabled, final String... mirrorOfUrls )
+    {
+        if ( mirrorOfUrls.length < 1 )
+        {
+            throw new IllegalArgumentException( "Cannot construct a mirror route without at least one mirror-of URL." );
+        }
+        
+        this.id = id;
+        this.mirrorOfUrls = normalizeMirrorOfUrls( Arrays.asList( mirrorOfUrls ) );
+        this.routeUrl = routeUrl;
+        this.weight = weight;
+        this.enabled = enabled;
+    }
+
+    public MirrorRoute( final String id, final String routeUrl, final int weight,
+                        final boolean enabled, final Collection<String> mirrorOfUrls )
+    {
+        if ( mirrorOfUrls.size() < 1 )
+        {
+            throw new IllegalArgumentException( "Cannot construct a mirror route without at least one mirror-of URL." );
+        }
+        
+        this.id = id;
+        this.mirrorOfUrls = normalizeMirrorOfUrls( mirrorOfUrls );
+        this.routeUrl = routeUrl;
+        this.weight = weight;
+        this.enabled = enabled;
+    }
+
+    private Set<String> normalizeMirrorOfUrls( Collection<String> src )
+    {
+        Set<String> result = new HashSet<String>( src.size() );
+        for ( String srcItem : src )
+        {
+            String item = srcItem.toLowerCase();
+            if ( item.endsWith( "/" ) )
+            {
+                item = item.substring( 0, item.length() - 1 );
+            }
+            
+            result.add( item );
+        }
+        
+        return result;
+    }
+
+    public String getId()
+    {
+        return id;
+    }
+
+    public String getRouteUrl()
+    {
+        return routeUrl;
+    }
+
+    public int getWeight()
+    {
+        return weight;
+    }
+
+    public boolean isEnabled()
+    {
+        return enabled;
+    }
+
+    public int compareTo( final MirrorRoute o )
+    {
+        return o.weight - weight;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "mirror [id: " + id + ", weight: " + weight + ", mirror-of urls: " + join( mirrorOfUrls.iterator(), ", " ) + ", route-url: " + routeUrl
+                        + ", enabled: " + enabled + "]";
+    }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ( ( routeUrl == null ) ? 0 : routeUrl.hashCode() );
+        return result;
+    }
+
+    @Override
+    public boolean equals( final Object obj )
+    {
+        if ( this == obj )
+        {
+            return true;
+        }
+        if ( obj == null )
+        {
+            return false;
+        }
+        if ( getClass() != obj.getClass() )
+        {
+            return false;
+        }
+        final MirrorRoute other = (MirrorRoute) obj;
+        if ( routeUrl == null )
+        {
+            if ( other.routeUrl != null )
+            {
+                return false;
+            }
+        }
+        else if ( !routeUrl.equals( other.routeUrl ) )
+        {
+            return false;
+        }
+        return true;
+    }
+
+    public Set<String> getMirrorOfUrls()
+    {
+        return mirrorOfUrls;
+    }
+
+    public boolean isMirrorOf( final String canonicalUrl )
+    {
+        String check = canonicalUrl.toLowerCase();
+        if ( check.endsWith( "/" ) )
+        {
+            check = check.substring( 0, check.length() - 1 );
+        }
+        
+        return mirrorOfUrls.contains( check );
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java Tue May 24 03:29:41 2011
@@ -0,0 +1,197 @@
+package org.apache.maven.repository.automirror;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.InstanceCreator;
+import com.google.gson.JsonParseException;
+import com.google.gson.reflect.TypeToken;
+
+import java.io.Reader;
+import java.io.Writer;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+public class MirrorRouteSerializer
+{
+
+    // NOTE: Gson is supposed to be threadsafe, so all this static stuff should be fine.
+    private static Gson gson;
+
+    public static void serializeLoose( final Set<MirrorRoute> mirrors, final Writer writer )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            getGson().toJson( mirrors, writer );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static String serializeLooseToString( final Set<MirrorRoute> mirrors )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            return getGson().toJson( mirrors );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static Set<MirrorRoute> deserializeLoose( final Reader reader )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            return getGson().fromJson( reader, RepositoryMirrorSetCreator.getType() );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static Set<MirrorRoute> deserializeLoose( final String source )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            return getGson().fromJson( source, RepositoryMirrorSetCreator.getType() );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static void serialize( final MirrorRoutingTable mirrors, final Writer writer )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            getGson().toJson( mirrors, writer );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static String serializeToString( final MirrorRoutingTable mirrors )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            return getGson().toJson( mirrors );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static MirrorRoutingTable deserialize( final Reader reader )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            return getGson().fromJson( reader, MirrorRoutingTable.class );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    public static MirrorRoutingTable deserialize( final String source )
+        throws MirrorRouterModelException
+    {
+        try
+        {
+            return getGson().fromJson( source, MirrorRoutingTable.class );
+        }
+        catch ( final JsonParseException e )
+        {
+            throw new MirrorRouterModelException( "Cannot read router-mirrors.", e );
+        }
+    }
+
+    private static Gson getGson()
+    {
+        if ( gson == null )
+        {
+            final GsonBuilder builder = new GsonBuilder();
+            builder.disableHtmlEscaping().disableInnerClassSerialization().setPrettyPrinting();
+            builder.registerTypeAdapter( RepositoryMirrorSetCreator.getType(), new RepositoryMirrorSetCreator() );
+            builder.registerTypeAdapter( RepositoryMirrorListCreator.getType(), new RepositoryMirrorListCreator() );
+
+            gson = builder.create();
+        }
+
+        return gson;
+    }
+
+    public static final class RepositoryMirrorListCreator
+        implements InstanceCreator<List<MirrorRoute>>
+    {
+
+        public List<MirrorRoute> createInstance( final Type type )
+        {
+            return new ArrayList<MirrorRoute>();
+        }
+
+        public static Type getType()
+        {
+            return new TypeToken<List<MirrorRoute>>()
+            {
+            }.getType();
+        }
+
+    }
+
+    public static final class RepositoryMirrorSetCreator
+        implements InstanceCreator<Set<MirrorRoute>>
+    {
+
+        public Set<MirrorRoute> createInstance( final Type type )
+        {
+            return new LinkedHashSet<MirrorRoute>();
+        }
+
+        public static Type getType()
+        {
+            return new TypeToken<Set<MirrorRoute>>()
+            {
+            }.getType();
+        }
+
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouteSerializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java Tue May 24 03:29:41 2011
@@ -0,0 +1,38 @@
+package org.apache.maven.repository.automirror;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class MirrorRouterModelException
+    extends Exception
+{
+
+    private static final long serialVersionUID = 1L;
+
+    public MirrorRouterModelException( final String message, final Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    public MirrorRouterModelException( final String message )
+    {
+        super( message );
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRouterModelException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java Tue May 24 03:29:41 2011
@@ -0,0 +1,227 @@
+package org.apache.maven.repository.automirror;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+public class MirrorRoutingTable
+    implements Iterable<MirrorRoute>
+{
+
+    private List<MirrorRoute> mirrors = new ArrayList<MirrorRoute>();
+
+    private final transient Random random = new Random();
+
+    private transient Map<String, int[]> indexGrabBags = new HashMap<String, int[]>();
+    
+    public synchronized MirrorRoutingTable addMirror( final MirrorRoute mirror )
+    {
+        if ( !mirrors.contains( mirror ) )
+        {
+            mirrors.add( mirror );
+            for ( String url : mirror.getMirrorOfUrls() )
+            {
+                indexGrabBags.remove( url );
+            }
+        }
+
+        return this;
+    }
+
+    public synchronized MirrorRoute getMirror( final String canonicalUrl )
+    {
+        return getWeightedRandomMirror( canonicalUrl );
+    }
+    
+//    private MirrorRoute getHighestPriorityMirror( final String canonicalUrl )
+//    {
+//        if ( mirrors.isEmpty() )
+//        {
+//            return null;
+//        }
+//
+//        final List<MirrorRoute> available = new ArrayList<MirrorRoute>( mirrors );
+//
+//        // sort by weight.
+//        Collections.sort( available );
+//
+//        for ( final MirrorRoute mirror : available )
+//        {
+//            // return the highest-priority ENABLED mirror.
+//            if ( mirror.isEnabled() && canonicalUrl.equals( mirror.getUrl() ) )
+//            {
+//                return mirror;
+//            }
+//        }
+//
+//        return null;
+//    }
+
+    private MirrorRoute getWeightedRandomMirror( final String canonicalUrl )
+    {
+        if ( mirrors.isEmpty() )
+        {
+            return null;
+        }
+
+        int[] indexGrabBag = null;
+        synchronized ( canonicalUrl )
+        {
+            indexGrabBag = indexGrabBags.get( canonicalUrl );
+            if ( indexGrabBag == null )
+            {
+                final List<Integer> gb = new ArrayList<Integer>();
+                for ( int idx = 0; idx < mirrors.size(); idx++ )
+                {
+                    final MirrorRoute mirror = mirrors.get( idx );
+                    if ( !mirror.isEnabled() || !mirror.isMirrorOf( canonicalUrl ) )
+                    {
+                        // only select from enabled mirrors that actually mirror the given canonical repository URL.
+                        continue;
+                    }
+
+                    if ( mirror.getWeight() < 1 )
+                    {
+                        // make sure this mirror has at least one chance of being picked.
+                        gb.add( idx );
+                    }
+                    else
+                    {
+                        // if weight == 9, give this mirror 9 chances of being picked randomly.
+                        for ( int i = 0; i < mirror.getWeight(); i++ )
+                        {
+                            gb.add( idx );
+                        }
+                    }
+                }
+
+                indexGrabBag = new int[gb.size()];
+                for ( int i = 0; i < indexGrabBag.length; i++ )
+                {
+                    indexGrabBag[i] = gb.get( i );
+                }
+
+                indexGrabBags.put( canonicalUrl, indexGrabBag );
+            }
+        }
+
+        if ( indexGrabBag.length == 0 )
+        {
+            return null;
+        }
+        
+        // generate a random number that will correspond to an index stored in the index grab bag.
+        int idx = Math.abs( random.nextInt() ) % indexGrabBag.length;
+
+        // use that random number to select the index of the mirror in the mirrors list.
+        idx = indexGrabBag[idx];
+
+        // lookup the mirror instance associated with the index from the grab bag.
+        return mirrors.get( idx );
+    }
+
+    public int size()
+    {
+        return mirrors.size();
+    }
+
+    public boolean isEmpty()
+    {
+        return mirrors.isEmpty();
+    }
+
+    public boolean contains( final MirrorRoute o )
+    {
+        return mirrors.contains( o );
+    }
+
+    public Iterator<MirrorRoute> iterator()
+    {
+        return mirrors.iterator();
+    }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ( ( mirrors == null ) ? 0 : mirrors.hashCode() );
+        return result;
+    }
+
+    @Override
+    public boolean equals( final Object obj )
+    {
+        if ( this == obj )
+        {
+            return true;
+        }
+        if ( obj == null )
+        {
+            return false;
+        }
+        if ( getClass() != obj.getClass() )
+        {
+            return false;
+        }
+        final MirrorRoutingTable other = (MirrorRoutingTable) obj;
+        if ( mirrors == null )
+        {
+            if ( other.mirrors != null )
+            {
+                return false;
+            }
+        }
+        else if ( !mirrors.equals( other.mirrors ) )
+        {
+            return false;
+        }
+        return true;
+    }
+
+    List<MirrorRoute> getMirrors()
+    {
+        return mirrors;
+    }
+
+    void setMirrors( final List<MirrorRoute> mirrors )
+    {
+        this.mirrors = mirrors;
+    }
+
+    public boolean containsMirrorOf( final String canonicalUrl )
+    {
+        for ( final MirrorRoute mirror : mirrors )
+        {
+            if ( mirror.isEnabled() && mirror.isMirrorOf( canonicalUrl ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/main/java/org/apache/maven/repository/automirror/MirrorRoutingTable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRouteTest.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRouteTest.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRouteTest.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRouteTest.java Tue May 24 03:29:41 2011
@@ -0,0 +1,50 @@
+/*
+ *  Copyright (C) 2011 John Casey.
+ *  
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.apache.maven.repository.automirror;
+
+import junit.framework.TestCase;
+
+public class MirrorRouteTest
+    extends TestCase
+{
+    
+    public void testMatchMirror()
+    {
+        String canonical = "http://repo1.maven.org/maven2";
+        MirrorRoute route = new MirrorRoute( "test", "http://localhost:8080/mirror", 10, true, canonical );
+        
+        assertTrue( route.isMirrorOf( canonical ) );
+    }
+
+    public void testMatchSecondMirrorOf()
+    {
+        String canonical = "http://repo1.maven.org/maven2";
+        MirrorRoute route = new MirrorRoute( "test", "http://localhost:8080/mirror", 10, true, "http://www.foo.bar.com/mirror", canonical );
+        
+        assertTrue( route.isMirrorOf( canonical ) );
+    }
+
+    public void testMatchMirrorWithTrailingSlashMismatched()
+    {
+        String canonical = "http://repo1.maven.org/maven2";
+        MirrorRoute route = new MirrorRoute( "test", "http://localhost:8080/mirror", 10, true, canonical + "/" );
+        
+        assertTrue( route.isMirrorOf( canonical ) );
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRouteTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRoutingTableTest.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRoutingTableTest.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRoutingTableTest.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRoutingTableTest.java Tue May 24 03:29:41 2011
@@ -0,0 +1,52 @@
+/*
+ *  Copyright (C) 2011 John Casey.
+ *  
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.apache.maven.repository.automirror;
+
+import junit.framework.TestCase;
+
+public class MirrorRoutingTableTest
+    extends TestCase
+{
+    
+    public void testFindMirrorMatch()
+    {
+        String canonical = "http://repo1.maven.org/maven2";
+        
+        MirrorRoutingTable table = new MirrorRoutingTable();
+        MirrorRoute route = new MirrorRoute( "test", "http://nowhere.com/mirror", 10, true, canonical );
+        table.addMirror( route );
+        
+        MirrorRoute result = table.getMirror( canonical );
+        
+        assertEquals( route, result );
+    }
+
+    public void testMirrorMatchNotFound()
+    {
+        String canonical = "http://repo1.maven.org/maven3";
+        
+        MirrorRoutingTable table = new MirrorRoutingTable();
+        MirrorRoute route = new MirrorRoute( "test", "http://nowhere.com/mirror", 10, true, "http://repo1.maven.org/maven2" );
+        table.addMirror( route );
+        
+        MirrorRoute result = table.getMirror( canonical );
+        
+        assertNull( result );
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/MirrorRoutingTableTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java
URL: http://svn.apache.org/viewvc/maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java?rev=1126850&view=auto
==============================================================================
--- maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java (added)
+++ maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java Tue May 24 03:29:41 2011
@@ -0,0 +1,67 @@
+package org.apache.maven.repository.automirror;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.StringWriter;
+
+import junit.framework.TestCase;
+
+public class RouterMirrorSerializerTest
+    extends TestCase
+{
+
+    public void testSerializeOneMirror()
+        throws Exception
+    {
+        final MirrorRoutingTable mirrorMap =
+            new MirrorRoutingTable().addMirror( new MirrorRoute( "central",
+                                                             "http://localhost:8081/nexus", 99, true, "http://repo1.maven.org/maven2" ) );
+
+        final StringWriter sw = new StringWriter();
+        MirrorRouteSerializer.serialize( mirrorMap, sw );
+
+        System.out.println( sw );
+    }
+
+    public void testSerializeToStringOneMirror()
+        throws Exception
+    {
+        final MirrorRoutingTable mirrorMap =
+            new MirrorRoutingTable().addMirror( new MirrorRoute( "central",
+                                                             "http://localhost:8081/nexus", 99, true, "http://repo1.maven.org/maven2" ) );
+
+        System.out.println( MirrorRouteSerializer.serializeToString( mirrorMap ) );
+    }
+
+    public void testRoundTripOneMirror()
+        throws Exception
+    {
+        final MirrorRoutingTable mirrorMap =
+            new MirrorRoutingTable().addMirror( new MirrorRoute( "central",
+                                                             "http://localhost:8081/nexus", 99, true, "http://repo1.maven.org/maven2" ) );
+
+        final String ser = MirrorRouteSerializer.serializeToString( mirrorMap );
+        final MirrorRoutingTable result = MirrorRouteSerializer.deserialize( ser );
+
+        assertEquals( mirrorMap, result );
+        assertTrue( result.getMirror( "http://repo1.maven.org/maven2" ).isEnabled() );
+    }
+
+}

Propchange: maven/maven-3/branches/mirror-group-routing/maven-routem-api/src/test/java/org/apache/maven/repository/automirror/RouterMirrorSerializerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message