/*
* 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 com.ericsson.ssa.sip;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipSessionsUtil;
import javax.servlet.sip.SipSession;
import com.ericsson.ssa.config.ConvergedContext;
import com.ericsson.ssa.config.ConvergedContextImpl;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jvnet.glassfish.comms.util.LogUtil;
/**
* Implementation class of SipSessionsUtil interface.
*
* @author jluehe
*/
public class SipSessionsUtilImpl implements SipSessionsUtil {
private ConvergedContextImpl ctx;
private SipSessionManager sipSessionManager;
private ConcurrentHashMap<Object, String> linkedSessions =
new ConcurrentHashMap<Object, String>();
private static final LocalStringManagerImpl localStrings =
new LocalStringManagerImpl(SipSessionsUtilImpl.class);
private static final Logger logger = LogUtil.SIP_LOGGER.getLogger();
// Does this SipSessionsUtil have cross-SAR visibility, i.e., does
// it have access to the SipSessionManagers associated with sibling SAR
// files embedded in the same EAR file, or does it have access to only
// its own SipSessionManager?
private boolean hasCrossSarVisibility = false;
/**
* Constructor.
*/
public SipSessionsUtilImpl(ConvergedContextImpl ctx) {
this(ctx, false);
}
/**
* Constructor.
*/
public SipSessionsUtilImpl(ConvergedContextImpl ctx,
boolean hasCrossSarVisibility) {
this.ctx = ctx;
this.sipSessionManager = ctx.getSipSessionManager();
this.hasCrossSarVisibility = hasCrossSarVisibility;
}
/**
* Gets the SipApplicationSession with the given id.
*
* @param sasId The id of the requested SipApplicationSession
*
* @return The SipApplicationSession with the given id, or null if no
* such SipApplicationSession exists
*/
public SipApplicationSession getApplicationSessionById(String sasId) {
if (sasId == null) {
throw new NullPointerException("Application Session ID argument is null");
}
if (!SipApplicationSessionUtil.isLocal(sasId)) {
return null;
}
SipApplicationSessionBase sas = null;
if (hasCrossSarVisibility) {
Set siblings = ctx.getSiblingContexts();
if (siblings != null) {
Iterator iter = siblings.iterator();
while (sas == null && iter.hasNext()) {
ConvergedContext cc = (ConvergedContext) iter.next();
SipSessionManager manager = cc.getSipSessionManager();
if (manager != null) {
try {
sas = manager.findSipApplicationSession(sasId);
} catch (RemoteLockException e) {
throw new RemoteLockRuntimeException(e);
}
}
}
}
} else {
SipSessionManager manager = ctx.getSipSessionManager();
if (manager != null) {
try {
sas = manager.findSipApplicationSession(sasId);
} catch (RemoteLockException e) {
throw new RemoteLockRuntimeException(e);
}
}
}
if (sas != null && !sas.isValid()) sas = null;
return sas;
}
public void linkSession(Object key, String applicationSessionId) {
linkedSessions.put(key, applicationSessionId);
}
public SipApplicationSession getLinkedSession(Object key) {
return getApplicationSessionById(linkedSessions.get(key));
}
public void removeSessionsMapping(SipApplicationSessionImpl sas) {
// TODO make a more preforment delete from linked sessions and not
// a scan.
Iterator<String> i = linkedSessions.values().iterator();
while (i.hasNext()) {
if (sas.getId().equals(i.next())) {
i.remove();
break;
}
}
}
public SipApplicationSession getApplicationSessionByKey(
String applicationSessionKey, boolean create) {
if(applicationSessionKey == null) {
throw new NullPointerException(localStrings.getLocalString(
"ssu_get_sas_by_key_null_key",
"applicationSessionKey is null."));
}
/**
* Construct the SAS-ID for the given SAK, with no random part.
*/
String sasId = SipApplicationSessionUtil.createSasId(
applicationSessionKey, ctx.getAppName(), null);
SipApplicationSessionImpl sas = null;
try {
sas = sipSessionManager.findSipApplicationSession(sasId);
} catch (RemoteLockException ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
// Should we throw this exception or just return null???
throw new RemoteLockRuntimeException(ex);
}
/**
* SAS could be invalid. But, it is upto the application to check that.
* JSR289 does not say that we should create a new one if the SAS is invalid.
*/
if (sas != null || !create) {
return sas;
}
/**
* If create=true, create a new SAS and return it.
*/
sas = sipSessionManager.createSipApplicationSession(sasId,
ctx.getSipApplicationListeners());
sas.setShouldBePersisted(); // on a SAS created by key we want to persist
return sas;
}
/**
* For a session from Join/Replace request, return the corresponding session
* of the original request. If incoming session is null, it can throw NPE.
*
* @param session
* @param headerName
* @return Corresponding Session.
*/
public SipSession getCorrespondingSipSession(SipSession session,
String headerName) {
String linkedSessionId = ((SipSessionImplBase)
session).getLinkedCorrespondingSipSessionId(headerName);
if (linkedSessionId != null) {
return
session.getApplicationSession().getSipSession(linkedSessionId);
}
return null;
}
}