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.JAXWSAdapterRegistry;
040: import com.sun.enterprise.webservice.EjbRuntimeEndpointInfo;
041: import com.sun.enterprise.webservice.WebServiceEjbEndpointRegistry;
042: import com.sun.istack.NotNull;
043: import com.sun.xml.ws.api.server.Adapter;
044: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
045: import com.sun.xml.ws.transport.tcp.util.WSTCPURI;
046: import com.sun.xml.ws.transport.tcp.server.TCPAdapter;
047: import com.sun.xml.ws.transport.tcp.server.WSTCPAdapterRegistry;
048: import java.util.Map;
049: import java.util.concurrent.ConcurrentHashMap;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052:
053: /**
054: * @author Alexey Stashok
055: */
056: public final class WSTCPAdapterRegistryImpl implements
057: WSTCPAdapterRegistry {
058: private static final Logger logger = Logger
059: .getLogger(com.sun.xml.ws.transport.tcp.util.TCPConstants.LoggingDomain
060: + ".server");
061:
062: /**
063: * Registry holds correspondents between service name and adapter
064: */
065: final Map<String, TCPAdapter> registry = new ConcurrentHashMap<String, TCPAdapter>();
066: private static final WSTCPAdapterRegistryImpl instance = new WSTCPAdapterRegistryImpl();
067:
068: private WSTCPAdapterRegistryImpl() {
069: }
070:
071: public static @NotNull
072: WSTCPAdapterRegistryImpl getInstance() {
073: return instance;
074: }
075:
076: public TCPAdapter getTarget(@NotNull
077: final WSTCPURI requestURI) {
078: // path should have format like "/context-root/url-pattern", where context-root and url-pattern could be /xxxx/yyyy/zzzz
079:
080: WSEndpointDescriptor wsEndpointDescriptor = null;
081: String contextRoot = "/";
082: String urlPattern = "/";
083: // check if URI path is not empty
084: if (requestURI.path.length() > 0
085: && !requestURI.path.equals("/")) {
086:
087: // Try to check for most common situation "/context-root/url-pattern"
088: int nextSlashIndex = requestURI.path.indexOf('/', 1);
089: if (nextSlashIndex != -1) {
090: contextRoot = requestURI.path.substring(0,
091: nextSlashIndex);
092: urlPattern = requestURI.path.substring(nextSlashIndex,
093: requestURI.path.length());
094: wsEndpointDescriptor = AppServWSRegistry.getInstance()
095: .get(contextRoot, urlPattern);
096: }
097:
098: if (wsEndpointDescriptor == null) {
099: // Try to combine different context-root and url-pattern from given path
100: nextSlashIndex = -1;
101: do {
102: nextSlashIndex = requestURI.path.indexOf('/',
103: nextSlashIndex + 1);
104: int delim = nextSlashIndex != -1 ? nextSlashIndex
105: : requestURI.path.length();
106: contextRoot = delim > 0 ? requestURI.path
107: .substring(0, delim) : "/";
108: urlPattern = delim < requestURI.path.length() ? requestURI.path
109: .substring(delim, requestURI.path.length())
110: : "/";
111: wsEndpointDescriptor = AppServWSRegistry
112: .getInstance().get(contextRoot, urlPattern);
113: } while (nextSlashIndex != -1
114: && wsEndpointDescriptor == null);
115: }
116: } else {
117: wsEndpointDescriptor = AppServWSRegistry.getInstance().get(
118: contextRoot, urlPattern);
119: }
120:
121: if (wsEndpointDescriptor != null) {
122: TCPAdapter adapter = registry.get(requestURI.path);
123: if (adapter == null) {
124: try {
125: adapter = createWSAdapter(wsEndpointDescriptor);
126: registry.put(requestURI.path, adapter);
127: logger
128: .log(
129: Level.FINE,
130: "WSTCPAdapterRegistryImpl. Register adapter. Path: {0}",
131: requestURI.path);
132: } catch (Exception e) {
133: // This common exception is thrown from ejbEndPtInfo.prepareInvocation(true)
134: logger
135: .log(
136: Level.SEVERE,
137: "WSTCPAdapterRegistryImpl. "
138: + MessagesMessages
139: .WSTCP_0008_ERROR_TCP_ADAPTER_CREATE(wsEndpointDescriptor
140: .getWSServiceName()),
141: e);
142: }
143: }
144: return adapter;
145: }
146:
147: return null;
148: }
149:
150: public void deleteTargetFor(@NotNull
151: final String path) {
152: logger.log(Level.FINE,
153: "WSTCPAdapterRegistryImpl. DeRegister adapter for {0}",
154: path);
155: registry.remove(path);
156: }
157:
158: private TCPAdapter createWSAdapter(@NotNull
159: final WSEndpointDescriptor wsEndpointDescriptor) throws Exception {
160: Adapter adapter;
161: if (wsEndpointDescriptor.isEJB()) {
162: final EjbRuntimeEndpointInfo ejbEndPtInfo = (EjbRuntimeEndpointInfo) WebServiceEjbEndpointRegistry
163: .getRegistry()
164: .getEjbWebServiceEndpoint(
165: wsEndpointDescriptor.getURI(), "POST", null);
166: adapter = (Adapter) ejbEndPtInfo.prepareInvocation(true);
167: } else {
168: final String uri = wsEndpointDescriptor.getURI();
169: adapter = JAXWSAdapterRegistry.getInstance().getAdapter(
170: wsEndpointDescriptor.getContextRoot(), uri, uri);
171: }
172:
173: //@TODO implement checkAdapterSupportsTCP
174: // checkAdapterSupportsTCP(adapter);
175: final TCPAdapter tcpAdapter = new TCP109Adapter(
176: wsEndpointDescriptor.getWSServiceName().toString(),
177: wsEndpointDescriptor.getContextRoot(),
178: wsEndpointDescriptor.getUrlPattern(), adapter
179: .getEndpoint(), new ServletFakeArtifactSet(
180: wsEndpointDescriptor.getRequestURL(),
181: wsEndpointDescriptor.getUrlPattern()),
182: wsEndpointDescriptor.isEJB());
183:
184: return tcpAdapter;
185: }
186: }
|