KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > docoriented > client > ServiceLocator


1 /* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2
3  http://developer.sun.com/berkeley_license.html
4
5  $Id: ServiceLocator.java,v 1.3 2005/05/25 23:01:53 sean_brydon Exp $ */

6
7
8
9 package com.sun.j2ee.blueprints.docoriented.client;
10
11
12
13 import javax.naming.*;
14
15 import java.rmi.Remote JavaDoc;
16
17 import javax.xml.rpc.*;
18
19
20
21 /**
22
23  * Implements Service Locator pattern for Web services
24
25  * It looks up resources for service references
26
27  */

28
29 public class ServiceLocator {
30
31     
32
33     private transient InitialContext ic;
34
35
36
37     public ServiceLocator() throws ServiceLocatorException {
38
39         try {
40
41             setInitialContext();
42
43         } catch (Exception JavaDoc e) {
44
45             throw new ServiceLocatorException(e);
46
47         }
48
49     }
50
51
52
53     private void setInitialContext() throws javax.naming.NamingException JavaDoc {
54
55       ic = new InitialContext();
56
57     }
58
59
60
61     /**
62
63      * Service class acts as a factory of the Dynamic proxy for the target service endpoint.
64
65      * @see java.xml.rpc.Service.java
66
67      * @return the Service factory corresponding to jndi homeName
68
69      */

70
71     public Service getService(String JavaDoc jndiName) throws ServiceLocatorException {
72
73         try {
74
75             if (ic == null) setInitialContext();
76
77             return (Service) ic.lookup(jndiName);
78
79         } catch (Exception JavaDoc e) {
80
81             throw new ServiceLocatorException("ServiceLocator can not lookup jndiName=" + jndiName, e);
82
83         }
84
85     }
86
87
88
89     /**
90
91      * Service class acts as a factory of the Dynamic proxy for the target service endpoint.
92
93      * @see java.xml.rpc.Service.java
94
95      * @return the Service instance
96
97      */

98
99     public Remote JavaDoc getServicePort(String JavaDoc jndiName, Class JavaDoc className) throws ServiceLocatorException {
100
101         try {
102
103             if (ic == null) setInitialContext();
104
105             Service service = (Service) ic.lookup(jndiName);
106
107             return service.getPort(className);
108
109         } catch (Exception JavaDoc e) {
110
111             throw new ServiceLocatorException("ServiceLocator can not lookup jndiName=" + jndiName + " and className=" + className, e);
112
113         }
114
115     }
116
117 }
118
119
Popular Tags