View Javadoc
1   package com.atlassian.plugin.schema.impl;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.PluginParseException;
5   import com.atlassian.plugin.module.ContainerManagedPlugin;
6   import com.atlassian.plugin.schema.descriptor.DescribedModuleDescriptorFactory;
7   import com.atlassian.plugin.schema.spi.Schema;
8   import com.atlassian.plugin.schema.spi.SchemaFactory;
9   import com.google.common.collect.ImmutableSet;
10  
11  import javax.annotation.Nullable;
12  import java.util.Set;
13  
14  import static com.google.common.base.Preconditions.checkNotNull;
15  import static java.util.Collections.singleton;
16  
17  /**
18   * Described module descriptor factory for internal use
19   */
20  final class DescribedModuleTypeDescribedModuleDescriptorFactory<T extends ModuleDescriptor<?>> implements DescribedModuleDescriptorFactory {
21      private final ContainerManagedPlugin plugin;
22      private final String type;
23      private final Iterable<String> typeList;
24      private final Class<T> moduleDescriptorClass;
25      private final SchemaFactory schemaFactory;
26  
27      /**
28       * Constructs an instance using a specific host container
29       *
30       * @param type                  The type of module
31       * @param moduleDescriptorClass The descriptor class
32       * @param schemaFactory         the schema factory
33       * @since 3.0.0
34       */
35      DescribedModuleTypeDescribedModuleDescriptorFactory(ContainerManagedPlugin plugin,
36                                                          final String type,
37                                                          final Class<T> moduleDescriptorClass,
38                                                          SchemaFactory schemaFactory) {
39          this.plugin = checkNotNull(plugin);
40          this.moduleDescriptorClass = moduleDescriptorClass;
41          this.type = type;
42          this.schemaFactory = schemaFactory;
43          this.typeList = singleton(type);
44      }
45  
46      public ModuleDescriptor getModuleDescriptor(final String type) throws PluginParseException {
47          T result = null;
48          if (this.type.equals(type)) {
49              result = plugin.getContainerAccessor().createBean(moduleDescriptorClass);
50          }
51          return result;
52      }
53  
54      public boolean hasModuleDescriptor(final String type) {
55          return (this.type.equals(type));
56      }
57  
58      @Override
59      @Nullable
60      public Schema getSchema(String type) {
61          return (this.type.equals(type) ? schemaFactory.getSchema() : null);
62      }
63  
64      @Override
65      public Iterable<String> getModuleDescriptorKeys() {
66          return typeList;
67      }
68  
69      public Class<? extends ModuleDescriptor<?>> getModuleDescriptorClass(final String type) {
70          return (this.type.equals(type) ? moduleDescriptorClass : null);
71      }
72  
73      public Set<Class<? extends ModuleDescriptor>> getModuleDescriptorClasses() {
74          return ImmutableSet.of(moduleDescriptorClass);
75      }
76  }