Implemented a scheme to mark applications as scripting the Server

object itself. This is done by writing appname=self in the apps.properties,
which is probably a temporary solution, maybe it'll keep that way.
added getApplications() method to get an array of all applications.
This commit is contained in:
hns 2001-09-10 14:08:49 +00:00
parent 58e0306fb8
commit 7e1108e7c3
2 changed files with 58 additions and 19 deletions

View file

@ -47,9 +47,10 @@ public class ApplicationManager {
try { try {
for (Enumeration e = props.keys(); e.hasMoreElements (); ) { for (Enumeration e = props.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement (); String appName = (String) e.nextElement ();
boolean self = "self".equalsIgnoreCase (props.getProperty (appName));
if (applications.get (appName) == null) { if (applications.get (appName) == null) {
start (appName); start (appName, self);
register (appName); register (appName, self);
} }
} }
// then stop deleted ones // then stop deleted ones
@ -67,15 +68,18 @@ public class ApplicationManager {
} }
} }
private void start (String appName) { private void start (String appName, boolean self) {
Server.getLogger().log ("Building application "+appName); Server.getLogger().log ("Building application "+appName);
try { try {
Application app = new Application (appName, hopHome, Server.sysProps, Server.dbProps); Application app = new Application (appName, hopHome, Server.sysProps, Server.dbProps);
applications.put (appName, app); applications.put (appName, app);
// if we're running with the embedded web server, set app base uri to /appname // if we're running with the embedded web server, set app base uri to /appname
if (server.websrv != null) if (server.websrv != null && !self)
app.setBaseURI ("/"+java.net.URLEncoder.encode (appName)); app.setBaseURI ("/"+java.net.URLEncoder.encode (appName));
// the application is initialized later in the register method, when it's bound // check if the root object of the application is the Server itself
if (self)
app.setDataRoot (server);
// the application is started later in the register method, when it's bound
app.init (); app.init ();
} catch (Exception x) { } catch (Exception x) {
Server.getLogger().log ("Error creating application "+appName+": "+x); Server.getLogger().log ("Error creating application "+appName+": "+x);
@ -101,7 +105,7 @@ public class ApplicationManager {
applications.remove (appName); applications.remove (appName);
} }
private void register (String appName) { private void register (String appName, boolean self) {
try { try {
Server.getLogger().log ("Binding application "+appName); Server.getLogger().log ("Binding application "+appName);
Application app = (Application) applications.get (appName); Application app = (Application) applications.get (appName);
@ -110,9 +114,13 @@ public class ApplicationManager {
Naming.rebind ("//:"+port+"/"+appName, app); Naming.rebind ("//:"+port+"/"+appName, app);
} else { } else {
AcmeServletClient servlet = new AcmeServletClient (app); AcmeServletClient servlet = new AcmeServletClient (app);
if (self)
server.websrv.setDefaultServlet (servlet);
else {
server.websrv.addServlet ("/"+appName+"/", servlet); server.websrv.addServlet ("/"+appName+"/", servlet);
server.websrv.addServlet ("/"+appName+"/*", servlet); server.websrv.addServlet ("/"+appName+"/*", servlet);
} }
}
app.start (); app.start ();
} catch (Exception x) { } catch (Exception x) {
Server.getLogger().log ("Couldn't register and start app: "+x); Server.getLogger().log ("Couldn't register and start app: "+x);
@ -123,11 +131,13 @@ public class ApplicationManager {
try { try {
for (Enumeration e = props.keys(); e.hasMoreElements (); ) { for (Enumeration e = props.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement (); String appName = (String) e.nextElement ();
start (appName); boolean self = "self".equalsIgnoreCase (props.getProperty (appName));
start (appName, self);
} }
for (Enumeration e = props.keys(); e.hasMoreElements (); ) { for (Enumeration e = props.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement (); String appName = (String) e.nextElement ();
register (appName); boolean self = "self".equalsIgnoreCase (props.getProperty (appName));
register (appName, self);
} }
if (server.websrv != null) { if (server.websrv != null) {
File staticContent = new File (server.getHopHome(), "static"); File staticContent = new File (server.getHopHome(), "static");
@ -143,4 +153,11 @@ public class ApplicationManager {
} }
} }
/**
* Get an enumeration of all currently running applications.
*/
public Object[] getApplications () {
return applications.values ().toArray ();
}
} }

View file

@ -8,11 +8,8 @@ import java.rmi.*;
import java.rmi.server.*; import java.rmi.server.*;
import java.rmi.registry.*; import java.rmi.registry.*;
import java.net.*; import java.net.*;
import java.util.Hashtable; import java.util.*;
import java.util.StringTokenizer; // import helma.objectmodel.*;
import java.util.TimeZone;
import java.util.Locale;
import helma.objectmodel.*;
import helma.objectmodel.db.DbSource; import helma.objectmodel.db.DbSource;
import helma.framework.*; import helma.framework.*;
import helma.framework.core.*; import helma.framework.core.*;
@ -25,7 +22,7 @@ import com.sleepycat.db.*;
* Helma server main class. * Helma server main class.
*/ */
public class Server implements Runnable { public class Server {
public static boolean useTransactions = true; public static boolean useTransactions = true;
@ -179,10 +176,17 @@ import com.sleepycat.db.*;
// nmgr = new NodeManager (this, sysProps); // nmgr = new NodeManager (this, sysProps);
mainThread = new Thread (this); // Start running, finishing setup and then entering a loop to check changes
mainThread.start (); // in the apps.properties file.
mainThread = Thread.currentThread ();
run ();
} }
/**
* The main method of the Server. Basically, we set up Applications and than
* periodically check for changes in the apps.properties file, shutting down
* apps or starting new ones.
*/
public void run () { public void run () {
try { try {
@ -261,6 +265,16 @@ import com.sleepycat.db.*;
} }
/**
* Get an Iterator over the applications currently running on this Server.
*/
public Object[] getApplications () {
return appManager.getApplications ();
}
/**
* Get a logger to use for output in this server.
*/
protected static Logger getLogger () { protected static Logger getLogger () {
if (logger == null) { if (logger == null) {
String logDir = sysProps.getProperty ("logdir"); String logDir = sysProps.getProperty ("logdir");
@ -283,15 +297,23 @@ import com.sleepycat.db.*;
return logger; return logger;
} }
/**
* Get the Home directory of this server.
*/
public static File getHopHome () { public static File getHopHome () {
return hopHome; return hopHome;
} }
/**
* Get the Server's XML-RPC web server.
*/
public static WebServer getXmlRpcServer() { public static WebServer getXmlRpcServer() {
return xmlrpc; return xmlrpc;
} }
/**
* A primitive method to check whether a server is already running on our port.
*/
private void checkRunning () throws Exception { private void checkRunning () throws Exception {
try { try {
java.net.Socket socket = new java.net.Socket ("localhost", port); java.net.Socket socket = new java.net.Socket ("localhost", port);