001: /**
002: *
003: */package clime.messadmin.core;
004:
005: import java.util.Set;
006:
007: import clime.messadmin.model.Application;
008: import clime.messadmin.model.Server;
009: import clime.messadmin.model.Session;
010:
011: /**
012: * Manages actions on WebApp and Sessions. Used by admin servlet and JMX.
013: * @author Cédrik LIME
014: */
015: public class MessAdmin {
016:
017: private MessAdmin() {
018: super ();
019: }
020:
021: public static String getVersion() {
022: String result = "";//$NON-NLS-1$
023: Package p = MessAdmin.class.getPackage();
024: if (p != null) {
025: result = p.getImplementationVersion();
026: }
027: return result;
028: }
029:
030: /**
031: * If message if blank or null, remove ServletContext attribute, otherwise inject message into ServletContext
032: * @param applicationIds
033: * @param message
034: * @return number of modified sessions
035: */
036: public static int injectApplicationsPermanent(
037: String[] applicationIds, String message) {
038: if (null == applicationIds) {
039: return 0;
040: }
041: int nbAffectedApplications = 0;
042: for (int i = 0; i < applicationIds.length; ++i) {
043: String applicationId = applicationIds[i];
044: Application application = Server.getInstance()
045: .getApplication(applicationId);
046: if (null == application) {
047: // Shouldn't happen, but let's play nice...
048: //log("WARNING: can't inject message for null application " + applicationId);
049: continue;
050: }
051: boolean actionDone = application
052: .injectPermanentMessage(message);
053: if (actionDone) {
054: ++nbAffectedApplications;
055: }
056: }
057: return nbAffectedApplications;
058: }
059:
060: /**
061: * If message if blank or null, remove ServletContext attribute, otherwise inject message into ServletContext
062: * @param applicationIds
063: * @param message
064: * @return number of modified sessions
065: */
066: public static int injectApplicationsOnce(String[] applicationIds,
067: String message) {
068: if (null == applicationIds) {
069: return 0;
070: }
071: int nbAffectedApplications = 0;
072: for (int i = 0; i < applicationIds.length; ++i) {
073: String applicationId = applicationIds[i];
074: int result = injectAllSessions(applicationId, message);
075: if (result > 0) {
076: ++nbAffectedApplications;
077: }
078: }
079: return nbAffectedApplications;
080: }
081:
082: /**
083: * If message if blank or null, remove HttpSession attribute, otherwise inject message into HttpSessions
084: * @param message
085: * @return number of modified sessions
086: */
087: public static int injectAllSessions(String context, String message) {
088: Set/*<String>*/activeSessionIds = Server.getInstance()
089: .getApplication(context).getActiveSessionsIds();
090: String[] sessionIds = new String[activeSessionIds.size()];
091: sessionIds = (String[]) activeSessionIds.toArray(sessionIds);
092: return injectSessions(context, sessionIds, message);
093: }
094:
095: /**
096: * If message if blank or null, remove HttpSession attribute, otherwise inject message into HttpSessions
097: * @param sessionIds
098: * @param message
099: * @return number of modified sessions
100: */
101: public static int injectSessions(String context,
102: String[] sessionIds, String message) {
103: if (null == sessionIds) {
104: return 0;
105: }
106: int nbAffectedSessions = 0;
107: Application application = Server.getInstance().getApplication(
108: context);
109: for (int i = 0; i < sessionIds.length; ++i) {
110: String sessionId = sessionIds[i];
111: Session session = application.getSession(sessionId);
112: if (null == session) {
113: // Shouldn't happen, but let's play nice...
114: //log("WARNING: can't inject message for null session " + sessionId);
115: continue;
116: }
117: boolean actionDone = session.injectMessage(message);
118: if (actionDone) {
119: ++nbAffectedSessions;
120: }
121: }
122: return nbAffectedSessions;
123: }
124:
125: /**
126: * Invalidate HttpSessions
127: * @param sessionIds
128: * @return number of invalidated sessions
129: */
130: public static int invalidateSessions(String context,
131: String[] sessionIds) {
132: if (null == sessionIds) {
133: return 0;
134: }
135: int nbAffectedSessions = 0;
136: Application application = Server.getInstance().getApplication(
137: context);
138: for (int i = 0; i < sessionIds.length; ++i) {
139: String sessionId = sessionIds[i];
140: Session session = application.getSession(sessionId);
141: if (null == session) {
142: // Shouldn't happen, but let's play nice...
143: //log("WARNING: can't invalidate null session " + sessionId);
144: continue;
145: }
146: try {
147: session.getSessionInfo().invalidate();
148: ++nbAffectedSessions;
149: //log("Invalidating session id " + sessionId);
150: } catch (IllegalStateException ise) {
151: //log("Can't invalidate already invalidated session id " + sessionId);
152: }
153: }
154: return nbAffectedSessions;
155: }
156:
157: /**
158: * Removes an attribute from an HttpSession
159: * @param sessionId
160: * @param attributeName
161: * @return true if there was an attribute removed, false otherwise
162: */
163: public static boolean removeSessionAttribute(String context,
164: String sessionId, String attributeName) {
165: Session session = Server.getInstance().getApplication(context)
166: .getSession(sessionId);
167: if (null == session) {
168: // Shouldn't happen, but let's play nice...
169: //log("WARNING: can't remove attribute '" + attributeName + "' for null session " + sessionId);
170: return false;
171: }
172: boolean wasPresent = false;
173: try {
174: wasPresent = (null != session.getSessionInfo()
175: .getAttribute(attributeName));
176: session.getSessionInfo().removeAttribute(attributeName);
177: } catch (IllegalStateException ise) {
178: //log("Can't remote attribute '" + attributeName + "' for invalidated session id " + session.getId());
179: }
180: return wasPresent;
181: }
182:
183: /**
184: * Sets the maximum inactive interval (session timeout) an HttpSession
185: * @param sessionId
186: * @param maxInactiveInterval in seconds
187: * @return old value for maxInactiveInterval
188: */
189: public static int setSessionMaxInactiveInterval(String context,
190: String sessionId, int maxInactiveInterval) {
191: Session session = Server.getInstance().getApplication(context)
192: .getSession(sessionId);
193: if (null == session) {
194: // Shouldn't happen, but let's play nice...
195: //log("WARNING: can't set timout for null session " + sessionId);
196: return 0;
197: }
198: try {
199: int oldMaxInactiveInterval = session.getSessionInfo()
200: .getMaxInactiveInterval();
201: session.getSessionInfo().setMaxInactiveInterval(
202: maxInactiveInterval);
203: return oldMaxInactiveInterval;
204: } catch (IllegalStateException ise) {
205: // invalidated session
206: return 0;
207: }
208: }
209: }
|