001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.server.glassfish;
038:
039: import com.sun.enterprise.webservice.EjbRuntimeEndpointInfo;
040: import com.sun.enterprise.webservice.WebServiceEjbEndpointRegistry;
041: import com.sun.istack.NotNull;
042: import com.sun.istack.Nullable;
043: import com.sun.enterprise.webservice.monitoring.WebServiceEngineFactory;
044: import com.sun.enterprise.webservice.monitoring.WebServiceEngine;
045: import com.sun.enterprise.webservice.monitoring.Endpoint;
046: import com.sun.enterprise.deployment.WebServiceEndpoint;
047: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.Map;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * @author Alexey Stashok
056: */
057: public final class AppServWSRegistry {
058: private static final Logger logger = Logger
059: .getLogger(com.sun.xml.ws.transport.tcp.util.TCPConstants.LoggingDomain
060: + ".server");
061:
062: private static final AppServWSRegistry instance = new AppServWSRegistry();
063:
064: private final Map<String, Map<String, WSEndpointDescriptor>> registry;
065:
066: public static AppServWSRegistry getInstance() {
067: return instance;
068: }
069:
070: private AppServWSRegistry() {
071: registry = new HashMap<String, Map<String, WSEndpointDescriptor>>();
072: final WSEndpointLifeCycleListener lifecycleListener = new WSEndpointLifeCycleListener();
073:
074: final WebServiceEngine engine = WebServiceEngineFactory
075: .getInstance().getEngine();
076: engine.addLifecycleListener(lifecycleListener);
077:
078: populateEndpoints(engine);
079: }
080:
081: /**
082: * Populate currently registered WS Endpoints and register them
083: */
084: private void populateEndpoints(@NotNull
085: final WebServiceEngine engine) {
086: final Iterator<Endpoint> endpoints = engine.getEndpoints();
087: while (endpoints.hasNext()) {
088: registerEndpoint(endpoints.next());
089: }
090: }
091:
092: /**
093: * Lookup endpoint's decriptor in registry
094: */
095: public @Nullable
096: WSEndpointDescriptor get(@NotNull
097: final String wsServiceName, @NotNull
098: final String endpointName) {
099: final Map<String, WSEndpointDescriptor> endpointMap = registry
100: .get(wsServiceName);
101: if (endpointMap != null) {
102: return endpointMap.get(endpointName);
103: }
104:
105: return null;
106: }
107:
108: /**
109: * Method is used by WS invoker to clear some EJB invoker state ???
110: */
111: public @NotNull
112: EjbRuntimeEndpointInfo getEjbRuntimeEndpointInfo(@NotNull
113: final String service, @NotNull
114: final String endpointName) {
115:
116: final WSEndpointDescriptor wsEndpointDescriptor = get(service,
117: endpointName);
118: EjbRuntimeEndpointInfo endpointInfo = null;
119:
120: if (wsEndpointDescriptor.isEJB()) {
121: endpointInfo = (EjbRuntimeEndpointInfo) WebServiceEjbEndpointRegistry
122: .getRegistry()
123: .getEjbWebServiceEndpoint(
124: wsEndpointDescriptor.getURI(), "POST", null);
125: }
126:
127: return endpointInfo;
128: }
129:
130: /**
131: * Register new WS Endpoint
132: */
133: protected void registerEndpoint(@NotNull
134: final Endpoint endpoint) {
135: final WebServiceEndpoint wsServiceDescriptor = endpoint
136: .getDescriptor();
137:
138: if (wsServiceDescriptor != null
139: && isTCPEnabled(wsServiceDescriptor)) {
140: final String contextRoot = getEndpointContextRoot(wsServiceDescriptor);
141: final String urlPattern = getEndpointUrlPattern(wsServiceDescriptor);
142:
143: // ContextRoot could be represented as leading slash or without (GF API changes from time to time)
144: // So we use slashed version for registries
145: final String slashedContextRoot = ensureSlash(contextRoot);
146: final String slashedUrlPattern = ensureSlash(urlPattern);
147:
148: final String path = slashedContextRoot + slashedUrlPattern;
149: if (logger.isLoggable(Level.FINE)) {
150: logger.log(Level.FINE, MessagesMessages
151: .WSTCP_1110_APP_SERV_REG_REGISTER_ENDPOINT(
152: wsServiceDescriptor.getServiceName(),
153: path, wsServiceDescriptor
154: .implementedByEjbComponent()));
155: }
156: final WSEndpointDescriptor descriptor = new WSEndpointDescriptor(
157: wsServiceDescriptor, contextRoot, urlPattern,
158: endpoint.getEndpointSelector());
159: addToRegistry(slashedContextRoot, slashedUrlPattern,
160: descriptor);
161: }
162: }
163:
164: /**
165: * Deregister WS Endpoint
166: */
167: protected void deregisterEndpoint(@NotNull
168: final Endpoint endpoint) {
169: final WebServiceEndpoint wsServiceDescriptor = endpoint
170: .getDescriptor();
171: final String contextRoot = getEndpointContextRoot(wsServiceDescriptor);
172: final String urlPattern = getEndpointUrlPattern(wsServiceDescriptor);
173:
174: // ContextRoot could be represented as leading slash or without (GF API changes from time to time)
175: // So we use slashed version for registries
176: final String slashedContextRoot = ensureSlash(contextRoot);
177: final String slashedUrlPattern = ensureSlash(urlPattern);
178:
179: final String path = slashedContextRoot + slashedUrlPattern;
180:
181: if (logger.isLoggable(Level.FINE)) {
182: logger.log(Level.FINE, MessagesMessages
183: .WSTCP_1111_APP_SERV_REG_DEREGISTER_ENDPOINT(
184: wsServiceDescriptor.getWebService()
185: .getName(), path,
186: wsServiceDescriptor
187: .implementedByEjbComponent()));
188: }
189: removeFromRegistry(slashedContextRoot, slashedUrlPattern);
190: WSTCPAdapterRegistryImpl.getInstance().deleteTargetFor(path);
191: }
192:
193: private void addToRegistry(@NotNull
194: String contextRoot, @NotNull
195: String urlPattern, @NotNull
196: final WSEndpointDescriptor wsDescriptor) {
197:
198: contextRoot = ensureSlash(contextRoot);
199: urlPattern = ensureSlash(urlPattern);
200: Map<String, WSEndpointDescriptor> endpointMap = registry
201: .get(contextRoot);
202: if (endpointMap == null) {
203: endpointMap = new HashMap<String, WSEndpointDescriptor>();
204: registry.put(contextRoot, endpointMap);
205: }
206:
207: endpointMap.put(urlPattern, wsDescriptor);
208: }
209:
210: private WSEndpointDescriptor removeFromRegistry(@NotNull
211: final String wsServiceName, @NotNull
212: final String endpointName) {
213: final Map<String, WSEndpointDescriptor> endpointMap = registry
214: .get(wsServiceName);
215: if (endpointMap != null) {
216: return endpointMap.remove(endpointName);
217: }
218:
219: return null;
220: }
221:
222: private @NotNull
223: String getEndpointContextRoot(@NotNull
224: final WebServiceEndpoint wsServiceDescriptor) {
225: String contextRoot;
226: if (!wsServiceDescriptor.implementedByEjbComponent()) {
227: contextRoot = wsServiceDescriptor.getWebComponentImpl()
228: .getWebBundleDescriptor().getContextRoot();
229: logger
230: .log(
231: Level.FINE,
232: MessagesMessages
233: .WSTCP_1112_APP_SERV_REG_GET_ENDP_CR_NON_EJB(contextRoot));
234: } else {
235: final String[] path = wsServiceDescriptor
236: .getEndpointAddressUri().split("/");
237: contextRoot = "/" + path[1];
238: logger
239: .log(
240: Level.FINE,
241: MessagesMessages
242: .WSTCP_1113_APP_SERV_REG_GET_ENDP_CR_EJB(contextRoot));
243: }
244:
245: return contextRoot;
246: }
247:
248: private @NotNull
249: String getEndpointUrlPattern(@NotNull
250: final WebServiceEndpoint wsServiceDescriptor) {
251: String urlPattern;
252: if (!wsServiceDescriptor.implementedByEjbComponent()) {
253: urlPattern = wsServiceDescriptor.getEndpointAddressUri();
254: logger
255: .log(
256: Level.FINE,
257: MessagesMessages
258: .WSTCP_1114_APP_SERV_REG_GET_ENDP_URL_PATTERN_NON_EJB(urlPattern));
259: } else {
260: final String[] path = wsServiceDescriptor
261: .getEndpointAddressUri().split("/");
262: if (path.length < 3) {
263: return "";
264: }
265:
266: urlPattern = "/" + path[2];
267: logger
268: .log(
269: Level.FINE,
270: MessagesMessages
271: .WSTCP_1115_APP_SERV_REG_GET_ENDP_URL_PATTERN_EJB(urlPattern));
272: }
273:
274: return urlPattern;
275: }
276:
277: private @Nullable
278: String ensureSlash(@Nullable
279: String s) {
280: if (s != null && s.length() > 0 && s.charAt(0) != '/') {
281: return "/" + s;
282: }
283:
284: return s;
285: }
286:
287: private boolean isTCPEnabled(
288: final com.sun.enterprise.deployment.WebServiceEndpoint webServiceDesc) {
289: return true;
290: }
291: }
|