avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: jakarta-avalon-apps/enterprise/orb/src/java/org/apache/orb/corbaloc package.html Handler.java CorbalocURLConnection.java
Date Mon, 03 Jun 2002 14:41:26 GMT
mcconnell    2002/06/03 07:41:26

  Added:       enterprise/orb/src/java/org/apache/orb/corbaloc package.html
                        Handler.java CorbalocURLConnection.java
  Log:
  corbaloc URL handlers
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-apps/enterprise/orb/src/java/org/apache/orb/corbaloc/package.html
  
  Index: package.html
  ===================================================================
  
  <body>
  <p>Resources supporting the creation and management URLs using the corbaloc protocol.</p>
  </body>
  
  
  
  1.1                  jakarta-avalon-apps/enterprise/orb/src/java/org/apache/orb/corbaloc/Handler.java
  
  Index: Handler.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   *
   * Original contribution OSM SARL 2002, http://www.osm.net
   */
  
  package org.apache.orb.corbaloc;
  
  import java.io.IOException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.URLStreamHandler;
  import org.omg.CORBA.ORB;
  
  /**
   * <p>Corbaloc URL protocol handler. Creation of a new corbaloc URL 
   * requires that the appropriate handler be supplied to the URL
   * constructor.  The handler (this class) is created with a an
   * ORB as a constructor argument.  The ORB is subsequently passed
   * by the handler to its connection handler which in turn handles
   * URL to object dereferencing using the ORB string_to_object 
   * operation.  This URL handler implementation is limited to the 
   * representation of a single CORBA end-point (whereas corbaloc URLs
   * can define multiple endpoints.</p>
   * <p>
   * <strong>URL creation example:</strong></p>
   * <p>
   * <pre>
   *   ORB orb = ORB.init( new String[0], null );
   *   String path = "corbaloc::mybusiness.com:2056/myService";
   *   URL url = new URL( null, path, new Handler( orb ) );
   *   Object object = url.getContent();
   * </pre>
   * </p>
   * @see org.apache.orb.corbaloc.CorbalocURLConnection
   */
  public class Handler extends URLStreamHandler
  {
      /**
       * the current ORB.
       */
       private ORB m_orb;
  
      /**
       * The connection object.
       */
       private URLConnection m_connection;
  
      /**
       * <p>Creation of a new handler.</p>
       * <p><strong>URL creation example using the handler:</strong></p>
       * <p>
       * <pre>
       *   ORB orb = ORB.init( new String[0], null );
       *   String path = "corbaloc::mybusiness.com:2056/myService";
       *   URL url = new URL( null, path, new Handler( orb ) );
       *   Object object = url.getContent();
       * </pre>
       * </p>
       * @param orb the current ORB that will be supplied to the 
       *  connection handler to support object reference establishment.
       *  If null, the URL will be able to perform parsing operations 
       *  (see toExternalForm), however any attempt to connect will 
       *  raise an exception. 
       */
       public Handler( ORB orb ) throws NullPointerException
       {
           m_orb = orb;
       }
  
      /**
       * Opens a connection to the specified URL.
       *
       * @param url A URL to open a connection to.
       * @return The established connection.
       * @throws IOException If a connection failure occurs.
       */
      protected URLConnection openConnection( final URL url )
        throws IOException
      {
          if( m_connection != null ) 
          {
              return m_connection;
          }
  
          if( m_orb == null )
          {
              throw new IOException("Handler has not been supplied with a ORB - cannot connect.");
          }
          m_connection = new CorbalocURLConnection( m_orb, url );
          m_connection.connect();
          return m_connection;
      }
  
      /**
       * Parses the string representation of a <code>URL</code> into a
       * <code>URL</code> object.
       * e.g. corbaloc:iiop:1.2@mybusiness.com/myService
       * or,  corbaloc::mybusiness.com/myService
       * <p>
       * If there is any inherited context, then it has already been
       * copied into the <code>URL</code> argument.
       *
       * @param   url     the <code>URL</code> to receive the result of parsing
       *                  the spec.
       * @param   spec    the <code>String</code> representing the URL that
       *                  must be parsed.
       * @param   start   the character index at which to begin parsing. This is
       *                  just past the '<code>:</code>' (if there is one) that
       *                  specifies the determination of the protocol name.
       * @param   limit   the character position to stop parsing at. This is the
       *                  end of the string or the position of the
       *                  "<code>#</code>" character, if present. All information
       *                  after the sharp sign indicates an anchor.
       */
      protected void parseURL( URL url, String spec, int start, int limit )
      {
  
          // 
          // get the ref component
          // (from # to the end of the spec)
          //
  
          String ref = null;
          String remainder = spec.substring( start, limit );
          int refLoc = spec.indexOf("#", start );
          if( refLoc > -1 ) 
          {
              ref = spec.substring( refLoc + 1 );
              remainder = spec.substring( start, refLoc );
          }
  
          // 
          // get the query component
          // (from ? to the end of the remainder)
          //
  
          String query = null;
          int queryLoc = remainder.indexOf("?");
          if( queryLoc > -1 ) 
          {
              query = remainder.substring( queryLoc + 1, remainder.length() );
              remainder = remainder.substring( 0, queryLoc );
          }
  
          // 
          // get the path component
          // (from / to the end of the remainder)
          //
  
          String path = null;
          int pathLoc = remainder.indexOf("/");
          if( pathLoc > -1 ) 
          {
              path = remainder.substring( pathLoc, remainder.length() );
              remainder = remainder.substring( 0, pathLoc );
          }
  
          //
          // get the host
          // if a iiop version is included, then the host is between the "@"
          // character and the end of the reamining string, otherwise we are 
          // using a default IIOP protocol flag in which case the first ":" 
          // signals the beginning of the host declaration
          //
  
          String user = null;
          String hostAndPort = null;
          int hostLoc = remainder.indexOf("@");
          //System.out.println("hostAndPort: " + remainder );
  
          if( hostLoc < 0 )
          {
              if( remainder.startsWith(":") )
              {
                  user = "iiop:1.2";
                  hostAndPort = remainder.substring(1,remainder.length());
              }
              else
              {
                  throw new IllegalArgumentException(
                    "Missing @ delimiter - corbaloc:iiop:<major>.<minor>@<host>/<path>"
);
              }
          }
          else
          {
              // handle user info and host
  
              user = remainder.substring(0,hostLoc);
              hostAndPort = remainder.substring( hostLoc + 1, remainder.length() );
          }
  
          int port = -1;
          String host = null;
          int portLoc = hostAndPort.indexOf(":");
          if( portLoc > -1 ) 
          {
              host = hostAndPort.substring(0,portLoc);
              try
              {
                  port = Integer.parseInt( hostAndPort.substring( portLoc + 1, hostAndPort.length()
) );
              }
              catch( Throwable e )
              {
                  throw new IllegalArgumentException(
                    "Invalid port value: " + spec );
              }
          }
  
          //
          // create authority string dependending of non-default port reference
          //
  
          String authority = null;
          if( port == getDefaultPort() )
          {
              authority = host;
          }
          else
          {
              authority = host + ":" + port;
          }
  
          //
          // set the URL parameters
          //
  
          setURL( url, "corbaloc", host, port, authority, user, path, query, ref );
  
      } 
  
     /**
      * Retuns the default port for a CORBA application.
      * @return int the default port value
      */
      protected int getDefaultPort()
      {
          return 2809;
      }
  
     /**
      * Returns the URL in a string form.
      * @param url to the URL to externalize
      * @return String the external form of the URL
      */
      protected String toExternalForm( URL url )
      {
  	  StringBuffer result = new StringBuffer( url.getProtocol());
          result.append(":");
          if( url.getUserInfo() == null )
          {
              result.append(":");
              result.append( url.getAuthority() );
          }
          else
          {
              result.append( url.getUserInfo() );
              result.append( "@" );
              result.append( url.getAuthority() );
          }
          if (url.getFile() != null) 
          {
              result.append(url.getFile());
          }
  	  if (url.getRef() != null) 
          {
  	      result.append("#");
              result.append(url.getRef());
  	  }
  	  return result.toString();
      }
  }
  
  
  
  1.1                  jakarta-avalon-apps/enterprise/orb/src/java/org/apache/orb/corbaloc/CorbalocURLConnection.java
  
  Index: CorbalocURLConnection.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   *
   * Original contribution OSM SARL 2002, http://www.osm.net
   */
  
  package org.apache.orb.corbaloc;
  
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.URLStreamHandler;
  
  import org.omg.CORBA.ORB;
  
  /**
   * URL connection handler for the corbaloc protocol. New instances of 
   * the <code>CorbalocURLConnection</code> are created by a corbaloc
   * URL <code>Handler</code> class.
   * @see org.apache.orb.corbaloc.Handler
   */
  public class CorbalocURLConnection extends URLConnection
  {
  
      /**
       * The current ORB.
       */
       private ORB m_orb;
  
      /**
       * The corbaloc URL.
       */
       private URL m_url;
  
      /**
       * the primary object resolved during the establishment of a connection.
       */
       private org.omg.CORBA.Object m_object;
  
       
      /**
       * Creation of a new <code>CorbalocURLConnection</code> handler.
       * @param orb the current orb
       * @param url the corbaloc URL
       */
       public CorbalocURLConnection( ORB orb, URL url )
       {
           super( url );
           m_orb = orb;
           m_url = url;
       }
  
      /**
       * Establishment of a connection to the object defined by the URL.
       * The implementation uses the underlying ORB to resolve a CORBA 
       * object reference using the <code>org.omg.CORBA.ORB.string_to_object</code>
       * operation.
       *
       * @exception IOException if an error is raised while attempting to 
       *  establish the connection.
       */
       public void connect() throws IOException
       {
          if(( m_orb == null ) || ( m_url == null ))
          {
              throw new IOException("ORB or URL has not been initialized.");
          }
  
          //
          // make sure that we are only dealing with the strict corbaloc URL
          // (i.e. no query or ref elements in the URL)
          //
  
          String path = url.toExternalForm();
          int queryIndex = path.indexOf("?");
          if( queryIndex > -1 )
          {
              path = path.substring( 0, queryIndex );
          }
          int refIndex = path.indexOf("#");
          if( refIndex > -1 )
          {
              path = path.substring( 0, refIndex );
          }
  
          //
          // establish the primary object reference using the strict URL
          //
  
          try
          {
              m_object = m_orb.string_to_object( path );
          }
          catch( Throwable e )
          {
              throw new IOException( e.toString() );
          }
       }
  
      /**
       * Returns the object referenced by the URL.  If a connection has not been 
       * performed, a commection and retrival of the object reference will be 
       * performed.  A client application should narrow the object to the 
       * appropriate CORBA type.
       * @return Object and instance of org.omg.CORBA.Object
       * @exception IOException if an error occurs whhile attempting to retireve 
       *   the object reference.
       */
       public Object getContent() throws IOException
       {
           return getContent( new Class[0] );
       }
  
      /**
       * Retrieves the contents of this URL connection. 
       *
       * @param classes the <code>Class</code> array 
       * indicating the requested types
       * @return     the object fetched that is the first match of the type
       *               specified in the classes array. null if none of 
       *               the requested types are supported.
       *               The <code>instanceOf</code> operation should be used to

       *               determine the specific kind of object returned.
       * @exception  IOException              if an I/O error occurs while
       *               getting the content.
       * @exception  UnknownServiceException  if the protocol does not support
       *               the content type.
       */
      public Object getContent( Class[] classes ) throws IOException 
      {
          //
          // currently returns the primary object established by the 
          // raw corbaloc URL - needs to be upgraded to allow association
          // of a content handler factory and handling of responses via
          // return getContentHandler().getContent(this, classes);
          //
          // Requirement is to take a url referencing a servant implemeting
          // a query handler abstract interface, apply the query, and return 
          // the query result using a local handler.
          //
          // Examples:
          //
          // The following URL would return an object reference to a 
          // a workspace.
          //    corbaloc::home.osm.net/workspace?id=1234 
          // The getContent() operation would return the result of resolving
          // the workspace instance (i.e. a workspace object reference)
          //
          // A getContent( new Class[]{ "x.y.text.plain.MyPlainTextContentHandler" } ) 
          // would return the value of MyPlainTextContentHandler.getContent().
          //
  
          if( m_object == null )
          {
              connect();
          }
  
          // if the query portion of the URL is not null, and the object reference
          // is narrowable to a query handler, then narrow and invoke a query and 
          // return the result
  
          
          return m_object();
      }
  
  }
  
  
  

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


Mime
View raw message