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
|
public static String CONFIG_FILE_NAME = "";
|
50
|
|
51
|
private static String REAL_APPLICATION_CONTEXT = null;
|
52
|
|
53
|
private static Logger logMetacat = Logger.getLogger(ServiceService.class);
|
54
|
private static Hashtable<String, BaseService> serviceList = new Hashtable<String, BaseService>();
|
55
|
|
56
|
/**
|
57
|
* private constructor since this is a singleton
|
58
|
*/
|
59
|
private ServiceService(ServletContext servletContext) {
|
60
|
REAL_CONFIG_DIR = servletContext.getRealPath(CONFIG_DIR);
|
61
|
REAL_SKIN_DIR = servletContext.getRealPath(SKIN_DIR);
|
62
|
|
63
|
CONFIG_FILE_NAME = servletContext.getInitParameter("configFileName");
|
64
|
|
65
|
REAL_APPLICATION_CONTEXT = SystemUtil.discoverApplicationContext(servletContext);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Get the single instance of ServiceService.
|
70
|
*
|
71
|
* @return the single instance of ServiceService
|
72
|
*/
|
73
|
public static ServiceService getInstance(ServletContext servletContext) {
|
74
|
if (serviceService == null) {
|
75
|
serviceService = new ServiceService(servletContext);
|
76
|
}
|
77
|
return serviceService;
|
78
|
}
|
79
|
|
80
|
/**
|
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
|
throw new ServiceException("ServiceService.registerService - Service: " + serviceName + " is already registered."
|
92
|
+ " Use ServiceService.reregister() to replace the service.");
|
93
|
}
|
94
|
logMetacat.info("ServiceService.registerService - Registering Service: " + serviceName);
|
95
|
serviceList.put(serviceName, service);
|
96
|
}
|
97
|
|
98
|
/**
|
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
|
throw new ServiceException("ServiceService.refreshService - Service: " + serviceName + " is not registered.");
|
108
|
}
|
109
|
|
110
|
BaseService baseService = serviceList.get(serviceName);
|
111
|
if (!baseService.refreshable()) {
|
112
|
throw new ServiceException("ServiceService.refreshService - Service: " + serviceName + " is not refreshable.");
|
113
|
}
|
114
|
logMetacat.info("ServiceService.refreshService - Refreshing Service: " + serviceName);
|
115
|
baseService.refresh();
|
116
|
}
|
117
|
|
118
|
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
|
/**
|
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
|
throw new ServiceException("ServiceService.getRealConfigDir - Cannot access config dir before Service has been initialized");
|
139
|
}
|
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
|
throw new ServiceException("ServiceService.getRealSkinDir - Cannot access skin dir before Service has been initialized");
|
150
|
}
|
151
|
return REAL_SKIN_DIR;
|
152
|
}
|
153
|
|
154
|
/**
|
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
|
throw new ServiceException("ServiceService.getRealApplicationContext - Application context name is null");
|
161
|
}
|
162
|
return REAL_APPLICATION_CONTEXT;
|
163
|
}
|
164
|
|
165
|
}
|