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.context;
005:
006: import org.apache.xmlbeans.XmlException;
007: import org.apache.xmlbeans.XmlObject;
008:
009: import com.tc.config.schema.IllegalConfigurationChangeHandler;
010: import com.tc.config.schema.defaults.DefaultValueProvider;
011: import com.tc.config.schema.dynamic.BooleanConfigItem;
012: import com.tc.config.schema.dynamic.BooleanXPathBasedConfigItem;
013: import com.tc.config.schema.dynamic.ConfigItem;
014: import com.tc.config.schema.dynamic.FileConfigItem;
015: import com.tc.config.schema.dynamic.FileXPathBasedConfigItem;
016: import com.tc.config.schema.dynamic.IntConfigItem;
017: import com.tc.config.schema.dynamic.IntXPathBasedConfigItem;
018: import com.tc.config.schema.dynamic.StringArrayConfigItem;
019: import com.tc.config.schema.dynamic.StringArrayXPathBasedConfigItem;
020: import com.tc.config.schema.dynamic.StringConfigItem;
021: import com.tc.config.schema.dynamic.StringXPathBasedConfigItem;
022: import com.tc.config.schema.dynamic.SubstitutedFileXPathBasedConfigItem;
023: import com.tc.config.schema.listen.ConfigurationChangeListener;
024: import com.tc.config.schema.repository.BeanRepository;
025: import com.tc.util.Assert;
026:
027: import java.io.File;
028:
029: /**
030: * Binds together a {@link BeanRepository} and a {@link DefaultValueProvider}.
031: */
032: public class StandardConfigContext implements ConfigContext {
033:
034: private final BeanRepository beanRepository;
035: private final DefaultValueProvider defaultValueProvider;
036: private final IllegalConfigurationChangeHandler illegalConfigurationChangeHandler;
037: private final File configDirectory;
038:
039: public StandardConfigContext(
040: BeanRepository beanRepository,
041: DefaultValueProvider defaultValueProvider,
042: IllegalConfigurationChangeHandler illegalConfigurationChangeHandler,
043: File configDirectory) {
044: Assert.assertNotNull(beanRepository);
045: Assert.assertNotNull(defaultValueProvider);
046: Assert.assertNotNull(illegalConfigurationChangeHandler);
047:
048: this .beanRepository = beanRepository;
049: this .defaultValueProvider = defaultValueProvider;
050: this .illegalConfigurationChangeHandler = illegalConfigurationChangeHandler;
051: this .configDirectory = configDirectory;
052: }
053:
054: public IllegalConfigurationChangeHandler illegalConfigurationChangeHandler() {
055: return this .illegalConfigurationChangeHandler;
056: }
057:
058: public void ensureRepositoryProvides(Class theClass) {
059: beanRepository.ensureBeanIsOfClass(theClass);
060: }
061:
062: public boolean hasDefaultFor(String xpath) throws XmlException {
063: return this .defaultValueProvider
064: .possibleForXPathToHaveDefault(xpath)
065: && this .defaultValueProvider
066: .hasDefault(this .beanRepository
067: .rootBeanSchemaType(), xpath);
068: }
069:
070: public XmlObject defaultFor(String xpath) throws XmlException {
071: return this .defaultValueProvider.defaultFor(this .beanRepository
072: .rootBeanSchemaType(), xpath);
073: }
074:
075: public boolean isOptional(String xpath) throws XmlException {
076: return this .defaultValueProvider.isOptional(this .beanRepository
077: .rootBeanSchemaType(), xpath);
078: }
079:
080: public XmlObject bean() {
081: return this .beanRepository.bean();
082: }
083:
084: public Object syncLockForBean() {
085: return this .beanRepository;
086: }
087:
088: public void itemCreated(ConfigItem item) {
089: if (item instanceof ConfigurationChangeListener)
090: this .beanRepository
091: .addListener((ConfigurationChangeListener) item);
092: }
093:
094: public IntConfigItem intItem(String xpath) {
095: return new IntXPathBasedConfigItem(this , xpath);
096: }
097:
098: public StringConfigItem stringItem(String xpath) {
099: return new StringXPathBasedConfigItem(this , xpath);
100: }
101:
102: public StringArrayConfigItem stringArrayItem(String xpath) {
103: return new StringArrayXPathBasedConfigItem(this , xpath);
104: }
105:
106: public FileConfigItem fileItem(String xpath) {
107: return new FileXPathBasedConfigItem(this , xpath);
108: }
109:
110: public FileConfigItem substitutedFileItem(String xpath) {
111: return new SubstitutedFileXPathBasedConfigItem(this , xpath);
112: }
113:
114: public BooleanConfigItem booleanItem(String xpath) {
115: return new BooleanXPathBasedConfigItem(this , xpath);
116: }
117:
118: public BooleanConfigItem booleanItem(String xpath,
119: boolean defaultValue) {
120: return new BooleanXPathBasedConfigItem(this , xpath,
121: defaultValue);
122: }
123:
124: public FileConfigItem configRelativeSubstitutedFileItem(String xpath) {
125: return new SubstitutedFileXPathBasedConfigItem(this , xpath,
126: configDirectory);
127: }
128:
129: public String toString() {
130: return "<ConfigContext around repository: "
131: + this .beanRepository + ">";
132: }
133:
134: }
|