Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that controls other services 
4
 *  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
import javax.servlet.ServletContext;
30
import org.apache.log4j.Logger;
31

    
32
import java.util.Hashtable;
33
import java.util.Set;
34

    
35
import edu.ucsb.nceas.metacat.util.SystemUtil;
36

    
37
public class ServiceService {
38
	
39
	private static ServiceService serviceService = null;
40
	
41
    private static final String CONFIG_DIR = "WEB-INF";
42
	private static String REAL_CONFIG_DIR = "";
43
    
44
	private static final String SKIN_DIR = "/style/skins";
45
	private static String REAL_SKIN_DIR = "";
46
	
47
	private static String REAL_APPLICATION_CONTEXT = null;
48
	
49
	private static Logger logMetacat = Logger.getLogger(ServiceService.class);
50
	private static Hashtable<String, BaseService> serviceList = new Hashtable<String, BaseService>();
51
	
52
	/**
53
	 * private constructor since this is a singleton
54
	 */
55
	private ServiceService(ServletContext servletContext) {
56
		REAL_CONFIG_DIR = servletContext.getRealPath(CONFIG_DIR);
57
		REAL_SKIN_DIR = servletContext.getRealPath(SKIN_DIR);
58
		
59
		REAL_APPLICATION_CONTEXT = SystemUtil.discoverApplicationContext(servletContext);
60
	}
61
	
62
	/**
63
	 * Get the single instance of ServiceService.
64
	 * 
65
	 * @return the single instance of ServiceService
66
	 */
67
	public static ServiceService getInstance(ServletContext servletContext) {
68
		if (serviceService == null) {
69
			serviceService = new ServiceService(servletContext);
70
		}
71
		return serviceService;
72
	}
73
	
74
	/**
75
	 * Register a service with the system.
76
	 * 
77
	 * @param serviceName
78
	 *            the name of the service
79
	 * @param service
80
	 *            the singleton instance of the service
81
	 */
82
	public static void registerService(String serviceName, BaseService service)
83
			throws ServiceException {
84
		if (serviceList.containsKey(serviceName)) {
85
			throw new ServiceException("ServiceService.registerService - Service: " + serviceName + " is already registered."
86
					+ " Use ServiceService.reregister() to replace the service.");
87
		}
88
		logMetacat.info("ServiceService.registerService - Registering Service: " + serviceName);
89
		serviceList.put(serviceName, service);
90
	}
91
	
92
	/**
93
	 * Refresh a service.
94
	 * 
95
	 * @param serviceName
96
	 *            the name of the service to refresh
97
	 */
98
	public static void refreshService(String serviceName)
99
			throws ServiceException {
100
		if (!serviceList.containsKey(serviceName)) {
101
			throw new ServiceException("ServiceService.refreshService - Service: " + serviceName + " is not registered.");
102
		}
103
		
104
		BaseService baseService = serviceList.get(serviceName);
105
		if (!baseService.refreshable()) {
106
			throw new ServiceException("ServiceService.refreshService - Service: " + serviceName + " is not refreshable.");
107
		}
108
		logMetacat.info("ServiceService.refreshService - Refreshing Service: " + serviceName);
109
		baseService.refresh();
110
	}
111
	
112
	public static void stopAllServices() {
113
		Set<String> keySet = serviceList.keySet();
114
		
115
		for (String key : keySet) {
116
			try {
117
				logMetacat.info("ServiceService- stopAllServices: Stopping Service: " + key);
118
				serviceList.get(key).stop();
119
			} catch (ServiceException se) {
120
				logMetacat.error("ServiceService.stopAllServices - problem starting service: " 
121
						+ key + " : " + se.getMessage());
122
			}
123
		}
124
	}
125
	
126
	/**
127
	 * Convert the relative config directory to a full path
128
	 * @return the full config path
129
	 */
130
	public static String getRealConfigDir() throws ServiceException {
131
		if (serviceService == null) {
132
			throw new ServiceException("ServiceService.getRealConfigDir - Cannot access config dir before Service has been initialized");
133
		}
134
		return REAL_CONFIG_DIR;
135
	}
136
	
137
	/**
138
	 * Convert the relative skins directory to a full path
139
	 * @return the full skins directory path
140
	 */
141
	public static String getRealSkinDir() throws ServiceException {
142
		if (serviceService == null) {
143
			throw new ServiceException("ServiceService.getRealSkinDir - Cannot access skin dir before Service has been initialized");
144
		}
145
		return REAL_SKIN_DIR;
146
	}
147
	
148
	/**
149
	 * Get the servlet context name
150
	 * @return a string holding the context name
151
	 */
152
	public static String getRealApplicationContext() throws ServiceException {
153
		if (REAL_APPLICATION_CONTEXT == null) {
154
			throw new ServiceException("ServiceService.getRealApplicationContext - Application context name is null");
155
		}
156
		return REAL_APPLICATION_CONTEXT;
157
	}
158
	
159
}
(5-5/9)