1 4 package com.tc.config.schema.context; 5 6 import org.apache.xmlbeans.XmlException; 7 import org.apache.xmlbeans.XmlObject; 8 9 import com.tc.config.schema.IllegalConfigurationChangeHandler; 10 import com.tc.config.schema.defaults.DefaultValueProvider; 11 import com.tc.config.schema.dynamic.BooleanConfigItem; 12 import com.tc.config.schema.dynamic.BooleanXPathBasedConfigItem; 13 import com.tc.config.schema.dynamic.ConfigItem; 14 import com.tc.config.schema.dynamic.FileConfigItem; 15 import com.tc.config.schema.dynamic.FileXPathBasedConfigItem; 16 import com.tc.config.schema.dynamic.IntConfigItem; 17 import com.tc.config.schema.dynamic.IntXPathBasedConfigItem; 18 import com.tc.config.schema.dynamic.StringArrayConfigItem; 19 import com.tc.config.schema.dynamic.StringArrayXPathBasedConfigItem; 20 import com.tc.config.schema.dynamic.StringConfigItem; 21 import com.tc.config.schema.dynamic.StringXPathBasedConfigItem; 22 import com.tc.config.schema.dynamic.SubstitutedFileXPathBasedConfigItem; 23 import com.tc.config.schema.listen.ConfigurationChangeListener; 24 import com.tc.config.schema.repository.BeanRepository; 25 import com.tc.util.Assert; 26 27 import java.io.File ; 28 29 32 public class StandardConfigContext implements ConfigContext { 33 34 private final BeanRepository beanRepository; 35 private final DefaultValueProvider defaultValueProvider; 36 private final IllegalConfigurationChangeHandler illegalConfigurationChangeHandler; 37 private final File configDirectory; 38 39 public StandardConfigContext(BeanRepository beanRepository, DefaultValueProvider defaultValueProvider, 40 IllegalConfigurationChangeHandler illegalConfigurationChangeHandler, 41 File configDirectory) { 42 Assert.assertNotNull(beanRepository); 43 Assert.assertNotNull(defaultValueProvider); 44 Assert.assertNotNull(illegalConfigurationChangeHandler); 45 46 this.beanRepository = beanRepository; 47 this.defaultValueProvider = defaultValueProvider; 48 this.illegalConfigurationChangeHandler = illegalConfigurationChangeHandler; 49 this.configDirectory = configDirectory; 50 } 51 52 public IllegalConfigurationChangeHandler illegalConfigurationChangeHandler() { 53 return this.illegalConfigurationChangeHandler; 54 } 55 56 public void ensureRepositoryProvides(Class theClass) { 57 beanRepository.ensureBeanIsOfClass(theClass); 58 } 59 60 public boolean hasDefaultFor(String xpath) throws XmlException { 61 return this.defaultValueProvider.possibleForXPathToHaveDefault(xpath) 62 && this.defaultValueProvider.hasDefault(this.beanRepository.rootBeanSchemaType(), xpath); 63 } 64 65 public XmlObject defaultFor(String xpath) throws XmlException { 66 return this.defaultValueProvider.defaultFor(this.beanRepository.rootBeanSchemaType(), xpath); 67 } 68 69 public boolean isOptional(String xpath) throws XmlException { 70 return this.defaultValueProvider.isOptional(this.beanRepository.rootBeanSchemaType(), xpath); 71 } 72 73 public XmlObject bean() { 74 return this.beanRepository.bean(); 75 } 76 77 public Object syncLockForBean() { 78 return this.beanRepository; 79 } 80 81 public void itemCreated(ConfigItem item) { 82 if (item instanceof ConfigurationChangeListener) this.beanRepository 83 .addListener((ConfigurationChangeListener) item); 84 } 85 86 public IntConfigItem intItem(String xpath) { 87 return new IntXPathBasedConfigItem(this, xpath); 88 } 89 90 public StringConfigItem stringItem(String xpath) { 91 return new StringXPathBasedConfigItem(this, xpath); 92 } 93 94 public StringArrayConfigItem stringArrayItem(String xpath) { 95 return new StringArrayXPathBasedConfigItem(this, xpath); 96 } 97 98 public FileConfigItem fileItem(String xpath) { 99 return new FileXPathBasedConfigItem(this, xpath); 100 } 101 102 public FileConfigItem substitutedFileItem(String xpath) { 103 return new SubstitutedFileXPathBasedConfigItem(this, xpath); 104 } 105 106 public BooleanConfigItem booleanItem(String xpath) { 107 return new BooleanXPathBasedConfigItem(this, xpath); 108 } 109 110 public BooleanConfigItem booleanItem(String xpath, boolean defaultValue) { 111 return new BooleanXPathBasedConfigItem(this, xpath, defaultValue); 112 } 113 114 public FileConfigItem configRelativeSubstitutedFileItem(String xpath) { 115 return new SubstitutedFileXPathBasedConfigItem(this, xpath, configDirectory); 116 } 117 118 public String toString() { 119 return "<ConfigContext around repository: " + this.beanRepository + ">"; 120 } 121 122 } 123 | Popular Tags |