/** * Returns the current stream connection that is opened. * * @return a stream connection, can be a cached one, never <code>null</code>. * @throws IOException in case of I/O problems creating the stream connection. */ private StreamConnection getStreamConnection() throws IOException { if (this.connection == null) { final String uri = this.config.getConnectionURI(); if (LOG.isLoggable(Level.INFO)) { LOG.info("Connecting to " + uri); } this.connection = createStreamConnection(uri); } return this.connection; }
/** Requests an HTTP resource and returns its response as a string */ public static String request(String httpUrl, Transaction tx) throws IOException { checkTransaction(tx); DataBuffer buffer = new DataBuffer(256, false); InputStream is = null; Connection conn = null; try { // append connection suffix httpUrl += getConnectionSuffix(); Logger.log("Opening URL: " + httpUrl); conn = Connector.open(httpUrl); tx.setNetworkOperation(conn, is); if (conn instanceof HttpConnection) { HttpConnection httpConn = (HttpConnection) conn; int responseCode = httpConn.getResponseCode(); is = httpConn.openInputStream(); tx.setNetworkOperation(conn, is); int length = is.read(buffer.getArray()); buffer.setLength(length); String response = new String(buffer.getArray(), buffer.getArrayStart(), buffer.getArrayLength()); if (responseCode == 200) { Logger.log("HTTP response: " + response); return response; } else { Logger.warn("HTTP error response: " + response); throw new IOException("Http error: " + responseCode + ", " + response); } } else { throw new IOException("Can not make HTTP connection for URL '" + httpUrl + "'"); } } finally { PushUtils.close(conn, is, null); tx.clearNetworkOperation(); } }
/** * A representation of the LogicSniffer device. * * @author J.W. Janssen */ public class LogicSnifferDevice implements Device { // CONSTANTS private static final String NAME = "OpenBench LogicSniffer"; private static final Logger LOG = Logger.getLogger(LogicSnifferDevice.class.getName()); // VARIABLES private LogicSnifferConfig config; private volatile DependencyManager dependencyManager; private volatile ManagedServiceFactory deviceProfileManagerServiceFactory; private volatile ConnectorService connectorService; private volatile StreamConnection connection; private volatile LogicSnifferConfigDialog configDialog; // METHODS public LogicSnifferConfig getConfig() { return config; } /** {@inheritDoc} */ @Override public void close() throws IOException { if (this.connection != null) { this.connection.close(); this.connection = null; } } /** {@inheritDoc} */ @Override public AcquisitionTask createAcquisitionTask(final AcquisitionProgressListener aProgressListener) throws IOException { return new LogicSnifferAcquisitionTask( this.config, getStreamConnection(), getDeviceProfileManager(), aProgressListener); } /** {@inheritDoc} */ @Override public CancelTask createCancelTask() throws IOException { if (this.config.isRleEnabled()) { return new LogicSnifferCancelTask(getStreamConnection()); } // Simply use the default behaviour... return null; } /** @see nl.lxtreme.ols.api.devices.Device#getName() */ public String getName() { return NAME; } /** @see nl.lxtreme.ols.api.devices.Device#isSetup() */ @Override public boolean isSetup() { return this.config != null; } /** * Displays the device controller dialog with enabled configuration portion and waits for user * input. * * @see nl.lxtreme.ols.api.devices.Device#setupCapture() */ @Override public boolean setupCapture(final Window aOwner) { // Just to be sure... disposeConfigDialog(); this.configDialog = new LogicSnifferConfigDialog(aOwner, this); try { boolean configConfirmed = this.configDialog.showDialog(); if (configConfirmed) { this.config = this.configDialog.getConfiguration(); } return configConfirmed; } finally { this.configDialog.dispose(); this.configDialog = null; } } /** * @param uri * @return * @throws IOException */ final StreamConnection createStreamConnection(final String uri) throws IOException { return (StreamConnection) this.connectorService.open(uri, ConnectorService.READ_WRITE, true /* timeouts */); } /** * Returns the default device profile. * * @return a default profile, never <code>null</code>. */ final DeviceProfile getDefaultProfile() { return getDeviceProfileManager().getDefaultProfile(); } /** * Returns the current device profile manager. * * @return a device profile manager, never <code>null</code>. */ final DeviceProfileManager getDeviceProfileManager() { return (DeviceProfileManager) this.deviceProfileManagerServiceFactory; } /** Called when this class is unregistered as OSGi service. */ protected void destroy(final Component aComponent) { disposeConfigDialog(); } /** * Called when this class is registered as OSGi service. * * @param aComponent the bundle context to use, cannot be <code>null</code>. */ protected void init(final Component aComponent) { final String pmFilter = String.format("(%s=%s)", Constants.SERVICE_PID, DeviceProfileManager.SERVICE_PID); aComponent // .add( this.dependencyManager .createServiceDependency() // .setService(ManagedServiceFactory.class, pmFilter) // .setAutoConfig("deviceProfileManagerServiceFactory") // .setInstanceBound(true) // .setRequired(true)) // .add( this.dependencyManager .createServiceDependency() // .setService(ConnectorService.class) // .setAutoConfig("connectorService") // .setInstanceBound(true) // .setRequired(true) // ); } /** * Disposes the current configuration dialog, if one is still visible on screen. If no * configuration dialog is visible, this method does nothing. */ private void disposeConfigDialog() { if (this.configDialog != null) { SwingComponentUtils.dispose(this.configDialog); this.configDialog = null; } } /** * Returns the current stream connection that is opened. * * @return a stream connection, can be a cached one, never <code>null</code>. * @throws IOException in case of I/O problems creating the stream connection. */ private StreamConnection getStreamConnection() throws IOException { if (this.connection == null) { final String uri = this.config.getConnectionURI(); if (LOG.isLoggable(Level.INFO)) { LOG.info("Connecting to " + uri); } this.connection = createStreamConnection(uri); } return this.connection; } }