KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcsimulator > DistributedGlobalIdGenerator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tcsimulator;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.simulator.app.GlobalIdGenerator;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 public class DistributedGlobalIdGenerator implements GlobalIdGenerator {
15
16   private final List JavaDoc currentId = new ArrayList JavaDoc();
17
18   public long nextId() {
19     synchronized (currentId) {
20       Long JavaDoc newId;
21       if (currentId.size() == 0) {
22         newId = new Long JavaDoc(0);
23         currentId.add(newId);
24       } else {
25         Long JavaDoc id = (Long JavaDoc) currentId.get(0);
26         newId = new Long JavaDoc(id.longValue() + 1);
27         currentId.set(0, newId);
28       }
29       return newId.longValue();
30     }
31   }
32
33   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper cfg) {
34     String JavaDoc classname = DistributedGlobalIdGenerator.class.getName();
35     TransparencyClassSpec spec = cfg.getOrCreateSpec(classname);
36     spec.addRoot("DistributedGlobalIdGeneratorcurrentId", classname + ".currentId");
37     cfg.addWriteAutolock("long " + classname + ".nextId()");
38   }
39
40   public static void visitDSOApplicationConfig(com.tc.object.config.ConfigVisitor visitor,
41                                                com.tc.object.config.DSOApplicationConfig config) {
42     String JavaDoc classname = DistributedGlobalIdGenerator.class.getName();
43     config.addIncludePattern(classname);
44     config.addRoot("DistributedGlobalIdGeneratorcurrentId", classname + ".currentId");
45     config.addWriteAutolock("* " + classname + ".*(..)");
46   }
47
48 }
Free Books   Free Magazines  
Popular Tags