KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > WebServiceEjbEndpointRegistry


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.webservice;
25
26 import java.util.*;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29
30 // jaxrpc spi
31
import com.sun.xml.rpc.spi.runtime.SystemHandlerDelegate;
32
33 import com.sun.enterprise.deployment.WebServiceEndpoint;
34 import com.sun.enterprise.webservice.monitoring.WebServiceEngineImpl;
35 import com.sun.enterprise.webservice.monitoring.WebServiceEngineFactory;
36 import com.sun.ejb.containers.StatelessSessionContainer;
37 import com.sun.logging.LogDomains;
38 import com.sun.enterprise.util.i18n.StringManager;
39
40 /**
41  * This class acts as a registry of all the webservice EJB end points
42  * enabled in this application server. This a singleton class, use
43  * getRegistry() to obtain the registry instance
44  *
45  * @author Jerome Dochez
46  */

47 public class WebServiceEjbEndpointRegistry {
48     
49     private static StringManager localStrings =
50         StringManager.getManager( WebServiceEjbEndpointRegistry.class );
51     Logger JavaDoc logger = LogDomains.getLogger(LogDomains.EJB_LOGGER);
52
53     private static WebServiceEjbEndpointRegistry registry = null;
54     
55     // Ejb service endpoint info.
56
private Hashtable webServiceEjbEndpoints = new Hashtable();
57
58     // Derived set of all ejb web service related context roots. Used
59
// to optimize the check that determines whether an HTTP request is
60
// for an ejb. NOTE that ejb endpoints may share the same context
61
// root, but that context root must not be used by any web application.
62
// So if the context root portion of the request is in this set, we know
63
// the call is for an ejb.
64
private Set ejbContextRoots = new HashSet();
65     
66     
67     /** Creates a new instance of WebServiceEjbEndpointRegistry */
68     private WebServiceEjbEndpointRegistry() {
69     }
70     
71     /**
72      * @return the registry instance
73      */

74     public static WebServiceEjbEndpointRegistry getRegistry() {
75         if (registry==null) {
76             synchronized(WebServiceEjbEndpointRegistry.class) {
77                 if (registry==null) {
78                     registry = new WebServiceEjbEndpointRegistry();
79                 }
80             }
81         }
82         return registry;
83     }
84     
85     public void registerEjbWebServiceEndpoint(EjbRuntimeEndpointInfo endpoint) throws Exception JavaDoc {
86         synchronized(webServiceEjbEndpoints) {
87             String JavaDoc uriRaw = endpoint.getEndpointAddressUri();
88             String JavaDoc uri = (uriRaw.charAt(0)=='/') ? uriRaw.substring(1) : uriRaw;
89             if (webServiceEjbEndpoints.containsKey(uri)) {
90                 logger.log(Level.SEVERE,
91                         localStrings.getString("enterprise.webservice.duplicateService",
92                         new Object JavaDoc[]{uri}));
93             }
94             webServiceEjbEndpoints.put(uri, endpoint);
95             regenerateEjbContextRoots();
96         }
97         
98         // notify monitoring layers that a new endpoint is being created.
99
WebServiceEngineImpl engine = (WebServiceEngineImpl) WebServiceEngineFactory.getInstance().getEngine();
100         if (endpoint.getEndpoint().getWebService().getMappingFileUri()!=null) {
101             engine.createHandler((com.sun.xml.rpc.spi.runtime.SystemHandlerDelegate)null, endpoint.getEndpoint());
102         } else {
103             engine.createHandler((com.sun.xml.ws.spi.runtime.SystemHandlerDelegate)null, endpoint.getEndpoint());
104             // Safe to assume that it's a JAXWS endpoint
105
endpoint.initRuntimeInfo();
106         }
107     }
108
109     public void unregisterEjbWebServiceEndpoint(String JavaDoc endpointAddressUri) {
110         
111         EjbRuntimeEndpointInfo endpoint = null;
112         
113         synchronized(webServiceEjbEndpoints) {
114             String JavaDoc uriRaw = endpointAddressUri;
115             String JavaDoc uri = (uriRaw.charAt(0)=='/') ? uriRaw.substring(1) : uriRaw;
116             endpoint = (EjbRuntimeEndpointInfo) webServiceEjbEndpoints.remove(uri);
117             regenerateEjbContextRoots();
118         }
119         
120         if (endpoint==null) {
121             return;
122         }
123         
124         // notify the monitoring layers that an endpoint is destroyed
125
WebServiceEngineImpl engine = (WebServiceEngineImpl) WebServiceEngineFactory.getInstance().getEngine();
126         engine.removeHandler(endpoint.getEndpoint());
127     }
128     
129     /**
130      * Creates a new EjbRuntimeEndpointInfo instance depending on the type
131      * and version of the web service implementation.
132      * @param
133      */

134     public EjbRuntimeEndpointInfo createEjbEndpointInfo(WebServiceEndpoint webServiceEndpoint,
135                                   StatelessSessionContainer ejbContainer,
136                                   Object JavaDoc servant, Class JavaDoc tieClass) {
137         EjbRuntimeEndpointInfo info;
138         if ("1.1".compareTo(webServiceEndpoint.getWebService().getWebServicesDescriptor().getSpecVersion())>=0) {
139             info = new Ejb2RuntimeEndpointInfo(webServiceEndpoint, ejbContainer, servant, tieClass);
140         } else {
141             info = new EjbRuntimeEndpointInfo(webServiceEndpoint, ejbContainer, servant);
142         }
143         return info;
144     }
145
146     public EjbRuntimeEndpointInfo getEjbWebServiceEndpoint
147         (String JavaDoc uriRaw, String JavaDoc method, String JavaDoc query) {
148         EjbRuntimeEndpointInfo endpoint = null;
149         
150         if (uriRaw==null || uriRaw.length()==0) {
151             return null;
152         }
153         
154         // Strip off any leading slash.
155
String JavaDoc uri = (uriRaw.charAt(0) == '/') ? uriRaw.substring(1) : uriRaw;
156
157         synchronized(webServiceEjbEndpoints) {
158
159             if( method.equals("GET") ) {
160                 // First check for a context root match so we avoid iterating
161
// through all ejb endpoints. This logic will be used for
162
// all HTTP GETs, so it's important to reduce the overhead in
163
// the likely most common case that the request is for a web
164
// component.
165
String JavaDoc contextRoot = getContextRootForUri(uri);
166                 if( ejbContextRoots.contains(contextRoot) ) {
167                     // Now check for a match with a specific ejb endpoint.
168
Collection values = webServiceEjbEndpoints.values();
169                     for(Iterator iter = values.iterator(); iter.hasNext();) {
170                         EjbRuntimeEndpointInfo next = (EjbRuntimeEndpointInfo)
171                             iter.next();
172                         if( next.getEndpoint().matchesEjbPublishRequest
173                             (uri, query)) {
174                             endpoint = next;
175                             break;
176                         }
177                     }
178                 }
179             } else {
180                 // In this case the uri must match exactly to be an ejb web
181
// service invocation, so do a direct table lookup.
182
endpoint = (EjbRuntimeEndpointInfo)
183                     webServiceEjbEndpoints.get(uri);
184             }
185         }
186         return endpoint;
187     }
188
189     public Collection getEjbWebServiceEndpoints() {
190         return webServiceEjbEndpoints.entrySet();
191     }
192
193     private String JavaDoc getContextRootForUri(String JavaDoc uri) {
194         StringTokenizer tokenizer = new StringTokenizer(uri, "/");
195         if (tokenizer.hasMoreTokens()) {
196             return tokenizer.nextToken();
197         } else {
198             return null;
199         }
200         
201     }
202
203     private void regenerateEjbContextRoots() {
204         synchronized(webServiceEjbEndpoints) {
205             Set contextRoots = new HashSet();
206             for(Iterator iter = webServiceEjbEndpoints.keySet().iterator();
207                 iter.hasNext();) {
208                 String JavaDoc uri = (String JavaDoc) iter.next();
209                 String JavaDoc contextRoot = getContextRootForUri(uri);
210                 if( (contextRoot != null) && !contextRoot.equals("") ) {
211                     contextRoots.add(contextRoot);
212                 }
213             }
214             ejbContextRoots = contextRoots;
215         }
216     }
217     
218 }
219
Popular Tags