Coverage Report - org.apache.maven.archiva.repository.RepositoryContentFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryContentFactory
0%
0/53
0%
0/16
0
 
 1  
 package org.apache.maven.archiva.repository;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 23  
 import org.apache.maven.archiva.configuration.ConfigurationNames;
 24  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 25  
 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
 26  
 import org.codehaus.plexus.PlexusContainer;
 27  
 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
 28  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 29  
 import org.codehaus.plexus.context.Context;
 30  
 import org.codehaus.plexus.context.ContextException;
 31  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
 32  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 33  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 34  
 import org.codehaus.plexus.registry.Registry;
 35  
 import org.codehaus.plexus.registry.RegistryListener;
 36  
 
 37  
 import java.util.HashMap;
 38  
 import java.util.Map;
 39  
 
 40  
 /**
 41  
  * RepositoryContentRequest 
 42  
  *
 43  
  * @version $Id: RepositoryContentFactory.java 727229 2008-12-17 00:10:13Z jdumay $
 44  
  * 
 45  
  * @plexus.component 
 46  
  *      role="org.apache.maven.archiva.repository.RepositoryContentFactory"
 47  
  */
 48  
 public class RepositoryContentFactory
 49  
     implements Contextualizable, RegistryListener, Initializable
 50  
 {
 51  
     /**
 52  
      * @plexus.requirement
 53  
      */
 54  
     private ArchivaConfiguration archivaConfiguration;
 55  
 
 56  
     private final Map<String, ManagedRepositoryContent> managedContentMap;
 57  
 
 58  
     private final Map<String, RemoteRepositoryContent> remoteContentMap;
 59  
 
 60  
     private PlexusContainer container;
 61  
 
 62  
     public RepositoryContentFactory()
 63  0
     {
 64  0
         managedContentMap = new HashMap<String, ManagedRepositoryContent>();
 65  0
         remoteContentMap = new HashMap<String, RemoteRepositoryContent>();
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Get the ManagedRepositoryContent object for the repository Id specified.
 70  
      * 
 71  
      * @param repoId the repository id to fetch.
 72  
      * @return the ManagedRepositoryContent object associated with the repository id.
 73  
      * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
 74  
      * @throws RepositoryException the repository content object cannot be loaded due to configuration issue.
 75  
      */
 76  
     public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
 77  
         throws RepositoryNotFoundException, RepositoryException
 78  
     {
 79  0
         ManagedRepositoryContent repo = managedContentMap.get( repoId );
 80  
 
 81  0
         if ( repo != null )
 82  
         {
 83  0
             return repo;
 84  
         }
 85  
 
 86  0
         ManagedRepositoryConfiguration repoConfig = archivaConfiguration.getConfiguration()
 87  
             .findManagedRepositoryById( repoId );
 88  0
         if ( repoConfig == null )
 89  
         {
 90  0
             throw new RepositoryNotFoundException( "Unable to find managed repository configuration for id:" + repoId );
 91  
         }
 92  
 
 93  
         try
 94  
         {
 95  0
             repo = (ManagedRepositoryContent) container.lookup( ManagedRepositoryContent.class, repoConfig.getLayout() );
 96  0
             repo.setRepository( repoConfig );
 97  0
             managedContentMap.put( repoId, repo );
 98  
         }
 99  0
         catch ( ComponentLookupException e )
 100  
         {
 101  0
             throw new RepositoryException( "Specified layout [" + repoConfig.getLayout()
 102  
                 + "] on managed repository id [" + repoId + "] is not valid.", e );
 103  0
         }
 104  
 
 105  0
         return repo;
 106  
     }
 107  
 
 108  
     public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
 109  
         throws RepositoryNotFoundException, RepositoryException
 110  
     {
 111  0
         RemoteRepositoryContent repo = remoteContentMap.get( repoId );
 112  
 
 113  0
         if ( repo != null )
 114  
         {
 115  0
             return repo;
 116  
         }
 117  
 
 118  0
         RemoteRepositoryConfiguration repoConfig = archivaConfiguration.getConfiguration()
 119  
             .findRemoteRepositoryById( repoId );
 120  0
         if ( repoConfig == null )
 121  
         {
 122  0
             throw new RepositoryNotFoundException( "Unable to find remote repository configuration for id:" + repoId );
 123  
         }
 124  
 
 125  
         try
 126  
         {
 127  0
             repo = (RemoteRepositoryContent) container.lookup( RemoteRepositoryContent.class, repoConfig.getLayout() );
 128  0
             repo.setRepository( repoConfig );
 129  0
             remoteContentMap.put( repoId, repo );
 130  
         }
 131  0
         catch ( ComponentLookupException e )
 132  
         {
 133  0
             throw new RepositoryException( "Specified layout [" + repoConfig.getLayout()
 134  
                 + "] on remote repository id [" + repoId + "] is not valid.", e );
 135  0
         }
 136  
 
 137  0
         return repo;
 138  
     }
 139  
 
 140  
     public void contextualize( Context context )
 141  
         throws ContextException
 142  
     {
 143  0
         container = (PlexusContainer) context.get( "plexus" );
 144  0
     }
 145  
 
 146  
     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
 147  
     {
 148  0
         if ( ConfigurationNames.isManagedRepositories( propertyName )
 149  
             || ConfigurationNames.isRemoteRepositories( propertyName ) )
 150  
         {
 151  0
             initMaps();
 152  
         }
 153  0
     }
 154  
 
 155  
     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
 156  
     {
 157  
         /* do nothing */
 158  0
     }
 159  
 
 160  
     public void initialize()
 161  
         throws InitializationException
 162  
     {
 163  0
         archivaConfiguration.addChangeListener( this );
 164  0
     }
 165  
 
 166  
     private void initMaps()
 167  
     {
 168  0
         synchronized ( managedContentMap )
 169  
         {
 170  
             // First, return any references to the container.
 171  0
             for ( ManagedRepositoryContent repo : managedContentMap.values() )
 172  
             {
 173  
                 try
 174  
                 {
 175  0
                     container.release( repo );
 176  
                 }
 177  0
                 catch ( ComponentLifecycleException e )
 178  
                 {
 179  
                     /* ignore */
 180  0
                 }
 181  
             }
 182  
 
 183  
             // Next clear the map.
 184  0
             managedContentMap.clear();
 185  0
         }
 186  
 
 187  0
         synchronized ( remoteContentMap )
 188  
         {
 189  
             // First, return any references to the container.
 190  0
             for ( RemoteRepositoryContent repo : remoteContentMap.values() )
 191  
             {
 192  
                 try
 193  
                 {
 194  0
                     container.release( repo );
 195  
                 }
 196  0
                 catch ( ComponentLifecycleException e )
 197  
                 {
 198  
                     /* ignore */
 199  0
                 }
 200  
             }
 201  
 
 202  
             // Next clear the map.
 203  0
             remoteContentMap.clear();
 204  0
         }
 205  0
     }
 206  
 }