001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.session;
025:
026: import jacareto.system.Environment;
027: import jacareto.system.EnvironmentMember;
028: import jacareto.toolkit.JacaretoSystemClassLoader;
029: import jacareto.toolkit.StringToolkit;
030:
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: /**
035: * The list of all sessions.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.02
039: */
040: public class SessionList extends EnvironmentMember {
041: /** The vector for the sessions. */
042: private Vector sessions;
043:
044: /** The vector of the session list listeners. */
045: private Vector sessionListListeners;
046:
047: /** The index of the actual session. */
048: private int actual;
049:
050: /** Counts the sessions which have been added since the creation of the session list. */
051: private int sessionCounter;
052:
053: /**
054: * Creates a new session list.
055: *
056: * @param env the environment.
057: */
058: public SessionList(Environment env) {
059: super (env);
060:
061: sessions = new Vector(5);
062: sessionListListeners = new Vector(5);
063: actual = -1;
064: }
065:
066: /**
067: * Adds a session to the session list. This session will be the actual session after it has
068: * been added.
069: *
070: * @param session the session to add.
071: */
072: public void add(Session session) {
073: sessions.add(session);
074: sessionCounter++;
075: fireSessionListChange(new SessionListEvent(env,
076: SessionListEvent.SESSION_ADDED, session, size() - 1));
077: setActual(session);
078: }
079:
080: /**
081: * Returns the number of sessions in the session list.
082: *
083: * @return DOCUMENT ME!
084: */
085: public int size() {
086: return sessions.size();
087: }
088:
089: /**
090: * Returns the session with the given index.
091: *
092: * @param i the index of the session
093: *
094: * @return the session with the index <code>i</code>, or <code>null</code> if the specified
095: * index is invalid
096: */
097: public Session get(int i) {
098: if ((0 <= i) && (i < sessions.size())) {
099: return (Session) sessions.get(i);
100: } else {
101: getLogger().error(
102: getLanguage().getString(
103: "CleverPHL.SessionList.Error.InvalidIndex")
104: + " " + i);
105:
106: return null;
107: }
108: }
109:
110: /**
111: * Returns the session with the given name.
112: *
113: * @param name the name of the session
114: *
115: * @return the session with that name, or <code>null</code> if there is no open session with
116: * the given name
117: */
118: public Session get(String name) {
119: Iterator it = iterator();
120:
121: while (it.hasNext()) {
122: Session session = (Session) it.next();
123:
124: if (StringToolkit.areEqual(session.getName(), name)) {
125: return session;
126: }
127: }
128:
129: return null;
130: }
131:
132: /**
133: * Sets the actual session.
134: *
135: * @param i the index of the session which should be the actual one
136: */
137: public void setActual(int i) {
138: if ((0 <= i) && (i < sessions.size())) {
139: if ((actual != -1) && (actual < sessions.size())) {
140: getActual().setActive(false);
141: }
142:
143: actual = i;
144:
145: Session newActiveSession = get(i);
146: newActiveSession.setActive(true);
147:
148: JacaretoSystemClassLoader systemClassLoader = getEnvironment()
149: .getJacaretoSystemClassLoader();
150:
151: if (systemClassLoader != null) {
152: systemClassLoader.setActiveIdentifier(newActiveSession
153: .getTargetApplicationClassLoader()
154: .getIdentifier());
155: }
156:
157: fireSessionListChange(new SessionListEvent(env,
158: SessionListEvent.NEW_ACTUAL_SESSION, get(i), i));
159: getLogger().debug(
160: getLanguage().getString(
161: "CleverPHL.SessionList.Msg.ActualSession")
162: + " " + get(i).getName());
163: } else {
164: getLogger().error(
165: getLanguage().getString(
166: "CleverPHL.SessionList.Error.InvalidIndex")
167: + " " + i);
168: }
169: }
170:
171: /**
172: * Sets the actual session specified by its name
173: *
174: * @param sessionName the name of the session
175: */
176: public void setActual(String sessionName) {
177: for (int i = 0; i < sessions.size(); i++) {
178: Session tmp = (Session) sessions.get(i);
179:
180: if (tmp.getName().equals(sessionName)) {
181: setActual(i);
182:
183: return;
184: }
185: }
186: }
187:
188: /**
189: * Sets the actual session. The specified session must be contained in the list.
190: *
191: * @param session the new actual session
192: */
193: public void setActual(Session session) {
194: if (contains(session)) {
195: setActual(sessions.indexOf(session));
196: } else {
197: getLogger()
198: .error(
199: getLanguage()
200: .getString(
201: "CleverPHL.SessionList.Error.InvalidSession")
202: + " " + session.getName());
203: }
204: }
205:
206: /**
207: * Removes the session with the specified index. The session will be closed before.
208: *
209: * @param i session's index
210: */
211: public void remove(int i) {
212: if ((0 <= i) && (i < sessions.size())) {
213: Session session = get(i);
214: boolean sessionClosed = session.close();
215:
216: if (sessionClosed) {
217: sessions.remove(i);
218:
219: JacaretoSystemClassLoader systemClassLoader = getEnvironment()
220: .getJacaretoSystemClassLoader();
221:
222: if (systemClassLoader != null) {
223: systemClassLoader.removeChildren(session
224: .getTargetApplicationClassLoader()
225: .getIdentifier());
226: }
227:
228: fireSessionListChange(new SessionListEvent(env,
229: SessionListEvent.SESSION_REMOVED, session, i));
230:
231: if (isEmpty()) {
232: actual = -1;
233: } else if (i == size()) {
234: setActual(i - 1);
235: } else {
236: setActual(i);
237: }
238: }
239: } else {
240: getLogger().error(
241: getLanguage().getString(
242: "CleverPHL.SessionList.Error.InvalidIndex")
243: + " " + i);
244: }
245: }
246:
247: /**
248: * Removes the actual session (closes it before).
249: */
250: public void removeActual() {
251: if (!isEmpty()) {
252: remove(actual);
253: }
254: }
255:
256: /**
257: * Returns the number of sessions which have been added since the creation of the session list.
258: * This method does not return the number of sessions in this list; therefore you have to call
259: * the method {@link #size()}.
260: *
261: * @return DOCUMENT ME!
262: */
263: public int getSessionCounter() {
264: return sessionCounter;
265: }
266:
267: /**
268: * Returns whether or not the session list contains the specified session.
269: *
270: * @param session DOCUMENT ME!
271: *
272: * @return <code>true</code> if the session list contains the specified session, otherwise
273: * <code>false</code>
274: */
275: public boolean contains(Session session) {
276: return sessions.contains(session);
277: }
278:
279: /**
280: * Returns whether or not the session list contains a session with the specified name.
281: *
282: * @param sessionName DOCUMENT ME!
283: *
284: * @return DOCUMENT ME!
285: */
286: public boolean contains(String sessionName) {
287: Iterator it = sessions.iterator();
288:
289: while (it.hasNext()) {
290: Session tmp = (Session) it.next();
291:
292: if (tmp.getName().equals(sessionName)) {
293: return true;
294: }
295: }
296:
297: return false;
298: }
299:
300: /**
301: * Returns an iterator on the sessions
302: *
303: * @return the iterator
304: */
305: public Iterator iterator() {
306: return sessions.iterator();
307: }
308:
309: /**
310: * Returns the actual session. return the actual session, or <code>null</code> if the session
311: * list is empty.
312: *
313: * @return DOCUMENT ME!
314: */
315: public Session getActual() {
316: if (!isEmpty()) {
317: return get(actual);
318: } else {
319: return null;
320: }
321: }
322:
323: /**
324: * Returns whether there are sessions in the session list or not.
325: *
326: * @return <code>true</code> if there are no sessions in the session list, otherwise
327: * <code>false</code>
328: */
329: public boolean isEmpty() {
330: return sessions.isEmpty();
331: }
332:
333: /**
334: * Adds a session list listener to the list of listeners. All listeners will be notified when
335: * this session list changes.
336: *
337: * @param listener the listener to add
338: */
339: public void addSessionListListener(SessionListListener listener) {
340: sessionListListeners.add(listener);
341: }
342:
343: /**
344: * Removes a session list listener from the list of listeners.
345: *
346: * @param listener the listener to remove
347: */
348: public void removeSessionListListener(SessionListListener listener) {
349: sessionListListeners.remove(listener);
350: }
351:
352: /**
353: * Fires a session list change to all listeners.
354: *
355: * @param event the event which contains the information of the change
356: */
357: protected void fireSessionListChange(SessionListEvent event) {
358: Iterator i = sessionListListeners.iterator();
359:
360: while (i.hasNext()) {
361: ((SessionListListener) i.next()).sessionListChanged(event);
362: }
363: }
364: }
|