View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.limmen.crs.api;
17  
18  import java.util.Iterator;
19  import java.util.ServiceLoader;
20  
21  public class ConfigurationServiceFactory {
22  
23  	private static ConfigurationServiceFactory current = new ConfigurationServiceFactory();
24  
25  	public static ConfigurationServiceFactory getInstance() {
26  
27  		return current;
28  	}
29  
30  	private final ConfigurationManagementService configurationManagementService;
31  
32     private final ConfigurationService configurationService;
33  
34  	private ConfigurationServiceFactory() {
35  
36  		super();
37  
38  		ServiceLoader<ConfigurationService> services =
39  			ServiceLoader.load(ConfigurationService.class);
40  
41  		Iterator<ConfigurationService> servicesIterator = services.iterator();
42  
43  		configurationService = servicesIterator.next();
44  
45  		if (!(configurationService instanceof ConfigurationManagementService)) {
46  
47  			ServiceLoader<ConfigurationManagementService> managementServices =
48  				ServiceLoader.load(ConfigurationManagementService.class);
49  
50  			Iterator<ConfigurationManagementService> managementServicesIterator =
51  				managementServices.iterator();
52  
53  			configurationManagementService = managementServicesIterator.next();
54  		}
55  		else {
56  
57  			configurationManagementService = (ConfigurationManagementService) configurationService;
58  		}
59  	}
60  
61  	public ConfigurationManagementService getConfigurationManagementService() {
62  
63  	   return configurationManagementService;
64     }
65  
66  	public ConfigurationService getConfigurationService() {
67  
68  		return configurationService;
69  	}
70  }