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  {
22      private final ContainerManagedPlugin plugin;
23      private final String type;
24      private final Iterable<String> typeList;
25      private final Class<T> moduleDescriptorClass;
26      private final SchemaFactory schemaFactory;
27  
28      /**
29       * Constructs an instance using a specific host container
30       *
31       * @param type The type of module
32       * @param moduleDescriptorClass The descriptor class
33       * @param schemaFactory the schema factory
34       * @since 3.0.0
35       */
36      DescribedModuleTypeDescribedModuleDescriptorFactory(ContainerManagedPlugin plugin,
37                                                          final String type,
38                                                          final Class<T> moduleDescriptorClass,
39                                                          SchemaFactory schemaFactory)
40      {
41          this.plugin = checkNotNull(plugin);
42          this.moduleDescriptorClass = moduleDescriptorClass;
43          this.type = type;
44          this.schemaFactory = schemaFactory;
45          this.typeList = singleton(type);
46      }
47  
48      public ModuleDescriptor getModuleDescriptor(final String type) throws PluginParseException, IllegalAccessException, InstantiationException, ClassNotFoundException
49      {
50          T result = null;
51          if (this.type.equals(type))
52          {
53              result = plugin.getContainerAccessor().createBean(moduleDescriptorClass);
54          }
55          return result;
56      }
57  
58      public boolean hasModuleDescriptor(final String type)
59      {
60          return (this.type.equals(type));
61      }
62  
63      @Override
64      @Nullable
65      public Schema getSchema(String type)
66      {
67          return (this.type.equals(type) ? schemaFactory.getSchema() : null);
68      }
69  
70      @Override
71      public Iterable<String> getModuleDescriptorKeys()
72      {
73          return typeList;
74      }
75  
76      public Class<? extends ModuleDescriptor<?>> getModuleDescriptorClass(final String type)
77      {
78          return (this.type.equals(type) ? moduleDescriptorClass : null);
79      }
80  
81      public Set<Class<? extends ModuleDescriptor>> getModuleDescriptorClasses()
82      {
83          return ImmutableSet.<Class<? extends ModuleDescriptor>>of(moduleDescriptorClass);
84      }
85  }