View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.init;
20  
21  import java.io.Serializable;
22  import java.lang.reflect.Field;
23  import java.lang.reflect.Modifier;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Objects;
33  import java.util.Set;
34  import java.util.stream.Collectors;
35  import org.apache.commons.lang3.ArrayUtils;
36  import org.apache.syncope.client.console.ConsoleProperties;
37  import org.apache.syncope.client.console.annotations.AMPage;
38  import org.apache.syncope.client.console.annotations.IdMPage;
39  import org.apache.syncope.client.console.pages.BaseExtPage;
40  import org.apache.syncope.client.console.pages.BasePage;
41  import org.apache.syncope.client.console.widgets.BaseExtWidget;
42  import org.apache.syncope.client.console.widgets.ExtAlertWidget;
43  import org.apache.syncope.client.ui.commons.annotations.BinaryPreview;
44  import org.apache.syncope.client.ui.commons.annotations.ExtPage;
45  import org.apache.syncope.client.ui.commons.annotations.ExtWidget;
46  import org.apache.syncope.client.ui.commons.markup.html.form.preview.BinaryPreviewer;
47  import org.apache.syncope.client.ui.commons.panels.BaseSSOLoginFormPanel;
48  import org.apache.syncope.common.lib.policy.AccountRuleConf;
49  import org.apache.syncope.common.lib.policy.PasswordRuleConf;
50  import org.apache.syncope.common.lib.report.ReportConf;
51  import org.apache.syncope.common.lib.to.AnyObjectTO;
52  import org.apache.syncope.common.lib.to.GroupTO;
53  import org.apache.syncope.common.lib.to.UserTO;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  import org.springframework.beans.factory.config.BeanDefinition;
57  import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
58  import org.springframework.core.type.filter.AssignableTypeFilter;
59  import org.springframework.util.ClassUtils;
60  
61  public class ClassPathScanImplementationLookup implements Serializable {
62  
63      private static final long serialVersionUID = 5047756409117925203L;
64  
65      private static final Logger LOG = LoggerFactory.getLogger(ClassPathScanImplementationLookup.class);
66  
67      private static final String DEFAULT_BASE_PACKAGE = "org.apache.syncope";
68  
69      public static final Set<String> USER_FIELD_NAMES = new HashSet<>();
70  
71      public static final Set<String> GROUP_FIELD_NAMES = new HashSet<>();
72  
73      public static final Set<String> ANY_OBJECT_FIELD_NAMES = new HashSet<>();
74  
75      static {
76          initFieldNames(UserTO.class, USER_FIELD_NAMES);
77          initFieldNames(GroupTO.class, GROUP_FIELD_NAMES);
78          initFieldNames(AnyObjectTO.class, ANY_OBJECT_FIELD_NAMES);
79      }
80  
81      private static void initFieldNames(final Class<?> entityClass, final Set<String> keys) {
82          List<Class<?>> classes = org.apache.commons.lang3.ClassUtils.getAllSuperclasses(entityClass);
83          classes.add(entityClass);
84          classes.forEach(clazz -> {
85              for (Field field : clazz.getDeclaredFields()) {
86                  if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers())
87                          && !Collection.class.isAssignableFrom(field.getType())
88                          && !Map.class.isAssignableFrom(field.getType())) {
89  
90                      keys.add(field.getName());
91                  }
92              }
93          });
94      }
95  
96      /**
97       * This method can be overridden by subclasses to customize classpath scan.
98       *
99       * @return basePackage for classpath scanning
100      */
101     protected static String getBasePackage() {
102         return DEFAULT_BASE_PACKAGE;
103     }
104 
105     private final Collection<ClassPathScanImplementationContributor> contributors;
106 
107     private Map<String, List<Class<?>>> classes;
108 
109     private List<Class<? extends BasePage>> idRepoPages;
110 
111     private List<Class<? extends BasePage>> idmPages;
112 
113     private List<Class<? extends BasePage>> amPages;
114 
115     private final ConsoleProperties props;
116 
117     public ClassPathScanImplementationLookup(
118             final Collection<ClassPathScanImplementationContributor> contributors,
119             final ConsoleProperties props) {
120 
121         this.contributors = contributors;
122         this.props = props;
123     }
124 
125     protected ClassPathScanningCandidateComponentProvider scanner() {
126         ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
127 
128         scanner.addIncludeFilter(new AssignableTypeFilter(BasePage.class));
129         scanner.addIncludeFilter(new AssignableTypeFilter(BaseExtPage.class));
130         scanner.addIncludeFilter(new AssignableTypeFilter(BaseExtWidget.class));
131         scanner.addIncludeFilter(new AssignableTypeFilter(ExtAlertWidget.class));
132         scanner.addIncludeFilter(new AssignableTypeFilter(BaseSSOLoginFormPanel.class));
133         scanner.addIncludeFilter(new AssignableTypeFilter(BinaryPreviewer.class));
134         scanner.addIncludeFilter(new AssignableTypeFilter(ReportConf.class));
135         scanner.addIncludeFilter(new AssignableTypeFilter(AccountRuleConf.class));
136         scanner.addIncludeFilter(new AssignableTypeFilter(PasswordRuleConf.class));
137 
138         contributors.forEach(contributor -> contributor.extend(scanner));
139 
140         return scanner;
141     }
142 
143     protected void addClass(final String category, final Class<?> clazz) {
144         List<Class<?>> clazzes = classes.get(category);
145         if (clazzes == null) {
146             clazzes = new ArrayList<>();
147             classes.put(category, clazzes);
148         }
149         clazzes.add(clazz);
150     }
151 
152     @SuppressWarnings("unchecked")
153     public void load() {
154         classes = new HashMap<>();
155         idRepoPages = new ArrayList<>();
156         idmPages = new ArrayList<>();
157         amPages = new ArrayList<>();
158 
159         for (BeanDefinition bd : scanner().findCandidateComponents(getBasePackage())) {
160             try {
161                 Class<?> clazz = ClassUtils.resolveClassName(
162                         Objects.requireNonNull(bd.getBeanClassName()), ClassUtils.getDefaultClassLoader());
163                 if (Modifier.isAbstract(clazz.getModifiers())) {
164                     continue;
165                 }
166 
167                 if (BaseExtPage.class.isAssignableFrom(clazz)) {
168                     if (clazz.isAnnotationPresent(ExtPage.class)) {
169                         addClass(BaseExtPage.class.getName(), clazz);
170                     } else {
171                         LOG.error("Could not find annotation {} in {}, ignoring",
172                                 ExtPage.class.getName(), clazz.getName());
173                     }
174                 } else if (BaseExtWidget.class.isAssignableFrom(clazz)) {
175                     if (clazz.isAnnotationPresent(ExtWidget.class)) {
176                         addClass(BaseExtWidget.class.getName(), clazz);
177                     } else {
178                         LOG.error("Could not find annotation {} in {}, ignoring",
179                                 ExtWidget.class.getName(), clazz.getName());
180                     }
181                 } else if (ExtAlertWidget.class.isAssignableFrom(clazz)) {
182                     if (clazz.isAnnotationPresent(ExtWidget.class)) {
183                         addClass(ExtAlertWidget.class.getName(), clazz);
184                     } else {
185                         LOG.error("Could not find annotation {} in {}, ignoring",
186                                 ExtAlertWidget.class.getName(), clazz.getName());
187                     }
188                 } else if (BasePage.class.isAssignableFrom(clazz)) {
189                     if (clazz.isAnnotationPresent(IdMPage.class)) {
190                         if (!clazz.getName().endsWith("Topology")
191                                 || (clazz.getName().equals(props.getPage().get("topology").getName()))) {
192                             idmPages.add((Class<? extends BasePage>) clazz);
193                         }
194                     } else if (clazz.isAnnotationPresent(AMPage.class)) {
195                         amPages.add((Class<? extends BasePage>) clazz);
196                     } else {
197                         idRepoPages.add((Class<? extends BasePage>) clazz);
198                     }
199                 } else if (BaseSSOLoginFormPanel.class.isAssignableFrom(clazz)) {
200                     addClass(BaseSSOLoginFormPanel.class.getName(), clazz);
201                 } else if (BinaryPreviewer.class.isAssignableFrom(clazz)) {
202                     addClass(BinaryPreviewer.class.getName(), clazz);
203                 } else if (ReportConf.class.isAssignableFrom(clazz)) {
204                     addClass(ReportConf.class.getName(), clazz);
205                 } else if (AccountRuleConf.class.isAssignableFrom(clazz)) {
206                     addClass(AccountRuleConf.class.getName(), clazz);
207                 } else if (PasswordRuleConf.class.isAssignableFrom(clazz)) {
208                     addClass(PasswordRuleConf.class.getName(), clazz);
209                 } else {
210                     contributors.forEach(contributor -> contributor.getLabel(clazz).
211                             ifPresent(label -> addClass(label, clazz)));
212                 }
213             } catch (Throwable t) {
214                 LOG.warn("Could not inspect class {}", bd.getBeanClassName(), t);
215             }
216         }
217 
218         idRepoPages = Collections.unmodifiableList(idRepoPages);
219 
220         idmPages.sort(Comparator.comparing(o -> o.getAnnotation(IdMPage.class).priority()));
221         idmPages = Collections.unmodifiableList(idmPages);
222 
223         amPages.sort(Comparator.comparing(o -> o.getAnnotation(AMPage.class).priority()));
224         amPages = Collections.unmodifiableList(amPages);
225 
226         if (classes.containsKey(BaseExtPage.class.getName())) {
227             classes.get(BaseExtPage.class.getName()).
228                     sort(Comparator.comparing(o -> o.getAnnotation(ExtPage.class).priority()));
229         }
230 
231         if (classes.containsKey(BaseExtWidget.class.getName())) {
232             classes.get(BaseExtWidget.class.getName()).
233                     sort(Comparator.comparing(o -> o.getAnnotation(ExtWidget.class).priority()));
234         }
235 
236         if (classes.containsKey(ExtAlertWidget.class.getName())) {
237             classes.get(ExtAlertWidget.class.getName()).
238                     sort(Comparator.comparing(o -> o.getAnnotation(ExtWidget.class).priority()));
239         }
240 
241         classes.forEach((category, clazzes) -> LOG.debug("{} found: {}", category, clazzes));
242     }
243 
244     public List<Class<? extends BasePage>> getIdRepoPageClasses() {
245         return idRepoPages;
246     }
247 
248     public List<Class<? extends BasePage>> getIdMPageClasses() {
249         return idmPages;
250     }
251 
252     public List<Class<? extends BasePage>> getAMPageClasses() {
253         return amPages;
254     }
255 
256     @SuppressWarnings("unchecked")
257     public <T> List<Class<? extends T>> getClasses(final Class<T> reference) {
258         return classes.getOrDefault(reference.getName(), List.of()).stream().
259                 map(clazz -> (Class<? extends T>) clazz).
260                 collect(Collectors.toList());
261     }
262 
263     @SuppressWarnings("unchecked")
264     public List<Class<? extends ExtAlertWidget<?>>> getExtAlertWidgetClasses() {
265         return classes.getOrDefault(ExtAlertWidget.class.getName(), List.of()).stream().
266                 map(clazz -> (Class<? extends ExtAlertWidget<?>>) clazz).
267                 collect(Collectors.toList());
268     }
269 
270     public Class<? extends BinaryPreviewer> getPreviewerClass(final String mimeType) {
271         LOG.debug("Searching for previewer class for MIME type: {}", mimeType);
272 
273         Class<? extends BinaryPreviewer> previewer = null;
274         for (Class<? extends BinaryPreviewer> candidate : getClasses(BinaryPreviewer.class)) {
275             LOG.debug("Evaluating previewer class {} for MIME type {}", candidate.getName(), mimeType);
276             if (candidate.isAnnotationPresent(BinaryPreview.class)
277                     && ArrayUtils.contains(candidate.getAnnotation(BinaryPreview.class).mimeTypes(), mimeType)) {
278                 LOG.debug("Found existing previewer for MIME type {}: {}", mimeType, candidate.getName());
279                 previewer = candidate;
280             }
281         }
282         return previewer;
283     }
284 }