Author: david.lloyd(a)jboss.com
Date: 2008-02-21 13:17:50 -0500 (Thu, 21 Feb 2008)
New Revision: 3470
Added:
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/BufferFactory.java
Removed:
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/BufferFactory.java
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/ExceptionUtil.java
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/Resource.java
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/URIUtil.java
Modified:
remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/stream/Streams.java
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java
Log:
Delete and move a bunch of unused stuff out of the util package
Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/stream/Streams.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/stream/Streams.java 2008-02-21
18:11:00 UTC (rev 3469)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/stream/Streams.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -2,13 +2,9 @@
import java.io.EOFException;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
-import org.jboss.cx.remoting.core.util.BufferFactory;
/**
*
@@ -17,24 +13,6 @@
private Streams() {
}
- private static final ByteBuffer emptyBuffer = ByteBuffer.allocate(0);
-
- public static InputStream getInputStream(ObjectSource<ByteBuffer> source,
boolean propagateClose) {
- return new ByteBufferSourceInputStream(source, propagateClose);
- }
-
- public static OutputStream getOutputStream(ObjectSink<ByteBuffer> sink, final
BufferFactory bufferFactory, final boolean propagateFlush, boolean propagateClose) {
- return new ByteBufferSinkOutputStream(sink, bufferFactory, propagateFlush,
propagateClose);
- }
-
- public static ObjectSource<ByteBuffer> getObjectSource(InputStream stream,
final BufferFactory bufferFactory, boolean propagateClose) {
- return new InputStreamByteBufferSource(stream, bufferFactory, propagateClose);
- }
-
- public static ObjectSink<ByteBuffer> getObjectSink(OutputStream stream, boolean
propagateFlush, boolean propagateClose) {
- return new OutputStreamByteBufferSink(stream, propagateFlush, propagateClose);
- }
-
public static <T> ObjectSink<T>
getCollectionObjectSink(Collection<T> target) {
return new CollectionObjectSink<T>(target);
}
@@ -43,258 +21,6 @@
return new IteratorObjectSource<T>(iterator);
}
- private static final class ByteBufferSourceInputStream extends InputStream {
- private final ObjectSource<ByteBuffer> source;
- private ByteBuffer current = emptyBuffer;
- private final boolean propagateClose;
-
- public void close() throws IOException {
- if (propagateClose) {
- source.close();
- }
- }
-
- private ByteBufferSourceInputStream(final ObjectSource<ByteBuffer> source,
final boolean propagateClose) {
- this.source = source;
- this.propagateClose = propagateClose;
- }
-
- public int read() throws IOException {
- if (current == null) {
- return -1;
- }
- while (!current.hasRemaining()) {
- if (!next()) {
- return -1;
- }
- }
- return current.get() & 0xff;
- }
-
- public int read(byte b[], int off, int len) throws IOException {
- if (current == null) {
- return -1;
- }
- final int total = len;
- loop:
- while (len > 0) {
- while (!current.hasRemaining()) {
- if (!next()) {
- break loop;
- }
- }
- final int count = Math.min(len, current.remaining());
- current.get(b, off, count);
- len -= count;
- off += count;
- }
- return total - len;
- }
-
- public long skip(long n) throws IOException {
- final long total = n;
- loop:
- while (n > 0L) {
- while (!current.hasRemaining()) {
- if (!next()) {
- break loop;
- }
- }
- // since remaining is an int, the result will always fit in an int
- final int count = (int) Math.min(n, (long) current.remaining());
- current.position(current.position() + count);
- n -= (long) count;
- }
- return total - n;
- }
-
- public int available() throws IOException {
- return current == null ? 0 : current.remaining();
- }
-
- private boolean next() throws IOException {
- if (source.hasNext()) {
- current = source.next();
- return true;
- } else {
- current = null;
- return false;
- }
- }
- }
-
- private static final class ByteBufferSinkOutputStream extends OutputStream {
- private final ObjectSink<ByteBuffer> sink;
- private final boolean propagateClose;
- private final boolean propagateFlush;
- private final BufferFactory bufferFactory;
-
- private ByteBuffer buffer;
-
- public void close() throws IOException {
- if (propagateClose) {
- sink.close();
- }
- }
-
- private ByteBufferSinkOutputStream(final ObjectSink<ByteBuffer> sink, final
BufferFactory bufferFactory, final boolean propagateFlush, final boolean propagateClose)
{
- this.sink = sink;
- this.bufferFactory = bufferFactory;
- this.propagateClose = propagateClose;
- this.propagateFlush = propagateFlush;
- }
-
- public void write(int b) throws IOException {
- if (buffer == null) {
- buffer = bufferFactory.create();
- }
- buffer.put((byte) b);
- if (!buffer.hasRemaining()) {
- doFlush();
- }
- }
-
- public void write(byte b[], int off, int len) throws IOException {
- while (len > 0) {
- if (buffer == null) {
- buffer = bufferFactory.create();
- }
- final int count = Math.min(buffer.remaining(), len);
- buffer.put(b, off, count);
- len -= count;
- off += count;
- if (!buffer.hasRemaining()) {
- doFlush();
- }
- }
- }
-
- public void flush() throws IOException {
- if (buffer == null) {
- buffer = bufferFactory.create();
- }
- if (buffer.position() > 0) {
- doFlush();
- }
- if (propagateFlush) {
- sink.flush();
- }
- }
-
- private void doFlush() throws IOException {
- buffer.flip();
- try {
- sink.accept(buffer);
- } finally {
- buffer = null;
- }
- }
- }
-
- private static final class InputStreamByteBufferSource implements
ObjectSource<ByteBuffer> {
- private final InputStream stream;
- private final boolean propagateClose;
- private final BufferFactory bufferFactory;
-
- private ByteBuffer buffer;
-
- public InputStreamByteBufferSource(final InputStream stream, final BufferFactory
bufferFactory, final boolean propagateClose) {
- this.stream = stream;
- this.bufferFactory = bufferFactory;
- this.propagateClose = propagateClose;
- }
-
- public boolean hasNext() throws IOException {
- return buffer != null && buffer.hasRemaining() || populate();
- }
-
- public ByteBuffer next() throws IOException {
- if (buffer == null || !buffer.hasRemaining()) {
- if (!populate()) {
- throw new EOFException("End of file reached");
- }
- }
- try {
- return buffer;
- } finally {
- buffer = null;
- }
- }
-
- private boolean populate() throws IOException {
- if (buffer == null) {
- buffer = bufferFactory.create();
- }
- int count;
- int pos = buffer.position();
- int rem = buffer.remaining();
- while (rem > 0 && (count = stream.read(buffer.array(), pos, rem))
> 0) {
- pos += count;
- rem -= count;
- }
- buffer.position(pos);
- return buffer.hasRemaining();
- }
-
- public void close() throws IOException {
- if (propagateClose) {
- stream.close();
- }
- }
- }
-
- private static final class OutputStreamByteBufferSink implements
ObjectSink<ByteBuffer> {
- private final OutputStream stream;
- private final boolean propagateClose;
- private final boolean propagateFlush;
- private byte[] tempStore;
-
- public OutputStreamByteBufferSink(final OutputStream stream, final boolean
propagateFlush, final boolean propagateClose) {
- this.stream = stream;
- this.propagateClose = propagateClose;
- this.propagateFlush = propagateFlush;
- }
-
- public void accept(final ByteBuffer buffer) throws IOException {
- if (!buffer.hasRemaining()) {
- return;
- }
- if (buffer.hasArray()) {
- // Optimization: we can write data directly from the buffer array, avoid
a copy
- final byte[] bytes = buffer.array();
- final int offs = buffer.arrayOffset();
- final int len = buffer.remaining();
- stream.write(bytes, offs, len);
- buffer.clear();
- } else {
- // gotta copy the data out a bit at a time
- final byte[] tempStore;
- if (this.tempStore == null) {
- tempStore = this.tempStore = new byte[1024];
- } else {
- tempStore = this.tempStore;
- }
- while (buffer.hasRemaining()) {
- int count = Math.min(buffer.remaining(), tempStore.length);
- buffer.get(tempStore, 0, count);
- stream.write(tempStore, 0, count);
- }
- }
- }
-
- public void flush() throws IOException {
- if (propagateFlush) {
- stream.flush();
- }
- }
-
- public void close() throws IOException {
- if (propagateClose) {
- stream.close();
- }
- }
- }
-
private static final class CollectionObjectSink<T> implements
ObjectSink<T> {
private final Collection<T> target;
Modified:
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java
===================================================================
---
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java 2008-02-21
18:11:00 UTC (rev 3469)
+++
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -18,7 +18,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
-import org.jboss.cx.remoting.core.util.BufferFactory;
+import org.jboss.cx.remoting.core.security.sasl.BufferFactory;
import org.jboss.cx.remoting.core.util.IoUtil;
import javax.crypto.BadPaddingException;
Copied:
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/BufferFactory.java
(from rev 3461,
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/BufferFactory.java)
===================================================================
---
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/BufferFactory.java
(rev 0)
+++
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/BufferFactory.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -0,0 +1,59 @@
+package org.jboss.cx.remoting.core.security.sasl;
+
+import java.nio.ByteBuffer;
+
+/**
+ *
+ */
+public abstract class BufferFactory {
+ protected final int size;
+ protected final boolean direct;
+
+ protected BufferFactory(final int size, final boolean direct) {
+ this.size = size;
+ this.direct = direct;
+ }
+
+ public abstract ByteBuffer create();
+
+ public final int getSize() {
+ return size;
+ }
+
+ public final boolean isDirect() {
+ return direct;
+ }
+
+ public static BufferFactory create(int size, boolean direct) throws
IllegalArgumentException {
+ if (size < 0) {
+ throw new IllegalArgumentException("Negative buffer size given");
+ }
+ if (direct) {
+ return new DirectFactoryImpl(size);
+ } else {
+ return new HeapFactoryImpl(size);
+ }
+ }
+
+ private static final class HeapFactoryImpl extends BufferFactory {
+
+ private HeapFactoryImpl(final int size) {
+ super(size, false);
+ }
+
+ public ByteBuffer create() {
+ return ByteBuffer.allocate(size);
+ }
+ }
+
+ private static final class DirectFactoryImpl extends BufferFactory {
+
+ private DirectFactoryImpl(final int size) {
+ super(size, true);
+ }
+
+ public ByteBuffer create() {
+ return ByteBuffer.allocateDirect(size);
+ }
+ }
+}
Deleted:
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/BufferFactory.java
===================================================================
---
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/BufferFactory.java 2008-02-21
18:11:00 UTC (rev 3469)
+++
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/BufferFactory.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -1,59 +0,0 @@
-package org.jboss.cx.remoting.core.util;
-
-import java.nio.ByteBuffer;
-
-/**
- *
- */
-public abstract class BufferFactory {
- protected final int size;
- protected final boolean direct;
-
- protected BufferFactory(final int size, final boolean direct) {
- this.size = size;
- this.direct = direct;
- }
-
- public abstract ByteBuffer create();
-
- public final int getSize() {
- return size;
- }
-
- public final boolean isDirect() {
- return direct;
- }
-
- public static BufferFactory create(int size, boolean direct) throws
IllegalArgumentException {
- if (size < 0) {
- throw new IllegalArgumentException("Negative buffer size given");
- }
- if (direct) {
- return new DirectFactoryImpl(size);
- } else {
- return new HeapFactoryImpl(size);
- }
- }
-
- private static final class HeapFactoryImpl extends BufferFactory {
-
- private HeapFactoryImpl(final int size) {
- super(size, false);
- }
-
- public ByteBuffer create() {
- return ByteBuffer.allocate(size);
- }
- }
-
- private static final class DirectFactoryImpl extends BufferFactory {
-
- private DirectFactoryImpl(final int size) {
- super(size, true);
- }
-
- public ByteBuffer create() {
- return ByteBuffer.allocateDirect(size);
- }
- }
-}
Deleted:
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/ExceptionUtil.java
===================================================================
---
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/ExceptionUtil.java 2008-02-21
18:11:00 UTC (rev 3469)
+++
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/ExceptionUtil.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -1,77 +0,0 @@
-package org.jboss.cx.remoting.core.util;
-
-import java.io.EOFException;
-import java.io.IOException;
-
-import javax.transaction.xa.XAException;
-
-/**
- *
- */
-public final class ExceptionUtil {
- private ExceptionUtil() {
- }
-
- public static IOException ioException(String msg, Exception wrap) {
- final String newMsg;
- final String wrapMessage = wrap.getMessage();
- if (msg == null) {
- if (wrapMessage == null) {
- newMsg = "No message";
- } else {
- newMsg = wrapMessage;
- }
- } else {
- if (wrapMessage == null) {
- newMsg = msg;
- } else {
- newMsg = msg + ": " + wrapMessage;
- }
- }
- IOException ioException = new IOException(newMsg);
- ioException.setStackTrace(wrap.getStackTrace());
- return ioException;
- }
-
- public static XAException xaException(String msg, Exception wrap) {
- final String newMsg;
- final String wrapMessage = wrap.getMessage();
- if (msg == null) {
- if (wrapMessage == null) {
- newMsg = "No message";
- } else {
- newMsg = wrapMessage;
- }
- } else {
- if (wrapMessage == null) {
- newMsg = msg;
- } else {
- newMsg = msg + ": " + wrapMessage;
- }
- }
- XAException xaException = new XAException(newMsg);
- xaException.setStackTrace(wrap.getStackTrace());
- return xaException;
- }
-
- public static EOFException eofException(String msg, Exception wrap) {
- final String newMsg;
- final String wrapMessage = wrap.getMessage();
- if (msg == null) {
- if (wrapMessage == null) {
- newMsg = "No message";
- } else {
- newMsg = wrapMessage;
- }
- } else {
- if (wrapMessage == null) {
- newMsg = msg;
- } else {
- newMsg = msg + ": " + wrapMessage;
- }
- }
- EOFException eofException = new EOFException(newMsg);
- eofException.setStackTrace(wrap.getStackTrace());
- return eofException;
- }
-}
Deleted: remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/Resource.java
===================================================================
---
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/Resource.java 2008-02-21
18:11:00 UTC (rev 3469)
+++
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/Resource.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -1,146 +0,0 @@
-package org.jboss.cx.remoting.core.util;
-
-/**
- *
- */
-public final class Resource {
- private int count;
- private final Object monitor = new Object();
- private State state = State.DOWN;
-
- private enum State {
- UP,
- STOPPING,
- DOWN,
- DEAD,
- }
-
- public Resource() { /*nothing*/ }
-
- public final boolean isStateUp() {
- return state == State.UP;
- }
-
- public final boolean isStateStopping() {
- return state == State.STOPPING;
- }
-
- public final boolean isStateDown() {
- return state == State.DOWN;
- }
-
- public final boolean isStateDead() {
- return state == State.DEAD;
- }
-
- public final void doAcquire() throws IllegalStateException {
- synchronized(monitor) {
- if (state != State.UP) {
- throw new IllegalStateException("Resource may not be acquired (not
up)");
- }
- count ++;
- }
- }
-
- public final void doRelease() throws IllegalStateException {
- synchronized(monitor) {
- if (state == State.DEAD || state == State.DOWN) {
- throw new IllegalStateException("Resource may not be released
(down)");
- }
- if (count == 0) {
- throw new IllegalStateException("Resource may not be released (count
is zero)");
- }
- count --;
- monitor.notify();
- }
- }
-
- public final void doStart(final Runnable postStartAction) throws
IllegalStateException {
- synchronized(monitor) {
- if (state == State.DEAD) {
- throw new IllegalStateException("Registration has been unregistered
and may not be started again");
- }
- if (state != State.DOWN) {
- throw new IllegalStateException("Resource not stopped");
- }
- // DOWN -> UP
- state = State.UP;
- count = 0;
- }
- if (postStartAction != null) {
- postStartAction.run();
- }
- }
-
- public final void doTerminate(final Runnable postTerminateAction) throws
IllegalStateException {
- synchronized(monitor) {
- if (state == State.DEAD) {
- return;
- }
- if (state != State.DOWN) {
- throw new IllegalStateException("Resource is not down");
- }
- // DOWN -> DEAD
- state = State.DEAD;
- }
- if (postTerminateAction != null) {
- postTerminateAction.run();
- }
- }
-
- public final void doStop(Runnable shutdownOperation, final Runnable
postShutdownAction) throws IllegalStateException {
- synchronized(monitor) {
- if (state == State.DOWN) {
- return;
- }
- if (state != State.UP) {
- throw new IllegalStateException("Resource is not up");
- }
- // UP -> STOPPING
- state = State.STOPPING;
- }
- if (shutdownOperation != null) {
- shutdownOperation.run();
- }
- // state is still STOPPING since nothing changes the state from that except this
code
- boolean intr = Thread.interrupted();
- try {
- synchronized (monitor) {
- while (count > 0) {
- try {
- monitor.wait();
- } catch (InterruptedException e) {
- intr = Thread.interrupted();
- }
- }
- // STOPPING -> DOWN
- state = State.DOWN;
- }
- if (postShutdownAction != null) {
- postShutdownAction.run();
- }
- } finally {
- if (intr) {
- Thread.currentThread().interrupt();
- }
- }
- }
-
- public final void runIfUp(Runnable operation) {
- synchronized(monitor) {
- if (state != State.UP) {
- throw new IllegalStateException("Resource is not up");
- }
- operation.run();
- }
- }
-
- public final void runIfDown(Runnable operation) {
- synchronized(monitor) {
- if (state != State.DOWN) {
- throw new IllegalStateException("Configuration may not be changed in
state " + state.toString());
- }
- operation.run();
- }
- }
-}
Deleted: remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/URIUtil.java
===================================================================
---
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/URIUtil.java 2008-02-21
18:11:00 UTC (rev 3469)
+++
remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/core/util/URIUtil.java 2008-02-21
18:17:50 UTC (rev 3470)
@@ -1,24 +0,0 @@
-package org.jboss.cx.remoting.core.util;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-
-/**
- *
- */
-public final class URIUtil {
- private URIUtil() { /* empty */ }
-
- public static String getAbsolutePath(URI baseUri, String relativePath) {
- try {
- return getAbsolutePath(baseUri, new URI(null, null, relativePath, null));
- } catch (URISyntaxException e) {
- throw new IllegalArgumentException(e.getMessage());
- }
- }
-
- public static String getAbsolutePath(URI baseUri, URI relativePath) {
- return baseUri.resolve(relativePath).getPath();
- }
-
-}