View Javadoc
1   package fr.ifremer.quadrige3.core.service;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  import fr.ifremer.quadrige3.core.dao.technical.Assert;
26  import fr.ifremer.quadrige3.core.security.remote.AuthenticationRemoteService;
27  import fr.ifremer.quadrige3.core.service.administration.program.ProgramService;
28  import fr.ifremer.quadrige3.core.service.administration.user.UserService;
29  import fr.ifremer.quadrige3.core.service.decorator.DecoratorService;
30  import fr.ifremer.quadrige3.core.service.extraction.ExtractionRestClientService;
31  import fr.ifremer.quadrige3.core.service.http.HttpService;
32  import fr.ifremer.quadrige3.core.service.persistence.PersistenceService;
33  import fr.ifremer.quadrige3.synchro.service.client.SynchroClientService;
34  import fr.ifremer.quadrige3.synchro.service.client.SynchroHistoryService;
35  import fr.ifremer.quadrige3.synchro.service.client.SynchroRestClientService;
36  import org.springframework.context.annotation.Bean;
37  import org.springframework.security.authentication.AuthenticationManager;
38  
39  import java.util.Iterator;
40  import java.util.ServiceLoader;
41  
42  /**
43   * @author peck7 on 30/06/2017.
44   */
45  public class ClientServiceLocator extends ServiceLocator {
46  
47      /**
48       * The shared instance of this ServiceLocator.
49       */
50      private static ClientServiceLocator instance = new ClientServiceLocator();
51  
52      /**
53       * replace the default shared instance of this Class
54       *
55       * @param newInstance the new shared service locator instance.
56       */
57      public static void setInstance(ClientServiceLocator newInstance) {
58          instance = newInstance;
59          ServiceLocator.setInstance(instance);
60      }
61  
62      /**
63       * Gets the shared instance of this Class
64       *
65       * @return the shared service locator instance.
66       */
67      public static ClientServiceLocator instance() {
68          return instance;
69      }
70  
71      private DecoratorService decoratorService = null;
72  
73      /**
74       * Get the decorator service. <p/>
75       * This method will NOT start Spring is not already started (could be call at UI initialization)
76       *
77       * @return a not null DecoratorService
78       */
79      @Bean(name = "decoratorService")
80      public DecoratorService getDecoratorService() {
81          if (decoratorService != null) {
82              return decoratorService;
83          }
84  
85          // WARNING : special case: load without Spring, to be able to get it without
86          //           starting the spring context (and the database)
87          ServiceLoader<DecoratorService> decoratorServiceLoader = ServiceLoader.load(DecoratorService.class);
88          Iterator<DecoratorService> dsIterator = decoratorServiceLoader.iterator();
89  
90          // If many, just take the first one
91          if (dsIterator.hasNext()) {
92              decoratorService = dsIterator.next();
93          }
94  
95          Assert.notNull(decoratorService,
96                  String.format("Service 'decoratorService' could not be loaded. Make sure file [META-INF/services/%s] exists.",
97                          DecoratorService.class.getName()
98                  ));
99          return decoratorService;
100     }
101 
102     public SynchroClientService getSynchroClientService() {
103         return getService("synchroClientService", SynchroClientService.class);
104     }
105 
106     public SynchroRestClientService getSynchroRestClientService() {
107         return getService("synchroRestClientService", SynchroRestClientService.class);
108     }
109 
110     public SynchroHistoryService getSynchroHistoryService() {
111         return getService("synchroHistoryService", SynchroHistoryService.class);
112     }
113 
114     public ProgramService getProgramService() {
115         return getService("programService", ProgramService.class);
116     }
117 
118     public PersistenceService getPersistenceService() {
119         return getService("persistenceService", PersistenceService.class);
120     }
121 
122     public AuthenticationManager getAuthenticationManager() {
123         return getService("authenticationManager", AuthenticationManager.class);
124     }
125 
126     public UserService getUserService() {
127         return getService("userService", UserService.class);
128     }
129 
130     public AuthenticationRemoteService getAuthenticationRemoteService() {
131         return getService("authenticationRemoteService", AuthenticationRemoteService.class);
132     }
133 
134     public ExtractionRestClientService getExtractionRestClientService() {
135         return getService("extractionRestClientService", ExtractionRestClientService.class);
136     }
137 
138     public HttpService getHttpService() {
139         return getService("httpService", HttpService.class);
140     }
141 }