diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/channel/MapBasedChannelResolver.java b/org.springframework.integration/src/main/java/org/springframework/integration/channel/MapBasedChannelResolver.java new file mode 100644 index 0000000000..9251a45edb --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/channel/MapBasedChannelResolver.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2008 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.channel; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.util.Assert; + +/** + * {@link ChannelResolver} implementation that resolves {@link MessageChannel} + * instances by matching the channel name against keys within a Map. + * + * @author Mark Fisher + */ +public class MapBasedChannelResolver implements ChannelResolver { + + private volatile Map channelMap = new HashMap(); + + + public void setChannelMap(Map channelMap) { + Assert.notNull(channelMap, "channelMap must not be null"); + this.channelMap = channelMap; + } + + public MessageChannel resolveChannelName(String channelName) { + return this.channelMap.get(channelName); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/channel/MapBasedChannelResolverTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/channel/MapBasedChannelResolverTests.java new file mode 100644 index 0000000000..2bb8ce2a93 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/channel/MapBasedChannelResolverTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2002-2008 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.channel; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +/** + * @author Mark Fisher + */ +public class MapBasedChannelResolverTests { + + @Test + public void mapContainsChannel() { + MessageChannel testChannel = new QueueChannel(); + Map channelMap = new HashMap(); + channelMap.put("testChannel", testChannel); + MapBasedChannelResolver resolver = new MapBasedChannelResolver(); + resolver.setChannelMap(channelMap); + MessageChannel result = resolver.resolveChannelName("testChannel"); + assertNotNull(result); + assertEquals(testChannel, result); + } + + @Test + public void mapDoesNotContainChannel() { + MessageChannel testChannel = new QueueChannel(); + Map channelMap = new HashMap(); + channelMap.put("testChannel", testChannel); + MapBasedChannelResolver resolver = new MapBasedChannelResolver(); + resolver.setChannelMap(channelMap); + MessageChannel result = resolver.resolveChannelName("noSuchChannel"); + assertNull(result); + } + + @Test + public void emptyMap() { + Map channelMap = new HashMap(); + MapBasedChannelResolver resolver = new MapBasedChannelResolver(); + resolver.setChannelMap(channelMap); + MessageChannel result = resolver.resolveChannelName("testChannel"); + assertNull(result); + } + + @Test(expected = IllegalArgumentException.class) + public void nullMapRejected() { + MapBasedChannelResolver resolver = new MapBasedChannelResolver(); + resolver.setChannelMap(null); + } + +}