@Override public void setPassword(String inPassword) { failIfStarted(Messages.ILLEGAL_STATE_CHANGE_PASSWORD); mPassword = inPassword; }
@Override public void handleError(Throwable inT) { if(inT instanceof Exception) { mDataEmitter.onException((Exception)inT); } } @Override
/** * Sends the connection change status to the adapter. * * @param inOldStatus old status. * @param inNewStatus new status. */ private void sendConnectedChanged(boolean inOldStatus, boolean inNewStatus) { mAdapter.connectionStatusChanged(inOldStatus, inNewStatus); }
@Override protected void preStart() throws ModuleException { //Check if the broker URL is supplied String url = getURL(); if(url == null) { throw new ModuleException(Messages.START_FAIL_NO_URL); } try { mDataEmitter = new RemoteDataEmitter(url, getUsername(), mPassword, this); } catch(Exception e) { throw new ModuleException(e, Messages.ERROR_STARTING_MODULE); } }
MessagingDelegate delegate = (MessagingDelegate) mContext.getBean( "delegate", MessagingDelegate.class); //$NON-NLS-1$ delegate.setDataEmitter(this); setLastFailure(null); sendConnectedChanged(false, true);
@Override protected void preStop() throws ModuleException { mDataEmitter.close(); mDataEmitter = null; }
@Override public boolean isConnected() { return getState().isStarted() && mDataEmitter != null && mDataEmitter.isConnected(); }
/** * Closes the connection to the remote source of data. * This instance is unusable after this method is invoked. * <p> * If one needs to reconnect to the remote source, a new instance * of this class should be created. * <p> * This method does not fail if the attempt to close the connection fails. * In case of failures, the failures are logged and the method returns * silently. */ public synchronized void close() { if(mContext == null) { return; } boolean isConnected = getLastFailure() == null; try { mContext.close(); } catch (Exception e) { //Swallow the exception as it prevents the module from stopping. //If the receiver closed the connection from its end //this method always fails. Messages.LOG_ERROR_CLOSING_CONNECTION.warn(this, e); } mContext = null; if(isConnected) { sendConnectedChanged(isConnected, false); } }
/** * Receives an object * * @param inObject The received object. */ public void handleMessage(Object inObject) { mDataEmitter.receive(inObject); }
/** * Receives the data received from the remote source. * The received data is handed off to the adapter. * * @param inObject the data received from the remote source. */ void receive(Object inObject) { mAdapter.receiveData(inObject); }
/** * Sets the value of the last failure exception and sends out an * attribute change notification for {@link #isConnected()} if necessary. * <p> * It's assumed that this method can only be invoked when the module * is started. * * @param inLastFailure the failure exception, can be null. */ private void setLastFailure(Exception inLastFailure) { boolean oldConnected = mLastFailure == null; mLastFailure = inLastFailure; boolean newConnected = mLastFailure == null; if(oldConnected != newConnected) { sendConnectedChanged(oldConnected, newConnected); } }
@Override public String getLastFailure() { if(mDataEmitter != null && mDataEmitter.getLastFailure() != null) { return mDataEmitter.getLastFailure().toString(); } return null; }
/** * This method is invoked when any failures are encountered receiving * messages from the remote receiver. * <p> * The text of the failure is available via {@link #getLastFailure()}. * <p> * The a non-null parameter to this method causes {@link #isConnected()} * to return false. * * @param inException the failure encountered. */ void onException(Exception inException) { Messages.LOG_ERROR_RECEIVER_CONNECTION.warn(this, inException); setLastFailure(inException); }
@Override public Module create(Object... inParameters) throws ModuleCreationException { return new EmitterModule(new ModuleURN(PROVIDER_URN, (String)inParameters[0])); }
/** * Verifies if the module is not started. * * @param inMessage the message to use when the module is started. * * @throws IllegalStateException if the module is started. */ private void failIfStarted(I18NMessage0P inMessage) { if(getState().isStarted()) { throw new IllegalStateException(inMessage.getText()); } }
@Override public synchronized final void stop() { SLF4JLoggerProxy.debug(this, "Closing Strategy Agent Client"); //$NON-NLS-1$ try { try { doStop(); } catch (Exception ignored) {} try { if(emitter != null) { emitter.close(); } } catch (Exception ignored) {} } finally { emitter = null; running.set(false); SLF4JLoggerProxy.debug(this, "Closed Strategy Agent Client"); //$NON-NLS-1$ } } /**
@Override public void setUsername(String inUsername) { failIfStarted(Messages.ILLEGAL_STATE_CHANGE_USERNAME); mUsername = inUsername; }
@Override public void onException(IOException inException) { mDataEmitter.onException(inException); } /* (non-Javadoc)
@Override public void setURL(String inURL) { failIfStarted(Messages.ILLEGAL_STATE_CHANGE_URL); mURL = inURL; }
@Override public void onException(JMSException inException) { mDataEmitter.onException(inException); }