001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.lucane.server;
021:
022: import java.util.*;
023:
024: import org.lucane.common.*;
025: import org.lucane.common.concepts.ServiceConcept;
026: import org.lucane.server.store.Store;
027:
028: public class ServiceManager {
029: //-- instance management
030: private static ServiceManager instance = null;
031:
032: public static ServiceManager getInstance() {
033: if (instance == null)
034: instance = new ServiceManager();
035:
036: return instance;
037: }
038:
039: //-- attributes
040: private Store store;
041: private HashMap services;
042:
043: /**
044: * Constructor
045: */
046: private ServiceManager() {
047: this .store = Server.getInstance().getStore();
048: this .services = new HashMap();
049: }
050:
051: /**
052: * Load the services from the jar files
053: */
054: protected void loadAllServices() {
055: String baseURL = Server.getInstance().getWorkingDirectory()
056: + Server.APPLICATIONS_DIRECTORY;
057: //get all services from store
058: Iterator services;
059: try {
060: services = store.getServiceStore().getAllServices();
061: } catch (Exception e) {
062: Logging.getLogger()
063: .severe("Unable get service list : " + e);
064: services = new ArrayList().iterator();
065: }
066:
067: LucaneClassLoader loader = LucaneClassLoader.getInstance();
068: while (services.hasNext()) {
069: ServiceConcept service = (ServiceConcept) services.next();
070: String servicename = service.getName();
071: try {
072: String jarPath = baseURL + servicename + ".jar";
073: loader.addUrl("jar:file:///" + jarPath + "!/");
074:
075: String className = JarUtils.getServiceClass(jarPath);
076: if (className == null) {
077: Logging.getLogger().warning(
078: "Ignoring service " + servicename
079: + ": no Service-Class attribute.");
080: continue;
081: }
082:
083: Service serv = (Service) Class.forName(className, true,
084: loader).newInstance();
085: this .services.put(serv.getName(), serv);
086: Logging.getLogger().info(
087: "Service '" + servicename + "' loaded.");
088: } catch (Exception e) {
089: Logging.getLogger().warning(
090: "Unable to load service '" + servicename);
091: e.printStackTrace();
092: }
093: }
094: }
095:
096: /**
097: * Start all services
098: */
099: protected void startAllServices() {
100:
101: Iterator services = this .services.values().iterator();
102: while (services.hasNext()) {
103: Service serv = (Service) services.next();
104: try {
105: // start the service
106: ServiceConcept service = store.getServiceStore()
107: .getService(serv.getName());
108: serv.init(Server.getInstance());
109:
110: // install if necessary
111: if (!service.isInstalled()) {
112: serv.install();
113: service.setInstalled();
114: store.getServiceStore().updateService(service);
115: }
116:
117: //add the service connect info
118: ConnectInfo serverInfo = ConnectInfoManager
119: .getInstance().getServerInfo();
120: ConnectInfo serviceInfo = new ConnectInfo(serv
121: .getName(), serverInfo.getHostName(),
122: serverInfo.getPort(),
123: serverInfo.getPublicKey(), "service");
124: ConnectInfoManager.getInstance().addConnectInfo(
125: serviceInfo);
126:
127: Logging.getLogger().info(
128: "Service '" + serv.getName() + "' started.");
129: } catch (Exception e) {
130: Logging.getLogger().warning(
131: "Unable to start service : " + serv.getName()
132: + " : " + e);
133: e.printStackTrace();
134: }
135: }
136: }
137:
138: /**
139: * Shutdown all services
140: */
141: protected void shutdownAllServices() {
142:
143: Iterator services = this .services.values().iterator();
144: while (services.hasNext()) {
145: Service serv = (Service) services.next();
146: serv.shutdown();
147:
148: //remove the connectInfo
149: ConnectInfoManager.getInstance().removeConnectInfo(
150: serv.getName());
151:
152: Logging.getLogger().info(
153: "Service '" + serv.getName() + "' shutdowned.");
154: }
155: }
156:
157: /**
158: * Get a service by its name
159: *
160: * @param name the service name
161: * @return the service instance
162: */
163: public Service getService(String name) {
164: return (Service) this.services.get(name);
165: }
166: }
|