001: package com.quadcap.server;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedInputStream;
042: import java.io.FileInputStream;
043: import java.io.FileReader;
044:
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.Properties;
048:
049: import org.xml.sax.InputSource;
050:
051: import com.quadcap.util.ConfigString;
052: import com.quadcap.util.Debug;
053:
054: /**
055: * Main service container object.
056: *
057: * @author Stan Bailes
058: */
059: public class ServiceContainer {
060: static HashMap services = new HashMap();
061:
062: /**
063: * Default constructor
064: */
065: public ServiceContainer() {
066: }
067:
068: /**
069: * Add the service with the specified name, class name, and config
070: * file.
071: *
072: * @param Exception may be thrown during service construction or
073: * initialization
074: */
075: public void addService(String serviceName, String serviceClass,
076: Properties serviceProps) throws Exception {
077: Class sc = Class.forName(serviceClass);
078: Service service = (Service) sc.newInstance();
079: Debug.println(0, "Starting service: " + serviceName
080: + ", config = " + serviceProps + ", service = "
081: + service);
082: serviceProps.put("service.name", serviceName);
083: service.init(this , serviceProps);
084: services.put(serviceName, service);
085: }
086:
087: /**
088: * Parse the server.xml file and load all the services defined therein.
089: */
090: public void init(String configFile) throws Exception {
091: ServerConfigParser p = new ServerConfigParser(this );
092: p.parse(configFile);
093: }
094:
095: /**
096: * Return the service with the specified name
097: *
098: * @param name
099: */
100: public static Service getService(String name) {
101: return (Service) services.get(name);
102: }
103:
104: /**
105: * Return an iterator of service names
106: */
107: public Iterator serviceNames() {
108: return services.keySet().iterator();
109: }
110:
111: /**
112: * Main program: find config file and point the container at it.
113: */
114: public static void main(String[] args) {
115: try {
116: ServiceContainer c = new ServiceContainer();
117: ConfigString cs = ConfigString.find("server.config",
118: "server.xml");
119: c.init(cs.toString());
120: } catch (Exception e) {
121: Debug.print(e);
122: }
123: }
124: }
|