View Javadoc

1   package org.treetank.access.conf;
2   
3   import java.security.Key;
4   
5   import org.treetank.access.conf.ResourceConfiguration.IResourceConfigurationFactory;
6   import org.treetank.access.conf.SessionConfiguration.ISessionConfigurationFactory;
7   import org.treetank.api.IMetaEntryFactory;
8   import org.treetank.api.IDataFactory;
9   import org.treetank.io.IBackend;
10  import org.treetank.io.IBackend.IBackendFactory;
11  import org.treetank.io.berkeley.BerkeleyStorage;
12  import org.treetank.io.bytepipe.ByteHandlerPipeline;
13  import org.treetank.io.bytepipe.Encryptor;
14  import org.treetank.io.bytepipe.IByteHandler;
15  import org.treetank.io.bytepipe.IByteHandler.IByteHandlerPipeline;
16  import org.treetank.io.jclouds.JCloudsStorage;
17  import org.treetank.revisioning.IRevisioning;
18  import org.treetank.revisioning.SlidingSnapshot;
19  
20  import com.google.inject.AbstractModule;
21  import com.google.inject.assistedinject.FactoryModuleBuilder;
22  
23  /**
24   * Builder-like construction to generate AbstractModules based on former set values;
25   * 
26   * @author Sebastian Graf, University of Konstanz.
27   * 
28   */
29  public class ModuleSetter {
30  
31      /** Class for DataFactory. */
32      private Class<? extends IDataFactory> mDataFacClass;
33      /** Class for MetaFactory. */
34      private Class<? extends IMetaEntryFactory> mMetaFacClass;
35      /** Class for Revision. */
36      private Class<? extends IRevisioning> mRevisioningClass = SlidingSnapshot.class;
37      /** Class for IBackend. */
38      private Class<? extends IBackend> mBackend = JCloudsStorage.class;
39      /** Instance for Key. */
40      private Key mKey = StandardSettings.KEY;
41      /** Instance for ByteHandler. */
42      private IByteHandlerPipeline mByteHandler = new ByteHandlerPipeline(new Encryptor(mKey));
43  
44      /**
45       * Setting an {@link IDataFactory}-class
46       * 
47       * @param pDataFac
48       *            to be set
49       * @return the current ModuleSetter
50       */
51      public ModuleSetter setDataFacClass(final Class<? extends IDataFactory> pDataFac) {
52          this.mDataFacClass = pDataFac;
53          return this;
54      }
55  
56      /**
57       * Setting an {@link IMetaEntryFactory}-class
58       * 
59       * @param pMetaFac
60       *            to be set
61       * @return the current ModuleSetter
62       */
63      public ModuleSetter setMetaFacClass(final Class<? extends IMetaEntryFactory> pMetaFac) {
64          this.mMetaFacClass = pMetaFac;
65          return this;
66      }
67  
68      /**
69       * Setting an {@link IRevisioning}-class
70       * 
71       * @param pRevision
72       *            to be set
73       * @return the current ModuleSetter
74       */
75      public ModuleSetter setRevisioningClass(final Class<? extends IRevisioning> pRevision) {
76          this.mRevisioningClass = pRevision;
77          return this;
78      }
79  
80      /**
81       * Setting an {@link IBackend}-class
82       * 
83       * @param pBackend
84       *            to be set
85       * @return the current ModuleSetter
86       */
87      public ModuleSetter setBackendClass(final Class<? extends IBackend> pBackend) {
88          this.mBackend = pBackend;
89          return this;
90      }
91  
92      /**
93       * Setting an {@link IByteHandler}-instance.
94       * 
95       * @param pByteHandler
96       *            to be set
97       * @return the current ModuleSetter
98       */
99      public ModuleSetter setIByteHandlerInstance(final IByteHandlerPipeline pByteHandler) {
100         this.mByteHandler = pByteHandler;
101         return this;
102     }
103 
104     /**
105      * Setting an {@link Key}-instance.
106      * 
107      * @param pKey
108      *            to be set
109      * @return the current ModuleSetter
110      */
111     public ModuleSetter setKeyInstance(final Key pKey) {
112         this.mKey = pKey;
113         return this;
114     }
115 
116     /**
117      * Creating an Guice Module based on the parameters set.
118      * 
119      * @return the {@link AbstractModule} to be set
120      */
121     public AbstractModule createModule() {
122         return new AbstractModule() {
123             @Override
124             protected void configure() {
125                 bind(IDataFactory.class).to(mDataFacClass);
126                 bind(IMetaEntryFactory.class).to(mMetaFacClass);
127                 bind(IRevisioning.class).to(mRevisioningClass);
128                 bind(IByteHandlerPipeline.class).toInstance(mByteHandler);
129                 install(new FactoryModuleBuilder().implement(IBackend.class, mBackend).build(
130                     IBackendFactory.class));
131                 install(new FactoryModuleBuilder().build(IResourceConfigurationFactory.class));
132                 bind(Key.class).toInstance(mKey);
133                 install(new FactoryModuleBuilder().build(ISessionConfigurationFactory.class));
134             }
135         };
136     }
137 
138 }