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.services;
028:
029: import java.util.*;
030: import java.lang.*;
031:
032: final class SystemServiceManagerImpl extends SystemServiceManager {
033:
034: /** Registered service entries */
035: private Hashtable serviceEntries = null;
036:
037: SystemServiceManagerImpl() {
038: serviceEntries = new Hashtable();
039: }
040:
041: class SystemServiceEntry {
042: boolean isStarted = false;
043: SystemService service = null;
044:
045: SystemServiceEntry(SystemService service) {
046: this .isStarted = false;
047: this .service = service;
048: }
049: }
050:
051: synchronized public void registerService(SystemService service) {
052: addService(service);
053: }
054:
055: synchronized public SystemService getService(String serviceID) {
056: SystemServiceEntry entry = (SystemServiceEntry) serviceEntries
057: .get(serviceID);
058:
059: if (entry == null) {
060: return null;
061: }
062:
063: if (!entry.isStarted) {
064: entry.service.start();
065: entry.isStarted = true;
066: }
067:
068: return entry.service;
069: }
070:
071: public void shutdown() {
072: removeAllServices();
073: }
074:
075: private void addService(SystemService service) {
076: removeService(service.getServiceID());
077:
078: SystemServiceEntry entry = new SystemServiceEntry(service);
079: serviceEntries.put(service.getServiceID(), entry);
080: }
081:
082: private void removeService(String serviceID) {
083: SystemServiceEntry entry = (SystemServiceEntry) serviceEntries
084: .get(serviceID);
085:
086: if (entry == null) {
087: return;
088: }
089:
090: if (entry.isStarted) {
091: entry.service.stop();
092: entry.isStarted = false;
093: }
094:
095: serviceEntries.remove(serviceID);
096: }
097:
098: private void removeAllServices() {
099: Enumeration e = serviceEntries.keys();
100: for (; e.hasMoreElements();) {
101: String serviceID = (String) e.nextElement();
102: removeService(serviceID);
103: }
104: }
105: }
|