1 17 package org.apache.ldap.server.db.jdbm; 18 19 20 import jdbm.RecordManager; 21 import jdbm.helper.StringComparator; 22 import org.apache.ldap.common.util.BigIntegerComparator; 23 import org.apache.ldap.server.db.MasterTable; 24 import org.apache.ldap.server.schema.SerializableComparator; 25 26 import javax.naming.NamingException ; 27 import javax.naming.directory.Attributes ; 28 import java.math.BigInteger ; 29 30 31 37 public class JdbmMasterTable extends JdbmTable implements MasterTable 38 { 39 private static final StringComparator STRCOMP = new StringComparator(); 40 private static final SerializableComparator BIG_INTEGER_COMPARATOR = 41 new SerializableComparator( "1.2.6.1.4.1.18060.1.1.1.2.2" ) 42 { 43 private static final long serialVersionUID = 4048791282048841016L; 44 45 public int compare( Object o1, Object o2 ) 46 { 47 return BigIntegerComparator.INSTANCE.compare( o1, o2 ); 48 } 49 }; 50 private static final SerializableComparator STRING_COMPARATOR = 51 new SerializableComparator( "1.2.6.1.4.1.18060.1.1.1.2.3" ) 52 { 53 private static final long serialVersionUID = 3258689922792961845L; 54 55 public int compare( Object o1, Object o2 ) 56 { 57 return STRCOMP.compare( o1, o2 ); 58 } 59 }; 60 61 private JdbmTable adminTbl = null; 62 63 64 70 public JdbmMasterTable( RecordManager recMan ) 71 throws NamingException 72 { 73 super( DBF, recMan, BIG_INTEGER_COMPARATOR ); 74 adminTbl = new JdbmTable( "admin", recMan, STRING_COMPARATOR ); 75 String seqValue = ( String ) adminTbl.get( SEQPROP_KEY ); 76 77 if ( null == seqValue ) 78 { 79 adminTbl.put( SEQPROP_KEY, BigInteger.ZERO.toString() ); 80 } 81 } 82 83 84 91 public Attributes get( BigInteger id ) throws NamingException 92 { 93 return ( Attributes ) super.get( id ); 94 } 95 96 97 107 public Attributes put( Attributes entry, BigInteger id ) throws NamingException 108 { 109 return ( Attributes ) super.put( id, entry ); 110 } 111 112 113 120 public Attributes delete( BigInteger id ) throws NamingException 121 { 122 return ( Attributes ) super.remove( id ); 123 } 124 125 126 134 public BigInteger getCurrentId() throws NamingException 135 { 136 BigInteger id = null; 137 138 synchronized ( adminTbl ) 139 { 140 id = new BigInteger ( ( String ) adminTbl.get( SEQPROP_KEY ) ); 141 142 if ( null == id ) 143 { 144 adminTbl.put( SEQPROP_KEY, BigInteger.ZERO.toString() ); 145 id = BigInteger.ZERO; 146 } 147 } 148 149 return id; 150 } 151 152 153 163 public BigInteger getNextId() throws NamingException 164 { 165 BigInteger lastVal = null; 166 BigInteger nextVal = null; 167 168 synchronized ( adminTbl ) 169 { 170 lastVal = new BigInteger ( ( String ) 171 adminTbl.get( SEQPROP_KEY ) ); 172 173 if ( null == lastVal ) 174 { 175 adminTbl.put( SEQPROP_KEY, BigInteger.ONE.toString() ); 176 return BigInteger.ONE; 177 } 178 else 179 { 180 nextVal = lastVal.add( BigInteger.ONE ); 181 adminTbl.put( SEQPROP_KEY, nextVal.toString() ); 182 } 183 } 184 185 return nextVal; 186 } 187 188 189 196 public String getProperty( String property ) throws NamingException 197 { 198 synchronized ( adminTbl ) 199 { 200 return ( String ) adminTbl.get( property ); 201 } 202 } 203 204 205 212 public void setProperty( String property, String value ) throws NamingException 213 { 214 synchronized ( adminTbl ) 215 { 216 adminTbl.put( property, value ); 217 } 218 } 219 } 220 | Popular Tags |