/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.enterprise.ee.web.sessmgmt;
import com.sun.enterprise.ee.cms.core.DistributedStateCache;
import com.sun.enterprise.ee.cms.core.GMSFactory;
import com.sun.enterprise.ee.cms.core.GroupManagementService;
import com.sun.enterprise.ee.cms.ext.IiopInfo;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
/**
* @author bhavanishankar@dev.java.net
*/
public class PlainSocketChannel extends AbstractSocketChannel {
private HashMap publishAddress = new HashMap(); // contains HOSTIP, LISTENPORT
// keys for publishing the socket address to DistributedStateCache.
private static final String COMP_NAME = "SSR";
private static final String HOSTIP_ATTR = "HOSTIP";
private static final String LISTENPORT_ATTR = "LISTENPORT";
private static final String SOCKET_ADDR = "SOCKETADDR";
// range of ports.
int startPort = 9701;
int endPort = 9999;
private ServerSocket serverSocket = null;
private static volatile PlainSocketChannel INSTANCE = null;
public static PlainSocketChannel getInstance() {
if (INSTANCE == null) {
synchronized (PlainSocketChannel.class) {
if (INSTANCE == null) {
INSTANCE = new PlainSocketChannel();
}
}
}
return INSTANCE;
}
private PlainSocketChannel() {
initialize();
startAccepting();
}
// open a server socket and listen for connections.
private void initialize() {
String hostIP = getHostIP();
for (int port = endPort; port >= startPort; --port) {
try {
serverSocket = hostIP != null ?
new ServerSocket(port, 0, InetAddress.getByName(hostIP)) :
new ServerSocket(port);
break; // successfully created the socket;
} catch (Exception ex) {
/// try next port.
}
}
if (serverSocket == null) {
logger.warning("PlainSocketChannel failed to create server socket");
return;
}
setPublishAddress(hostIP); // populate publishAddress map.
publishAddress(); // publish to DSC.
logger.info("PlainSocketChannel created server socket at " +
serverSocket.getInetAddress() + ":" + serverSocket.getLocalPort() +
", PublishAddress = " + publishAddress);
}
// initialize the publishedAddress variable.
private void setPublishAddress(String hostIP) {
int serverPort = serverSocket.getLocalPort();
String serverAdress = hostIP;
if (serverAdress == null || serverAdress.startsWith("0.0.0.0")) {
try {
serverAdress = InetAddress.getLocalHost().getHostAddress();
} catch (Exception ex) {
logger.log(Level.WARNING, ex.getMessage(), ex);
}
}
publishAddress.put(HOSTIP_ATTR, serverAdress);
publishAddress.put(LISTENPORT_ATTR, serverPort);
}
// get the configured iiop-listener address.
private String getHostIP() {
String hostAddress = null;
try {
GroupManagementService gms =
GMSFactory.getGMSModule(ReplicationUtil.getClusterName());
Map memberDetails =
gms.getMemberDetails(ReplicationUtil.getInstanceName());
List<IiopInfo> iiopInfos =
(List<IiopInfo>) memberDetails.get(IiopInfo.IIOP_MEMBER_DETAILS_KEY);
if (iiopInfos != null && !iiopInfos.isEmpty()) {
hostAddress = iiopInfos.get(0).getAddress();
}
} catch (Exception ex) {
}
return hostAddress;
}
private DistributedStateCache getDSC() {
DistributedStateCache cache = null;
try {
GroupManagementService gms =
GMSFactory.getGMSModule(ReplicationUtil.getClusterName());
cache = gms.getGroupHandle().getDistributedStateCache();
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return cache;
}
// publish the server socket address in DSC.
public void publishAddress() {
try {
DistributedStateCache cache = getDSC();
String memberTokenId = ReplicationUtil.getInstanceName();
cache.addToCache(COMP_NAME, memberTokenId, SOCKET_ADDR, publishAddress);
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
}
// get the remote socket address from DSC.
public HashMap getPublishedAddress(String instanceName) {
DistributedStateCache cache = getDSC();
String memberTokenId = instanceName;
try {
HashMap props = ((HashMap) cache.getFromCache(
COMP_NAME, memberTokenId, SOCKET_ADDR));
return props;
} catch (Exception ex) {
return null;
}
}
// connect to the remote host by grabbing the address published in DSC.
@Override
protected Socket connect(String instanceName) {
HashMap publishedAddress = getPublishedAddress(instanceName);
String hostIP = null;
int port = -1;
Socket s = null;
if (publishedAddress != null) {
hostIP = (String) publishedAddress.get(HOSTIP_ATTR);
port = (Integer) publishedAddress.get(LISTENPORT_ATTR);
}
if (hostIP == null || port == -1) {
logger.warning("Unable to connect to " + instanceName +
". Host information is not found in DistributedStateCache");
} else {
try {
s = new Socket(hostIP, port);
s.setSoTimeout(0);
} catch (Exception ex) {
logger.log(Level.WARNING, "Unable to connect to " + hostIP + ":" + port, ex);
}
}
return s;
}
@Override
public ServerSocket getServerSocket() {
return serverSocket;
}
}