00001
00004 package cast.server;
00005
00006 import java.io.File;
00007 import java.net.MalformedURLException;
00008 import java.util.Properties;
00009
00010 import org.apache.log4j.PropertyConfigurator;
00011
00012 import Ice.Application;
00013 import Ice.Communicator;
00014 import Ice.Identity;
00015 import Ice.ObjectAdapter;
00016 import Ice.SignalPolicy;
00017 import cast.cdl.CASTRELEASESTRING;
00018 import cast.cdl.JAVASERVERPORT;
00019 import cast.cdl.LOGGINGENVVAR;
00020 import cast.cdl.LOGGINGPROPERTIESFILE;
00021 import cast.core.logging.ComponentLayout;
00022 import cast.core.logging.ComponentLogger;
00023 import cast.core.logging.LogAdditions;
00024
00029 public class ComponentServer extends Application {
00030
00031 static {
00032 initLogging();
00033 }
00034
00049 private static void initLogging() {
00050
00051
00052 if (configureFromEnvironment()) {
00053 return;
00054 } else if (configureFromCWD()) {
00055 return;
00056
00057 } else if (configureFromHome()) {
00058 return;
00059 } else {
00060 configureDefault();
00061 }
00062 }
00063
00064 private static void configureDefault() {
00065 Properties prop = new Properties();
00066 prop.put("log4j.rootLogger", "INFO, stdout");
00067 prop.put("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
00068 prop.put("log4j.appender.stdout.layout",
00069 ComponentLayout.class.getName());
00070 PropertyConfigurator.configure(prop);
00071 }
00072
00077 private static boolean configureFromEnvironment() {
00078 String propertiesFileString = System.getenv(LOGGINGENVVAR.value);
00079 if (propertiesFileString != null) {
00080 return configureFromFile(propertiesFileString);
00081 }
00082 return false;
00083 }
00084
00085 private static boolean configureFromCWD() {
00086 String propertiesFileString = "./" + LOGGINGPROPERTIESFILE.value;
00087 return configureFromFile(propertiesFileString);
00088 }
00089
00090 private static boolean configureFromHome() {
00091 String propertiesFileString = System.getProperty("user.home") + "/."
00092 + LOGGINGPROPERTIESFILE.value;
00093 return configureFromFile(propertiesFileString);
00094 }
00095
00101 private static boolean configureFromFile(String _filename) {
00102 try {
00103
00104 File propertiesFile = new File(_filename);
00105 if (propertiesFile.exists()) {
00106
00107 PropertyConfigurator.configure(propertiesFile.toURI().toURL());
00108 return true;
00109 } else {
00110
00111
00112
00113
00114 }
00115 } catch (MalformedURLException e) {
00116
00117
00118 }
00119 return false;
00120 }
00121
00122 private ComponentLogger m_logger;
00123 private LogAdditions m_logAddy;
00124
00128 public ComponentServer() {
00129 m_logger = ComponentLogger
00130 .getLogger("cast.server.java.ComponentServer");
00131 m_logAddy = new LogAdditions("cast.server.java.ComponentServer", "");
00132 }
00133
00137 public ComponentServer(SignalPolicy _signalPolicy) {
00138 super(_signalPolicy);
00139 }
00140
00141 private void info(Object _o) {
00142 m_logger.info(_o, m_logAddy);
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 @Override
00160 public int run(String[] arg0) {
00161
00162 info("Java server: \"" + CASTRELEASESTRING.value + "\"");
00163
00164 Communicator ic = communicator();
00165 ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints(
00166 "ComponentServer1", "default -p " + JAVASERVERPORT.value);
00167
00168 Identity id = new Identity("ComponentFactory", "ComponentFactory");
00169 adapter.add(new CASTComponentFactory(), id);
00170
00171 Identity manid = new Identity("comp.man",
00172 CASTComponentManager.class.getCanonicalName());
00173 adapter.add(new CASTComponentManager(), manid);
00174
00175
00176 adapter.activate();
00177 adapter.waitForDeactivate();
00178 return 0;
00179 }
00180
00184 public static void main(String[] args) {
00185 ComponentServer app = new ComponentServer();
00186 int status = app.main("cast.server.ComponentServer", args);
00187 System.exit(status);
00188 }
00189 }