001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.suspend;
028:
029: import com.sun.midp.security.ImplicitlyTrustedClass;
030: import com.sun.midp.security.SecurityToken;
031: import com.sun.midp.security.SecurityInitializer;
032:
033: import java.util.Enumeration;
034: import java.util.Vector;
035:
036: /**
037: * An abstract subsystem that may contain other subsystems.
038: * Contained subsytems are suspended after this subsystem suspend
039: * and resumed prior to the subsystem resuming.
040: *
041: */
042: public abstract class AbstractSubsystem implements Subsystem {
043: /** Class registered in SecurityInitializer. */
044: private static class SecurityTrusted implements
045: ImplicitlyTrustedClass {
046: }
047:
048: /**
049: * Security token for provileged access to internal API's.
050: * Note it must stay package private.
051: */
052: static SecurityToken classSecurityToken = SecurityInitializer
053: .requestToken(new SecurityTrusted());
054:
055: /** State transition synchronization lock. */
056: final Object lock = new Object();
057:
058: /** Current subsystem state. */
059: int state = SUSPENDED;
060:
061: /** Subsystems this one depends on. */
062: private final Vector subsystems = new Vector();
063:
064: /**
065: * A set of suspend dependencies. System is considered to be suspended
066: * when all the subsystems are suspended and all the dependencies
067: * removed.
068: */
069: private final Vector dependencies = new Vector(4, 2);
070:
071: /**
072: * Returns the current state.
073: * @return current state if the subsystem
074: */
075: public final int getState() {
076: return state;
077: }
078:
079: /**
080: * Registers a subsystem this one depends on.
081: * @param s subsystem this one depends on.
082: */
083: public void registerSubsystem(Subsystem s) {
084: synchronized (subsystems) {
085: if (!subsystems.contains(s)) {
086: subsystems.addElement(s);
087: }
088: }
089: }
090:
091: /**
092: * Unregisters a subsystem this one depends on.
093: * @param s subsystem this one depends on.
094: */
095: public void unregisterSubsystem(Subsystem s) {
096: synchronized (subsystems) {
097: subsystems.removeElement(s);
098: }
099: }
100:
101: /**
102: * If the current state is <code>SUSPENDED</code>, changes the state to
103: * <code>RESUMING</code>, performs object-specific activation
104: * operations and then changes the state to <code>ACTIVE</code>.
105: */
106: public final void resume() throws StateTransitionException {
107: synchronized (lock) {
108: if (state == SUSPENDED) {
109: state = RESUMING;
110:
111: Enumeration subs = subsystems.elements();
112: while (subs.hasMoreElements()) {
113: ((Subsystem) subs.nextElement()).resume();
114: }
115:
116: resumeImpl();
117: state = ACTIVE;
118: resumed();
119: }
120: }
121: }
122:
123: /**
124: * If the current state is <code>ACTIVE</code>, changes the state to
125: * <code>SUSPENDING</code>, and initiates suspend routine. The suspend
126: * routine is pstponed until all suspend dependences are removed.
127: * If there are no dependencies currently, the suspend routine is
128: * invoked immediately.
129: */
130: public void suspend() throws StateTransitionException {
131: synchronized (lock) {
132: if (state == ACTIVE) {
133: state = SUSPENDING;
134: updateSuspendStatus();
135: }
136: }
137: }
138:
139: /**
140: * Adds a dependency that prevents from system suspend.
141: * @param dep dependency to add
142: */
143: public void addSuspendDependency(SuspendDependency dep) {
144: synchronized (lock) {
145: if (!dependencies.contains(dep)) {
146: dependencies.addElement(dep);
147: }
148: }
149:
150: }
151:
152: /**
153: * Removes dependency that does not prevent from system suspend any more.
154: * Then invokes suspend notification if there are no dependencies left.
155: * @param dep dependency to remove
156: */
157: public void removeSuspendDependency(SuspendDependency dep) {
158: synchronized (lock) {
159: dependencies.removeElement(dep);
160: updateSuspendStatus();
161: }
162: }
163:
164: /**
165: * Checks if there are dependencies that prevent from system suspend,
166: * if there are no ones, and the state is SUSPENDING suspend routine.
167: * The suspend routine performs first object-specific ations then
168: * invokes suspend() methods for all registered subsytems.
169: */
170: protected void updateSuspendStatus() {
171: synchronized (lock) {
172: if (state == SUSPENDING && 0 == dependencies.size()) {
173: suspendImpl();
174:
175: Enumeration subs = subsystems.elements();
176: while (subs.hasMoreElements()) {
177: ((Subsystem) subs.nextElement()).suspend();
178: }
179:
180: state = SUSPENDED;
181: suspended();
182: }
183: }
184: }
185:
186: /**
187: * Performs object-specific activation operations.
188: * Default implementation makes nothing.
189: */
190: protected void resumeImpl() {
191: }
192:
193: /**
194: * Performs object-specific suspend operations.
195: * Default implementation makes nothing.
196: */
197: protected void suspendImpl() {
198: }
199:
200: /**
201: * Confirms subsystem has been suspended. Listeners/waiters
202: * can be invoked here.
203: */
204: void suspended() {
205: }
206:
207: /**
208: * Confirms subsystem has been resumed. Listeners/waiters
209: * can be invoked here.
210: */
211: void resumed() {
212: }
213: }
|