public void setListener(ConnectionEventListener eventListener) { if (!type.equals(Type.CONNECTION)) return; switch (state) { case START: eventListener.onConnectionStart((Connection) object); break; case ERROR: eventListener.onConnectionError((Connection) object, message); break; case FINISH: eventListener.onConnectionFinish((Connection) object); break; } }
/** * Fires a new connection event on all listeners. * * @param closed <code>true</code> if <code>close</code> has been called on the connection; <code> * false</code> if the <code>sqlException</code> represents an error where the connection may * not longer be used. * @param sqlException the SQLException to pass to the listeners */ public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) { if (_listeners.size() > 0) { ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException); Iterator iterator = _listeners.iterator(); while (iterator.hasNext()) { ConnectionEventListener listener = (ConnectionEventListener) iterator.next(); if (closed) { listener.connectionClosed(connectionEvent); } else { listener.connectionErrorOccurred(connectionEvent); } } } }
/** * Fires a new connection event on all listeners. * * @param closed <code>true</code> if <code>close</code> has been called on the connection; <code> * false</code> if the <code>sqlException</code> represents an error where the connection may * not longer be used. * @param sqlException the SQLException to pass to the listeners */ public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) { if (listeners.size() > 0) { ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException); Iterator iterator = listeners.iterator(); while (iterator.hasNext()) { ConnectionEventListener listener = (ConnectionEventListener) iterator.next(); if (closed) { listener.connectionClosed(connectionEvent); } else { try { if (connection == null || connection.isClosed()) { listener.connectionErrorOccurred(connectionEvent); } } catch (SQLException ex) { // Will never occur } } } } }
public void fireConnectionEvent(int event) { ConnectionEvent connnectionEvent = new ConnectionEvent(this, event); connnectionEvent.setConnectionHandle(this.fileConnection); for (ConnectionEventListener listener : this.listeners) { switch (event) { case LOCAL_TRANSACTION_STARTED: listener.localTransactionStarted(connnectionEvent); break; case LOCAL_TRANSACTION_COMMITTED: listener.localTransactionCommitted(connnectionEvent); break; case LOCAL_TRANSACTION_ROLLEDBACK: listener.localTransactionRolledback(connnectionEvent); break; case CONNECTION_CLOSED: listener.connectionClosed(connnectionEvent); break; default: throw new IllegalArgumentException("Unknown event: " + event); } } }
public synchronized void receiveClose(short channel, Close close) { setClosedForInput(true); _connectionEventListener.closeReceived(); switch (_state) { case UNOPENED: case AWAITING_OPEN: Error error = new Error(); error.setCondition(ConnectionError.CONNECTION_FORCED); error.setDescription("Connection close sent before connection was opened"); connectionError(error); break; case OPEN: sendClose(new Close()); break; case CLOSE_SENT: default: } }
public synchronized void receiveBegin(short channel, Begin begin) { short myChannelId; if (begin.getRemoteChannel() != null) { myChannelId = begin.getRemoteChannel().shortValue(); SessionEndpoint endpoint; try { endpoint = _sendingSessions[myChannelId]; } catch (IndexOutOfBoundsException e) { final Error error = new Error(); error.setCondition(ConnectionError.FRAMING_ERROR); error.setDescription( "BEGIN received on channel " + channel + " with given remote-channel " + begin.getRemoteChannel() + " which is outside the valid range of 0 to " + _channelMax + "."); connectionError(error); return; } if (endpoint != null) { if (_receivingSessions[channel] == null) { _receivingSessions[channel] = endpoint; endpoint.setReceivingChannel(channel); endpoint.setNextIncomingId(begin.getNextOutgoingId()); endpoint.setOutgoingSessionCredit(begin.getIncomingWindow()); } else { final Error error = new Error(); error.setCondition(ConnectionError.FRAMING_ERROR); error.setDescription( "BEGIN received on channel " + channel + " which is already in use."); connectionError(error); } } else { final Error error = new Error(); error.setCondition(ConnectionError.FRAMING_ERROR); error.setDescription( "BEGIN received on channel " + channel + " with given remote-channel " + begin.getRemoteChannel() + " which is not known as a begun session."); connectionError(error); } } else // Peer requesting session creation { myChannelId = getFirstFreeChannel(); if (myChannelId == -1) { // close any half open channel myChannelId = getFirstFreeChannel(); } if (_receivingSessions[channel] == null) { SessionEndpoint endpoint = new SessionEndpoint(this, begin); _receivingSessions[channel] = endpoint; _sendingSessions[myChannelId] = endpoint; Begin beginToSend = new Begin(); endpoint.setReceivingChannel(channel); endpoint.setSendingChannel(myChannelId); beginToSend.setRemoteChannel(UnsignedShort.valueOf(channel)); beginToSend.setNextOutgoingId(endpoint.getNextOutgoingId()); beginToSend.setOutgoingWindow(endpoint.getOutgoingWindowSize()); beginToSend.setIncomingWindow(endpoint.getIncomingWindowSize()); send(myChannelId, beginToSend); _connectionEventListener.remoteSessionCreation(endpoint); } else { final Error error = new Error(); error.setCondition(ConnectionError.FRAMING_ERROR); error.setDescription("BEGIN received on channel " + channel + " which is already in use."); connectionError(error); } } }