1 25 26 package org.jrobin.core; 27 28 import java.util.HashMap ; 29 import java.io.IOException ; 30 31 62 public abstract class RrdBackendFactory { 63 private static final HashMap factories = new HashMap (); 64 private static RrdBackendFactory defaultFactory; 65 66 static { 67 try { 68 RrdFileBackendFactory fileFactory = new RrdFileBackendFactory(); 69 registerFactory(fileFactory); 70 RrdMemoryBackendFactory memoryFactory = new RrdMemoryBackendFactory(); 71 registerFactory(memoryFactory); 72 RrdNioBackendFactory nioFactory = new RrdNioBackendFactory(); 73 registerFactory(nioFactory); 74 75 defaultFactory = nioFactory; 77 78 } catch (RrdException e) { 79 throw new RuntimeException ("FATAL: Cannot register RRD backend factories: " + e); 80 } 81 } 82 83 98 public static synchronized RrdBackendFactory getFactory(String name) throws RrdException { 99 RrdBackendFactory factory = (RrdBackendFactory) factories.get(name); 100 if(factory != null) { 101 return factory; 102 } 103 else { 104 throw new RrdException("No backend factory found with the name specified [" + name + "]"); 105 } 106 } 107 108 114 public static synchronized void registerFactory(RrdBackendFactory factory) 115 throws RrdException { 116 String name = factory.getFactoryName(); 117 if(!factories.containsKey(name)) { 118 factories.put(name, factory); 119 } 120 else { 121 throw new RrdException("Backend factory of this name2 (" + name + 122 ") already exists and cannot be registered"); 123 } 124 } 125 126 133 public static synchronized void registerAndSetAsDefaultFactory(RrdBackendFactory factory) 134 throws RrdException { 135 registerFactory(factory); 136 setDefaultFactory(factory.getFactoryName()); 137 } 138 139 144 public static RrdBackendFactory getDefaultFactory() { 145 return defaultFactory; 146 } 147 148 157 public static void setDefaultFactory(String factoryName) throws RrdException { 158 if(RrdBackend.getCount() == 0) { 160 defaultFactory = getFactory(factoryName); 161 } 162 else { 163 throw new RrdException("Could not change the default backend factory. " + 164 "This method must be called before the first RRD gets created"); 165 } 166 } 167 168 178 protected abstract RrdBackend open(String path, boolean readOnly, int lockMode) 179 throws IOException ; 180 181 186 protected abstract boolean exists(String path) throws IOException ; 187 188 192 public abstract String getFactoryName(); 193 } 194 | Popular Tags |