commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HostConfiguration.java
Date Tue, 03 Dec 2002 14:07:50 GMT
oglueck     2002/12/03 06:07:50

  Added:       httpclient/src/java/org/apache/commons/httpclient
                        HostConfiguration.java
  Log:
  added missing file
  
  Revision  Changes    Path
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java
  
  Index: HostConfiguration.java
  ===================================================================
  package org.apache.commons.httpclient;
  
  
  /**
   */
  public class HostConfiguration implements Cloneable{
  
      private String host;
      private int port;
      private String protocol;
      private boolean hostSet;
      
      private String proxyHost;
      private int proxyPort;
      private boolean proxySet;
      
      /**
       * Constructor for HostConfiguration.
       */
      public HostConfiguration() {
          
          this.host = null;
          this.port = -1;
          this.protocol = null;
          this.hostSet = false;
          
          this.proxyHost = null;
          this.proxyPort = -1;
          this.proxySet = false;
          
      }
      
      /**
       * Copy constructor for HostConfiguration
       * 
       * @param hostConfiguration the hostConfiguration to copy
       */
      public HostConfiguration( HostConfiguration hostConfiguration ) {
          
          // wrap all of the assignments in a synchronized block to avoid
          // having to negotiate the monitor for each method call
          synchronized( hostConfiguration ) {            
              this.host = hostConfiguration.getHost();
              this.port = hostConfiguration.getPort();
              this.protocol = hostConfiguration.getProtocol();
              this.hostSet = hostConfiguration.isHostSet();
              
              this.proxyHost = hostConfiguration.getProxyHost();
              this.proxyPort = hostConfiguration.getProxyPort();
              this.proxySet = hostConfiguration.isProxySet();
          }
          
      }
  
      /**
       * @see java.lang.Object#clone()
       */
      public Object clone() {
          return new HostConfiguration(this);
      }
      
      /**
       * Tests if the host configuration equals the configuraiton set on the
       * connection. True only if the host, port and protocol are equal.  If no
       * host configuration has been set false will be returned.
       * 
       * @param connection the connection to test against
       * @return true if the connection's host information equals that of this
       * configuration
       * 
       * @see #proxyEquals(HttpConnection)
       */
      public synchronized boolean hostEquals(HttpConnection connection) {
          
          if ( hostSet ) {
              
              return ( 
                  host.equalsIgnoreCase(connection.getHost())
                  && port == connection.getPort()
                  && protocol.equalsIgnoreCase(connection.getProtocol())
              );
              
          } else {
              return false;   
          }
          
      }
  
      /**
       * Tests if the proxy configuration equals the configuraiton set on the
       * connection. True only if the proxyHost and proxyPort are equal.
       *
       * @param connection the connection to test against
       * @return true if the connection's proxy information equals that of this
       * configuration
       *
       * @see #hostEquals(HttpConnection)
       */
      public synchronized boolean proxyEquals(HttpConnection connection) {
          
          if ( proxyHost == null ) {
              return connection.getProxyHost() == null;   
          } else {
              return (
                  proxyHost.equalsIgnoreCase(connection.getProxyHost())
                  && proxyPort == connection.getProxyPort()
              );
          }
          
      }    
      
      public synchronized boolean isHostSet() {
          return hostSet;
      }
  
      public synchronized void setHost( 
          String host, 
          int port, 
          String protocol 
      ) {
          if ( host == null ) {
              throw new IllegalArgumentException( "host must not be null" );   
          }
          if ( protocol == null ) {
              throw new IllegalArgumentException( "protocol must not be null" );   
          }
          
          this.host = host;
          this.port = port;
          this.protocol = protocol;
          
          this.hostSet = true;
          
      }
  
      public synchronized String getHostURL() {
          
          if ( !hostSet ) {
              throw new IllegalStateException( 
                  "a default host must be set to create a host URL" 
              );   
          }
          
          String url = protocol + "://" + host;
          
          if ( port != -1 ) {
              url += ":" + port;
          }
          
          return url;
          
      }
  
      /**
       * Returns the host.
       * @return String
       */
      public synchronized String getHost() {
          return host;
      }
  
      /**
       * Returns the port.
       * @return int
       */
      public synchronized int getPort() {
          return port;
      }
  
      /**
       * Returns the protocol.
       * @return String
       */
      public synchronized String getProtocol() {
          return protocol;
      }
      
      public synchronized boolean isProxySet() {
          return proxySet;   
      }
  
      public synchronized void setProxy( String proxyHost, int proxyPort ) {
          
          this.proxyHost = proxyHost;
          this.proxyPort = proxyPort;
          
          this.proxySet = true;
          
      }
  
      /**
       * Returns the proxyHost.
       * @return String
       */
      public synchronized String getProxyHost() {
          return proxyHost;
      }
  
      /**
       * Returns the proxyPort.
       * @return int
       */
      public synchronized int getProxyPort() {
          return proxyPort;
      }
  
      /**
       * @see java.lang.Object#equals(java.lang.Object)
       */
      public boolean equals(Object o) {
          
          if ( o instanceof HostConfiguration ) {
              
              HostConfiguration config = (HostConfiguration)o;
              
              return (
                  proxyPort == config.getProxyPort()
                  && ( 
                      proxyHost == null 
                      ? config.getProxyHost() == null 
                      : proxyHost.equals( config.getProxyHost() )
                  )
                  && getHostURL().equals( config.getHostURL() ) 
              );
  
          } else {
              return false;
          }
          
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message