View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.pi;
20  
21  import static org.mycore.pi.MCRPIJobService.CREATION_PREDICATE;
22  
23  import java.util.List;
24  import java.util.stream.Collectors;
25  
26  import org.mycore.common.config.MCRConfiguration2;
27  
28  public class MCRPIServiceManager {
29  
30      public static final String REGISTRATION_SERVICE_CONFIG_PREFIX = "MCR.PI.Service.";
31  
32      public static MCRPIServiceManager getInstance() {
33          return InstanceHolder.INSTANCE;
34      }
35  
36      public List<String> getServiceIDList() {
37          return MCRConfiguration2.getInstantiatablePropertyKeys(REGISTRATION_SERVICE_CONFIG_PREFIX)
38              .map(s -> s.substring(MCRPIServiceManager.REGISTRATION_SERVICE_CONFIG_PREFIX.length()))
39              .collect(Collectors.toList());
40      }
41  
42      public List<MCRPIService<MCRPersistentIdentifier>> getServiceList() {
43          return getServiceIDList()
44              .stream()
45              .map(this::getRegistrationService)
46              .collect(Collectors.toList());
47      }
48  
49      public List<MCRPIJobService<MCRPersistentIdentifier>> getAutoCreationList() {
50          return getServiceList()
51              .stream()
52              .filter(service -> service instanceof MCRPIJobService)
53              .map(s -> (MCRPIJobService<MCRPersistentIdentifier>) s)
54              .filter(service -> MCRConfiguration2
55                  .getString(REGISTRATION_SERVICE_CONFIG_PREFIX + service.getServiceID() + "." +
56                      CREATION_PREDICATE)
57                  .isPresent())
58              .collect(Collectors.toList());
59      }
60  
61      public <T extends MCRPersistentIdentifier> MCRPIService<T> getRegistrationService(String id) {
62          return MCRConfiguration2
63              .<MCRPIService<T>>getSingleInstanceOf(MCRPIServiceManager.REGISTRATION_SERVICE_CONFIG_PREFIX + id).get();
64      }
65  
66      private static class InstanceHolder {
67          private static final MCRPIServiceManager INSTANCE = new MCRPIServiceManager();
68      }
69  
70  }