KickJava   Java API By Example, From Geeks To Geeks.

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


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.IRegistry;
21 import org.apache.juddi.datatype.CategoryBag;
22 import org.apache.juddi.datatype.Description;
23 import org.apache.juddi.datatype.KeyedReference;
24 import org.apache.juddi.datatype.Name;
25 import org.apache.juddi.datatype.OverviewDoc;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.binding.AccessPoint;
28 import org.apache.juddi.datatype.binding.BindingTemplate;
29 import org.apache.juddi.datatype.binding.BindingTemplates;
30 import org.apache.juddi.datatype.binding.HostingRedirector;
31 import org.apache.juddi.datatype.binding.InstanceDetails;
32 import org.apache.juddi.datatype.binding.TModelInstanceDetails;
33 import org.apache.juddi.datatype.binding.TModelInstanceInfo;
34 import org.apache.juddi.datatype.response.ServiceDetail;
35 import org.apache.juddi.datatype.service.BusinessService;
36 import org.apache.juddi.util.xml.XMLUtils;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * "Knows about the creation and populating of ServiceDetail objects.
41  * Returns ServiceDetail."
42  *
43  * @author Steve Viens (sviens@apache.org)
44  */

45 public class ServiceDetailHandler extends AbstractHandler
46 {
47   public static final String JavaDoc TAG_NAME = "serviceDetail";
48
49   private HandlerMaker maker = null;
50
51   protected ServiceDetailHandler(HandlerMaker maker)
52   {
53     this.maker = maker;
54   }
55
56   public RegistryObject unmarshal(Element JavaDoc element)
57   {
58     ServiceDetail obj = new ServiceDetail();
59     Vector JavaDoc nodeList = null;
60     AbstractHandler handler = null;
61
62     // We could use the generic attribute value to
63
// determine which version of UDDI was used to
64
// format the request XML. - Steve
65

66     // Attributes
67
obj.setGeneric(element.getAttribute("generic"));
68     obj.setOperator(element.getAttribute("operator"));
69
70     // We can ignore the xmlns attribute since we
71
// can always determine it's value using the
72
// "generic" attribute. - Steve
73

74     String JavaDoc truncValue = element.getAttribute("truncated");
75     if (truncValue != null)
76       obj.setTruncated(truncValue.equalsIgnoreCase("true"));
77
78     // Text Node Value
79
// {none}
80

81     // Child Elements
82
nodeList = XMLUtils.getChildElementsByTagName(element,BusinessServiceHandler.TAG_NAME);
83     for (int i=0; i<nodeList.size(); i++)
84     {
85       handler = maker.lookup(BusinessServiceHandler.TAG_NAME);
86       obj.addBusinessService((BusinessService)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
87     }
88
89     return obj;
90   }
91
92   public void marshal(RegistryObject object,Element JavaDoc parent)
93   {
94     ServiceDetail detail = (ServiceDetail)object;
95     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
96     AbstractHandler handler = null;
97
98     String JavaDoc generic = detail.getGeneric();
99     if ((generic != null) && (generic.trim().length() > 0))
100     {
101       element.setAttribute("generic",generic);
102
103       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
104         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
105       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
106         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
107       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
108         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
109     }
110     else // Default to UDDI v2 values
111
{
112       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
113       element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
114     }
115
116     String JavaDoc operator = detail.getOperator();
117     if (operator != null)
118       element.setAttribute("operator",operator);
119     else
120       element.setAttribute("operator","");
121
122     boolean truncated = detail.isTruncated();
123     if (truncated)
124       element.setAttribute("truncated","true");
125
126     Vector JavaDoc vector = detail.getBusinessServiceVector();
127     if ((vector!=null) && (vector.size() > 0))
128     {
129       handler = maker.lookup(BusinessServiceHandler.TAG_NAME);
130       for (int i=0; i < vector.size(); i++)
131         handler.marshal((BusinessService)vector.elementAt(i),element);
132     }
133
134     parent.appendChild(element);
135   }
136
137
138   /***************************************************************************/
139   /***************************** TEST DRIVER *********************************/
140   /***************************************************************************/
141
142
143   public static void main(String JavaDoc args[])
144     throws Exception JavaDoc
145   {
146     HandlerMaker maker = HandlerMaker.getInstance();
147     AbstractHandler handler = maker.lookup(ServiceDetailHandler.TAG_NAME);
148
149     Element JavaDoc parent = XMLUtils.newRootElement();
150     Element JavaDoc child = null;
151
152     OverviewDoc overDoc = new OverviewDoc();
153     overDoc.setOverviewURL("http://www.sviens.com/service.html");
154     overDoc.addDescription(new Description("over-doc Descr"));
155     overDoc.addDescription(new Description("over-doc Descr Two","en"));
156
157     InstanceDetails instDetails = new InstanceDetails();
158     instDetails.addDescription(new Description("body-isa-wunder"));
159     instDetails.addDescription(new Description("sweetchild-o-mine","it"));
160     instDetails.setInstanceParms("inst-det parameters");
161     instDetails.setOverviewDoc(overDoc);
162
163     TModelInstanceInfo tModInstInfo = new TModelInstanceInfo();
164     tModInstInfo.setTModelKey("uuid:ae0f9fd4-287f-40c9-be91-df47a7c72fd9");
165     tModInstInfo.addDescription(new Description("tMod-Inst-Info"));
166     tModInstInfo.addDescription(new Description("tMod-Inst-Info too","es"));
167     tModInstInfo.setInstanceDetails(instDetails);
168
169     TModelInstanceDetails tModInstDet = new TModelInstanceDetails();
170     tModInstDet.addTModelInstanceInfo(tModInstInfo);
171
172     BindingTemplate binding = new BindingTemplate();
173     binding.setBindingKey("c9613c3c-fe55-4f34-a3da-b3167afbca4a");
174     binding.setServiceKey("997a55bc-563d-4c04-8a94-781604870d31");
175     binding.addDescription(new Description("whatever"));
176     binding.addDescription(new Description("whatever too","fr"));
177     binding.setHostingRedirector(new HostingRedirector("92658289-0bd7-443c-8948-0bb4460b44c0"));
178     binding.setAccessPoint(new AccessPoint(AccessPoint.HTTP,"http://www.sviens.com/service.wsdl"));
179     binding.setTModelInstanceDetails(tModInstDet);
180
181     BindingTemplates bindings = new BindingTemplates();
182     bindings.addBindingTemplate(binding);
183
184     CategoryBag catBag = new CategoryBag();
185     catBag.addKeyedReference(new KeyedReference("keyName","keyValue"));
186     catBag.addKeyedReference(new KeyedReference("uuid:dfddb58c-4853-4a71-865c-f84509017cc7","keyName2","keyValue2"));
187
188     BusinessService service = new BusinessService();
189     service.setServiceKey("fe8af00d-3a2c-4e05-b7ca-95a1259aad4f");
190     service.setBusinessKey("b8cc7266-9eed-4675-b621-34697f252a77");
191     service.setBindingTemplates(bindings);
192     service.setCategoryBag(catBag);
193     service.addName(new Name("serviceNm"));
194     service.addName(new Name("serviceNm2","en"));
195     service.addDescription(new Description("service whatever"));
196     service.addDescription(new Description("service whatever too","it"));
197
198     ServiceDetail detail = new ServiceDetail();
199     //detail.setGeneric("2.0");
200
//detail.setOperator("jUDDI.org");
201
detail.setTruncated(false);
202     detail.addBusinessService(service);
203     detail.addBusinessService(service);
204
205     System.out.println();
206
207     RegistryObject regObject = detail;
208     handler.marshal(regObject,parent);
209     child = (Element JavaDoc)parent.getFirstChild();
210     parent.removeChild(child);
211     XMLUtils.writeXML(child,System.out);
212
213     System.out.println();
214
215     regObject = handler.unmarshal(child);
216     handler.marshal(regObject,parent);
217     child = (Element JavaDoc)parent.getFirstChild();
218     parent.removeChild(child);
219     XMLUtils.writeXML(child,System.out);
220
221     System.out.println();
222   }
223 }
Popular Tags