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.grizzly;
038:
039: import com.sun.enterprise.web.connector.grizzly.SelectorThread;
040: import com.sun.istack.NotNull;
041: import com.sun.xml.ws.transport.tcp.util.TCPConstants;
042: import com.sun.xml.ws.transport.tcp.server.IncomeMessageProcessor;
043: import com.sun.xml.ws.transport.tcp.server.TCPMessageListener;
044: import com.sun.xml.ws.transport.tcp.server.WSTCPConnector;
045: import java.io.IOException;
046: import java.net.InetAddress;
047: import java.util.Properties;
048:
049: /**
050: * @author Alexey Stashok
051: */
052: public class GrizzlyTCPConnector implements WSTCPConnector {
053: private SelectorThread selectorThread;
054:
055: private String host;
056: private int port;
057: private TCPMessageListener listener;
058: private final Properties properties;
059:
060: private final boolean isPortUnificationMode;
061:
062: public GrizzlyTCPConnector(@NotNull
063: final String host, final int port, @NotNull
064: final TCPMessageListener listener) {
065: this .host = host;
066: this .port = port;
067: this .listener = listener;
068: isPortUnificationMode = false;
069: properties = new Properties();
070: }
071:
072: public GrizzlyTCPConnector(@NotNull
073: final TCPMessageListener listener, @NotNull
074: final Properties properties) {
075: this .listener = listener;
076: isPortUnificationMode = true;
077: this .properties = properties;
078: }
079:
080: public void listen() throws IOException {
081: if (isPortUnificationMode) {
082: listenOnUnifiedPort();
083: } else {
084: listenOnNewPort();
085: }
086: }
087:
088: public void listenOnNewPort() throws IOException {
089: try {
090: IncomeMessageProcessor.registerListener(port, listener,
091: properties);
092:
093: selectorThread = new SelectorThread();
094: selectorThread.setClassLoader(WSTCPStreamAlgorithm.class
095: .getClassLoader());
096: selectorThread
097: .setAlgorithmClassName(WSTCPStreamAlgorithm.class
098: .getName());
099: selectorThread.setAddress(InetAddress.getByName(host));
100: selectorThread.setPort(port);
101: selectorThread
102: .setBufferSize(TCPConstants.DEFAULT_FRAME_SIZE);
103: selectorThread.initEndpoint();
104: selectorThread.start();
105: } catch (IOException e) {
106: close();
107: throw e;
108: } catch (InstantiationException e) {
109: close();
110: throw new IOException(e.getClass().getName() + ": "
111: + e.getMessage());
112: }
113: }
114:
115: public void listenOnUnifiedPort() {
116: WSTCPProtocolHandler
117: .setIncomingMessageProcessor(IncomeMessageProcessor
118: .registerListener(0, listener, properties));
119: }
120:
121: public void close() {
122: if (selectorThread != null) {
123: selectorThread.stopEndpoint();
124: IncomeMessageProcessor.releaseListener(selectorThread
125: .getPort());
126: selectorThread = null;
127: }
128: }
129:
130: public String getHost() {
131: return host;
132: }
133:
134: public void setHost(final String host) {
135: this .host = host;
136: }
137:
138: public int getPort() {
139: return port;
140: }
141:
142: public void setPort(final int port) {
143: this .port = port;
144: }
145:
146: public TCPMessageListener getListener() {
147: return listener;
148: }
149:
150: public void setListener(final TCPMessageListener listener) {
151: this .listener = listener;
152: }
153:
154: public void setFrameSize(final int frameSize) {
155: selectorThread.setBufferSize(frameSize);
156: }
157:
158: public int getFrameSize() {
159: return selectorThread.getBufferSize();
160: }
161: }
|