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.util;
038:
039: import com.sun.xml.ws.transport.tcp.client.WSConnectionManager;
040: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
041: import com.sun.xml.ws.transport.tcp.servicechannel.ServiceChannelException;
042: import java.io.IOException;
043: import java.net.URI;
044: import java.net.URISyntaxException;
045: import java.util.HashMap;
046: import java.util.Map;
047: import javax.xml.bind.annotation.adapters.XmlAdapter;
048:
049: /**
050: * @author Alexey Stashok
051: */
052: public final class WSTCPURI
053: implements
054: com.sun.xml.ws.transport.tcp.connectioncache.spi.transport.ContactInfo<ConnectionSession> {
055: public String host;
056: public int port;
057: public String path;
058:
059: private String uri2string;
060: private Map<String, String> params;
061:
062: /**
063: * This constructor should be used just by JAXB runtime
064: */
065: public WSTCPURI() {
066: }
067:
068: private WSTCPURI(String host, int port, String path,
069: Map<String, String> params, String uri2string) {
070: this .host = host;
071: this .port = port;
072: this .path = path;
073: this .params = params;
074: this .uri2string = uri2string;
075: }
076:
077: public String getParameter(final String name) {
078: if (params != null) {
079: return params.get(name);
080: }
081:
082: return null;
083: }
084:
085: public static WSTCPURI parse(final String uri) {
086: try {
087: return parse(new URI(uri));
088: } catch (URISyntaxException ex) {
089: return null;
090: }
091: }
092:
093: public static WSTCPURI parse(final URI uri) {
094: final String path = uri.getPath();
095: final String query = uri.getQuery();
096: Map<String, String> params = null;
097:
098: if (query != null && query.length() > 0) {
099: final String[] paramsStr = query.split(";");
100: params = new HashMap<String, String>(paramsStr.length);
101: for (String paramStr : paramsStr) {
102: if (paramStr.length() > 0) {
103: final String[] paramAsgn = paramStr.split("=");
104: if (paramAsgn != null && paramAsgn.length == 2
105: && paramAsgn[0].length() > 0
106: && paramAsgn[1].length() > 0) {
107: params.put(paramAsgn[0], paramAsgn[1]);
108: }
109: }
110: }
111: }
112:
113: return new WSTCPURI(uri.getHost(), uri.getPort(), path, params,
114: uri.toASCIIString());
115: }
116:
117: public String toString() {
118: return uri2string;
119: }
120:
121: public boolean equals(Object o) {
122: if (o instanceof WSTCPURI) {
123: WSTCPURI toCmp = (WSTCPURI) o;
124: return port == toCmp.port && host.equals(toCmp.host);
125: }
126:
127: return false;
128: }
129:
130: public int hashCode() {
131: return host.hashCode() + port;
132: }
133:
134: public ConnectionSession createConnection() throws IOException {
135: try {
136: return WSConnectionManager.getInstance()
137: .createConnectionSession(this );
138: } catch (VersionMismatchException e) {
139: throw new IOException(e.getMessage());
140: } catch (ServiceChannelException e) {
141: throw new IOException(MessagesMessages
142: .WSTCP_0024_SERVICE_CHANNEL_EXCEPTION(e
143: .getFaultInfo().getErrorCode(), e
144: .getMessage()));
145: }
146: }
147:
148: /**
149: * Class is used to translate WSTCPURI to String and vice versa
150: * This is used in JAXB serialization/deserialization
151: */
152: public static final class WSTCPURI2StringJAXBAdapter extends
153: XmlAdapter<String, WSTCPURI> {
154: public String marshal(final WSTCPURI tcpURI) throws Exception {
155: return tcpURI.toString();
156: }
157:
158: public WSTCPURI unmarshal(final String uri) throws Exception {
159: return WSTCPURI.parse(uri);
160: }
161:
162: }
163: }
|