akarasulu    2003/11/10 14:01:20

  Added:       repository/impl/src/java/org/apache/avalon/repository/impl
                        DefaultRepositoryFactory.java
  Log:
  
  
  Revision  Changes    Path
  1.1                  
avalon-sandbox/repository/impl/src/java/org/apache/avalon/repository/impl/DefaultRepositoryFactory.java
  
  Index: DefaultRepositoryFactory.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
      "Apache Software Foundation"  must not be used to endorse or promote
      products derived  from this  software without  prior written
      permission. For written permission, please contact [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.avalon.repository.impl ;
  
  
  import java.io.File ;
  import java.io.IOException ;
  import java.io.InputStream ;
  import java.io.FileInputStream ;
  
  import java.util.Properties ;
  import java.net.Authenticator ;
  import java.net.MalformedURLException ;
  import java.net.PasswordAuthentication ;
  
  import org.apache.avalon.defaults.Defaults ;
  import org.apache.avalon.defaults.DefaultsFinder ;
  import org.apache.avalon.defaults.SimpleDefaultsFinder ;
  import org.apache.avalon.defaults.SystemDefaultsFinder ;
  
  import org.apache.avalon.repository.Repository ;
  import org.apache.avalon.repository.ProxyContext ;
  import org.apache.avalon.repository.RepositoryContext ;
  import org.apache.avalon.repository.RepositoryFactory ;
  import org.apache.avalon.repository.RepositoryException ;
  
  
  /**
   * The default repository factory implementation.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a>
   * @author $Author: akarasulu $
   * @version $Revision: 1.1 $
   */
  public class DefaultRepositoryFactory implements RepositoryFactory
  {
      /** properties file where we pull defaults from */
      public static final String DEFAULTS = "repository.properties" ;
      /** single valued properties */
      private static final String [] s_singles = {
          "cache.dir",
          "proxyHost",
          "proxyPort",
          "proxyUsername",
          "proxyPassword"
      } ;
      /** the local repository base key where we download files into */
      public static final String CACHE_DIR = s_singles[0] ;
      /** the proxy host key */
      public static final String PROXY_HOST = s_singles[1] ;
      /** the proxy port key */
      public static final String PROXY_PORT = s_singles[2] ;
      /** the proxy username key */
      public static final String PROXY_USERNAME = s_singles[3] ;
      /** the proxy password key */
      public static final String PROXY_PASSWORD = s_singles[4] ;
  
      /** enumerated (multivalued) properties */
      public static final String [] s_multiple = {
          "remote.repository.url"
      } ;
      
      /** the key base for the remote repo enumerated property */
      public static final String REMOTE_REPO_BASE = s_multiple[0] ;
  
  
      /**
       * Creates a repository configuration and populates it with parameters 
       * obtained from a default bootstrap properties file, a hidden form of the
       * same properties file (dot '.' prefixed), and the system properties in 
       * that order.  Support property key macro expansion.
       * 
       * @see org.apache.avalon.repository.RepositoryFactory#getDefaultContext()
       */
      public RepositoryContext getDefaultContext()
          throws RepositoryException
      {
          Properties l_bootstrap = new Properties() ;
          ProxyContext l_proxy = null ;
          RepositoryContext l_config = new DefaultRepositoryConfig() ;
          InputStream l_in = Repository.class.getResourceAsStream( DEFAULTS ) ;
          
          /*
           * Load the jar relative default properties file first.  Then load into
           * the same properties the dot prefixed version of the defaults file 
           * within the user's home directory.
           */
          try 
          {
              l_bootstrap.load( l_in ) ;
              String l_userHome = System.getProperty( "user.home" ) ;
              File l_user = new File( l_userHome, "." + DEFAULTS ) ;
              
              if ( l_user.exists() ) 
              {    
                  l_in = new FileInputStream( l_user ) ;
                  l_bootstrap.load( l_in ) ;
              }
          }
          catch( IOException e )
          {
              throw new RepositoryException( "Failed to load defaults", e ) ;
          }
  
          /*
           * Create the finder (discovery policy), construct the defaults, and
           * macro expand the values.
           */
          final DefaultsFinder [] l_finders = {
              new SimpleDefaultsFinder( new Properties[] { l_bootstrap }, false ),
              new SystemDefaultsFinder()
          } ;
          
          final Defaults l_defaults = 
              new Defaults( s_singles, s_multiple, l_finders ) ;
          Defaults.macroExpand( l_defaults, null ) ;
          
          /*
           * Here we start to populate the empty repository configuration using
           * the values stored in the defaults.
           */
          
          l_config.setCacheDir( l_defaults.getProperty( CACHE_DIR ) ) ;
          
          try
          {
              l_config.setRemoteRepositoryUrls( 
                      l_defaults.getEnumerated( REMOTE_REPO_BASE ) ) ;
          }
          catch ( MalformedURLException e )
          {
              throw new RepositoryException( "Failed to set remote repos", e ) ;
          }
  
          if ( l_defaults.containsKey( PROXY_HOST ) )
          {    
              int l_port = Integer
                  .parseInt( l_defaults.getProperty( PROXY_PORT ) ) ;
              String l_host = l_defaults.getProperty( PROXY_HOST ) ;
      
              Authenticator l_authenticator = new Authenticator()
              {
                  protected PasswordAuthentication getPasswordAuthentication()
                  {
                      PasswordAuthentication l_pwdAuth = 
                          new PasswordAuthentication(
                          l_defaults.getProperty( PROXY_USERNAME ),
                          l_defaults.getProperty( PROXY_PASSWORD )
                              .toCharArray() ) ;
                      return l_pwdAuth ;
                  }
              } ;
              
              l_proxy = new ProxyContext( l_host, l_port, l_authenticator) ;
          }
          
          l_config.setProxyContext( l_proxy ) ;
          
          return l_config ;
      }
  
  
      /**
       * Creates a file repository using the properties of a configuration bean.
       * 
       * @see org.apache.avalon.repository.RepositoryFactory
       * create(org.apache.avalon.repository.RepositoryContext)
       */
      public Repository create( RepositoryContext context )
      {
          Repository l_repository = null ;
          File l_base = new File( context.getCacheDir() ) ;
          
          if ( null == context.getProxyContext() && 
               null == context.getRemoteRepositories() )
          {
              l_repository = new DefaultFileRepository( l_base ) ;
          }
          else if ( null == context.getProxyContext() )
          {
              l_repository = new DefaultFileRepository( l_base, 
                      context.getRemoteRepositories() ) ;
          }
          
          l_repository = new DefaultFileRepository( l_base, 
                  context.getProxyContext(), 
                  context.getRemoteRepositories() ) ;
          
          return l_repository ;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to