/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. 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 org.jvnet.glassfish.comms.replication.sessmgmt;
import com.ericsson.ssa.sip.SipSessionManager;
import com.ericsson.ssa.sip.SipStore;
import com.sun.enterprise.ee.web.sessmgmt.ExpatListElement;
import com.sun.enterprise.ee.web.sessmgmt.JxtaReplicationSender;
import com.sun.enterprise.ee.web.sessmgmt.ReplicationHealthChecker;
import com.sun.enterprise.ee.web.sessmgmt.ReplicationState;
import com.sun.enterprise.ee.web.sessmgmt.ReplicationUtil;
import java.util.logging.Logger;
import org.apache.catalina.session.IOUtilsCaller;
import org.jvnet.glassfish.comms.util.LogUtil;
public abstract class SipStoreBase implements SipStore {
protected static final Logger _logger =LogUtil.SSR_LOGGER.getLogger();
protected static final String MODE_SIP = ReplicationState.MODE_SIP;
protected SipSessionManager manager;
/**
* A utility class used to call into services from IOUtils
*/
private IOUtilsCaller webUtilsCaller = null;
/**
* Gets the session manager of this store.
*
* @return The session manager of this store
*/
public SipSessionManager getSipSessionManager() {
return manager;
}
/**
* Sets the session manager for this store.
*
* @param manager The session manager for this store.
*/
public void setSipSessionManager(SipSessionManager manager) {
this.manager = manager;
}
public String getApplicationId() {
return this.getSipSessionManager().getApplicationId();
}
protected boolean isExpectingExpatIdsMap() {
boolean result = false;
result = ((SipTransactionPersistentManager) manager).isExpectingExpatIdsMap();
return result;
}
protected abstract ExpatListElement getExpatListElementFor(String id);
protected boolean canTrustLocallyCachedState(String id, String version, ReplicationState localCachedState) {
boolean trustCachedState = (localCachedState != null);
if (trustCachedState) {
if (version == null) {
if (isExpectingExpatIdsMap()) {
String replicatingSource = (String) localCachedState.getProperty(
ReplicationState.ORIGINATING_INSTANCE_NAME);
// calling ReplicationHealthChecker.getLbEnabledList() or
// ReplcationHealthChecker.isReplicationSourceInstanceAlive are both expensive.
// So, first do a quick check.
trustCachedState = ((SipTransactionPersistentManager) manager).
canTrustReplicaDuringExpat(replicatingSource);
if(!trustCachedState) {
// no luck, now check lbEnabledList.
trustCachedState = !ReplicationHealthChecker.getInstance().
getLbEnabledList().contains(replicatingSource);
}
} else {
ExpatListElement expat = getExpatListElementFor(id);
trustCachedState = (expat == null ||
ReplicationUtil.getInstanceName().equals(expat.getInstanceName()));
}
} else {
trustCachedState = (ReplicationUtil.parseLong(version) == localCachedState.getVersion());
}
}
return trustCachedState;
}
/**
* send a load acknowledgement based on the ReplicationState
*
* @param bestState The ReplicationState
* @param command the command (type)
* @param beKey the beKey
*/
protected void sendLoadAcknowledgement(ReplicationState bestState, String command, String beKey) {
// Send acknowledgement of load receipt
// No response to wait for in this case
ReplicationState loadReceivedState =
ReplicationState.createBroadcastLoadReceivedState(
MODE_SIP, (String)bestState.getId(),
getApplicationId(), bestState.getVersion(),
ReplicationUtil.getInstanceName(), command);
sendLoadAcknowledgement(loadReceivedState, beKey);
}
protected void sendLoadAcknowledgement(String id, String command,
long version, String beKey) {
ReplicationState loadReceivedState =
ReplicationState.createBroadcastLoadReceivedState(
MODE_SIP, id,
getApplicationId(), version,
ReplicationUtil.getInstanceName(), command);
sendLoadAcknowledgement(loadReceivedState, beKey);
}
protected void __addToRemotelyLoadedSessionIds(String id) {
SipTransactionPersistentManager mgr =
(SipTransactionPersistentManager)getSipSessionManager();
mgr.getRemotelyLoadedSessionIds().add(id);
}
protected boolean __removeFromRemotelyLoadedSessionIds(String id) {
SipTransactionPersistentManager mgr =
(SipTransactionPersistentManager)getSipSessionManager();
return mgr.getRemotelyLoadedSessionIds().remove(id);
}
/**
* send a load acknowledgement based on the ReplicationState
*
* @param loadReceivedState The ReplicationState representing
* the load received acknowledgement
* @param beKey the beKey
*/
protected void sendLoadAcknowledgement(ReplicationState loadReceivedState, String beKey) {
// Send acknowledgement of load receipt
// No response to wait for in this case
JxtaReplicationSender sender
= JxtaReplicationSender.createInstance();
ReplicationUtil repUtil = ReplicationUtil.createReplicationUtil();
if ((beKey != null) && repUtil.isInstanceLoadBalancedByCLB()){
loadReceivedState.setProperty(ReplicationState.IGNORE_REMOVE_INSTANCE_NAME,
repUtil.getFailoverServerInstanceForBeKey(beKey));
} else {
ReplicationHealthChecker healthChecker = ReplicationHealthChecker.getInstance();
String currentReplicaPartner = healthChecker.getCurrentPartnerInstanceName();
loadReceivedState.setProperty(ReplicationState.IGNORE_REMOVE_INSTANCE_NAME,
currentReplicaPartner);
}
sender.sendBroadcastQuery(loadReceivedState);
}
public String getMode() {
return MODE_SIP;
}
}