KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > RegistryExceptionHandler


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.handler;
17
18 import java.util.Vector JavaDoc;
19
20 import org.apache.juddi.datatype.RegistryObject;
21 import org.apache.juddi.datatype.response.DispositionReport;
22 import org.apache.juddi.error.RegistryException;
23 import org.apache.juddi.util.xml.XMLUtils;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * Knows about the creation and populating of RegistryException
29  * objects.
30  *
31  * @author Steve Viens (sviens@apache.org)
32  */

33 public class RegistryExceptionHandler extends AbstractHandler
34 {
35   public static final String JavaDoc TAG_NAME = "fault";
36
37   private HandlerMaker maker = null;
38
39   protected RegistryExceptionHandler(HandlerMaker maker)
40   {
41     this.maker = maker;
42   }
43
44   public RegistryObject unmarshal(Element JavaDoc element)
45   {
46     Vector JavaDoc nodeList = null;
47     AbstractHandler handler = null;
48
49     // Attributes
50
// {none}
51

52     // Text Node Value
53
// {none}
54

55     // Child Elements
56
String JavaDoc fCode = null;
57     nodeList = XMLUtils.getChildElementsByTagName(element,"faultcode");
58     if (nodeList.size() > 0)
59       fCode = XMLUtils.getText((Element JavaDoc)nodeList.elementAt(0));
60
61     String JavaDoc fString = null;
62     nodeList = XMLUtils.getChildElementsByTagName(element,"faultstring");
63     if (nodeList.size() > 0)
64       fString = XMLUtils.getText((Element JavaDoc)nodeList.elementAt(0));
65
66     String JavaDoc fActor = null;
67     nodeList = XMLUtils.getChildElementsByTagName(element,"faultactor");
68     if (nodeList.size() > 0)
69       fActor = XMLUtils.getText((Element JavaDoc)nodeList.elementAt(0));
70
71     DispositionReport dispRpt = null;
72     nodeList = XMLUtils.getChildElementsByTagName(element,"detail");
73     if (nodeList.size() > 0)
74     {
75       nodeList = XMLUtils.getChildElementsByTagName((Element JavaDoc)nodeList.elementAt(0),DispositionReportHandler.TAG_NAME);
76       if (nodeList.size() > 0)
77       {
78         handler = maker.lookup(DispositionReportHandler.TAG_NAME);
79         dispRpt = ((DispositionReport)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
80       }
81     }
82
83     // Create RegistryException instance and return
84
RegistryException obj = new RegistryException(fCode,fString,fActor,dispRpt);
85     
86     return obj;
87   }
88
89   public void marshal(RegistryObject object,Element JavaDoc parent)
90   {
91     RegistryException regEx = (RegistryException)object;
92     Document JavaDoc document = parent.getOwnerDocument();
93     Element JavaDoc fault = document.createElementNS(null,TAG_NAME);
94
95     String JavaDoc fCode = regEx.getFaultCode();
96     if (fCode != null)
97     {
98       Element JavaDoc fCodeElement = document.createElement("faultcode");
99       fCodeElement.appendChild(document.createTextNode(fCode));
100       fault.appendChild(fCodeElement);
101     }
102
103     String JavaDoc fString = regEx.getFaultString();
104     if (fString == null)
105       fString = "";
106
107     Element JavaDoc fStringElement = document.createElement("faultstring");
108     fStringElement.appendChild(document.createTextNode(fString));
109     fault.appendChild(fStringElement);
110
111     String JavaDoc fActor = regEx.getFaultActor();
112     if (fActor != null)
113     {
114       Element JavaDoc fActorElement = document.createElement("faultactor");
115       fActorElement.appendChild(document.createTextNode(fActor));
116       fault.appendChild(fActorElement);
117     }
118
119     // check for a DispositionReport in the exception and if one exists,
120
// grab it, marshal it into xml and stuff it into a SOAP fault
121
// detail element.
122

123     DispositionReport dispRpt = regEx.getDispositionReport();
124     if (dispRpt != null)
125     {
126       Element JavaDoc fDetailElement = document.createElement("detail");
127       IHandler handler = maker.lookup(DispositionReportHandler.TAG_NAME);
128       handler.marshal(dispRpt,fDetailElement);
129       fault.appendChild(fDetailElement);
130     }
131
132     parent.appendChild(fault);
133   }
134
135
136   /***************************************************************************/
137   /***************************** TEST DRIVER *********************************/
138   /***************************************************************************/
139
140
141   public static void main(String JavaDoc args[])
142     throws Exception JavaDoc
143   {
144     HandlerMaker maker = HandlerMaker.getInstance();
145     AbstractHandler handler = maker.lookup(RegistryExceptionHandler.TAG_NAME);
146     Element JavaDoc parent = XMLUtils.newRootElement();
147     Element JavaDoc child = null;
148
149     RegistryException regex =
150         new org.apache.juddi.error.AuthTokenRequiredException("Test Exception");
151
152     System.out.println();
153
154     RegistryObject regObject = regex;
155     handler.marshal(regObject,parent);
156     child = (Element JavaDoc)parent.getFirstChild();
157     parent.removeChild(child);
158     XMLUtils.writeXML(child,System.out);
159
160     System.out.println();
161
162     regObject = handler.unmarshal(child);
163     handler.marshal(regObject,parent);
164     child = (Element JavaDoc)parent.getFirstChild();
165     parent.removeChild(child);
166     XMLUtils.writeXML(child,System.out);
167
168     System.out.println();
169   }
170 }
Popular Tags