1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sql.rowset.serial;
  27 
  28 import java.sql.*;
  29 import java.io.*;
  30 import java.net.URL;
  31 
  32 
  33 /**
  34  * A serialized mapping in the Java programming language of an SQL
  35  * <code>DATALINK</code> value. A <code>DATALINK</code> value
  36  * references a file outside of the underlying data source that the
  37  * data source manages.
  38  * <P>
  39  * <code>RowSet</code> implementations can use the method <code>RowSet.getURL</code>
  40  * to retrieve a <code>java.net.URL</code> object, which can be used
  41  * to manipulate the external data.
  42  * <pre>
  43  *      java.net.URL url = rowset.getURL(1);
  44  * </pre>
  45  */
  46 public class SerialDatalink implements Serializable, Cloneable {
  47 
  48     /**
  49      * The extracted URL field retrieved from the DATALINK field.
  50      * @serial
  51      */
  52     private URL url;
  53 
  54     /**
  55      * The SQL type of the elements in this <code>SerialDatalink</code>
  56      * object.  The type is expressed as one of the contants from the
  57      * class <code>java.sql.Types</code>.
  58      * @serial
  59      */
  60     private int baseType;
  61 
  62     /**
  63      * The type name used by the DBMS for the elements in the SQL
  64      * <code>DATALINK</code> value that this SerialDatalink object
  65      * represents.
  66      * @serial
  67      */
  68     private String baseTypeName;
  69 
  70     /**
  71       * Constructs a new <code>SerialDatalink</code> object from the given
  72       * <code>java.net.URL</code> object.
  73       * <P>
  74       * @throws SerialException if url parameter is a null
  75       */
  76     public SerialDatalink(URL url) throws SerialException {
  77         if (url == null) {
  78             throw new SerialException("Cannot serialize empty URL instance");
  79         }
  80         this.url = url;
  81     }
  82 
  83     /**
  84      * Returns a new URL that is a copy of this <code>SerialDatalink</code>
  85      * object.
  86      *
  87      * @return a copy of this <code>SerialDatalink</code> object as a
  88      * <code>URL</code> object in the Java programming language.
  89      * @throws SerialException if the <code>URL</code> object cannot be de-serialized
  90      */
  91     public URL getDatalink() throws SerialException {
  92 
  93         URL aURL = null;
  94 
  95         try {
  96             aURL = new URL((this.url).toString());
  97         } catch (java.net.MalformedURLException e) {
  98             throw new SerialException("MalformedURLException: " + e.getMessage());
  99         }
 100         return aURL;
 101     }
 102 
 103     /**
 104      * Compares this {@code SerialDatalink} to the specified object.
 105      * The result is {@code true} if and only if the argument is not
 106      * {@code null} and is a {@code SerialDatalink} object whose URL is
 107      * identical to this object's URL
 108      *
 109      * @param  obj The object to compare this {@code SerialDatalink} against
 110      *
 111      * @return  {@code true} if the given object represents a {@code SerialDatalink}
 112      *          equivalent to this SerialDatalink, {@code false} otherwise
 113      *
 114      */
 115     public boolean equals(Object obj) {
 116         if (this == obj) {
 117             return true;
 118         }
 119         if (obj instanceof SerialDatalink) {
 120             SerialDatalink sdl = (SerialDatalink) obj;
 121             return url.equals(sdl.url);
 122         }
 123         return false;
 124     }
 125     
 126     /**
 127      * Returns a hash code for this {@code SerialDatalink}. The hash code for a
 128      * {@code SerialDatalink} object is taken as the hash code of
 129      * the {@code URL} it stores
 130      *
 131      * @return  a hash code value for this object.
 132      */
 133     public int hashCode() {
 134         return 31 + url.hashCode();    
 135     }
 136     
 137     /**
 138      * Returns a clone of this {@code SerialDatalink}. 
 139      *  
 140      * @return  a clone of this SerialDatalink
 141      */ 
 142     public Object clone() {
 143         try {
 144             SerialDatalink sdl = (SerialDatalink) super.clone();
 145             return sdl;
 146         } catch (CloneNotSupportedException ex) {
 147             // this shouldn't happen, since we are Cloneable
 148             throw new InternalError();
 149         } 
 150     }
 151        
 152     /**
 153      * readObject is called to restore the state of the {@code SerialDatalink}
 154      * from a stream.
 155      */
 156     private void readObject(ObjectInputStream s)
 157             throws IOException, ClassNotFoundException {
 158         s.defaultReadObject();
 159     }
 160     
 161     /**
 162      * writeObject is called to save the state of the {@code SerialDatalink}
 163      * to a stream.
 164      */
 165     private void writeObject(ObjectOutputStream s)
 166             throws IOException {
 167         s.defaultWriteObject();
 168     }
 169 
 170     /**
 171          * The identifier that assists in the serialization of this <code>SerialDatalink</code>
 172      * object.
 173      */
 174     static final long serialVersionUID = 2826907821828733626L;
 175 }