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 {
for (Enumeration e = props.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement ();
boolean self = "self".equalsIgnoreCase (props.getProperty (appName));
if (applications.get (appName) == null) {
start (appName);
register (appName);
start (appName, self);
register (appName, self);
}
}
// 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);
try {
Application app = new Application (appName, hopHome, Server.sysProps, Server.dbProps);
applications.put (appName, app);
// 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));
// 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 ();
} catch (Exception x) {
Server.getLogger().log ("Error creating application "+appName+": "+x);
@ -101,7 +105,7 @@ public class ApplicationManager {
applications.remove (appName);
}
private void register (String appName) {
private void register (String appName, boolean self) {
try {
Server.getLogger().log ("Binding application "+appName);
Application app = (Application) applications.get (appName);
@ -110,8 +114,12 @@ public class ApplicationManager {
Naming.rebind ("//:"+port+"/"+appName, app);
} else {
AcmeServletClient servlet = new AcmeServletClient (app);
server.websrv.addServlet ("/"+appName+"/", servlet);
server.websrv.addServlet ("/"+appName+"/*", servlet);
if (self)
server.websrv.setDefaultServlet (servlet);
else {
server.websrv.addServlet ("/"+appName+"/", servlet);
server.websrv.addServlet ("/"+appName+"/*", servlet);
}
}
app.start ();
} catch (Exception x) {
@ -123,11 +131,13 @@ public class ApplicationManager {
try {
for (Enumeration e = props.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement ();
start (appName);
boolean self = "self".equalsIgnoreCase (props.getProperty (appName));
start (appName, self);
}
for (Enumeration e = props.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement ();
register (appName);
boolean self = "self".equalsIgnoreCase (props.getProperty (appName));
register (appName, self);
}
if (server.websrv != null) {
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.registry.*;
import java.net.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.Locale;
import helma.objectmodel.*;
import java.util.*;
// import helma.objectmodel.*;
import helma.objectmodel.db.DbSource;
import helma.framework.*;
import helma.framework.core.*;
@ -25,7 +22,7 @@ import com.sleepycat.db.*;
* Helma server main class.
*/
public class Server implements Runnable {
public class Server {
public static boolean useTransactions = true;
@ -179,10 +176,17 @@ import com.sleepycat.db.*;
// nmgr = new NodeManager (this, sysProps);
mainThread = new Thread (this);
mainThread.start ();
// Start running, finishing setup and then entering a loop to check changes
// 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 () {
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 () {
if (logger == null) {
String logDir = sysProps.getProperty ("logdir");
@ -283,15 +297,23 @@ import com.sleepycat.db.*;
return logger;
}
/**
* Get the Home directory of this server.
*/
public static File getHopHome () {
return hopHome;
}
/**
* Get the Server's XML-RPC web server.
*/
public static WebServer getXmlRpcServer() {
return xmlrpc;
}
/**
* A primitive method to check whether a server is already running on our port.
*/
private void checkRunning () throws Exception {
try {
java.net.Socket socket = new java.net.Socket ("localhost", port);