1 package org.codehaus.xfire.service;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.xml.namespace.QName;
10
11 /***
12 * Represents the base class for containers of <code>MessagePartInfo</code> objects.
13 *
14 * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a>
15 */
16 public abstract class MessagePartContainer
17 {
18 /***
19 * The operation that this <code>MessageInfo</code> is a part of.
20 */
21 private OperationInfo operation;
22 private Map messageHeaders = new HashMap();
23 private Map messageParts = new HashMap();
24 private List messageHeaderList = new LinkedList();
25 private List messagePartList = new LinkedList();
26
27 /***
28 * Initializes a new instance of the <code>MessagePartContainer</code>.
29 *
30 * @param operation the operation.
31 */
32 protected MessagePartContainer(OperationInfo operation)
33 {
34 this.operation = operation;
35 }
36
37 /***
38 * Returns the operation of this container.
39 *
40 * @return the operation.
41 */
42 public OperationInfo getOperation()
43 {
44 return operation;
45 }
46
47 /***
48 * Adds an message part to this conainer.
49 *
50 * @param name the qualified name of the message part.
51 * @param clazz the type of the message part.
52 */
53 public MessagePartInfo addMessagePart(QName name, Class clazz)
54 {
55 if (name == null)
56 {
57 throw new IllegalArgumentException("Invalid name [" + name + "]");
58 }
59 if (messageParts.containsKey(name))
60 {
61 throw new IllegalArgumentException(
62 "An message part with name [" + name + "] already exists in this container");
63 }
64
65 MessagePartInfo part = new MessagePartInfo(name, clazz, this);
66 addMessagePart(part);
67 return part;
68 }
69
70 /***
71 * Adds an message part to this conainer.
72 *
73 * @param name the qualified name of the message part.
74 * @param clazz the type of the message part.
75 */
76 public MessageHeaderInfo addMessageHeader(QName name, Class clazz)
77 {
78 if (name == null)
79 {
80 throw new IllegalArgumentException("Invalid name [" + name + "]");
81 }
82 if (messageParts.containsKey(name))
83 {
84 throw new IllegalArgumentException(
85 "An message header with name [" + name + "] already exists in this container");
86 }
87
88 MessageHeaderInfo part = new MessageHeaderInfo(name, clazz, this);
89 addMessageHeader(part);
90 return part;
91 }
92
93 /***
94 * Adds an message part to this container.
95 *
96 * @param part the message part.
97 */
98 public void addMessagePart(MessagePartInfo part)
99 {
100 messageParts.put(part.getName(), part);
101 messagePartList.add(part);
102 }
103
104 /***
105 * Adds an message header to this container.
106 *
107 * @param header the message part.
108 */
109 public void addMessageHeader(MessageHeaderInfo header)
110 {
111 messageHeaders.put(header.getName(), header);
112 messageHeaderList.add(header);
113 }
114
115 public int getMessagePartIndex(MessagePartInfo part)
116 {
117 return messagePartList.indexOf(part);
118 }
119
120 /***
121 * Removes an message part from this container.
122 *
123 * @param name the qualified message part name.
124 */
125 public void removeMessagePart(QName name)
126 {
127 MessagePartInfo messagePart = getMessagePart(name);
128 if (messagePart != null)
129 {
130 messageParts.remove(name);
131 messagePartList.remove(messagePart);
132 }
133 }
134
135 /***
136 * Removes an message header from this container.
137 *
138 * @param name the qualified message header name.
139 */
140 public void removeMessageHeader(QName name)
141 {
142 MessageHeaderInfo messageHeader = getMessageHeader(name);
143 if (messageHeader != null)
144 {
145 messageHeaders.remove(name);
146 messageHeaderList.remove(messageHeader);
147 }
148 }
149
150 /***
151 * Returns the message part with the given name, if found.
152 *
153 * @param name the qualified name.
154 * @return the message part; or <code>null</code> if not found.
155 */
156 public MessagePartInfo getMessagePart(QName name)
157 {
158 return (MessagePartInfo) messageParts.get(name);
159 }
160
161 /***
162 * Returns the message part with the given name, if found.
163 *
164 * @param name the qualified name.
165 * @return the message part; or <code>null</code> if not found.
166 */
167 public MessageHeaderInfo getMessageHeader(QName name)
168 {
169 return (MessageHeaderInfo) messageHeaders.get(name);
170 }
171
172 /***
173 * Returns all message parts for this message.
174 *
175 * @return all message parts.
176 */
177 public List getMessageParts()
178 {
179 return Collections.unmodifiableList(messagePartList);
180 }
181
182 /***
183 * Returns all message headers for this message.
184 *
185 * @return all message parts.
186 */
187 public List getMessageHeaders()
188 {
189 return Collections.unmodifiableList(messageHeaderList);
190 }
191 }