01: package org.osbl.domain;
02:
03: import org.apache.commons.logging.LogFactory;
04: import org.osbl.domain.model.Domain;
05: import org.osbl.domain.logic.DomainLogic;
06: import org.osbl.ServiceProvider;
07:
08: import java.util.*;
09:
10: public class DomainRegistry {
11: private static org.apache.commons.logging.Log LOG = LogFactory
12: .getLog(DomainRegistry.class);
13: private static final Map<String, Domain> domains = new HashMap<String, Domain>();
14: private static DomainLogic domainLogic;
15:
16: private static final Map<String, String> registrations = new HashMap<String, String>();
17:
18: private DomainRegistry() {
19: }
20:
21: public static void registerDomain(String key, String type) {
22: registrations.put(key, type);
23: LOG.info("Register Domain: " + key);
24: }
25:
26: public static Domain getDomain(String key) {
27: if (domains.size() == 0)
28: loadDomains();
29: return domains.get(key);
30: }
31:
32: public static Collection<Domain> getDomains() {
33: if (domains.size() == 0)
34: loadDomains();
35: return domains.values();
36: }
37:
38: public static DomainLogic getDomainLogic() {
39: if (domainLogic == null)
40: domainLogic = (DomainLogic) ServiceProvider.getInstance()
41: .getService("DomainLogic");
42: return domainLogic;
43: }
44:
45: private static void loadDomains() {
46: Collection<Domain> domains = getDomainLogic().loadDomains();
47: for (Domain domain : domains) {
48: DomainRegistry.domains.put(domain.getKey(), domain);
49: }
50: checkRegistrations();
51: }
52:
53: private static void checkRegistrations() {
54: for (Map.Entry<String, String> entry : registrations.entrySet()) {
55: String key = entry.getKey();
56: String type = entry.getValue();
57:
58: if (domains.get(key) == null) {
59: Domain domain = getDomainLogic().newDomain(key, type);
60: DomainRegistry.domains.put(domain.getKey(), domain);
61:
62: LOG.info("Create new Domain: " + key);
63: }
64: }
65: }
66:
67: public static void updateDomain(Domain domain) {
68: domains.put(domain.getKey(), domain);
69: }
70: }
|