Skip to content

Package: RoleManager

RoleManager

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.role.spi;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Iterator;
31: import java.util.List;
32: import java.util.Objects;
33: import java.util.ServiceLoader;
34: import it.tidalwave.util.LazySupplier;
35: import lombok.AccessLevel;
36: import lombok.NoArgsConstructor;
37: import lombok.extern.slf4j.Slf4j;
38:
39: /***********************************************************************************************************************
40: *
41: * A service which retrieves DCI Roles for a given object.
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: public interface RoleManager
47: {
48: /*******************************************************************************************************************
49: *
50: * A locator for the {@link RoleManager} which uses the {@link ServiceLoader} facility to be independent of
51: * any DI framework.
52: *
53: ******************************************************************************************************************/
54: @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE)
55: public static final class Locator
56: {
57: private static final LazySupplier<RoleManager> ROLE_MANAGER_REF =
58: LazySupplier.of(RoleManager.Locator::findRoleManager);
59:
60: private static final LazySupplier<RoleManagerProvider> ROLE_MANAGER_PROVIDER_REF =
61: LazySupplier.of(RoleManager.Locator::findRoleManagerProvider);
62:
63: /***************************************************************************************************************
64: *
65: **************************************************************************************************************/
66: @Nonnull
67: public static RoleManager find()
68: {
69: return ROLE_MANAGER_REF.get();
70: }
71:
72: /***************************************************************************************************************
73: *
74: **************************************************************************************************************/
75: @Nonnull
76: private static RoleManager findRoleManager()
77: {
78: return Objects.requireNonNull(ROLE_MANAGER_PROVIDER_REF.get().getRoleManager(),
79: "Cannot find RoleManager");
80: }
81:
82: /***************************************************************************************************************
83: *
84: **************************************************************************************************************/
85: @Nonnull
86: private static RoleManagerProvider findRoleManagerProvider()
87: {
88: final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
89: final Iterator<RoleManagerProvider> i =
90: ServiceLoader.load(RoleManagerProvider.class, classLoader).iterator();
91:
92: if (!i.hasNext())
93: {
94: throw new RuntimeException("No ServiceProvider for RoleManagerProvider");
95: }
96:
97: final RoleManagerProvider roleManagerProvider = Objects.requireNonNull(i.next(),
98: "roleManagerProvider is null");
99: assert roleManagerProvider != null; // for SpotBugs
100: log.info("RoleManagerProvider instantiated from META-INF: {}", roleManagerProvider);
101: return roleManagerProvider;
102: }
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * Retrieves the roles of the given class for the given owner object.
108: *
109: * @param <T> the static type of the roles
110: * @param owner the owner object
111: * @param roleType the dynamic type of the roles
112: * @return a list of roles
113: *
114: ******************************************************************************************************************/
115: @Nonnull
116: public <T> List<? extends T> findRoles (@Nonnull Object owner, @Nonnull Class<T> roleType);
117: }