comparison src/org/dancres/blitz/config/EntryConstraints.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.config;
2
3 import java.util.HashMap;
4
5 import java.util.logging.Level;
6
7 import net.jini.config.ConfigurationException;
8
9 import org.dancres.blitz.config.*;
10
11 /**
12 * @todo Move this into the config package and add a factory to track all
13 * of them. Also define a MINIMAL_CONSTRAINTS static which is an instance of
14 * this class created via the null constructor.
15 */
16 public class EntryConstraints {
17
18 public static final EntryConstraints MINIMUM =
19 new EntryConstraints();
20
21 private static int DEFAULT_CACHE_SIZE;
22 private static int DEFAULT_READ_AHEAD;
23
24 static {
25 try {
26 DEFAULT_CACHE_SIZE =
27 ((Integer)
28 ConfigurationFactory.getEntry("entryReposCacheSize",
29 int.class,
30 new Integer(200))).intValue();
31
32 ConfigurationFactory.theLogger.log(Level.INFO,
33 "Cache size: " + DEFAULT_CACHE_SIZE);
34
35 DEFAULT_READ_AHEAD =
36 ((Integer)
37 ConfigurationFactory.getEntry("entryReposReadahead",
38 int.class,
39 new Integer(0))).intValue();
40
41 ConfigurationFactory.theLogger.log(Level.INFO,
42 "Read ahead: " + DEFAULT_READ_AHEAD);
43 } catch (ConfigurationException aCE) {
44 }
45 }
46
47 private static HashMap theAllConstraints = new HashMap();
48
49 private HashMap theConstraints = new HashMap();
50 private String theType;
51
52 public static EntryConstraints getConstraints(String aType)
53 throws ConfigurationException {
54 synchronized(theAllConstraints) {
55 EntryConstraints myConstraints =
56 (EntryConstraints) theAllConstraints.get(aType);
57
58 if (myConstraints == null) {
59 myConstraints = new EntryConstraints(aType);
60
61 theAllConstraints.put(aType, myConstraints);
62 }
63
64 return myConstraints;
65 }
66 }
67
68 /**
69 Create an instance of EntryConstraints with no settings loaded from
70 config and a minimal set of useful constraints.
71 */
72 private EntryConstraints() {
73 theConstraints.put(CacheSize.class, new CacheSize(1));
74 }
75
76 private EntryConstraints(String aType) throws ConfigurationException {
77 theType = aType;
78
79 init();
80 }
81
82 private void init() throws ConfigurationException {
83 String myConfigName = theType.replaceAll("\\.", "_");
84 myConfigName = myConfigName.replaceAll("\\$", "_");
85
86 EntryConstraint[] myConstraints = (EntryConstraint[])
87 ConfigurationFactory.getEntry(myConfigName,
88 EntryConstraint[].class,
89 new EntryConstraint[0]);
90
91 for (int i = 0; i < myConstraints.length; i++) {
92 theConstraints.put(myConstraints[i].getClass(), myConstraints[i]);
93 }
94
95 if (theConstraints.get(CacheSize.class) == null) {
96 theConstraints.put(CacheSize.class,
97 new CacheSize(DEFAULT_CACHE_SIZE));
98 }
99
100 // If FIFO is enabled, force read ahead to zero
101 //
102 if (theConstraints.get(Fifo.class) != null) {
103 theConstraints.put(ReadAhead.class, new ReadAhead(0));
104 } else {
105 if (theConstraints.get(ReadAhead.class) == null) {
106 theConstraints.put(ReadAhead.class,
107 new ReadAhead(DEFAULT_READ_AHEAD));
108 }
109 }
110 }
111
112 public EntryConstraint get(Class aConstraint) {
113 EntryConstraint myResult =
114 (EntryConstraint) theConstraints.get(aConstraint);
115
116 return myResult;
117 }
118 }