001: package biz.hammurapi.web.mda;
002:
003: import java.io.Writer;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.LinkedHashMap;
007: import java.util.Map;
008:
009: /**
010: * Channel which uses StringWriter.
011: * @author Pavel
012: *
013: */
014: public abstract class AbstractChannel implements Channel {
015:
016: private Map parts = new LinkedHashMap();
017:
018: public synchronized SubChannel getChannel(String partName)
019: throws IllegalStateException {
020: Object ret = parts.get(partName);
021: if (ret == null) {
022: ret = createSubChannel();
023: parts.put(partName, ret);
024: }
025:
026: if (ret instanceof Channel) {
027: return (SubChannel) ret;
028: }
029:
030: throw new IllegalStateException("Part " + partName
031: + " is not a Channel");
032: }
033:
034: /**
035: * Creates new sub-channel.
036: * @return
037: */
038: protected abstract SubChannel createSubChannel();
039:
040: /**
041: * Creates new Writer
042: * @return
043: */
044: protected abstract Writer createWriter();
045:
046: public SubChannel getChannel(String[] path) {
047: if (path.length == 1) {
048: return getChannel(path[0]);
049: }
050:
051: String[] subPath = new String[path.length - 1];
052: System.arraycopy(path, 1, subPath, 0, subPath.length);
053:
054: return getChannel(path[0]).getChannel(subPath);
055: }
056:
057: public synchronized Writer getWriter(String partName)
058: throws IllegalStateException {
059: Object ret = parts.get(partName);
060: if (ret == null) {
061: ret = createWriter();
062: parts.put(partName, ret);
063: }
064:
065: if (ret instanceof Writer) {
066: return (Writer) ret;
067: }
068:
069: throw new IllegalStateException("Part " + partName
070: + " is not a Writer");
071: }
072:
073: public Writer getWriter(String[] path) {
074: if (path.length == 1) {
075: return getWriter(path[0]);
076: }
077:
078: String[] subPath = new String[path.length - 1];
079: System.arraycopy(path, 1, subPath, 0, subPath.length);
080:
081: return getChannel(path[0]).getWriter(subPath);
082: }
083:
084: private Map attributes = new HashMap();
085:
086: public Object getAttribute(Object name) {
087: return attributes.get(name);
088: }
089:
090: public Object removeAttribute(Object name) {
091: return attributes.remove(name);
092: }
093:
094: public void setAttribute(Object name, Object value) {
095: attributes.put(name, value);
096: }
097:
098: public Map getParts() {
099: return Collections.unmodifiableMap(parts);
100: }
101:
102: }
|