📄 deltamanager.java
字号:
*/
public void setStateTimestampDrop(boolean isTimestampDrop) {
this.stateTimestampDrop = isTimestampDrop;
}
/**
* Return the maximum number of active Sessions allowed, or -1 for no limit.
*/
public int getMaxActiveSessions() {
return (this.maxActiveSessions);
}
/**
* Set the maximum number of actives Sessions allowed, or -1 for no limit.
*
* @param max
* The new maximum number of sessions
*/
public void setMaxActiveSessions(int max) {
int oldMaxActiveSessions = this.maxActiveSessions;
this.maxActiveSessions = max;
support.firePropertyChange("maxActiveSessions", new Integer(oldMaxActiveSessions), new Integer(this.maxActiveSessions));
}
/**
*
* @return Returns the sendAllSessions.
*/
public boolean isSendAllSessions() {
return sendAllSessions;
}
/**
* @param sendAllSessions The sendAllSessions to set.
*/
public void setSendAllSessions(boolean sendAllSessions) {
this.sendAllSessions = sendAllSessions;
}
/**
* @return Returns the sendAllSessionsSize.
*/
public int getSendAllSessionsSize() {
return sendAllSessionsSize;
}
/**
* @param sendAllSessionsSize The sendAllSessionsSize to set.
*/
public void setSendAllSessionsSize(int sendAllSessionsSize) {
this.sendAllSessionsSize = sendAllSessionsSize;
}
/**
* @return Returns the notifySessionListenersOnReplication.
*/
public boolean isNotifySessionListenersOnReplication() {
return notifySessionListenersOnReplication;
}
/**
* @param notifyListenersCreateSessionOnReplication The notifySessionListenersOnReplication to set.
*/
public void setNotifySessionListenersOnReplication(boolean notifyListenersCreateSessionOnReplication) {
this.notifySessionListenersOnReplication = notifyListenersCreateSessionOnReplication;
}
public boolean isExpireSessionsOnShutdown() {
return expireSessionsOnShutdown;
}
public void setExpireSessionsOnShutdown(boolean expireSessionsOnShutdown) {
this.expireSessionsOnShutdown = expireSessionsOnShutdown;
}
public boolean isNotifyListenersOnReplication() {
return notifyListenersOnReplication;
}
public void setNotifyListenersOnReplication(boolean notifyListenersOnReplication) {
this.notifyListenersOnReplication = notifyListenersOnReplication;
}
/**
* @return Returns the defaultMode.
*/
public boolean isDefaultMode() {
return defaultMode;
}
/**
* @param defaultMode The defaultMode to set.
*/
public void setDefaultMode(boolean defaultMode) {
this.defaultMode = defaultMode;
}
public CatalinaCluster getCluster() {
return cluster;
}
public void setCluster(CatalinaCluster cluster) {
this.cluster = cluster;
}
/**
* Set the Container with which this Manager has been associated. If it is a
* Context (the usual case), listen for changes to the session timeout
* property.
*
* @param container
* The associated Container
*/
public void setContainer(Container container) {
// De-register from the old Container (if any)
if ((this.container != null) && (this.container instanceof Context))
((Context) this.container).removePropertyChangeListener(this);
// Default processing provided by our superclass
super.setContainer(container);
// Register with the new Container (if any)
if ((this.container != null) && (this.container instanceof Context)) {
setMaxInactiveInterval(((Context) this.container).getSessionTimeout() * 60);
((Context) this.container).addPropertyChangeListener(this);
}
}
// --------------------------------------------------------- Public Methods
/**
* Construct and return a new session object, based on the default settings
* specified by this Manager's properties. The session id will be assigned
* by this method, and available via the getId() method of the returned
* session. If a new session cannot be created for any reason, return
* <code>null</code>.
*
* @exception IllegalStateException
* if a new session cannot be instantiated for any reason
*
* Construct and return a new session object, based on the default settings
* specified by this Manager's properties. The session id will be assigned
* by this method, and available via the getId() method of the returned
* session. If a new session cannot be created for any reason, return
* <code>null</code>.
*
* @exception IllegalStateException
* if a new session cannot be instantiated for any reason
*/
public Session createSession(String sessionId) {
return createSession(sessionId, true);
}
/**
* create new session with check maxActiveSessions and send session creation
* to other cluster nodes.
*
* @param distribute
* @return The session
*/
public Session createSession(String sessionId, boolean distribute) {
if ((maxActiveSessions >= 0) && (sessions.size() >= maxActiveSessions)) {
rejectedSessions++;
throw new IllegalStateException(sm.getString("deltaManager.createSession.ise"));
}
DeltaSession session = (DeltaSession) super.createSession(sessionId) ;
if (distribute) {
sendCreateSession(session.getId(), session);
}
if (log.isDebugEnabled())
log.debug(sm.getString("deltaManager.createSession.newSession",session.getId(), new Integer(sessions.size())));
return (session);
}
/**
* Send create session evt to all backup node
* @param sessionId
* @param session
*/
protected void sendCreateSession(String sessionId, DeltaSession session) {
if(cluster.getMembers().length > 0 ) {
SessionMessage msg =
new SessionMessageImpl(getName(),
SessionMessage.EVT_SESSION_CREATED,
null,
sessionId,
sessionId + "-" + System.currentTimeMillis());
if (log.isDebugEnabled()) log.debug(sm.getString("deltaManager.sendMessage.newSession",name, sessionId));
msg.setTimestamp(session.getCreationTime());
counterSend_EVT_SESSION_CREATED++;
send(msg);
}
}
/**
* Send messages to other backup member (domain or all)
* @param msg Session message
*/
protected void send(SessionMessage msg) {
if(cluster != null) {
if(doDomainReplication())
cluster.sendClusterDomain(msg);
else
cluster.send(msg);
}
}
/**
* Create DeltaSession
* @see org.apache.catalina.Manager#createEmptySession()
*/
public Session createEmptySession() {
return getNewDeltaSession() ;
}
/**
* Get new session class to be used in the doLoad() method.
*/
protected DeltaSession getNewDeltaSession() {
return new DeltaSession(this);
}
/**
* Load Deltarequest from external node
* Load the Class at container classloader
* @see DeltaRequest#readExternal(java.io.ObjectInput)
* @param session
* @param data message data
* @return The request
* @throws ClassNotFoundException
* @throws IOException
*/
protected DeltaRequest deserializeDeltaRequest(DeltaSession session, byte[] data) throws ClassNotFoundException, IOException {
ReplicationStream ois = getReplicationStream(data);
session.getDeltaRequest().readExternal(ois);
ois.close();
return session.getDeltaRequest();
}
/**
* serialize DeltaRequest
* @see DeltaRequest#writeExternal(java.io.ObjectOutput)
*
* @param deltaRequest
* @return serialized delta request
* @throws IOException
*/
protected byte[] serializeDeltaRequest(DeltaRequest deltaRequest) throws IOException {
return deltaRequest.serialize();
}
/**
* Load sessions from other cluster node.
* FIXME replace currently sessions with same id without notifcation.
* FIXME SSO handling is not really correct with the session replacement!
* @exception ClassNotFoundException
* if a serialized class cannot be found during the reload
* @exception IOException
* if an input/output error occurs
*/
protected void deserializeSessions(byte[] data) throws ClassNotFoundException,IOException {
// Initialize our internal data structures
//sessions.clear(); //should not do this
// Open an input stream to the specified pathname, if any
ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
ObjectInputStream ois = null;
// Load the previously unloaded active sessions
try {
ois = getReplicationStream(data);
Integer count = (Integer) ois.readObject();
int n = count.intValue();
for (int i = 0; i < n; i++) {
DeltaSession session = (DeltaSession) createEmptySession();
session.readObjectData(ois);
session.setManager(this);
session.setValid(true);
session.setPrimarySession(false);
//in case the nodes in the cluster are out of
//time synch, this will make sure that we have the
//correct timestamp, isValid returns true, cause
// accessCount=1
session.access();
//make sure that the session gets ready to expire if
// needed
session.setAccessCount(0);
session.resetDeltaRequest();
// FIXME How inform other session id cache like SingleSignOn
// increment sessionCounter to correct stats report
if (findSession(session.getIdInternal()) == null ) {
sessionCounter++;
} else {
sessionReplaceCounter++;
// FIXME better is to grap this sessions again !
if (log.isWarnEnabled()) log.warn(sm.getString("deltaManager.loading.existing.session",session.getIdInternal()));
}
add(session);
}
} catch (ClassNotFoundException e) {
log.error(sm.getString("deltaManager.loading.cnfe", e), e);
throw e;
} catch (IOException e) {
log.error(sm.getString("deltaManager.loading.ioe", e), e);
throw e;
} finally {
// Close the input stream
try {
if (ois != null) ois.close();
} catch (IOException f) {
// ignored
}
ois = null;
if (originalLoader != null) Thread.currentThread().setContextClassLoader(originalLoader);
}
}
/**
* Save any currently active sessions in the appropriate persistence
* mechanism, if any. If persistence is not supported, this method returns
* without doing anything.
*
* @exception IOException
* if an input/output error occurs
*/
protected byte[] serializeSessions(Session[] currentSessions) throws IOException {
// Open an output stream to the specified pathname, if any
ByteArrayOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject(new Integer(currentSessions.length));
for(int i=0 ; i < currentSessions.length;i++) {
((DeltaSession)currentSessions[i]).writeObjectData(oos);
}
// Flush and close the output stream
oos.flush();
} catch (IOException e) {
log.error(sm.getString("deltaManager.unloading.ioe", e), e);
throw e;
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException f) {
;
}
oos = null;
}
}
// send object data as byte[]
return fos.toByteArray();
}
// ------------------------------------------------------ Lifecycle Methods
/**
* Add a lifecycle event listener to this component.
*
* @param listener
* The listener to add
*/
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}
/**
* Get the lifecycle listeners associated with this lifecycle. If this
* Lifecycle has no listeners registered, a zero-length array is returned.
*/
public LifecycleListener[] findLifecycleListeners() {
return lifecycle.findLifecycleListeners();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -