001/* 002 * Copyright (c) 2002-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 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.sql.Connection; 033 034import ptolemy.actor.TypedAtomicActor; 035import ptolemy.actor.TypedIOPort; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039 040////////////////////////////////////////////////////////////////////////// 041//// CloseDBConnection 042/** 043 * This actor disconnect from a database. The connection refernce is given by 044 * the dbcon input port. 045 * 046 * @UserLevelDocumentation This actor is used to release a database connection. 047 * The connection refernce is given by the dbcon input 048 * port. 049 * @author Efrat Jaeger 050 * @version $Id: CloseDBConnection.java 24234 2010-05-06 05:21:26Z welker $ 051 * @since Ptolemy II 3.0.2 052 */ 053public class CloseDBConnection extends TypedAtomicActor { 054 055 /** 056 * Construct an actor with the given container and name. 057 * 058 * @param container 059 * The container. 060 * @param name 061 * The name of this actor. 062 * @exception IllegalActionException 063 * If the actor cannot be contained by the proposed 064 * container. 065 * @exception NameDuplicationException 066 * If the container already has an actor with this name. 067 */ 068 069 public CloseDBConnection(CompositeEntity container, String name) 070 throws NameDuplicationException, IllegalActionException { 071 072 super(container, name); 073 074 dbcon = new TypedIOPort(this, "dbcon", true, false); 075 trigger = new TypedIOPort(this, "trigger", true, false); 076 trigger.setMultiport(true); 077 078 // Set the type constraints. 079 dbcon.setTypeEquals(DBConnectionToken.DBCONNECTION); 080 081 _attachText("_iconDescription", "<svg>\n" 082 + "<ellipse cx=\"0\" cy=\"-30\" " + "rx=\"20\" ry=\"10\"/>\n" 083 + "<line x1=\"20\" y1=\"0\" " + "x2=\"20\" y2=\"-30\"/>\n" 084 + "<line x1=\"-20\" y1=\"0\" " + "x2=\"-20\" y2=\"-30\"/>\n" 085 + "<line x1=\"-20\" y1=\"0\" " + "x2=\"20\" y2=\"0\"/>\n" 086 + "</svg>\n"); 087 088 } 089 090 // ///////////////////////////////////////////////////////////////// 091 // // ports and parameters //// 092 093 /** 094 * A reference to the database connection 095 * 096 * @UserLevelDocumentation A reference to the database connection 097 */ 098 public TypedIOPort dbcon; 099 100 /** 101 * A trigger for closing the db connection 102 * 103 * @UserLevelDocumentation This port is used to trigger the actor. 104 */ 105 public TypedIOPort trigger; 106 107 // ///////////////////////////////////////////////////////////////// 108 // // public methods //// 109 110 /** 111 * Closes the input database connection when triggered. 112 */ 113 114 public void fire() throws IllegalActionException { 115 try { 116 117 // super.fire(); 118 119 // Trigger the actor. 120 for (int i = 0; i < trigger.getWidth(); i++) { 121 if (trigger.hasToken(i)) { 122 trigger.get(i); 123 } 124 } 125 System.out.println("in close db con - after trigger"); 126 // Get the db connection. 127 DBConnectionToken _dbcon = (DBConnectionToken) dbcon.get(0); 128 Connection _con = null; 129 try { 130 _con = _dbcon.getValue(); 131 System.out.println(_con.toString()); 132 _con.close(); 133 System.out 134 .println("after closing the connection. Con is closed?" 135 + _con.isClosed()); 136 } catch (Exception ex) { 137 if (_con != null) { 138 // if the connection is still alive try to close it again. 139 if (!_con.isClosed()) { 140 _con.close(); 141 } 142 } 143 } 144 } catch (Exception ex) { 145 } 146 } 147}