1 5 package ve.luz.ica.jackass.daemon; 6 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.FileOutputStream ; 10 import java.io.IOException ; 11 import java.util.Properties ; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 29 public final class IDManager 30 { 31 private static final Log LOG = LogFactory.getLog(IDManager.class); 32 33 private static final String ID_SEQUENCE_NUMBER = "ID_sequence_number"; 34 private static final String OBJECT_ID_FILENAME = "ObjectID.db"; 35 private static final String SEPARATOR = "."; 36 private static IDManager manager = null; 37 38 private Properties objectIDDatabase; 39 40 44 public static IDManager getInstance() 45 { 46 if (manager == null) 47 { 48 manager = new IDManager(); 49 } 50 return manager; 51 } 52 53 56 private IDManager() 57 { 58 try 59 { 60 objectIDDatabase = new Properties (); 61 FileInputStream in = new FileInputStream (OBJECT_ID_FILENAME); 62 objectIDDatabase.load(in); 63 } 64 catch (FileNotFoundException e) 65 { 66 LOG.error("Object ID file not found"); 67 } 68 catch (IOException e) 69 { 70 LOG.error("Unable to read Object ID file"); 71 } 72 } 73 74 80 public byte[] getObjectID(String applicationName, String componentName) 81 { 82 String idString = (String ) this.objectIDDatabase.get(applicationName + SEPARATOR + componentName); 83 return idString.getBytes(); 84 } 85 86 92 public void storeObjectID(String applicationName, String componentName, byte[] objectID) 93 { 94 String key = applicationName + SEPARATOR + componentName; 95 String id = new String (objectID); 96 this.objectIDDatabase.put(key, id); 97 FileOutputStream out; 98 try 99 { 100 out = new FileOutputStream (OBJECT_ID_FILENAME); 101 this.objectIDDatabase.store(out, "Component ID database. Do not edit"); 102 } 103 catch (FileNotFoundException e) 104 { 105 LOG.error("Unable to create file ObjecID.db"); 106 e.printStackTrace(); 107 } 108 catch (IOException e) 109 { 110 LOG.error("Unable to create file ObjecID.db"); 111 e.printStackTrace(); 112 } 113 } 114 115 120 public void removeObjectID(String applicationName, String componentName) 121 { 122 String key = applicationName + SEPARATOR + componentName; 123 this.objectIDDatabase.remove(key); 124 FileOutputStream out; 125 try 126 { 127 out = new FileOutputStream (OBJECT_ID_FILENAME); 128 this.objectIDDatabase.store(out, "Component ID database. Do not edit"); 129 } 130 catch (FileNotFoundException e) 131 { 132 LOG.error("Unable to create file ObjecID.db"); 133 e.printStackTrace(); 134 } 135 catch (IOException e) 136 { 137 LOG.error("Unable to create file ObjecID.db"); 138 e.printStackTrace(); 139 } 140 } 141 142 146 public byte[] generateObjectId() 147 { 148 long idSequence = 0; 149 String str = (String ) objectIDDatabase.get(ID_SEQUENCE_NUMBER); 150 if (str != null) 151 { 152 idSequence = Long.parseLong(str); 153 } 154 idSequence++; 155 if (idSequence >= Long.MAX_VALUE) 156 { 157 idSequence = 0; 158 } 159 String s = Long.toString(idSequence); 160 this.objectIDDatabase.put(ID_SEQUENCE_NUMBER, s); 161 FileOutputStream out; 162 try 163 { 164 out = new FileOutputStream (OBJECT_ID_FILENAME); 165 this.objectIDDatabase.store(out, "Component ID database. Do not edit"); 166 } 167 catch (FileNotFoundException e) 168 { 169 LOG.error("Unable to create file ObjecID.db"); 170 e.printStackTrace(); 171 } 172 catch (IOException e) 173 { 174 LOG.error("Unable to create file " + OBJECT_ID_FILENAME); 175 e.printStackTrace(); 176 } 177 return s.getBytes(); 178 } 179 180 } 181 | Popular Tags |