📄 sessioncontextimpl.java
字号:
package servlet.http;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
// Referenced classes of package servlet.http:
// HttpSessionImpl
public class SessionContextImpl
implements HttpSessionContext
{
public SessionContextImpl()
{
sessions = new Hashtable();
initializeSessionIdTemplate();
}
public HttpSession getSession(String s)
{
return (HttpSession)sessions.get(s);
}
public Enumeration getIds()
{
return sessions.keys();
}
public synchronized String setSession(HttpSessionImpl httpsessionimpl)
{
String s = generateSessionId();
sessions.put(s, httpsessionimpl);
return s;
}
public void removeSession(String s)
{
if(getSession(s) != null)
sessions.remove(s);
}
public InetAddress getLocalHost()
{
if(mLocalHost == null)
synchronized(this)
{
if(mLocalHost == null)
try
{
mLocalHost = InetAddress.getLocalHost();
}
catch(UnknownHostException ex)
{
try
{
mLocalHost = InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException ex2) {}
}
}
return mLocalHost;
}
protected void initializeSessionIdTemplate()
{
mSessionIdTemplate = new byte[14];
int i = 8;
byte abyte0[] = getLocalHost().getAddress();
System.arraycopy(abyte0, 0, mSessionIdTemplate, i, 4);
i += 4;
int j = 0;
mSessionIdTemplate[i++] = (byte)(j >> 8 & 0xff);
mSessionIdTemplate[i++] = (byte)(j & 0xff);
}
synchronized int generateSessionIdCount()
{
return mSessionIdCounter++;
}
String generateSessionId()
{
byte abyte0[] = new byte[mSessionIdTemplate.length];
System.arraycopy(mSessionIdTemplate, 0, abyte0, 0, mSessionIdTemplate.length);
int i = 0;
int j = generateSessionIdCount();
int k = j;
long l = System.currentTimeMillis();
k = k * 39 + (int)(l >> 32 & -1L);
k = k * 39 + (int)(l & -1L);
abyte0[i++] = (byte)(k >> 24 & 0xff);
abyte0[i++] = (byte)(k >> 16 & 0xff);
abyte0[i++] = (byte)(k >> 8 & 0xff);
abyte0[i++] = (byte)(k & 0xff);
abyte0[i++] = (byte)(j >> 24 & 0xff);
abyte0[i++] = (byte)(j >> 16 & 0xff);
abyte0[i++] = (byte)(j >> 8 & 0xff);
abyte0[i++] = (byte)(j & 0xff);
return convertSessionIdBytesToSessionId(abyte0);
}
String convertSessionIdBytesToSessionId(byte abyte0[])
{
int i = abyte0.length * 8;
int j = i / 5;
if(j % 5 != 0)
j++;
char ac[] = new char[j];
int k = 0;
int l = 0;
int i1 = 0;
while(k < abyte0.length)
{
int j1 = 0;
if(l <= 3)
{
j1 = abyte0[k] >> 3 - l & 0x1f;
}
else
{
j1 = abyte0[k] << 5 - (8 - l);
if(k + 1 < abyte0.length)
{
int k1 = abyte0[k + 1] >> 8 - (5 - (8 - l));
k1 &= sSecondByteMasks[5 - (8 - l)];
j1 |= k1;
}
j1 &= 0x1f;
}
ac[i1++] = sBitChars[j1];
if((l += 5) >= 8)
{
k++;
l -= 8;
}
}
return new String(ac);
}
private int mSessionIdCounter;
private InetAddress mLocalHost;
private byte mSessionIdTemplate[];
private static final char sBitChars[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
'4', '5'
};
private static final int sSecondByteMasks[] = {
0, 1, 3, 7, 31
};
private Hashtable sessions;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -