You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2007/09/22 05:54:29 UTC
svn commit: r578367 - in
/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote:
message/ request/
Author: jdillon
Date: Fri Sep 21 20:54:29 2007
New Revision: 578367
URL: http://svn.apache.org/viewvc?rev=578367&view=rev
Log:
Add marker interface for message id's
Adding long and uuid impls, default to long for now
Modified:
geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/Message.java
geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageSupport.java
geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/Request.java
geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestManager.java
geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestResponseFilter.java
Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/Message.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/Message.java?rev=578367&r1=578366&r2=578367&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/Message.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/Message.java Fri Sep 21 20:54:29 2007
@@ -20,6 +20,7 @@
package org.apache.geronimo.gshell.remote.message;
import java.io.IOException;
+import java.io.Serializable;
import org.apache.geronimo.gshell.remote.codec.Externalizable;
import org.apache.mina.common.IoSession;
@@ -33,11 +34,11 @@
public interface Message
extends Externalizable
{
- Object getId();
+ ID getId();
- Object getCorrelationId();
+ ID getCorrelationId();
- void setCorrelationId(Object id);
+ void setCorrelationId(ID id);
MessageType getType() throws IOException;
@@ -56,4 +57,23 @@
boolean isFrozen();
WriteFuture reply(Message msg);
+
+ //
+ // ID
+ //
+
+ interface ID
+ extends Serializable
+ {
+ // Marker
+ }
+
+ //
+ // ID Generator
+ //
+
+ interface IDGenerator
+ {
+ ID generate();
+ }
}
Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageSupport.java?rev=578367&r1=578366&r2=578367&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageSupport.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageSupport.java Fri Sep 21 20:54:29 2007
@@ -20,6 +20,7 @@
package org.apache.geronimo.gshell.remote.message;
import java.io.IOException;
+import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.geronimo.gshell.common.tostring.ReflectionToStringBuilder;
@@ -37,15 +38,15 @@
public abstract class MessageSupport
implements Message
{
- private static final AtomicLong ID_COUNTER = new AtomicLong(0);
+ private static final IDGenerator ID_GENERATOR = new LongIDGenerator();
private static final AtomicLong SEQUENCE_COUNTER = new AtomicLong(0);
private MessageType type;
- private Object id;
+ private ID id;
- private Object correlationId;
+ private ID correlationId;
private Long sequence;
@@ -60,8 +61,8 @@
this.type = type;
- this.id = ID_COUNTER.getAndIncrement();
-
+ this.id = ID_GENERATOR.generate();
+
this.timestamp = System.currentTimeMillis();
}
@@ -73,15 +74,15 @@
return type;
}
- public Object getId() {
+ public ID getId() {
return id;
}
- public Object getCorrelationId() {
+ public ID getCorrelationId() {
return correlationId;
}
- public void setCorrelationId(final Object id) {
+ public void setCorrelationId(final ID id) {
ensureWritable();
assert id != null;
@@ -151,9 +152,9 @@
public void readExternal(final ByteBuffer in) throws Exception {
assert in != null;
- id = MarshallingUtil.readObject(in);
+ id = (ID) MarshallingUtil.readObject(in);
- correlationId = MarshallingUtil.readObject(in);
+ correlationId = (ID) MarshallingUtil.readObject(in);
timestamp = in.getLong();
@@ -172,5 +173,103 @@
sequence = SEQUENCE_COUNTER.getAndIncrement();
out.putLong(sequence);
+ }
+
+ //
+ // LongID Generator
+ //
+
+ private static class LongIDGenerator
+ implements IDGenerator
+ {
+ private static final AtomicLong ID_COUNTER = new AtomicLong(0);
+
+ public ID generate() {
+ return new LongID(ID_COUNTER.getAndIncrement());
+ }
+ }
+
+ //
+ // Long ID
+ //
+
+ private static class LongID
+ implements ID
+ {
+ private final Long value;
+
+ public LongID(final long value) {
+ this.value = value;
+ }
+
+ public int hashCode() {
+ return value.hashCode();
+ }
+
+ public boolean equals(final Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ else if (obj == null) {
+ return false;
+ }
+ else if (!(obj instanceof LongID)) {
+ return false;
+ }
+
+ return value.equals(((LongID)obj).value);
+ }
+
+ public String toString() {
+ return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+ }
+ }
+
+ //
+ // UUID Generator
+ //
+
+ private static class UUIDGenerator
+ implements IDGenerator
+ {
+ public ID generate() {
+ return new UUIDAdapter(UUID.randomUUID());
+ }
+ }
+
+ //
+ // UUID Adapter
+ //
+
+ private static class UUIDAdapter
+ implements ID
+ {
+ private final UUID value;
+
+ public UUIDAdapter(final UUID value) {
+ this.value = value;
+ }
+
+ public int hashCode() {
+ return value.hashCode();
+ }
+
+ public boolean equals(final Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ else if (obj == null) {
+ return false;
+ }
+ else if (!(obj instanceof UUIDAdapter)) {
+ return false;
+ }
+
+ return value.equals(((UUIDAdapter)obj).value);
+ }
+
+ public String toString() {
+ return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+ }
}
}
Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/Request.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/Request.java?rev=578367&r1=578366&r2=578367&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/Request.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/Request.java Fri Sep 21 20:54:29 2007
@@ -90,7 +90,7 @@
return message;
}
- public Object getId() {
+ public Message.ID getId() {
return getMessage().getId();
}
Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestManager.java?rev=578367&r1=578366&r2=578367&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestManager.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestManager.java Fri Sep 21 20:54:29 2007
@@ -26,6 +26,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
+import org.apache.geronimo.gshell.remote.message.Message;
import org.apache.geronimo.gshell.remote.util.SessionAttributeBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,7 +44,7 @@
private transient final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() + 1);
- private final Map<Object,Request> requests = Collections.synchronizedMap(new HashMap<Object, Request>());
+ private final Map<Message.ID,Request> requests = Collections.synchronizedMap(new HashMap<Message.ID, Request>());
private final Map<Request,TimeoutTask> timeouts = Collections.synchronizedMap(new HashMap<Request,TimeoutTask>());
@@ -51,20 +52,9 @@
// TODO: Lock on Request.getMutex(), and/or change the mutex to a read/write lock?
//
- /**
- * Helper to make sure that when calling methods that take an Object for the request ID that the requst object is not given by accident.
- */
- private void ensureValidRequestId(final Object obj) {
- if (obj instanceof Request) {
- throw new IllegalArgumentException("Expecting a request ID, not a request");
- }
- }
-
- public boolean contains(final Object id) {
+ public boolean contains(final Message.ID id) {
assert id != null;
- ensureValidRequestId(id);
-
return requests.containsKey(id);
}
@@ -81,7 +71,7 @@
throw new DuplicateRequestException(request);
}
- Object id = request.getId();
+ Message.ID id = request.getId();
if (log.isTraceEnabled()) {
log.trace("Adding: {}", request);
@@ -93,19 +83,15 @@
requests.put(id, request);
}
- public Request get(final Object id) {
+ public Request get(final Message.ID id) {
assert id != null;
- ensureValidRequestId(id);
-
return requests.get(id);
}
- public Request remove(final Object id) {
+ public Request remove(final Message.ID id) {
assert id != null;
- ensureValidRequestId(id);
-
log.debug("Removing: {}", id);
return requests.remove(id);
@@ -142,7 +128,7 @@
throw new DuplicateRequestException(request);
}
- Object id = request.getId();
+ Message.ID id = request.getId();
if (request != get(id)) {
throw new InvalidRequestMappingException(id);
@@ -167,7 +153,7 @@
public void cancel(final Request request) {
assert request != null;
- Object id = request.getId();
+ Message.ID id = request.getId();
TimeoutTask task = timeouts.remove(request);
@@ -196,7 +182,7 @@
private void timeout(final Request request) {
assert request != null;
- Object id = request.getId();
+ Message.ID id = request.getId();
if (log.isTraceEnabled()) {
log.trace("Triggering: {}", request);
Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestResponseFilter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestResponseFilter.java?rev=578367&r1=578366&r2=578367&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestResponseFilter.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/request/RequestResponseFilter.java Fri Sep 21 20:54:29 2007
@@ -82,7 +82,7 @@
public void messageReceived(final NextFilter nextFilter, final IoSession session, final Object message) throws Exception {
Message msg = null;
- Object id = null;
+ Message.ID id = null;
if (message instanceof Message) {
msg = (Message)message;