/* * WRITE YOUR OWN SERVER CLASS * --------------------------- * * You can create your own HttpServer by copying the Run_HttpServer class (in the server package) that * is provided as part of the licas package into your own code project. You then need to include the * licas jar in your own library and import the licas classes of org.licas.server.* and org.licas.util.* * into your new server class. * * The code below is an example of how easy it is to write your own server class. If you then run the * server from your own code project you will automatically have access to all of your own java classes * for coding, etc., as well as the licas ones. The alternative is to start the server in the licas package * itself and remotely load in your own classes when needed, through the remote jar file loading feature. * This is much more complicated however and you then need to provide the URIs of any remote jar files that * need to be accessed and make them available (possibly over the internet). This is the solution for a * working system that is allowing others to load their own services onto it. For you own programs or testing, * a local solution is easier. * * If you then start a server running, using a batch file command for example, you can start your own server * instead of the one in the licas package and have automatic access to all of your own classes as well. * * * -------------------------------------------------------------------------------------------------- */ /* * Run_HttpServer.java * * This is an example of how to add the licas classes to your own project * and run a server to load your own services onto. * * Author: Kieran Greer */ package org.your_package.server; import org.licas.server.*; import org.licas.ServiceAdmin; import org.licas.meta.ServerConfig; import org.licas.util.*; import org.licas_xml.abs.Element; import org.jlog2.*; import java.util.*; /** * This class runs the HTTP server. You can use this class to start an {@link HttpServer} * object running and also load an {@link ESB} object onto it. This can also be * configured with command line arguments passed in as a String[]. The arguments * do not all need to be included, but must be added in the following order: * 1. Server port to run on. * 2. 'Yes' or 'No' https protocol indicator. Yes means {@code https}, no means {@code http}. * 3. Password for the server. * 4. Service key for the server. * 5. A contact address for the server administrator - email or something. * 6. 'Yes' or 'No' contract indicator. * 7. (Alternative) class name of the class to load as the ESB. If this is not included * then the default 'ESB' is loaded. If this is included then it should extend * the 'Service' class and have all of the required jar files, etc., locally available * for loading. * 8. An alternative representation of the IP address that the server will run on. * @author Kieran Greer */ public class Run_HttpServer { /** * Creates a new instance of Run_HttpServer. * @param args the arguments. * @throws java.lang.Exception any error. */ protected Run_HttpServer(String[] args) throws Exception { runServer(args); } /** * Start the HTTP server. * @param args initialisation argument - must be the server URL. * @throws java.lang.Exception any error. */ protected void runServer(String[] args) throws Exception { boolean asHttps; //true if as https address boolean yesContract; //true if add a yes contract int counter; //counter int port; //server port String adminKey; //the service key String password; //the user password String esbClass; //the ESB class String ipAddress; //the ip address to run the server on String contact; //server admin contact address ArrayList params; //parameters Element adminXml; //admin xml ServerConfig serverConfig; //server config info counter = 0; System.setProperty("java.net.preferIPv4Stack" , "true"); port = 8888; if (args.length > counter) { try { port = Integer.parseInt(args[counter]); } catch (Exception ex) { counter++; if (args.length > counter) { try { port = Integer.parseInt(args[counter]); } catch (Exception ex2) {} } } } counter++; if (args.length > counter) asHttps = args[counter].equalsIgnoreCase("yes"); else asHttps = false; counter++; if (args.length > counter) password = args[counter]; else password = Const.ANON; counter++; if (args.length > counter) adminKey = args[counter]; else adminKey = Const.ANON; counter++; if (args.length > counter) contact = args[counter]; else contact = null; counter++; if (args.length > counter) yesContract = args[counter].equalsIgnoreCase("yes"); else yesContract = true; counter++; if (args.length > counter) esbClass = args[counter]; else esbClass = null; counter++; if (args.length > counter) { ipAddress = args[counter]; } else { ipAddress = ServerAddress.getLocalHost4Address().getHostAddress(); } try { //create the log files LoggerFactory.setLoggerFactory(new CustomLoggerFactory()); new DefaultServiceFactory(UuidHandler.getUuid(25)); //create default admin document if (yesContract) adminXml = ServiceAdminHandler.createAdminOnlyContractYes(); else adminXml = ServiceAdminHandler.createAdminOnlyContractNo(); //start the http server serverConfig = new ServerConfig(); serverConfig.setServerPort(port); serverConfig.setServerIP(ipAddress); serverConfig.setServerPassword(password); serverConfig.setServerAdminKey(adminKey); serverConfig.setServerContact(contact); serverConfig.setAsHttps(asHttps); HttpServer.setHttpServer(serverConfig); //load the enterprise service bus if (esbClass == null) { LocalServer.getApi(password).setESB(Const.HTTPSERVER, Const.HTTPSERVER, adminXml); } else { params = new ArrayList(); params.add(password); params.add(adminKey); params.add(adminXml); LocalServer.getApi(password).setESB(Const.HTTPSERVER, Const.HTTPSERVER, null, esbClass, params); } LocalServer.getApi(password).startRequestThread(adminKey); } catch (Exception ex) { throw ex; } } /** * @param args the command line arguments */ public static void main(String[] args) { try { new Run_HttpServer(args); } catch (Exception ex) { ex.printStackTrace(); } } }