Factored out default id generation to AbstractMessageMapper.

This commit is contained in:
Mark Fisher
2007-12-28 02:03:24 +00:00
parent 49b6361095
commit cafe82e945
2 changed files with 45 additions and 29 deletions

View File

@@ -0,0 +1,43 @@
/*
* 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.message;
import org.springframework.integration.util.RandomGuidUidGenerator;
import org.springframework.integration.util.UidGenerator;
import org.springframework.util.Assert;
/**
* Base class that provides the default {@link UidGenerator} as well as a setter
* for providing a custom id generator implementation.
*
* @author Mark Fisher
*/
public abstract class AbstractMessageMapper<M, O> implements MessageMapper<M, O> {
private UidGenerator uidGenerator = new RandomGuidUidGenerator();
public void setUidGenerator(UidGenerator uidGenerator) {
Assert.notNull(uidGenerator, "'uidGenerator' must not be null");
this.uidGenerator = uidGenerator;
}
public UidGenerator getUidGenerator() {
return this.uidGenerator;
}
}

View File

@@ -16,40 +16,13 @@
package org.springframework.integration.message;
import org.springframework.integration.util.RandomGuidUidGenerator;
import org.springframework.integration.util.UidGenerator;
import org.springframework.util.Assert;
/**
* A {@link MessageMapper} implementation that simply wraps and unwraps a
* payload object in a {@link DocumentMessage}.
*
* @author Mark Fisher
*/
public class SimplePayloadMessageMapper<T> implements MessageMapper<T,T> {
private UidGenerator uidGenerator;
/**
* Create a mapper with the provided id generation strategy.
*
* @param uidGenerator the generator to use for message ids
*/
public SimplePayloadMessageMapper(UidGenerator uidGenerator) {
Assert.notNull(uidGenerator, "uidGenerator must not be null");
this.uidGenerator = uidGenerator;
}
/**
* Create a mapper with the default id generation strategy.
*
* @see RandomGuidUidGenerator
*/
public SimplePayloadMessageMapper() {
this.uidGenerator = new RandomGuidUidGenerator();
}
public class SimplePayloadMessageMapper<T> extends AbstractMessageMapper<T,T> {
/**
* Return the payload of the given Message.
@@ -62,7 +35,7 @@ public class SimplePayloadMessageMapper<T> implements MessageMapper<T,T> {
* Return a {@link DocumentMessage} with the given object as its payload.
*/
public Message<T> toMessage(T source) {
return new GenericMessage<T>(uidGenerator.generateUid(), source);
return new GenericMessage<T>(this.getUidGenerator().generateUid(), source);
}
}