Project

General

Profile

1 4442 daigle
/**
2
 *  '$RCSfile$'
3 4710 daigle
 *    Purpose: A Class that controls other services
4 4442 daigle
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-10-09 09:53:18 -0700 (Thu, 09 Oct 2008) $'
10
 * '$Revision: 4429 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.service;
28
29 4710 daigle
import javax.servlet.ServletContext;
30 4442 daigle
import org.apache.log4j.Logger;
31
32 4981 daigle
import java.util.Hashtable;
33
import java.util.Set;
34
35 5015 daigle
import edu.ucsb.nceas.metacat.shared.BaseService;
36
import edu.ucsb.nceas.metacat.shared.ServiceException;
37 4795 daigle
import edu.ucsb.nceas.metacat.util.SystemUtil;
38
39 4710 daigle
public class ServiceService {
40 4442 daigle
41
	private static ServiceService serviceService = null;
42
43 4710 daigle
    private static final String CONFIG_DIR = "WEB-INF";
44
	private static String REAL_CONFIG_DIR = "";
45
46
	private static final String SKIN_DIR = "/style/skins";
47
	private static String REAL_SKIN_DIR = "";
48
49 5027 daigle
	public static String CONFIG_FILE_NAME = "";
50
51 4795 daigle
	private static String REAL_APPLICATION_CONTEXT = null;
52
53 4442 daigle
	private static Logger logMetacat = Logger.getLogger(ServiceService.class);
54 4710 daigle
	private static Hashtable<String, BaseService> serviceList = new Hashtable<String, BaseService>();
55
56 4442 daigle
	/**
57
	 * private constructor since this is a singleton
58
	 */
59 4710 daigle
	private ServiceService(ServletContext servletContext) {
60
		REAL_CONFIG_DIR = servletContext.getRealPath(CONFIG_DIR);
61
		REAL_SKIN_DIR = servletContext.getRealPath(SKIN_DIR);
62 4795 daigle
63 5027 daigle
		CONFIG_FILE_NAME = servletContext.getInitParameter("configFileName");
64
65 4795 daigle
		REAL_APPLICATION_CONTEXT = SystemUtil.discoverApplicationContext(servletContext);
66 4442 daigle
	}
67
68
	/**
69
	 * Get the single instance of ServiceService.
70
	 *
71
	 * @return the single instance of ServiceService
72
	 */
73 4710 daigle
	public static ServiceService getInstance(ServletContext servletContext) {
74 4442 daigle
		if (serviceService == null) {
75 4710 daigle
			serviceService = new ServiceService(servletContext);
76 4442 daigle
		}
77
		return serviceService;
78
	}
79
80 4710 daigle
	/**
81
	 * Register a service with the system.
82
	 *
83
	 * @param serviceName
84
	 *            the name of the service
85
	 * @param service
86
	 *            the singleton instance of the service
87
	 */
88
	public static void registerService(String serviceName, BaseService service)
89
			throws ServiceException {
90
		if (serviceList.containsKey(serviceName)) {
91 4981 daigle
			throw new ServiceException("ServiceService.registerService - Service: " + serviceName + " is already registered."
92 4710 daigle
					+ " Use ServiceService.reregister() to replace the service.");
93
		}
94 4981 daigle
		logMetacat.info("ServiceService.registerService - Registering Service: " + serviceName);
95 4710 daigle
		serviceList.put(serviceName, service);
96 4442 daigle
	}
97
98 4710 daigle
	/**
99
	 * Refresh a service.
100
	 *
101
	 * @param serviceName
102
	 *            the name of the service to refresh
103
	 */
104
	public static void refreshService(String serviceName)
105
			throws ServiceException {
106
		if (!serviceList.containsKey(serviceName)) {
107 4981 daigle
			throw new ServiceException("ServiceService.refreshService - Service: " + serviceName + " is not registered.");
108 4710 daigle
		}
109
110
		BaseService baseService = serviceList.get(serviceName);
111
		if (!baseService.refreshable()) {
112 4981 daigle
			throw new ServiceException("ServiceService.refreshService - Service: " + serviceName + " is not refreshable.");
113 4710 daigle
		}
114 4981 daigle
		logMetacat.info("ServiceService.refreshService - Refreshing Service: " + serviceName);
115 4710 daigle
		baseService.refresh();
116 4442 daigle
	}
117
118 4981 daigle
	public static void stopAllServices() {
119
		Set<String> keySet = serviceList.keySet();
120
121
		for (String key : keySet) {
122
			try {
123
				logMetacat.info("ServiceService- stopAllServices: Stopping Service: " + key);
124
				serviceList.get(key).stop();
125
			} catch (ServiceException se) {
126
				logMetacat.error("ServiceService.stopAllServices - problem starting service: "
127
						+ key + " : " + se.getMessage());
128
			}
129
		}
130
	}
131
132 4710 daigle
	/**
133
	 * Convert the relative config directory to a full path
134
	 * @return the full config path
135
	 */
136
	public static String getRealConfigDir() throws ServiceException {
137
		if (serviceService == null) {
138 4981 daigle
			throw new ServiceException("ServiceService.getRealConfigDir - Cannot access config dir before Service has been initialized");
139 4710 daigle
		}
140
		return REAL_CONFIG_DIR;
141
	}
142
143
	/**
144
	 * Convert the relative skins directory to a full path
145
	 * @return the full skins directory path
146
	 */
147
	public static String getRealSkinDir() throws ServiceException {
148
		if (serviceService == null) {
149 4981 daigle
			throw new ServiceException("ServiceService.getRealSkinDir - Cannot access skin dir before Service has been initialized");
150 4710 daigle
		}
151
		return REAL_SKIN_DIR;
152
	}
153
154 4795 daigle
	/**
155
	 * Get the servlet context name
156
	 * @return a string holding the context name
157
	 */
158
	public static String getRealApplicationContext() throws ServiceException {
159
		if (REAL_APPLICATION_CONTEXT == null) {
160 4981 daigle
			throw new ServiceException("ServiceService.getRealApplicationContext - Application context name is null");
161 4795 daigle
		}
162
		return REAL_APPLICATION_CONTEXT;
163
	}
164
165 4442 daigle
}