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.shared.BaseService;
36
import edu.ucsb.nceas.metacat.shared.ServiceException;
37
import edu.ucsb.nceas.metacat.util.SystemUtil;
38

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