01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tcsimulator;
05:
06: import com.tc.object.config.ConfigVisitor;
07: import com.tc.object.config.DSOClientConfigHelper;
08: import com.tc.object.config.TransparencyClassSpec;
09: import com.tc.simulator.app.GlobalIdGenerator;
10:
11: import java.util.ArrayList;
12: import java.util.List;
13:
14: public class DistributedGlobalIdGenerator implements GlobalIdGenerator {
15:
16: private final List currentId = new ArrayList();
17:
18: public long nextId() {
19: synchronized (currentId) {
20: Long newId;
21: if (currentId.size() == 0) {
22: newId = new Long(0);
23: currentId.add(newId);
24: } else {
25: Long id = (Long) currentId.get(0);
26: newId = new Long(id.longValue() + 1);
27: currentId.set(0, newId);
28: }
29: return newId.longValue();
30: }
31: }
32:
33: public static void visitL1DSOConfig(ConfigVisitor visitor,
34: DSOClientConfigHelper cfg) {
35: String classname = DistributedGlobalIdGenerator.class.getName();
36: TransparencyClassSpec spec = cfg.getOrCreateSpec(classname);
37: spec.addRoot("DistributedGlobalIdGeneratorcurrentId", classname
38: + ".currentId");
39: cfg.addWriteAutolock("long " + classname + ".nextId()");
40: }
41:
42: public static void visitDSOApplicationConfig(
43: com.tc.object.config.ConfigVisitor visitor,
44: com.tc.object.config.DSOApplicationConfig config) {
45: String classname = DistributedGlobalIdGenerator.class.getName();
46: config.addIncludePattern(classname);
47: config.addRoot("DistributedGlobalIdGeneratorcurrentId",
48: classname + ".currentId");
49: config.addWriteAutolock("* " + classname + ".*(..)");
50: }
51:
52: }
|