View Javadoc
1   package fr.ifremer.quadrige2.core.service;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 com.google.common.base.Preconditions;
26  import fr.ifremer.quadrige2.core.security.remote.AuthenticationRemoteService;
27  import fr.ifremer.quadrige2.core.service.administration.program.ProgramService;
28  import fr.ifremer.quadrige2.core.service.administration.user.UserService;
29  import fr.ifremer.quadrige2.core.service.decorator.DecoratorService;
30  import fr.ifremer.quadrige2.core.service.persistence.PersistenceService;
31  import fr.ifremer.quadrige2.synchro.service.client.SynchroClientService;
32  import fr.ifremer.quadrige2.synchro.service.client.SynchroHistoryService;
33  import fr.ifremer.quadrige2.synchro.service.client.SynchroRestClientService;
34  import org.springframework.context.annotation.Bean;
35  import org.springframework.security.authentication.AuthenticationManager;
36  
37  import java.util.Iterator;
38  import java.util.ServiceLoader;
39  
40  /**
41   * @author peck7 on 30/06/2017.
42   */
43  public class ClientServiceLocator extends ServiceLocator {
44  
45      /**
46       * The shared instance of this ServiceLocator.
47       */
48      private static ClientServiceLocator instance = new ClientServiceLocator();
49  
50      /**
51       * replace the default shared instance of this Class
52       *
53       * @param newInstance the new shared service locator instance.
54       */
55      public static void setInstance(ClientServiceLocator newInstance) {
56          instance = newInstance;
57          ServiceLocator.setInstance(instance);
58      }
59  
60      /**
61       * Gets the shared instance of this Class
62       *
63       * @return the shared service locator instance.
64       */
65      public static ClientServiceLocator instance() {
66          return instance;
67      }
68  
69      private DecoratorService decoratorService = null;
70  
71      /**
72       * Get the decorator service. <p/>
73       * This method will NOT start Spring is not already started (could be call at UI initialization)
74       *
75       * @return a not null DecoratorService
76       */
77      @Bean(name = "decoratorService")
78      public DecoratorService getDecoratorService() {
79          if (decoratorService != null) {
80              return decoratorService;
81          }
82  
83          // WARNING : special case: load without Spring, to be able to get it without
84          //           starting the spring context (and the database)
85          ServiceLoader<DecoratorService> decoratorServiceLoader = ServiceLoader.load(DecoratorService.class);
86          Iterator<DecoratorService> dsIterator = decoratorServiceLoader.iterator();
87  
88          // If many, just take the first one
89          if (dsIterator.hasNext()) {
90              decoratorService = dsIterator.next();
91          }
92  
93          Preconditions.checkNotNull(decoratorService,
94                  String.format("Service 'decoratorService' could not be loaded. Make sure file [META-INF/services/%s] exists.",
95                          DecoratorService.class.getName()
96                  ));
97          return decoratorService;
98      }
99  
100     public SynchroClientService getSynchroClientService() {
101         return getService("synchroClientService", SynchroClientService.class);
102     }
103 
104     public SynchroRestClientService getSynchroRestClientService() {
105         return getService("synchroRestClientService", SynchroRestClientService.class);
106     }
107 
108     public SynchroHistoryService getSynchroHistoryService() {
109         return getService("synchroHistoryService", SynchroHistoryService.class);
110     }
111 
112     public ProgramService getProgramService() {
113         return getService("programService", ProgramService.class);
114     }
115 
116     public PersistenceService getPersistenceService() {
117         return getService("persistenceService", PersistenceService.class);
118     }
119 
120     public AuthenticationManager getAuthenticationManager() {
121         return getService("authenticationManager", AuthenticationManager.class);
122     }
123 
124     public UserService getUserService() {
125         return getService("userService", UserService.class);
126     }
127 
128     public AuthenticationRemoteService getAuthenticationRemoteService() {
129         return getService("authenticationRemoteService", AuthenticationRemoteService.class);
130     }
131 }