1 5 package org.prevayler; 6 7 import java.io.IOException ; 8 import java.io.Serializable ; 9 10 import org.prevayler.implementation.PrevaylerImpl; 11 import org.prevayler.implementation.clock.MachineClock; 12 import org.prevayler.implementation.logging.PersistentLogger; 13 import org.prevayler.implementation.logging.TransactionLogger; 14 import org.prevayler.implementation.logging.TransientLogger; 15 import org.prevayler.implementation.publishing.CentralPublisher; 16 import org.prevayler.implementation.publishing.TransactionPublisher; 17 import org.prevayler.implementation.publishing.censorship.LiberalTransactionCensor; 18 import org.prevayler.implementation.publishing.censorship.StrictTransactionCensor; 19 import org.prevayler.implementation.publishing.censorship.TransactionCensor; 20 import org.prevayler.implementation.replication.ServerListener; 21 import org.prevayler.implementation.replication.ClientPublisher; 22 import org.prevayler.implementation.snapshot.NullSnapshotManager; 23 import org.prevayler.implementation.snapshot.SnapshotManager; 24 25 31 public class PrevaylerFactory { 32 33 private Object _prevalentSystem; 34 private Clock _clock; 35 36 private boolean _transactionFiltering = true; 37 38 private boolean _transientMode; 39 private String _prevalenceBase; 40 private SnapshotManager _snapshotManager; 41 42 private long _transactionLogSizeThreshold; 43 private long _transactionLogAgeThreshold; 44 45 private int _serverPort = -1; 46 private String _remoteServerIpAddress; 47 private int _remoteServerPort; 48 public static final int DEFAULT_REPLICATION_PORT = 8756; 49 50 51 54 public static Prevayler createPrevayler(Serializable newPrevalentSystem) throws IOException , ClassNotFoundException { 55 return createPrevayler(newPrevalentSystem, "PrevalenceBase"); 56 } 57 58 59 63 public static Prevayler createPrevayler(Serializable newPrevalentSystem, String prevalenceBase) throws IOException , ClassNotFoundException { 64 PrevaylerFactory factory = new PrevaylerFactory(); 65 factory.configurePrevalentSystem(newPrevalentSystem); 66 factory.configurePrevalenceBase(prevalenceBase); 67 return factory.create(); 68 } 69 70 71 77 public static Prevayler createTransientPrevayler(Serializable newPrevalentSystem) { 78 PrevaylerFactory factory = new PrevaylerFactory(); 79 factory.configurePrevalentSystem(newPrevalentSystem); 80 factory.configureSnapshotManager(new NullSnapshotManager(newPrevalentSystem, "Transient Prevaylers are unable to take snapshots.")); 81 factory.configureTransientMode(true); 82 try { 83 return factory.create(); 84 } catch (Exception e) { 85 e.printStackTrace(); return null; 87 } 88 } 89 90 91 93 public static Prevayler createTransientPrevayler(Serializable newPrevalentSystem, String snapshotDirectory) { 94 return createCheckpointPrevayler(newPrevalentSystem, snapshotDirectory); 95 } 96 97 101 public static Prevayler createCheckpointPrevayler(Serializable newPrevalentSystem, String snapshotDirectory) { 102 PrevaylerFactory factory = new PrevaylerFactory(); 103 factory.configurePrevalentSystem(newPrevalentSystem); 104 factory.configurePrevalenceBase(snapshotDirectory); 105 factory.configureTransientMode(true); 106 try { 107 return factory.create(); 108 } catch (Exception e) { 109 e.printStackTrace(); return null; 111 } 112 } 113 114 115 private Clock clock() { 116 return _clock != null ? _clock : new MachineClock(); 117 } 118 119 120 122 public void configureTransientMode(boolean transientMode) { 123 _transientMode = transientMode; 124 } 125 126 127 129 public void configureClock(Clock clock) { 130 _clock = clock; 131 } 132 133 134 137 public void configurePrevalenceBase(String prevalenceBase) { 138 _prevalenceBase = prevalenceBase; 139 } 140 141 142 146 public void configurePrevalentSystem(Object newPrevalentSystem) { 147 _prevalentSystem = newPrevalentSystem; 148 } 149 150 151 153 public void configureReplicationClient(String remoteServerIpAddress, int remoteServerPort) { 154 _remoteServerIpAddress = remoteServerIpAddress; 155 _remoteServerPort = remoteServerPort; 156 } 157 158 159 161 public void configureReplicationServer(int port) { 162 _serverPort = port; 163 } 164 165 166 168 public void configureSnapshotManager(SnapshotManager snapshotManager) { 169 _snapshotManager = snapshotManager; 170 } 171 172 173 175 public void configureTransactionFiltering(boolean transactionFiltering) { 176 _transactionFiltering = transactionFiltering; 177 } 178 179 180 184 public Prevayler create() throws IOException , ClassNotFoundException { 185 SnapshotManager snapshotManager = snapshotManager(); 186 TransactionPublisher publisher = publisher(snapshotManager); 187 if (_serverPort != -1) new ServerListener(publisher, _serverPort); 188 return new PrevaylerImpl(snapshotManager, publisher); 189 } 190 191 192 private String prevalenceBase() { 193 return _prevalenceBase != null ? _prevalenceBase : "PrevalenceBase"; 194 } 195 196 197 private Object prevalentSystem() { 198 if (_prevalentSystem == null) throw new IllegalStateException ("The prevalent system must be configured."); 199 return _prevalentSystem; 200 } 201 202 203 private TransactionPublisher publisher(SnapshotManager snapshotManager) throws IOException , ClassNotFoundException { 204 if (_remoteServerIpAddress != null) return new ClientPublisher(_remoteServerIpAddress, _remoteServerPort); 205 return new CentralPublisher(clock(), censor(snapshotManager), logger()); 206 } 207 208 209 private TransactionCensor censor(SnapshotManager snapshotManager) { 210 return _transactionFiltering 211 ? (TransactionCensor) new StrictTransactionCensor(snapshotManager) 212 : new LiberalTransactionCensor(); 213 } 214 215 216 private TransactionLogger logger() throws IOException , ClassNotFoundException { 217 return _transientMode 218 ? (TransactionLogger)new TransientLogger() 219 : new PersistentLogger(prevalenceBase(), _transactionLogSizeThreshold, _transactionLogAgeThreshold); 220 } 221 222 223 private SnapshotManager snapshotManager() throws ClassNotFoundException , IOException { 224 return _snapshotManager != null 225 ? _snapshotManager 226 : new SnapshotManager(prevalentSystem(), prevalenceBase()); 227 } 228 229 230 public void configureTransactionLogFileSizeThreshold(long sizeInBytes) { 231 _transactionLogSizeThreshold = sizeInBytes; 232 } 233 234 235 public void configureTransactionLogFileAgeThreshold(long ageInMilliseconds) { 236 _transactionLogAgeThreshold = ageInMilliseconds; 237 } 238 239 } 240 | Popular Tags |