KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > proxy > RegistryProxy


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.proxy;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.apache.juddi.AbstractRegistry;
25 import org.apache.juddi.IRegistry;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.request.Admin;
28 import org.apache.juddi.datatype.request.AuthInfo;
29 import org.apache.juddi.datatype.request.Inquiry;
30 import org.apache.juddi.datatype.request.Publish;
31 import org.apache.juddi.datatype.request.SecurityPolicy;
32 import org.apache.juddi.datatype.response.AuthToken;
33 import org.apache.juddi.error.RegistryException;
34 import org.apache.juddi.handler.HandlerMaker;
35 import org.apache.juddi.handler.IHandler;
36 import org.apache.juddi.util.Loader;
37 import org.apache.juddi.util.xml.XMLUtils;
38
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * Represents a version 2.0 UDDI registry and implements
44  * all services as specified in the v2.0 specification.
45  *
46  * @author Steve Viens (sviens@apache.org)
47  */

48 public class RegistryProxy extends AbstractRegistry
49 {
50   // jUDDI XML Handler maker
51
private static HandlerMaker maker = HandlerMaker.getInstance();
52
53   // jUDDI Proxy Property File Name
54
private static final String JavaDoc PROPFILE_NAME = "juddi.properties";
55   
56   // jUDDI Proxy Property Names
57
public static final String JavaDoc INQUIRY_ENDPOINT_PROPERTY_NAME = "juddi.proxy.inquiryURL";
58   public static final String JavaDoc PUBLISH_ENDPOINT_PROPERTY_NAME = "juddi.proxy.publishURL";
59   public static final String JavaDoc ADMIN_ENDPOINT_PROPERTY_NAME = "juddi.proxy.adminURL";
60   public static final String JavaDoc TRANSPORT_CLASS_PROPERTY_NAME = "juddi.proxy.transportClass";
61   public static final String JavaDoc SECURITY_PROVIDER_PROPERTY_NAME = "juddi.proxy.securityProvider";
62   public static final String JavaDoc PROTOCOL_HANDLER_PROPERTY_NAME = "juddi.proxy.protocolHandler";
63   public static final String JavaDoc UDDI_VERSION_PROPERTY_NAME = "juddi.proxy.uddiVersion";
64   public static final String JavaDoc UDDI_NAMESPACE_PROPERTY_NAME = "juddi.proxy.uddiNamespace";
65
66   // jUDDI Proxy Default Property Values
67
public static final String JavaDoc DEFAULT_INQUIRY_ENDPOINT = "http://localhost/juddi/inquiry";
68   public static final String JavaDoc DEFAULT_PUBLISH_ENDPOINT = "http://localhost/juddi/publish";
69   public static final String JavaDoc DEFAULT_ADMIN_ENDPOINT = "http://localhost/juddi/admin";
70   public static final String JavaDoc DEFAULT_TRANSPORT_CLASS = "org.apache.juddi.proxy.AxisTransport";
71   public static final String JavaDoc DEFAULT_SECURITY_PROVIDER = "com.sun.net.ssl.internal.ssl.Provider";
72   public static final String JavaDoc DEFAULT_PROTOCOL_HANDLER = "com.sun.net.ssl.internal.www.protocol";
73   public static final String JavaDoc DEFAULT_UDDI_VERSION = "2.0";
74   public static final String JavaDoc DEFAULT_UDDI_NAMESPACE = "urn:uddi-org:api_v2";
75   
76   // jUDDI Proxy Properties
77
private URL JavaDoc inquiryURL;
78   private URL JavaDoc publishURL;
79   private URL JavaDoc adminURL;
80   private Transport transport;
81   private String JavaDoc securityProvider;
82   private String JavaDoc protocolHandler;
83   private String JavaDoc uddiVersion;
84   private String JavaDoc uddiNamespace;
85   
86   /**
87    * Create a new instance of RegistryProxy. This constructor
88    * looks in the classpath for a file named 'juddi.properties'
89    * and uses property values in this file to initialize the
90    * new instance. Default values are used if the file does not
91    * exist or if a particular property value is not present.
92    */

93   public RegistryProxy()
94   {
95     super();
96     
97     // attempt to load proxy properties from
98
// the juddi.properties file which should
99
// be located in the classpath.
100
Properties JavaDoc props = new Properties JavaDoc();
101
102     try {
103       InputStream JavaDoc stream = Loader.getResourceAsStream(PROPFILE_NAME);
104       if (stream != null)
105         props.load(stream);
106     }
107     catch (IOException JavaDoc ioex) {
108       ioex.printStackTrace();
109     }
110
111     this.init(props);
112   }
113
114   /**
115    * Creates a new instance of RegistryProxy. This constructor
116    * uses the property values passed in the Properties parameter
117    * to initialize the new RegistryProxy instance. Default values
118    * are used if the file does not exist or if a particular
119    * property value is not present.
120    */

121   public RegistryProxy(Properties JavaDoc props)
122   {
123     super();
124     
125     this.init(props);
126   }
127
128   /**
129    *
130    */

131   private void init(Properties JavaDoc props)
132   {
133     // We need to have a non-null Properties
134
// instance so initialization takes place.
135
if (props == null)
136       props = new Properties JavaDoc();
137     
138     // Override defaults with specific specific values
139
try
140     {
141       String JavaDoc iURL = props.getProperty(INQUIRY_ENDPOINT_PROPERTY_NAME);
142       if (iURL != null)
143         this.setInquiryURL(new URL JavaDoc(iURL));
144       else
145         this.setInquiryURL(new URL JavaDoc(DEFAULT_INQUIRY_ENDPOINT));
146       
147       String JavaDoc pURL = props.getProperty(PUBLISH_ENDPOINT_PROPERTY_NAME);
148       if (pURL != null)
149         this.setPublishURL(new URL JavaDoc(pURL));
150       else
151         this.setPublishURL(new URL JavaDoc(DEFAULT_PUBLISH_ENDPOINT));
152       
153       String JavaDoc aURL = props.getProperty(ADMIN_ENDPOINT_PROPERTY_NAME);
154       if (aURL != null)
155         this.setAdminURL(new URL JavaDoc(aURL));
156       else
157         this.setAdminURL(new URL JavaDoc(DEFAULT_ADMIN_ENDPOINT));
158     }
159     catch(MalformedURLException JavaDoc muex) {
160       muex.printStackTrace();
161     }
162             
163     String JavaDoc secProvider = props.getProperty(SECURITY_PROVIDER_PROPERTY_NAME);
164     if (secProvider != null)
165       this.setSecurityProvider(secProvider);
166     else
167       this.setSecurityProvider(DEFAULT_SECURITY_PROVIDER);
168   
169     String JavaDoc protoHandler = props.getProperty(PROTOCOL_HANDLER_PROPERTY_NAME);
170     if (protoHandler != null)
171       this.setProtocolHandler(protoHandler);
172     else
173       this.setProtocolHandler(DEFAULT_PROTOCOL_HANDLER);
174
175     String JavaDoc uddiVer = props.getProperty(UDDI_VERSION_PROPERTY_NAME);
176     if (uddiVer != null)
177       this.setUddiVersion(uddiVer);
178     else
179       this.setUddiVersion(DEFAULT_UDDI_VERSION);
180   
181     String JavaDoc uddiNS = props.getProperty(UDDI_NAMESPACE_PROPERTY_NAME);
182     if (uddiNS != null)
183       this.setUddiNamespace(uddiNS);
184     else
185       this.setUddiNamespace(DEFAULT_UDDI_NAMESPACE);
186
187     String JavaDoc transClass = props.getProperty(TRANSPORT_CLASS_PROPERTY_NAME);
188     if (transClass != null)
189       this.setTransport(this.getTransport(transClass));
190     else
191       this.setTransport(this.getTransport(DEFAULT_TRANSPORT_CLASS));
192   }
193    
194    /**
195    * @return Returns the adminURL.
196    */

197   public URL JavaDoc getAdminURL()
198   {
199     return this.adminURL;
200   }
201   
202   /**
203    * @param adminURL The adminURL to set.
204    */

205   public void setAdminURL(URL JavaDoc url)
206   {
207     this.adminURL = url;
208   }
209   
210   /**
211    * @return Returns the inquiryURL.
212    */

213   public URL JavaDoc getInquiryURL()
214   {
215     return this.inquiryURL;
216   }
217   
218   /**
219    * @param inquiryURL The inquiryURL to set.
220    */

221   public void setInquiryURL(URL JavaDoc url)
222   {
223     this.inquiryURL = url;
224   }
225   
226   /**
227    * @return Returns the publishURL.
228    */

229   public URL JavaDoc getPublishURL()
230   {
231     return this.publishURL;
232   }
233   
234   /**
235    * @param publishURL The publishURL to set.
236    */

237   public void setPublishURL(URL JavaDoc url)
238   {
239     this.publishURL = url;
240   }
241   
242   /**
243    * @return Returns the transport.
244    */

245   public Transport getTransport()
246   {
247     return transport;
248   }
249   
250   /**
251    * @param transport The transport to set.
252    */

253   public void setTransport(Transport transport)
254   {
255     this.transport = transport;
256   }
257
258   /**
259    * @return Returns the protocolHandler.
260    */

261   public String JavaDoc getProtocolHandler()
262   {
263     return this.protocolHandler;
264   }
265
266   /**
267    * @param protocolHandler The protocolHandler to set.
268    */

269   public void setProtocolHandler(String JavaDoc protoHandler)
270   {
271     this.protocolHandler = protoHandler;
272   }
273
274   /**
275    * @return Returns the securityProvider.
276    */

277   public String JavaDoc getSecurityProvider()
278   {
279     return this.securityProvider;
280   }
281
282   /**
283    * @param securityProvider The securityProvider to set.
284    */

285   public void setSecurityProvider(String JavaDoc secProvider)
286   {
287     this.securityProvider = secProvider;
288   }
289
290   /**
291    * @return Returns the uddiNS.
292    */

293   public String JavaDoc getUddiNamespace()
294   {
295     return this.uddiNamespace;
296   }
297   
298   /**
299    * @param uddiNS The uddiNS to set.
300    */

301   public void setUddiNamespace(String JavaDoc uddiNS)
302   {
303     this.uddiNamespace = uddiNS;
304   }
305   
306   /**
307    * @return Returns the uddiVersion.
308    */

309   public String JavaDoc getUddiVersion()
310   {
311     return this.uddiVersion;
312   }
313   
314   /**
315    * @param uddiVersion The uddiVersion to set.
316    */

317   public void setUddiVersion(String JavaDoc uddiVer)
318   {
319     this.uddiVersion = uddiVer;
320   }
321
322   /**
323    *
324    */

325   public RegistryObject execute(RegistryObject uddiRequest)
326     throws RegistryException
327   {
328     // using the type of the request determine
329
// which URL we're going to point this
330
// web service request at.
331

332     URL JavaDoc endPointURL = null;
333     if (uddiRequest instanceof Inquiry)
334       endPointURL = this.getInquiryURL();
335     else if (uddiRequest instanceof Publish || uddiRequest instanceof SecurityPolicy)
336       endPointURL = this.getPublishURL();
337     else if (uddiRequest instanceof Admin)
338       endPointURL = this.getAdminURL();
339     else
340       throw new RegistryException("Unsupported Request: The " +
341         "request '"+uddiRequest.getClass().getName()+"' is an " +
342         "invalid or unknown request type.");
343
344     // create a new 'temp' XML element. This
345
// element is used as a container in which
346
// to marshal the UDDI request into.
347

348     Document JavaDoc document = XMLUtils.createDocument();
349     Element JavaDoc temp = document.createElement("temp");
350
351     // lookup the appropriate request handler
352
// and marshal the RegistryObject into the
353
// appropriate xml format (we only support
354
// uddi v2.0 at this time) attaching results
355
// to the temporary 'temp' element.
356

357     String JavaDoc requestName = uddiRequest.getClass().getName();
358     IHandler requestHandler = maker.lookup(requestName);
359     requestHandler.marshal(uddiRequest,temp);
360     Element JavaDoc request = (Element JavaDoc)temp.getFirstChild();
361
362     request.setAttribute("generic",this.getUddiVersion());
363     request.setAttribute("xmlns",this.getUddiNamespace());
364
365     // A SOAP request is made and a SOAP response
366
// is returned.
367

368     Element JavaDoc response = transport.send(request,endPointURL);
369
370     // First, let's make sure that a response
371
// (any response) is found in the SOAP Body.
372

373     String JavaDoc responseName = response.getLocalName();
374     if (responseName == null)
375     {
376       throw new RegistryException("Unsupported response " +
377         "from registry. A value was not present.");
378     }
379
380     // Let's now try to determine which UDDI response
381
// we received and unmarshal it appropriately or
382
// throw a RegistryException if it's unknown.
383

384     IHandler handler = maker.lookup(responseName.toLowerCase());
385     if (handler == null)
386     {
387       throw new RegistryException("Unsupported response " +
388         "from registry. Response type '" + responseName +
389         "' is unknown.");
390     }
391
392     // Well, we have now determined that something was
393
// returned and it is "a something" that we know
394
// about so let's unmarshal it into a RegistryObject
395

396     RegistryObject uddiResponse = handler.unmarshal(response);
397
398     // Next, let's make sure we didn't recieve a SOAP
399
// Fault. If it is a SOAP Fault then throw it
400
// immediately.
401

402     if (uddiResponse instanceof RegistryException)
403       throw ((RegistryException)uddiResponse);
404
405     // That's it. Return the response to the caller.
406

407     return uddiResponse;
408   }
409
410
411   /**
412    *
413    * @param uddiRequest
414    * @return
415    * @throws RegistryException
416    */

417   public String JavaDoc execute(String JavaDoc uddiRequest, String JavaDoc urltype)
418     throws RegistryException
419   {
420     URL JavaDoc endPointURL = null;
421     if(urltype.equalsIgnoreCase("INQUIRY"))
422       endPointURL = this.getInquiryURL();
423     else endPointURL = this.getPublishURL();
424
425     // A SOAP request is made and a SOAP response
426
// is returned.
427

428     return transport.send(uddiRequest,endPointURL);
429  }
430
431
432   /**
433    * Returns an implementation of Transport based on the className passed in.
434    * If a null value is passed then the default Transport implementation
435    * "org.apache.juddi.proxy.AxisTransport" is created and returned.
436    *
437    * @return Transport
438    */

439   public Transport getTransport(String JavaDoc className)
440   {
441     Transport transport = null;
442     Class JavaDoc transportClass = null;
443     
444     // If a Transport class name isn't supplied use
445
// the default Transport implementation.
446
if (className == null)
447       className = DEFAULT_TRANSPORT_CLASS;
448     
449     try {
450       // instruct class loader to load the TransportFactory
451
transportClass = Loader.getClassForName(className);
452     }
453     catch(ClassNotFoundException JavaDoc cnfex) {
454       cnfex.printStackTrace();
455     }
456
457     try {
458       // try to instantiate the TransportFactory
459
transport = (Transport)transportClass.newInstance();
460     }
461     catch(java.lang.Exception JavaDoc ex) {
462       ex.printStackTrace();
463     }
464
465     return transport;
466   }
467
468   
469   /***************************************************************************/
470   /***************************** TEST DRIVER *********************************/
471   /***************************************************************************/
472
473
474   public static void main(String JavaDoc[] args)
475     throws RegistryException
476   {
477     // Option #1 (grab proxy property values from uddi.properties in classpath)
478
//IRegistry registry = new RegistryProxy();
479

480     // Option #2 (import proxy property values from a specified properties file)
481
//Properties props = new Properties();
482
//props.load(new FileInputStream("proxy.properties"));
483
//IRegistry registry = new RegistryProxy(props);
484

485     // Option #3 (explicitly set the proxy property values)
486
//Properties props = new Properties();
487
//props.setProperty(ADMIN_ENDPOINT_PROPERTY_NAME,"http://localhost:8080/uddi/admin");
488
//props.setProperty(INQUIRY_ENDPOINT_PROPERTY_NAME,"http://localhost:8080/uddi/inquiry");
489
//props.setProperty(PUBLISH_ENDPOINT_PROPERTY_NAME,"http://localhost:8080/uddi/publish");
490
//props.setProperty(TRANSPORT_CLASS_PROPERTY_NAME,"org.apache.juddi.proxy.AxisTransport");
491
//props.setProperty(SECURITY_PROVIDER_PROPERTY_NAME,"com.sun.net.ssl.internal.ssl.Provider");
492
//props.setProperty(PROTOCOL_HANDLER_PROPERTY_NAME,"com.sun.net.ssl.internal.www.protocol");
493
//IRegistry registry = new RegistryProxy(props);
494

495     // Option #4 (Microsoft Test Site)
496
Properties JavaDoc props = new Properties JavaDoc();
497     props.setProperty(INQUIRY_ENDPOINT_PROPERTY_NAME,"http://test.uddi.microsoft.com/inquire");
498     props.setProperty(PUBLISH_ENDPOINT_PROPERTY_NAME,"https://test.uddi.microsoft.com/publish");
499     props.setProperty(TRANSPORT_CLASS_PROPERTY_NAME,"org.apache.juddi.proxy.AxisTransport");
500     props.setProperty(SECURITY_PROVIDER_PROPERTY_NAME,"com.sun.net.ssl.internal.ssl.Provider");
501     props.setProperty(PROTOCOL_HANDLER_PROPERTY_NAME,"com.sun.net.ssl.internal.www.protocol");
502     IRegistry registry = new RegistryProxy(props);
503     
504     AuthToken authToken = registry.getAuthToken("sviens","password");
505     AuthInfo authInfo = authToken.getAuthInfo();
506
507     System.out.println(authInfo.getValue());
508   }
509 }
Popular Tags