/src/java/com/spinn3r/api/Factory.java

https://code.google.com/p/spinn3r-client/ · Java · 130 lines · 95 code · 29 blank · 6 comment · 5 complexity · 99f738157fede2a551a5461d68c34d9a MD5 · raw file

  1. package com.spinn3r.api;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.Collection;
  6. import java.util.LinkedList;
  7. import com.google.inject.AbstractModule;
  8. import com.google.inject.Guice;
  9. import com.google.inject.Injector;
  10. import com.google.inject.Module;
  11. import com.google.inject.Provider;
  12. import com.google.inject.Provides;
  13. import com.google.inject.Singleton;
  14. public class Factory {
  15. /*
  16. * This module provides a rotating log manager when
  17. */
  18. protected static class LogManagerModule extends AbstractModule {
  19. public LogManagerModule(File saveDirectory, int maxLogSize) {
  20. super();
  21. this.saveDirectory = saveDirectory;
  22. this.maxLogSize = maxLogSize;
  23. }
  24. protected void configure() {
  25. bind(OutputStream.class).toProvider(TempStreamManager.class);
  26. }
  27. @Provides
  28. protected TransactionHistoryManager getLogManager(Provider<SingleFileHistoryManager> provider) {
  29. return new RotatingFileManager(maxLogSize, provider);
  30. }
  31. @Provides
  32. public File getTempFile() throws IOException {
  33. return File.createTempFile("transaction", ".log", saveDirectory);
  34. }
  35. private final File saveDirectory;
  36. private final int maxLogSize;
  37. }
  38. protected static class FreshStartModule extends AbstractModule {
  39. @Override
  40. protected void configure() {
  41. bind(UniversalCounter.class).in(Singleton.class);
  42. }
  43. @Provides
  44. protected SingleFileHistoryManager getSingleLogManager(OutputStream os, UniversalCounter counter) {
  45. return new SingleFileHistoryManager(os, counter);
  46. }
  47. }
  48. protected static class RestartModule extends AbstractModule {
  49. private final long counter;
  50. private final String url;
  51. protected RestartModule(long counter, String url) {
  52. this.counter= counter;
  53. this.url = url;
  54. }
  55. @Override
  56. protected void configure() {
  57. bind(UniversalCounter.class).toInstance(new UniversalCounter(counter));
  58. }
  59. @Provides
  60. protected SingleFileHistoryManager getSingleLogManager(OutputStream os, UniversalCounter counter) {
  61. SingleFileHistoryManager manager = new SingleFileHistoryManager(os, counter);
  62. manager.log(url);
  63. return manager;
  64. }
  65. }
  66. /*
  67. * This module provides a Null Log Manager.
  68. */
  69. protected static class NullLogModule extends AbstractModule {
  70. @Override
  71. protected void configure() {
  72. bind(TransactionHistoryManager.class).to(NullLogManager.class).in(Singleton.class);
  73. }
  74. }
  75. private File saveDirectory;
  76. private int maxLogSize;
  77. public void enableLogging(File saveDirectory, int maxLogSize) {
  78. this.saveDirectory = saveDirectory;
  79. this.maxLogSize = maxLogSize;
  80. }
  81. private long counter;
  82. private String restartUrl;
  83. public void enableRestart(long counter, String url) {
  84. this.counter = counter;
  85. this.restartUrl = url;
  86. }
  87. protected Injector getInjector() {
  88. Collection<Module> modules = new LinkedList<Module>();
  89. if (saveDirectory == null) {
  90. modules.add(new NullLogModule());
  91. }
  92. else {
  93. modules.add(new LogManagerModule(saveDirectory, maxLogSize));
  94. if (restartUrl == null)
  95. modules.add(new FreshStartModule());
  96. else
  97. modules.add(new RestartModule(counter, restartUrl));
  98. }
  99. return Guice.createInjector(modules);
  100. }
  101. }