001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema.setup;
005:
006: import org.apache.xmlbeans.XmlObject;
007:
008: import com.tc.config.schema.IllegalConfigurationChangeHandler;
009: import com.tc.config.schema.context.ConfigContext;
010: import com.tc.config.schema.context.StandardConfigContext;
011: import com.tc.config.schema.defaults.DefaultValueProvider;
012: import com.tc.config.schema.repository.ApplicationsRepository;
013: import com.tc.config.schema.repository.BeanRepository;
014: import com.tc.config.schema.repository.ChildBeanFetcher;
015: import com.tc.config.schema.repository.ChildBeanRepository;
016: import com.tc.config.schema.repository.MutableBeanRepository;
017: import com.tc.config.schema.repository.StandardApplicationsRepository;
018: import com.tc.config.schema.repository.StandardBeanRepository;
019: import com.tc.config.schema.utils.XmlObjectComparator;
020: import com.tc.object.config.schema.NewDSOApplicationConfig;
021: import com.tc.object.config.schema.NewDSOApplicationConfigObject;
022: import com.tc.object.config.schema.NewSpringApplicationConfig;
023: import com.tc.object.config.schema.NewSpringApplicationConfigObject;
024: import com.tc.util.Assert;
025: import com.terracottatech.config.Application;
026: import com.terracottatech.config.Client;
027: import com.terracottatech.config.DsoApplication;
028: import com.terracottatech.config.Servers;
029: import com.terracottatech.config.SpringApplication;
030: import com.terracottatech.config.System;
031:
032: import java.io.File;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.Map;
036: import java.util.Set;
037:
038: /**
039: * A base class for all TVS configuration setup managers.
040: */
041: public class BaseTVSConfigurationSetupManager {
042:
043: private final MutableBeanRepository clientBeanRepository;
044: private final MutableBeanRepository serversBeanRepository;
045: private final MutableBeanRepository systemBeanRepository;
046: private final ApplicationsRepository applicationsRepository;
047:
048: private final DefaultValueProvider defaultValueProvider;
049: private final XmlObjectComparator xmlObjectComparator;
050: private final IllegalConfigurationChangeHandler illegalConfigurationChangeHandler;
051:
052: private final Map dsoApplicationConfigs;
053: private final Map springApplicationConfigs;
054:
055: public BaseTVSConfigurationSetupManager(
056: DefaultValueProvider defaultValueProvider,
057: XmlObjectComparator xmlObjectComparator,
058: IllegalConfigurationChangeHandler illegalConfigurationChangeHandler) {
059: Assert.assertNotNull(defaultValueProvider);
060: Assert.assertNotNull(xmlObjectComparator);
061: Assert.assertNotNull(illegalConfigurationChangeHandler);
062:
063: this .systemBeanRepository = new StandardBeanRepository(
064: System.class);
065: this .clientBeanRepository = new StandardBeanRepository(
066: Client.class);
067: this .serversBeanRepository = new StandardBeanRepository(
068: Servers.class);
069: this .applicationsRepository = new StandardApplicationsRepository();
070:
071: this .defaultValueProvider = defaultValueProvider;
072: this .xmlObjectComparator = xmlObjectComparator;
073: this .illegalConfigurationChangeHandler = illegalConfigurationChangeHandler;
074:
075: this .dsoApplicationConfigs = new HashMap();
076: this .springApplicationConfigs = new HashMap();
077: }
078:
079: protected final MutableBeanRepository clientBeanRepository() {
080: return this .clientBeanRepository;
081: }
082:
083: protected final MutableBeanRepository serversBeanRepository() {
084: return this .serversBeanRepository;
085: }
086:
087: protected final MutableBeanRepository systemBeanRepository() {
088: return this .systemBeanRepository;
089: }
090:
091: protected final ApplicationsRepository applicationsRepository() {
092: return this .applicationsRepository;
093: }
094:
095: protected final XmlObjectComparator xmlObjectComparator() {
096: return this .xmlObjectComparator;
097: }
098:
099: protected final void runConfigurationCreator(
100: ConfigurationCreator configurationCreator)
101: throws ConfigurationSetupException {
102: configurationCreator.createConfigurationIntoRepositories(
103: clientBeanRepository, serversBeanRepository,
104: systemBeanRepository, applicationsRepository);
105: }
106:
107: public String[] applicationNames() {
108: Set names = new HashSet();
109:
110: names.addAll(this .dsoApplicationConfigs.keySet());
111: names.addAll(this .springApplicationConfigs.keySet());
112:
113: return (String[]) names.toArray(new String[names.size()]);
114: }
115:
116: protected final ConfigContext createContext(
117: BeanRepository beanRepository, File configFilePath) {
118: Assert.assertNotNull(beanRepository);
119: return new StandardConfigContext(beanRepository,
120: this .defaultValueProvider,
121: this .illegalConfigurationChangeHandler, configFilePath);
122: }
123:
124: public synchronized NewDSOApplicationConfig dsoApplicationConfigFor(
125: String applicationName) {
126: // When we support multiple applications, just take this assertion out.
127: Assert
128: .eval(applicationName
129: .equals(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME));
130:
131: NewDSOApplicationConfig out = (NewDSOApplicationConfig) this .dsoApplicationConfigs
132: .get(applicationName);
133: if (out == null) {
134: out = createNewDSOApplicationConfig(applicationName);
135: this .dsoApplicationConfigs.put(applicationName, out);
136: }
137:
138: return out;
139: }
140:
141: public synchronized NewSpringApplicationConfig springApplicationConfigFor(
142: String applicationName) {
143: // When we support multiple applications, just take this assertion out.
144: Assert
145: .eval(applicationName
146: .equals(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME));
147:
148: NewSpringApplicationConfig out = (NewSpringApplicationConfig) this .springApplicationConfigs
149: .get(applicationName);
150: if (out == null) {
151: out = createNewSpringApplicationConfig(applicationName);
152: this .springApplicationConfigs.put(applicationName, out);
153: }
154:
155: return out;
156: }
157:
158: protected NewDSOApplicationConfig createNewDSOApplicationConfig(
159: String applicationName) {
160: return new NewDSOApplicationConfigObject(createContext(
161: new ChildBeanRepository(this .applicationsRepository
162: .repositoryFor(applicationName),
163: DsoApplication.class, new ChildBeanFetcher() {
164: public XmlObject getChild(XmlObject parent) {
165: return ((Application) parent).getDso();
166: }
167: }), null));
168: }
169:
170: protected NewSpringApplicationConfig createNewSpringApplicationConfig(
171: String applicationName) {
172: return new NewSpringApplicationConfigObject(createContext(
173: new ChildBeanRepository(this .applicationsRepository
174: .repositoryFor(applicationName),
175: SpringApplication.class,
176: new ChildBeanFetcher() {
177: public XmlObject getChild(XmlObject parent) {
178: return ((Application) parent)
179: .getSpring();
180: }
181: }), null));
182: }
183:
184: }
|