1 4 package com.openedit.util; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.util.Properties ; 11 12 import com.openedit.OpenEditRuntimeException; 13 14 public class IntCounter 15 { 16 File fieldCounterFile; 17 18 public synchronized int incrementCount() 19 { 20 try 21 { 22 int i = getCustomerIdCounter(); 23 i++; 24 saveCount(i); 25 return i; 26 } catch (IOException ex) 27 { 28 throw new OpenEditRuntimeException(ex); 29 } 30 } 31 32 35 protected void saveCount(int inCount) throws IOException 36 { 37 FileOutputStream out = new FileOutputStream (getCounterFile()); 38 try 39 { 40 Properties fieldCustomerIdCounter = new Properties (); 41 fieldCustomerIdCounter.setProperty("currentCount",String.valueOf(inCount)); 42 fieldCustomerIdCounter.store(out,""); 43 } 44 finally 45 { 46 out.close(); 47 } 48 } 49 50 protected synchronized int getCustomerIdCounter() throws IOException , IllegalStateException 51 { 52 Properties fieldCustomerIdCounter = new Properties (); 53 if ( !getCounterFile().exists() ) 54 { 55 saveCount(100); 56 } 57 58 FileInputStream in = new FileInputStream ( getCounterFile() ); 59 try 60 { 61 fieldCustomerIdCounter.load( in ); 62 } 63 finally 64 { 65 in.close(); 66 } 67 68 String count = fieldCustomerIdCounter.getProperty( "currentCount" ); 69 if ( count == null ) 70 { 71 throw new IllegalStateException ( "Could not find valid ID counter " + getCounterFile() ); 72 } 73 74 int i = Integer.parseInt( count ); 75 return i; 76 } 77 78 public File getCounterFile() 79 { 80 return fieldCounterFile; 81 } 82 83 public void setCounterFile(File inCounterFile) 84 { 85 fieldCounterFile = inCounterFile; 86 } 87 88 89 90 } 91 | Popular Tags |