KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > base > WSIFDefaultMessage


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.base;
59
60 import java.io.ByteArrayInputStream JavaDoc;
61 import java.io.ByteArrayOutputStream JavaDoc;
62 import java.io.ObjectOutputStream JavaDoc;
63 import java.io.Serializable JavaDoc;
64 import java.lang.reflect.InvocationTargetException JavaDoc;
65 import java.lang.reflect.Method JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.Iterator JavaDoc;
68 import java.util.Map JavaDoc;
69 import javax.wsdl.Message;
70
71 import org.apache.wsif.WSIFConstants;
72 import org.apache.wsif.WSIFException;
73 import org.apache.wsif.WSIFMessage;
74 import org.apache.wsif.logging.Trc;
75
76 /**
77  * A DefaultWSIFMessage is a default implementation of
78  * WSIFMessage holding a collection of WSIFParts corresponding
79  * to the parts of a message as defined in WSDL.
80  *
81  * @author Paul Fremantle
82  * @author Alekander Slominski
83  * @author Matthew J. Duftler
84  * @author Sanjiva Weerawarana
85  * @author Nirmal Mukhi
86  * @author Owen Burroughs <owenb@apache.org>
87  * @author Ant Elder <antelder@apache.org>
88  * @author Jeremy Hughes <hughesj@apache.org>
89  * @author Mark Whitlock <whitlock@apache.org>
90
91  */

92 public class WSIFDefaultMessage implements WSIFMessage {
93     
94     private static final long serialVersionUID = 1L;
95     
96     protected Map JavaDoc parts;
97     protected String JavaDoc name;
98     protected String JavaDoc style;
99     protected Message msgDefinition;
100
101     public WSIFDefaultMessage() {
102         Trc.entry(this);
103         Trc.exit();
104     }
105
106     /**
107      * Set the name of this message.
108      */

109     public String JavaDoc getName() {
110         Trc.entry(this);
111         Trc.exit(name);
112         return name;
113     }
114
115     /**
116      * Get the name of this message.
117      */

118     public void setName(String JavaDoc name) {
119         Trc.entry(this, name);
120         this.name = name;
121         Trc.exit();
122     }
123
124     /**
125      * Return the representation style for all parts in this message
126      */

127     public String JavaDoc getRepresentationStyle() {
128         Trc.entry(this);
129         Trc.exit(style);
130         return style;
131     }
132
133     /**
134      * Set the representation style for all parts in this message
135      */

136     public void setRepresentationStyle(String JavaDoc rStyle) {
137         Trc.entry(this, rStyle);
138         style = rStyle;
139         Trc.exit();
140     }
141
142     /**
143      * Return an iterator of the parts in the message.
144      * Supercedes void getParts(Map)
145      */

146     public Iterator JavaDoc getParts() {
147         Trc.entry(this);
148         if (parts == null) {
149             parts = new HashMap JavaDoc();
150         }
151         Iterator JavaDoc it = parts.values().iterator();
152         Trc.exit(it);
153         return it;
154     }
155
156     public void setParts(Map JavaDoc sourceParts) {
157         Trc.entry(this, sourceParts);
158         if (parts == null) {
159             parts = new HashMap JavaDoc();
160         } else {
161             parts.clear();
162         }
163
164         for (Iterator JavaDoc names = sourceParts.keySet().iterator(); names.hasNext();) {
165             String JavaDoc name = (String JavaDoc) names.next();
166             Object JavaDoc part = sourceParts.get(name);
167             parts.put(name, part);
168         }
169         Trc.exit();
170     }
171
172     public Message getMessageDefinition() {
173         Trc.entry(this);
174         Trc.exit(msgDefinition);
175         return msgDefinition;
176     }
177         
178     public void setMessageDefinition(Message msgDef) {
179         Trc.entry(this, msgDefinition);
180         msgDefinition = msgDef;
181         Trc.exit();
182     }
183
184     public void setObjectPart(String JavaDoc name, Object JavaDoc part) throws WSIFException {
185         Trc.entry(this, name, part);
186         if (parts == null) {
187             parts = new HashMap JavaDoc();
188         }
189         parts.put(name, part);
190         Trc.exit();
191     }
192
193     public Object JavaDoc getObjectPart(String JavaDoc name) throws WSIFException {
194         Trc.entry(this, name);
195         if (parts == null) {
196             handleNoPartsException(name, "Object");
197             Trc.exit(null);
198             return null;
199         }
200         if (parts.get(name) == null) {
201             if (!parts.keySet().contains(name)) {
202                 handlePartNotFoundException(name);
203             }
204             Trc.exit(null);
205             return null;
206         }
207         Object JavaDoc o = parts.get(name);
208         Trc.exit(o);
209         return o;
210     }
211
212     public Object JavaDoc getObjectPart(String JavaDoc name, Class JavaDoc sourceClass)
213         throws WSIFException {
214         Trc.entry(this, name, sourceClass);
215         if (parts == null) {
216             handleNoPartsException(name, "Object");
217             Trc.exit(null);
218             return null;
219         }
220         Object JavaDoc part = parts.get(name);
221         if (part == null) {
222             if (!parts.keySet().contains(name)) {
223                 handlePartNotFoundException(name);
224             }
225             Trc.exit(null);
226             return null;
227         } else {
228             if (part.getClass().getName().equals(sourceClass.getName())) {
229                 Trc.exit(part);
230                 return part;
231             } else {
232                 handleSourcedPartNotFoundException(name, sourceClass);
233                 Trc.exit(null);
234                 return null;
235             }
236         }
237     }
238
239     public byte getBytePart(String JavaDoc name) throws WSIFException {
240         Trc.entry(this, name);
241         if (parts == null)
242             handleNoPartsException(name, "byte");
243         try {
244             byte b = ((Byte JavaDoc) parts.get(name)).byteValue();
245             Trc.exit(b);
246             return b;
247         } catch (NullPointerException JavaDoc ne) {
248             Trc.exception(ne);
249             handlePartNotFoundException(name);
250             Trc.exit(0);
251             return 0;
252         } catch (ClassCastException JavaDoc ce) {
253             Trc.exception(ce);
254             handlePartCastException(name, parts.get(name).getClass().getName(), "Byte");
255             Trc.exit(0);
256             return 0;
257         }
258     }
259
260     public void setBytePart(String JavaDoc name, byte part) {
261         Trc.entry(this, name, new Byte JavaDoc(part));
262         if (parts == null) {
263             parts = new HashMap JavaDoc();
264         }
265         parts.put(name, new Byte JavaDoc(part));
266         Trc.exit();
267     }
268
269     public char getCharPart(String JavaDoc name) throws WSIFException {
270         Trc.entry(this, name);
271         if (parts == null)
272             handleNoPartsException(name, "char");
273         try {
274             char c = ((Character JavaDoc) parts.get(name)).charValue();
275             Trc.exit(c);
276             return c;
277         } catch (NullPointerException JavaDoc ne) {
278             Trc.exception(ne);
279             handlePartNotFoundException(name);
280             Trc.exit(0);
281             return 0;
282         } catch (ClassCastException JavaDoc ce) {
283             Trc.exception(ce);
284             handlePartCastException(
285                 name,
286                 parts.get(name).getClass().getName(),
287                 "Character");
288             Trc.exit(0);
289             return 0;
290         }
291     }
292
293     public void setCharPart(String JavaDoc name, char part) {
294         Trc.entry(this, name, new Character JavaDoc(part));
295         if (parts == null) {
296             parts = new HashMap JavaDoc();
297         }
298         parts.put(name, new Character JavaDoc(part));
299         Trc.exit();
300     }
301
302     public int getIntPart(String JavaDoc name) throws WSIFException {
303         Trc.entry(this, name);
304         if (parts == null)
305             handleNoPartsException(name, "int");
306         try {
307             int i = ((Integer JavaDoc) parts.get(name)).intValue();
308             Trc.exit(i);
309             return i;
310         } catch (NullPointerException JavaDoc ne) {
311             Trc.exception(ne);
312             handlePartNotFoundException(name);
313             Trc.exit(0);
314             return 0;
315         } catch (ClassCastException JavaDoc ce) {
316             Trc.exception(ce);
317             handlePartCastException(name, parts.get(name).getClass().getName(), "Integer");
318             Trc.exit(0);
319             return 0;
320         }
321     }
322
323     public void setIntPart(String JavaDoc name, int part) {
324         Trc.entry(this, name, new Integer JavaDoc(part));
325         if (parts == null) {
326             parts = new HashMap JavaDoc();
327         }
328         parts.put(name, new Integer JavaDoc(part));
329         Trc.exit();
330     }
331
332     public long getLongPart(String JavaDoc name) throws WSIFException {
333         Trc.entry(this, name);
334         if (parts == null)
335             handleNoPartsException(name, "long");
336         try {
337             long l = ((Long JavaDoc) parts.get(name)).longValue();
338             Trc.exit(new Long JavaDoc(l));
339             return l;
340         } catch (NullPointerException JavaDoc ne) {
341             Trc.exception(ne);
342             handlePartNotFoundException(name);
343             Trc.exit(0);
344             return 0;
345         } catch (ClassCastException JavaDoc ce) {
346             Trc.exception(ce);
347             handlePartCastException(name, parts.get(name).getClass().getName(), "Long");
348             Trc.exit(0);
349             return 0;
350         }
351     }
352
353     public void setLongPart(String JavaDoc name, long part) {
354         Trc.entry(this, name, new Long JavaDoc(part));
355         if (parts == null) {
356             parts = new HashMap JavaDoc();
357         }
358         parts.put(name, new Long JavaDoc(part));
359         Trc.exit();
360     }
361
362     public short getShortPart(String JavaDoc name) throws WSIFException {
363         Trc.entry(this, name);
364         if (parts == null)
365             handleNoPartsException(name, "short");
366         try {
367             short s = ((Short JavaDoc) parts.get(name)).shortValue();
368             Trc.exit(s);
369             return s;
370         } catch (NullPointerException JavaDoc ne) {
371             Trc.exception(ne);
372             handlePartNotFoundException(name);
373             Trc.exit(0);
374             return 0;
375         } catch (ClassCastException JavaDoc ce) {
376             Trc.exception(ce);
377             handlePartCastException(name, parts.get(name).getClass().getName(), "Short");
378             Trc.exit(0);
379             return 0;
380         }
381     }
382
383     public void setShortPart(String JavaDoc name, short part) {
384         Trc.entry(this, name, new Short JavaDoc(part));
385         if (parts == null) {
386             parts = new HashMap JavaDoc();
387         }
388         parts.put(name, new Short JavaDoc(part));
389         Trc.exit();
390     }
391
392     public float getFloatPart(String JavaDoc name) throws WSIFException {
393         Trc.entry(this, name);
394         if (parts == null)
395             handleNoPartsException(name, "float");
396         try {
397             float f = ((Float JavaDoc) parts.get(name)).floatValue();
398             Trc.exit(new Float JavaDoc(f));
399             return f;
400         } catch (NullPointerException JavaDoc ne) {
401             Trc.exception(ne);
402             handlePartNotFoundException(name);
403             Trc.exit(0);
404             return 0;
405         } catch (ClassCastException JavaDoc ce) {
406             Trc.exception(ce);
407             handlePartCastException(name, parts.get(name).getClass().getName(), "Float");
408             Trc.exit(0);
409             return 0;
410         }
411     }
412
413     public void setFloatPart(String JavaDoc name, float part) {
414         Trc.entry(this, name, new Float JavaDoc(part));
415         if (parts == null) {
416             parts = new HashMap JavaDoc();
417         }
418         parts.put(name, new Float JavaDoc(part));
419         Trc.exit();
420     }
421
422     public double getDoublePart(String JavaDoc name) throws WSIFException {
423         Trc.entry(this, name);
424         if (parts == null)
425             handleNoPartsException(name, "double");
426         try {
427             double d = ((Double JavaDoc) parts.get(name)).doubleValue();
428             Trc.exit(new Double JavaDoc(d));
429             return d;
430         } catch (NullPointerException JavaDoc ne) {
431             Trc.exception(ne);
432             handlePartNotFoundException(name);
433             Trc.exit(0);
434             return 0;
435         } catch (ClassCastException JavaDoc ce) {
436             Trc.exception(ce);
437             handlePartCastException(name, parts.get(name).getClass().getName(), "Double");
438             Trc.exit(0);
439             return 0;
440         }
441     }
442
443     public void setDoublePart(String JavaDoc name, double part) {
444         Trc.entry(this, name, new Double JavaDoc(part));
445         if (parts == null) {
446             parts = new HashMap JavaDoc();
447         }
448         parts.put(name, new Double JavaDoc(part));
449         Trc.exit();
450     }
451
452     public boolean getBooleanPart(String JavaDoc name) throws WSIFException {
453         Trc.entry(this, name);
454         if (parts == null)
455             handleNoPartsException(name, "boolean");
456         try {
457             boolean b = ((Boolean JavaDoc) parts.get(name)).booleanValue();
458             Trc.exit(b);
459             return b;
460         } catch (NullPointerException JavaDoc ne) {
461             Trc.exception(ne);
462             handlePartNotFoundException(name);
463             Trc.exit(false);
464             return false;
465         } catch (ClassCastException JavaDoc ce) {
466             Trc.exception(ce);
467             handlePartCastException(name, parts.get(name).getClass().getName(), "Boolean");
468             Trc.exit(false);
469             return false;
470         }
471     }
472
473     public void setBooleanPart(String JavaDoc name, boolean part) {
474         Trc.entry(this, name, new Boolean JavaDoc(part));
475         if (parts == null) {
476             parts = new HashMap JavaDoc();
477         }
478         parts.put(name, new Boolean JavaDoc(part));
479         Trc.exit();
480     }
481
482     public Iterator JavaDoc getPartNames() {
483         Trc.entry(this);
484         if (parts == null) {
485             parts = new HashMap JavaDoc();
486         }
487         Iterator JavaDoc it = parts.keySet().iterator();
488         Trc.exit(it);
489         return it;
490     }
491
492     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
493         Trc.entry(this);
494         WSIFDefaultMessage dm = new WSIFDefaultMessage();
495         dm.setName(this.name);
496         dm.setRepresentationStyle(this.style);
497         dm.setMessageDefinition(this.msgDefinition);
498         Iterator JavaDoc it = getPartNames();
499         while (it.hasNext()) {
500             String JavaDoc pn = (String JavaDoc) it.next();
501             Object JavaDoc po = parts.get(pn);
502             if (po == null) {
503                 try {
504                     dm.setObjectPart(pn, null);
505                 } catch (Exception JavaDoc e) {
506                     Trc.exception(e);
507                     throw new CloneNotSupportedException JavaDoc(
508                         "Exception thrown whilst cloning part "
509                             + pn
510                             + ". Message is "
511                         + e.getMessage());
512                 }
513             } else if (po instanceof Cloneable JavaDoc) {
514                 Class JavaDoc cls = po.getClass();
515                 try {
516                     Method JavaDoc clone = cls.getMethod("clone", null);
517                     Object JavaDoc poc = clone.invoke(po, null);
518                     dm.setObjectPart(pn, poc);
519                 } catch (InvocationTargetException JavaDoc e) {
520                     Trc.exception(e);
521                     throw new CloneNotSupportedException JavaDoc(
522                         "Exception thrown whilst cloning part "
523                             + pn
524                             + ". Message is "
525                             + e.getTargetException().getMessage());
526                 } catch (Exception JavaDoc e) {
527                     Trc.exception(e);
528                     throw new CloneNotSupportedException JavaDoc(
529                         "Exception thrown whilst cloning part "
530                             + pn
531                             + ". Message is "
532                             + e.getMessage());
533                 }
534             } else if (po instanceof Serializable JavaDoc) {
535                 try {
536                     ByteArrayOutputStream JavaDoc b = new ByteArrayOutputStream JavaDoc();
537                     ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(b);
538                     out.writeObject(po);
539                     out.flush();
540                     out.close();
541                     byte[] data = b.toByteArray();
542                     WSIFObjectInputStream in = new WSIFObjectInputStream(new ByteArrayInputStream JavaDoc(data));
543                     Object JavaDoc poc = in.readObject();
544                     in.close();
545                     dm.setObjectPart(pn, poc);
546                 } catch (Exception JavaDoc e) {
547                     Trc.exception(e);
548                     throw new CloneNotSupportedException JavaDoc(
549                         "Exception thrown whilst cloning part "
550                             + pn
551                             + " by serialization. Message is "
552                             + e.getMessage());
553                 }
554             } else {
555                 throw new CloneNotSupportedException JavaDoc("Part " + pn + " cannot be cloned");
556             }
557         }
558         if (Trc.ON)
559             Trc.exit(dm.deep());
560         return dm;
561     }
562
563     private void handleNoPartsException(String JavaDoc part, String JavaDoc type)
564         throws WSIFException {
565         throw new WSIFException(
566             "Cannot get " + type + " part '" + part + "'. No parts are set on the message");
567     }
568
569     private void handlePartNotFoundException(String JavaDoc part) throws WSIFException {
570         throw new WSIFException(
571             "Cannot get part '" + part + "'. Part was not found in message");
572     }
573
574     private void handleSourcedPartNotFoundException(String JavaDoc part, Class JavaDoc sclass)
575         throws WSIFException {
576         throw new WSIFException(
577             "Cannot get part. Part '"
578                 + part
579                 + "' with source '"
580                 + sclass
581                 + "' was not found in message");
582     }
583
584     private void handlePartCastException(String JavaDoc part, String JavaDoc act, String JavaDoc exp)
585         throws WSIFException {
586         throw new WSIFException(
587             "Cannot get part '"
588                 + part
589                 + "'. Cannot convert "
590                 + "from "
591                 + act
592                 + " to "
593                 + exp);
594     }
595
596     public String JavaDoc toString() {
597         return deep();
598     }
599     
600     public String JavaDoc deep() {
601         String JavaDoc buff = "";
602         try {
603             buff = new String JavaDoc(super.toString());
604             buff += " name:" + name;
605             if (parts==null)
606                 buff += " parts:null";
607             else
608                 buff += Trc.brief(" parts",parts.values());
609         } catch (Exception JavaDoc e) {
610             Trc.exceptionInTrace(e);
611         }
612         return buff;
613     }
614 }
Popular Tags