Provided encapsulated payload transforming capability to AbstractMessage.

This commit is contained in:
Mark Fisher
2007-12-03 15:19:18 +00:00
parent 168a3e6f7c
commit c2ed374e1e
4 changed files with 57 additions and 13 deletions

View File

@@ -19,6 +19,8 @@ package org.springframework.integration.message;
import java.util.concurrent.locks.ReentrantLock;
import org.springframework.integration.SystemInterruptedException;
import org.springframework.integration.transformer.ObjectTransformer;
import org.springframework.util.Assert;
/**
* Base Message class defining common properties such as id, header, and lock.
@@ -31,14 +33,16 @@ public abstract class AbstractMessage implements Message {
private MessageHeader header = new MessageHeader();
private Object payload;
private ReentrantLock lock;
protected AbstractMessage(Object id) {
if (id == null) {
throw new IllegalArgumentException("id must not be null");
}
protected AbstractMessage(Object id, Object payload) {
Assert.notNull(id, "id must not be null");
Assert.notNull(payload, "payload must not be null");
this.id = id;
this.payload = payload;
}
public Object getId() {
@@ -53,6 +57,14 @@ public abstract class AbstractMessage implements Message {
this.header = header;
}
public Object getPayload() {
return this.payload;
}
protected void setPayload(Object newPayload) {
this.payload = newPayload;
}
public void lock() {
this.lock.lock();
}
@@ -70,4 +82,14 @@ public abstract class AbstractMessage implements Message {
lock.unlock();
}
public void transformPayload(ObjectTransformer transformer) {
this.lock();
try {
this.setPayload(transformer.transform(this.getPayload()));
}
finally {
this.unlock();
}
}
}

View File

@@ -23,16 +23,8 @@ package org.springframework.integration.message;
*/
public class DocumentMessage extends AbstractMessage {
private Object payload;
public DocumentMessage(Object id, Object payload) {
super(id);
this.payload = payload;
}
public Object getPayload() {
return this.payload;
super(id, payload);
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.message;
import org.springframework.integration.transformer.ObjectTransformer;
/**
* The central interface that any Message type must implement.
*
@@ -29,6 +31,8 @@ public interface Message {
Object getPayload();
void transformPayload(ObjectTransformer transformer);
void lock();
void unlock();

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.transformer;
/**
* @author Mark Fisher
*/
public interface ObjectTransformer {
public Object transform(Object source);
}