001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jumpimpl.module.lifecycle;
026:
027: import com.sun.jump.executive.JUMPIsolateProxy;
028: import com.sun.jump.executive.JUMPExecutive;
029: import com.sun.jump.executive.JUMPIsolateFactory;
030: import com.sun.jump.module.lifecycle.JUMPApplicationLifecycleModule;
031: import com.sun.jump.common.JUMPApplication;
032: import com.sun.jump.executive.JUMPApplicationProxy;
033: import java.util.Collection;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: public class OneLiveOnlyApplicationLifecycleModule implements
038: JUMPApplicationLifecycleModule {
039:
040: private final HashMap mapAppToAppProxy = new HashMap();
041:
042: //
043: // This is called on first use -- extract any configuration
044: // information here.
045: // Examples could be -- maximum number of instances allowed, etc.
046: //
047: public void load(Map config) {
048: }
049:
050: public void unload() {
051: mapAppToAppProxy.clear();
052: }
053:
054: /*
055: * Launch or return a running application for an installed
056: * application, according to the lifecycle policy for this
057: * lifecycle module.
058: */
059: public JUMPApplicationProxy launchApplication(JUMPApplication app,
060: String args[]) {
061: // This implementation allows for one live instance only.
062: // Either create it, or return existing instance.
063: synchronized (this ) {
064:
065: if (app == null) {
066: return null;
067: }
068:
069: // launch or return existing proxy
070: // Maintain map of
071: // JUMPApplication to existing running
072: // JUMPApplicationProxy.
073: //
074: JUMPApplicationProxy proxy = (JUMPApplicationProxy) mapAppToAppProxy
075: .get(app);
076:
077: if (proxy == null) {
078: // Get a handle to the isolate manager.
079: JUMPIsolateFactory ism = JUMPExecutive.getInstance()
080: .getIsolateFactory();
081:
082: // Create isolate, which returns JUMPIsolateProxy
083: JUMPIsolateProxy ip = ism.newIsolate(app.getAppType());
084:
085: // Create JUMPApplicationProxy by calling startApp()
086: // on the JUMPIsolateProxy
087: proxy = ip.startApp(app, args);
088:
089: // ...
090: // And put returned app proxy in map
091: mapAppToAppProxy.put(app, proxy);
092: }
093:
094: return proxy;
095: }
096: }
097:
098: /**
099: * Returns any running instances of a given JUMPApplication.
100: * Can be found by iterating over mapAppToAppProxy.
101: */
102: public JUMPApplicationProxy[] getApplications(JUMPApplication app) {
103: JUMPApplicationProxy proxy[] = new JUMPApplicationProxy[1];
104: proxy[0] = (JUMPApplicationProxy) mapAppToAppProxy.get(app);
105: if (proxy[0] == null) {
106: return null;
107: } else {
108: return proxy;
109: }
110: }
111:
112: /**
113: * Returns any running application instances for this lifecycle module
114: * Can be found by iterating over mapAppToAppProxy.
115: */
116: public JUMPApplicationProxy[] getApplications() {
117: Collection v = mapAppToAppProxy.values();
118: if (v == null) {
119: return null;
120: } else {
121: return (JUMPApplicationProxy[]) v
122: .toArray(new JUMPApplicationProxy[] {});
123: }
124: }
125: }
|