IoC Utils sample application

A sample application that demonstrate how to use IoC Utils on a real world scenario will provided next.

Suppose a file exporting utility to be used on a larger-scale project. This application must provide extension capabilities to support different file formats. Those extensions must be picked up automatically without any further source code intervention by the developer.

For the full source code of this sample application go to the Downloads page.

The interface and implementations

The IFileExporter interface

This interface describes the behavior of the file exporter implementations. It defines a method for exporting a file and a getter that returns a description for the exporter.

 1 public interface IFileExporter {
 2
 3   /** 
 4   * Getter for the exporter's description.
 5   * 
 6   * @return the exporter's description
 7   */
 8   public String getExporterDescriptor();
 9
10    /**
11    * Export file method.
12    * 
13    * @param fileName
14    *            the name of the file to save
15    * @return T if the operation succeeded, F otherwise
16    */
17    public boolean exportFile(String fileName);
18 }

The sample exporters

This sample provides three different exporters. All share the same mock-up implementation, just changing the description.

Notice the injection of the logger service. This will be automatically bound by the IoC.

FileExporterDocFormat
 1  public class FileExporterDocImpl implements IFileExporter {
 2  
 3      /** The logger. */
 4      @Inject
 5      public ILogWrapper logger;
 6      
 7      /**
 8       * @see pt.digitalis.sandbox.ioc.IFileExporter#exportFile(java.lang.String)
 9       */
10      public boolean exportFile(String fileName) {
11          
12          logger.info("Exported " + fileName + " (" + getExporterDescriptor() + ")");
13          
14          return true;
15      }
16  
17      /**
18       * @see pt.digitalis.sandbox.ioc.IFileExporter#getExporterDescriptor()
19       */
20      public String getExporterDescriptor() {
21          return "Microsoft Word Document file exporter";
22      }
23  }
FileExporterHTMLFormat
 1  public class FileExporterHTMLImpl implements IFileExporter {
 2  
 3      /** The logger. */
 4      @Inject
 5      public ILogWrapper logger;
 6      
 7      /**
 8       * @see pt.digitalis.sandbox.ioc.IFileExporter#exportFile(java.lang.String)
 9       */
10      public boolean exportFile(String fileName) {
11          
12          logger.info("Exported " + fileName + " (" + getExporterDescriptor() + ")");
13          
14          return true;
15      }
16  
17      /**
18       * @see pt.digitalis.sandbox.ioc.IFileExporter#getExporterDescriptor()
19       */
20      public String getExporterDescriptor() {
21          return "HTML Page exporter";
22      }
23  }
FileExporterPDFFormat
 1  public class FileExporterPDFImpl implements IFileExporter {
 2  
 3      /** The logger. */
 4      @Inject
 5      public ILogWrapper logger;
 6      
 7      /**
 8       * @see pt.digitalis.sandbox.ioc.IFileExporter#exportFile(java.lang.String)
 9       */
10      public boolean exportFile(String fileName) {
11          
12          logger.info("Exported " + fileName + " (" + getExporterDescriptor() + ")");
13          
14          return true;
15      }
16  
17      /**
18       * @see pt.digitalis.sandbox.ioc.IFileExporter#getExporterDescriptor()
19       */
20      public String getExporterDescriptor() {
21          return "PDF Document file exporter";
22      }
23  }

The module configuration

As stated before the IoC configuration has two elements: the module list on the modules.properties file and the bindings configuration on the IIoCModule implementing class

The module descriptor

The 'module.properties' file is as follows:

 1  modules=pt.digitalis.sandbox.ioc.SampleModule
The Module class

The module class file is as follows:

 1  public class SampleModule implements IIoCModule {
 2  
 3      /**
 4       * @see pt.digitalis.utils.ioc.modules.IIoCModule#configure(pt.digitalis.utils.ioc.modules.IoCBinder)
 5       */
 6      public void configure(IoCBinder binder) {
 7          binder.bind(ILogWrapper.class, LogWrapperLog4JImpl.class);
 8          
 9          binder.bind(IFileExporter.class,FileExporterDocImpl.class).withId("doc");
10          binder.bind(IFileExporter.class,FileExporterHTMLImpl.class).withId("html");
11          binder.bind(IFileExporter.class, FileExporterPDFImpl.class).withId("pdf");
12      }
13  }

Multiple contributions are bound to the service interface through an ID. This ID is used to distinguish among the implementations. If the ID is not supplied on a multiple-binding scenario an error notification is issued.

The client class

The caller class will ask the registry for the list of implementations available and run them. Heres the code:

 1      public static void main(String[] args) {
 2          
 3          IIoCRegistry registry = IoCRegistryGuiceImpl.getRegistry();
 4          
 5          ILogWrapper logger = registry.getImplementation(ILogWrapper.class);
 6          List<IFileExporter> exporters = registry.getImplementations(IFileExporter.class);
 7          
 8          logger.setInfoLogLevel();
 9          logger.info("Starting export of file to all " + exporters.size() + " supported formats...");
10          
11          for (IFileExporter exporter : exporters) {
12              exporter.exportFile("mockupFileName");
13          }
14  
15          logger.info("Export complete.");
16      }

Take note of the following details on the code above:

  • All the logger-related operations are reflected into the same instance. By default all bindings have the singleton scope, so the same instance will be returned.
  • The exporters list contains the three exporters that implement the IFileExporter interface . If new implementations are added they will be picked-up as well.

Running the code above will yield the following result:

Oct 22 23:49:25,055 [main] INFO   - Starting export of file to all 3 supported formats...
Oct 22 23:49:25,056 [main] INFO   - Exported mockupFileName (Microsoft Word Document file exporter)
Oct 22 23:49:25,056 [main] INFO   - Exported mockupFileName (HTML page exporter)
Oct 22 23:49:25,056 [main] INFO   - Exported mockupFileName (PDF Document file exporter)
Oct 22 23:49:25,056 [main] INFO   - Export complete.

Source code

For the full source code of this sample application go to the Downloads page.