001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: berkley $' 006 * '$Date: 2010-04-28 00:12:36 +0000 (Wed, 28 Apr 2010) $' 007 * '$Revision: 24000 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.geon; 031 032import java.io.Serializable; 033import java.sql.Connection; 034 035import ptolemy.data.Token; 036import ptolemy.data.type.BaseType; 037import ptolemy.data.type.Type; 038import ptolemy.kernel.util.IllegalActionException; 039 040////////////////////////////////////////////////////////////////////////// 041//// DBConnectionToken 042/** 043 * A token that contains a reference to a database connection. Note that when 044 * this token constructed, the object passed to the constructor is not cloned. 045 * Thus, care must be exercised to ensure that actors do not modify that object 046 * in a nondeterministic way, unless such nondeterminism is acceptable. 047 * 048 * @author jaeger 049 * @version $Id: DBConnectionToken.java 24000 2010-04-28 00:12:36Z berkley $ 050 */ 051public class DBConnectionToken extends Token { 052 053 /** 054 * Construct an empty token. 055 */ 056 public DBConnectionToken() { 057 super(); 058 } 059 060 /** 061 * Construct a token with a reference to the specified connection. 062 * 063 * @exception IllegalActionException 064 * If the argument is not of the appropriate type (may be 065 * thrown by derived classes, but is not thrown here). 066 */ 067 public DBConnectionToken(Connection value) throws IllegalActionException { 068 _value = value; 069 } 070 071 // ///////////////////////////////////////////////////////////////// 072 // // public methods //// 073 074 /** 075 * Convert the specified token into an instance of DBConnectionToken. This 076 * method does lossless conversion. If the argument is already an instance 077 * of DBConnectionToken, it is returned without any change. Otherwise, if 078 * the argument is below DBConnectionToken in the type hierarchy, it is 079 * converted to an instance of DBConnectionToken and returned. If none of 080 * the above condition is met, an exception is thrown. 081 * 082 * @param token 083 * The token to be converted to a DBConnectionToken. 084 * @return A DBConnectionToken. 085 * @exception IllegalActionException 086 * If the conversion cannot be carried out. 087 */ 088 public static DBConnectionToken convert(Token token) 089 throws IllegalActionException { 090 if (token instanceof DBConnectionToken) { 091 return (DBConnectionToken) token; 092 } 093 094 throw new IllegalActionException(notSupportedConversionMessage(token, 095 "dbconnection")); 096 } 097 098 /** 099 * Return true if the argument is an instance of DBConnectionToken and its 100 * contained object is equal to the object contained in this token, as 101 * tested by the equals() method of the contained object. 102 * 103 * @param object 104 * An instance of Object. 105 * @return True if the argument is an instance of DBConnectionToken and its 106 * contained object is equal to the object contained in this token. 107 */ 108 public boolean equals(Object object) { 109 // This test rules out subclasses. 110 if (object.getClass() != DBConnectionToken.class) { 111 return false; 112 } 113 114 if (((DBConnectionToken) object).getValue().equals(_value)) { 115 return true; 116 } 117 return false; 118 } 119 120 /** 121 * Return the type of this token. 122 * 123 * @return {@link #DBCONNECTION}, the least upper bound of all the database 124 * connections 125 */ 126 public Type getType() { 127 return DBCONNECTION; 128 } 129 130 /** 131 * Return the java.sql.connection. 132 * 133 * @return The connection in this token. 134 */ 135 public Connection getValue() { 136 return _value; 137 } 138 139 /** 140 * Return a hash code value for this token. This method returns the hash 141 * code of the contained object. 142 * 143 * @return A hash code value for this token. 144 */ 145 public int hashCode() { 146 return _value.hashCode(); 147 } 148 149 /** 150 * Return the value of this token as a string that can be parsed by the 151 * expression language to recover a token with the same value. The returned 152 * syntax looks like a function call to a one argument method named 153 * "dbconnection". The argument is the string representation of the 154 * contained object, or the string "null" if the object is null. Notice that 155 * this syntax is not currently parseable by the expression language. 156 * 157 * @return A String representing the object. 158 */ 159 public String toString() { 160 if (_value != null) { 161 return "dbconnection(" + _value.toString() + ")"; 162 } else { 163 return "dbconnection(null)"; 164 } 165 } 166 167 /** 168 * The database connection type. 169 */ 170 public static class DBConnectionType implements Type, Serializable { 171 172 // ///////////////////////////////////////////////////////////////// 173 // // constructors //// 174 // The constructor is private to make a type safe enumeration. 175 // We could extend BaseType, yet the BaseType(Class, String) 176 // Constructor is private. 177 private DBConnectionType() { 178 super(); 179 } 180 181 // ///////////////////////////////////////////////////////////////// 182 // // public methods //// 183 184 /** 185 * Return a new type which represents the type that results from adding 186 * a token of this type and a token of the given argument type. 187 * 188 * @param rightArgumentType 189 * The type to add to this type. 190 * @return A new type, or BaseType.GENERAL, if the operation does not 191 * make sense for the given types. 192 */ 193 public Type add(Type rightArgumentType) { 194 return this; 195 } 196 197 /** 198 * Return this, that is, return the reference to this object. 199 * 200 * @return A DBConnectionType 201 */ 202 public Object clone() { 203 // FIXME: Note that we do not call super.clone() here. Is 204 // that right? 205 return this; 206 } 207 208 /** 209 * Convert the specified token to a token having the type represented by 210 * this object. 211 * 212 * @param token 213 * A token. 214 * @return A token. 215 * @exception IllegalActionException 216 * If lossless conversion cannot be done. 217 */ 218 public Token convert(Token token) throws IllegalActionException { 219 if (token instanceof DBConnectionToken) { 220 return token; 221 } else { 222 throw new IllegalActionException("Attempt to convert token " 223 + token + " into a DBConnection token, " 224 + "which is not possible."); 225 } 226 } 227 228 /** 229 * Return a new type which represents the type that results from 230 * dividing a token of this type and a token of the given argument type. 231 * 232 * @param rightArgumentType 233 * The type to add to this type. 234 * @return A new type, or BaseType.GENERAL, if the operation does not 235 * make sense for the given types. 236 */ 237 public Type divide(Type rightArgumentType) { 238 return this; 239 } 240 241 /** 242 * Return the class for tokens that this basetype represents. The 243 * DBConectionToken class. 244 */ 245 public Class getTokenClass() { 246 return DBConnectionToken.class; 247 } 248 249 /** 250 * Return true if this type does not correspond to a single token class. 251 * This occurs if the type is not instantiable, or it represents either 252 * an abstract base class or an interface. 253 * 254 * @return Always return false, this token is instantiable. 255 */ 256 public boolean isAbstract() { 257 return false; 258 } 259 260 /** 261 * Test if the argument type is compatible with this type. The method 262 * returns true if this type is UNKNOWN, since any type is a 263 * substitution instance of it. If this type is not UNKNOWN, this method 264 * returns true if the argument type is less than or equal to this type 265 * in the type lattice, and false otherwise. 266 * 267 * @param type 268 * An instance of Type. 269 * @return True if the argument type is compatible with this type. 270 */ 271 public boolean isCompatible(Type type) { 272 return type == this; 273 } 274 275 /** 276 * Test if this Type is UNKNOWN. 277 * 278 * @return True if this Type is not UNKNOWN; false otherwise. 279 */ 280 public boolean isConstant() { 281 return true; 282 } 283 284 /** 285 * Return this type's node index in the (constant) type lattice. 286 * 287 * @return this type's node index in the (constant) type lattice. 288 */ 289 public int getTypeHash() { 290 return Type.HASH_INVALID; 291 } 292 293 /** 294 * Determine if this type corresponds to an instantiable token classes. 295 * A BaseType is instantiable if it does not correspond to an abstract 296 * token class, or an interface, or UNKNOWN. 297 * 298 * @return True if this type is instantiable. 299 */ 300 public boolean isInstantiable() { 301 return true; 302 } 303 304 /** 305 * Return true if the argument is a substitution instance of this type. 306 * 307 * @param type 308 * A Type. 309 * @return True if this type is UNKNOWN; false otherwise. 310 */ 311 public boolean isSubstitutionInstance(Type type) { 312 return this == type; 313 } 314 315 /** 316 * Return a new type which represents the type that results from 317 * moduloing a token of this type and a token of the given argument 318 * type. 319 * 320 * @param rightArgumentType 321 * The type to add to this type. 322 * @return A new type, or BaseType.GENERAL, if the operation does not 323 * make sense for the given types. 324 */ 325 public Type modulo(Type rightArgumentType) { 326 return this; 327 } 328 329 /** 330 * Return a new type which represents the type that results from 331 * multiplying a token of this type and a token of the given argument 332 * type. 333 * 334 * @param rightArgumentType 335 * The type to add to this type. 336 * @return A new type, or BaseType.GENERAL, if the operation does not 337 * make sense for the given types. 338 */ 339 public Type multiply(Type rightArgumentType) { 340 return this; 341 } 342 343 /** 344 * Return the type of the multiplicative identity for elements of this 345 * type. 346 * 347 * @return A new type, or BaseType.GENERAL, if the operation does not 348 * make sense for the given types. 349 */ 350 public Type one() { 351 return this; 352 } 353 354 /** 355 * Return a new type which represents the type that results from 356 * subtracting a token of this type and a token of the given argument 357 * type. 358 * 359 * @param rightArgumentType 360 * The type to add to this type. 361 * @return BaseType.GENERAL because the operation does not make sense 362 * for the given types. 363 */ 364 public Type subtract(Type rightArgumentType) { 365 return BaseType.GENERAL; 366 } 367 368 /** 369 * Return the string representation of this type. 370 * 371 * @return A String. 372 */ 373 public String toString() { 374 return "dbconnection"; 375 } 376 377 /** 378 * Return the type of the additive identity for elements of this type. 379 * 380 * @return Return BaseType.GENERAL, because the operation does not make 381 * sense for the given types. 382 */ 383 public Type zero() { 384 return BaseType.GENERAL; 385 } 386 } 387 388 /** 389 * The DBConnection type: the least upper bound of all the cryptographic key 390 * types. 391 */ 392 public static final Type DBCONNECTION = new DBConnectionType(); 393 394 // ///////////////////////////////////////////////////////////////// 395 // // protected variables //// 396 397 /* The java.sql.Connection */ 398 protected Connection _value = null; 399}