public static VersionedConnection getConnection(final Channel channel, final Map<String, ?> environment) throws IOException { ClientConnection connection = new ClientConnection(channel, environment); connection.start(); return connection; }
ClientConnection(final Channel channel, final Map<String, ?> environment) { super(channel); this.channel = channel; handlerRegistry = createHandlerRegistry(); Integer seconds = null; if (environment != null && environment.containsKey(TIMEOUT_KEY)) {
/** * Reserves a correlation ID by taking the next value and ensuring it is stored in the Map. * * @return the next reserved correlation ID */ private synchronized int reserveNextCorrelationId(VersionedIoFuture future) { Integer next = getNextCorrelationId(); // Not likely but possible to use all IDs and start back at beginning while // old request still in progress. while (requests.containsKey(next)) { next = getNextCorrelationId(); } requests.put(next, future); return next; }
@Override public void write(DataOutput output) throws IOException { output.writeByte(GET_MBEAN_INFO); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
private void sendVersionHeader() throws IOException { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeBytes("JMX"); output.writeByte(VersionOne.getVersionIdentifier()); } }); }
@SuppressWarnings("unchecked") @Override protected T readValue(DataInput input) throws IOException { Unmarshaller unmarshaller = prepareForUnMarshalling(input); try { return ((T) unmarshaller.readObject()); } catch (ClassNotFoundException e) { throw new IOException(e); } catch (ClassCastException e) { throw new IOException(e); } }
void start() throws IOException { sendVersionHeader(); IoFuture<String> futureConnectionId = ConnectionIdReceiver.getConnectionId(channel); IoFuture.Status result = futureConnectionId.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: connectionId = futureConnectionId.get(); mbeanServerConnection = new TheConnection(); localNotificationManager = new LocalNotificationManager(); channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureConnectionId.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } }
@Override public void write(DataOutput output) throws IOException { output.writeByte(UNREGISTER_MBEAN); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
public Integer getMBeanCount() throws IOException { VersionedIoFuture<TypeExceptionHolder<Integer>> future = new VersionedIoFuture<TypeExceptionHolder<Integer>>(); final int correlationId = reserveNextCorrelationId(future); try { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeByte(GET_MBEAN_COUNT); output.writeInt(correlationId); } }); log.tracef("[%d] getMBeanCount - Request Sent", correlationId); IoFuture.Status result = future.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: TypeExceptionHolder<Integer> response = future.get(); if (response.e == null) { return response.value; } jmRuntimeException(response.e); throw toIoException(response.e); case FAILED: throw future.getException(); default: throw new IOException("Unable to obtain MBeanCount, status=" + result.toString()); } } finally { releaseCorrelationId(correlationId); } }
@Override public void handle(DataInput input, int correlationId) throws IOException { log.trace("Notification"); byte paramType = input.readByte(); if (paramType != INTEGER) { throw new IOException("Unexpected paramType"); } int listenerId = input.readInt(); paramType = input.readByte(); if (paramType != NOTIFICATION) { throw new IOException("Unexpected paramType"); } try { Unmarshaller unmarshaller = prepareForUnMarshalling(input); Notification notification = unmarshaller.readObject(Notification.class); paramType = unmarshaller.readByte(); if (paramType != OBJECT) { throw new IOException("Unexpected paramType"); } Object handBack = unmarshaller.readObject(); localNotificationManager.notify(listenerId, notification, handBack); } catch (ClassNotFoundException cnfe) { throw new IOException(cnfe); } }
void start() throws IOException { sendVersionHeader(); IoFuture<String> futureConnectionId = ConnectionIdReceiver.getConnectionId(channel); IoFuture.Status result = futureConnectionId.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: connectionId = futureConnectionId.get(); mbeanServerConnection = new TheConnection(); localNotificationManager = new LocalNotificationManager(); channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureConnectionId.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } }
@Override public void write(DataOutput output) throws IOException { output.writeByte(GET_OBJECT_INSTANCE); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
public String getDefaultDomain() throws IOException { VersionedIoFuture<TypeExceptionHolder<String>> future = new VersionedIoFuture<TypeExceptionHolder<String>>(); final int correlationId = reserveNextCorrelationId(future); try { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeByte(GET_DEFAULT_DOMAIN); output.writeInt(correlationId); } }); log.tracef("[%d] getDefaultDomain - Request Sent", correlationId); IoFuture.Status result = future.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: TypeExceptionHolder<String> response = future.get(); if (response.e == null) { return response.value; } jmRuntimeException(response.e); throw toIoException(response.e); case FAILED: throw future.getException(); default: throw new IOException("Unable to obtain DefaultDomain, status=" + result.toString()); } } finally { releaseCorrelationId(correlationId); } }
public static VersionedConnection getConnection(final Channel channel, final Map<String, ?> environment) throws IOException { ClientConnection connection = new ClientConnection(channel, environment); connection.start(); return connection; }
Unmarshaller unmarshaller = prepareForUnMarshalling(input); response.e = unmarshaller.readObject(Exception.class); } else {
/** * Reserves a correlation ID by taking the next value and ensuring it is stored in the Map. * * @return the next reserved correlation ID */ private synchronized int reserveNextCorrelationId(VersionedIoFuture future) { Integer next = getNextCorrelationId(); // Not likely but possible to use all IDs and start back at beginning while // old request still in progress. while (requests.containsKey(next)) { next = getNextCorrelationId(); } requests.put(next, future); return next; }
void start() throws IOException { sendVersionHeader(); IoFuture<String> futureConnectionId = ConnectionIdReceiver.getConnectionId(channel); IoFuture.Status result = futureConnectionId.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: connectionId = futureConnectionId.get(); mbeanServerConnection = new TheConnection(); localNotificationManager = new LocalNotificationManager(); channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureConnectionId.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } }
ClientConnection(final Channel channel, final Map<String, ?> environment) { super(channel); this.channel = channel; handlerRegistry = createHandlerRegistry(); Integer seconds = null; if (environment != null && environment.containsKey(TIMEOUT_KEY)) {
@Override public void write(DataOutput output) throws IOException { output.writeByte(IS_REGISTERED); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
public String[] getDomains() throws IOException { VersionedIoFuture<TypeExceptionHolder<String[]>> future = new VersionedIoFuture<TypeExceptionHolder<String[]>>(); final int correlationId = reserveNextCorrelationId(future); try { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeByte(GET_DOMAINS); output.writeInt(correlationId); } }); log.tracef("[%d] getDomains - Request Sent", correlationId); IoFuture.Status result = future.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: TypeExceptionHolder<String[]> response = future.get(); if (response.e == null) { return response.value; } jmRuntimeException(response.e); throw toIoException(response.e); case FAILED: throw future.getException(); default: throw new IOException("Unable to obtain Domains, status=" + result.toString()); } } finally { releaseCorrelationId(correlationId); } }