Source code: nectar/ServicesUtil.java
1 /*
2 Copyright (C) 2003 Kai Schutte
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 * ServicesUtil.java -- static methods to reach/retrieve the major services.
19 *
20 * Created on March 7, 2003, 8:20 PM
21 */
22
23 package nectar;
24
25 import nectar.services.RecordService;
26 import nectar.services.RecordServiceConfiguration;
27 import nectar.services.AuthenticationService;
28 import nectar.services.AuthenticationConfiguration;
29 import nectar.template.TemplateService;
30 import nectar.template.TemplateConfiguration;
31 import nectar.configuration.ConfigurationService;
32 import nectar.data.DataException;
33 import nectar.data.DataAdapterService;
34 import nectar.data.DataAdapterConfiguration;
35 import nectar.configuration.ConfigurationException;
36
37 import java.sql.Connection;
38 import java.sql.Statement;
39 import javax.sql.DataSource;
40
41 import javax.naming.Context;
42 import javax.naming.InitialContext;
43 import javax.servlet.ServletContext;
44 import org.apache.struts.config.ModuleConfig;
45 import java.util.Iterator;
46
47 /** A collection of static methods to quickly retrieve the fundamental Services.
48 *
49 * @author Kai Schutte skander@skander.com
50 */
51 public final class ServicesUtil {
52 private static String configurationFile = null;
53 private static ConfigurationService configurationService = null;
54 private static RecordService recordService = null;
55 private static AuthenticationService authenticationService = null;
56 private static TemplateService templateService = null;
57 private static DataSource dataSource = null;
58 private static DataAdapterService dataAdapter = null;
59 private static ServletContext context = null;
60 private static ModuleConfig moduleConfig = null;
61 private static String extensionDir = null;
62 /** Sets the nectar_config.xml Configuration filepath for Nectar. This method is called by NectarPlugin, and shouldn't be used elsewhere.
63 * @param file the full filepath of the nectar_config.xml file.
64 */
65 public static void setConfigurationFile(String file) {
66 configurationFile = file;
67 }
68
69 /** Returns the full nectar_config.xml Configuration filepath for Nectar.
70 * @return the full filepath of the nectar_config.xml file.
71 */
72 public static String getConfigurationFile() {
73 return configurationFile;
74 }
75
76 public static void setExtensionDir(String path) {
77 extensionDir = path;
78 }
79
80 public static String getExtensionDir() {
81 return extensionDir;
82 }
83
84 /** Initialize the ConfigurationService, which in turn will initialize all the Services. This method is called by the NectarPlugin class, and shouldn't be used elsewhere.
85 * @throws ConfigurationException thrown where Configuration values are invalid or missing.
86 * @throws ServiceException thrown when initializing errors occur.
87 */
88 public static synchronized void init() throws ConfigurationException, ServiceException {
89 configurationService = new ConfigurationService();
90 configurationService.initService();
91 recordService = (RecordService)configurationService.getConfiguration(RecordServiceConfiguration.SERVICE_NAME).getService();
92 authenticationService = (AuthenticationService)configurationService.getConfiguration(AuthenticationConfiguration.SERVICE_NAME).getService();
93 templateService = (TemplateService)configurationService.getConfiguration(TemplateConfiguration.SERVICE_NAME).getService();
94 dataAdapter = (DataAdapterService)configurationService.getConfiguration(DataAdapterConfiguration.SERVICE_NAME).getService();
95 }
96
97 /** Initialize all the Nectar Services. This method is called by the NectarPlugin class, and shouldn't be used elsewise.
98 * @throws ConfigurationException thrown where Configuration values are invalid or missing.
99 * @throws ServiceException thrown when initializing errors occur.
100 */
101 public static synchronized void destroy() {
102 configurationService.killService();
103 configurationFile = null;
104 configurationService = null;
105 authenticationService = null;
106 templateService = null;
107 recordService = null;
108 dataSource = null;
109 dataAdapter = null;
110 context = null;
111 }
112
113 /** Returns a given Service by it's name
114 * @return Service the Service.
115 * @param name The global name that the Service can be identified by, like <CODE>RecordService.SERVICE_NAME</CODE>.
116 * @throws ConfigurationException thrown if the Service can't be found.
117 */
118 public static Service getService(String name) throws ConfigurationException {
119 return configurationService.getConfiguration(name).getService();
120 }
121
122
123 /** Returns the ConfigurationService
124 * @return ConfigurationService the ConfigurationService.
125 */
126 public static ConfigurationService getConfigurationService() {
127 return configurationService;
128 }
129
130 /** Returns the RecordService
131 * @return RecordService the RecordService.
132 */
133 public static RecordService getRecordService() {
134 return recordService;
135 }
136
137 /** Returns the AutenticationService
138 * @return the AutenticationService
139 */
140
141 public static AuthenticationService getAuthenticationService() {
142 return authenticationService;
143 }
144
145 /** Returns the TemplateService
146 * @return the TemplateService
147 */
148 public static TemplateService getTemplateService() {
149 return templateService;
150 }
151
152 /** Returns the DataAdapterService
153 * @return DataAdapterService the DataAdapterService.
154 */
155 public static DataAdapterService getDataAdapter() {
156 return dataAdapter;
157 }
158
159 /** Returns the DataSource configured for Nectar.
160 * @return DataSource the DataSource.
161 */
162 public static DataSource getDataSource() {
163 return dataSource;
164 }
165
166 /** Set the DataSource for Nectar. This function is called by NectarPlugin and shouldn't be used elsewhere.
167 * @param ds The DataSource to use for Nectar.
168 */
169 public static void setDataSource(DataSource ds) {
170 dataSource = ds;
171 }
172
173 /** Returns the ServletContext for the Nectar Servlet. This should really only
174 * be used for the configuration and initialization stage, when no actions are
175 * being executed.
176 * @return the ServletContext for this instance of Nectar WebServices.
177 */
178 public static ServletContext getServletContext() {
179 return context;
180 }
181
182 /** Sets the ServletContext for the Nectar Servlet. This is called by the Servlet
183 * framework through NectarPlugin, and shouldn't be used elsewise.
184 * @param the ServletContext for this instance of Nectar WebServices.
185 */
186 public static void setServletContext(ServletContext c) {
187 context = c;
188 }
189
190 public static ModuleConfig getModuleConfig() {
191 return moduleConfig;
192 }
193
194 public static void setModuleConfig(ModuleConfig c) {
195 moduleConfig = c;
196 }
197
198 /** Get an SQL Statement Object from the DataSource. This is a shortcut for SQLDataAdapters and creates a new Statement from one of the pooled connections in the DataSource.
199 * @return Statement the Statement object.
200 * @throws DataException thrown where exceptions are thrown that would originate from the DataSource and it's Connections.
201 */
202 public static Connection getConnection() throws DataException {
203 Connection connection;
204 try {
205 connection = dataSource.getConnection();
206 } catch (Exception e) {
207 throw new DataException("Can't get Connection from datasource", e);
208 }
209 if (connection == null) {
210 throw new DataException("Can't get Connection from datasource");
211 }
212 return connection;
213 }
214
215 /** Reset all the Services. Once a new Configuration file is written, this method can shutdown and reload all the services on the new configuration file.
216 * @throws ConfigurationException thrown when Configuration values are invalid or missing.
217 * @throws ServiceException thrown when shutdown or initialization errors occur.
218 */
219 public static synchronized void resetConfiguration() throws ConfigurationException, ServiceException {
220 configurationService.killService();
221 init();
222 }
223 }