1 package org.naftulin.configmgr; 2 3 import java.util.Date; 4 5 import org.apache.commons.lang.builder.ToStringBuilder; 6 import org.apache.commons.lang.builder.EqualsBuilder; 7 import org.apache.commons.lang.builder.HashCodeBuilder; 8 import org.apache.commons.lang.exception.ExceptionUtils; 9 import org.naftulin.configmgr.parsers.ConfigEntryParser; 10 11 /*** 12 * Configuration management entry associates a key to a configuration, stored as a content. 13 * In addition to key and content, it registers the file name from which a configuration was read, 14 * whether the configuration is in an error state, and whether or not it was overwritten. 15 * The entry is considered to be in error, if it cannot be parsed from the configuration file. 16 * In such case, instead of the configuration, an exception would be stored in the configuration entry 17 * content. If the configuration in error is requested, the exception is returned. 18 * The owervritten stands flag is to show whether a configuration was programmatically 19 * ovewrittend (by using {@link ConfigurationManagement#addConfigurationManagmentEntry(ConfigurationManagementEntry) 20 * add configuration method}. Otherwise it is false; it is also reset when reload is called 21 * on {@link ConfigurationManagement ConfigurationManagement}. It could also be set by user. 22 * 23 * @author Henry Naftulin 24 * @since 1.0 25 */ 26 public class ConfigurationManagementEntryImpl implements ConfigurationManagementEntry { 27 private static final long serialVersionUID = 1L; 28 private final String key; 29 private final String fileName; 30 private boolean overwritten; 31 private final boolean error; 32 private final Object content; 33 private final Date createDate; 34 private final transient ConfigEntryParser processor; 35 private final ConfigurationType configurationType; 36 private Date modifiedDate; 37 38 /*** 39 * Creates a configuration management entry. Sets error only if the key or if processor is 40 * not set or no file name is associated with the configuration entry. 41 * @param key the key entry is stored under 42 * @param fileName file that is associated with the entry. Usually file that the entry is read from. 43 * @param content the content of the entry. For example properties object. 44 * @param processor the processor that can process entry 45 */ 46 public ConfigurationManagementEntryImpl(final String key, final String fileName, 47 final Object content, final ConfigEntryParser processor, 48 final ConfigurationType configurationType) { 49 super(); 50 this.key = key; 51 this.fileName = fileName; 52 this.content = content; 53 this.processor = processor; 54 this.configurationType = configurationType; 55 long miliseconds = System.currentTimeMillis(); 56 createDate = new Date(miliseconds); 57 modifiedDate = new Date(miliseconds); 58 error = (key == null) || (processor == null) || (fileName == null) || (content instanceof Throwable); 59 } 60 61 /*** 62 * Returns key for the entry 63 * @return the key for the entry 64 */ 65 public String getKey() { 66 return key; 67 } 68 69 /*** 70 * Returns file name associated with the entry 71 * @return the file name associated with the entry 72 */ 73 public String getFileName() { 74 return fileName; 75 } 76 77 /*** 78 * Sets overwritten flag. Method should be called by configuration manager only 79 * to say that an entry overwrote another entry in the configuration. 80 * @param overwritten 81 */ 82 public void setOverwritten(final boolean overwritten) { 83 this.overwritten = overwritten; 84 } 85 86 /*** 87 * Returns true if this entry overwrote another entry with the same key. 88 * @return true if this entry overwrote another entry with the same key. 89 */ 90 public boolean isOverwritten() { 91 return overwritten; 92 } 93 94 /*** 95 * Returns true if the configuration does not have a key, or file name is not 96 * associated with the entry or there is no processor associated with the entry. 97 * @return true if the entry is in error condition 98 */ 99 public boolean isError() { 100 return this.error; 101 } 102 103 /*** 104 * Returns the content of the entry 105 * @return the content 106 */ 107 public Object getContent() { 108 return this.content; 109 } 110 111 /*** 112 * Returns the create date for this entry 113 * @return create date for the entry 114 */ 115 public Date getCreateDate() { 116 return (Date) createDate.clone(); 117 } 118 119 /*** 120 * Returns the processor for this entry 121 * @return the processor for this entry 122 */ 123 public ConfigEntryParser getProcessor() { 124 return this.processor; 125 } 126 127 /*** 128 * Returns the string representation of this entry. 129 * @return the string representation of this entry. 130 */ 131 public String toString() { 132 final ToStringBuilder sb = new ToStringBuilder(this) 133 .append("processor", this.processor) 134 .append("key", this.key) 135 .append("overwritten", this.overwritten) 136 .append("fileName", this.fileName); 137 if (content instanceof Throwable) { 138 Throwable t = (Throwable) content; 139 sb.append("content", t.getMessage() + ExceptionUtils.getFullStackTrace(t)); 140 } else { 141 sb.append("content", this.content); 142 } 143 sb.append("error", this.error) 144 .append("createDate", this.createDate) 145 .append("mofifiedDate", this.modifiedDate) 146 .append("cofiguration Type", this.configurationType); 147 return sb.toString(); 148 } 149 150 /*** 151 * Returns true if the object equals to this record. 152 * @return true if the object equals to this record. 153 */ 154 public boolean equals(Object object) { 155 if (!(object instanceof ConfigurationManagementEntryImpl)) { 156 return false; 157 } 158 final ConfigurationManagementEntryImpl rhs = (ConfigurationManagementEntryImpl) object; 159 return new EqualsBuilder().appendSuper(super.equals(object)).append( 160 this.key, rhs.key).append(this.fileName, rhs.fileName).append( 161 this.overwritten, rhs.overwritten).append(this.content, 162 rhs.content).append(this.createDate, rhs.createDate).append( 163 this.processor, rhs.processor).append(this.error, rhs.error) 164 .isEquals(); 165 } 166 167 /*** 168 * Returns hashcode of this record. 169 * @return hashcode of this record. 170 */ 171 public int hashCode() { 172 return new HashCodeBuilder(273882901, 72714823).appendSuper( 173 super.hashCode()).append(this.key).append(this.fileName) 174 .append(this.overwritten).append(this.content).append( 175 this.createDate).append(this.processor).append( 176 this.error).toHashCode(); 177 } 178 179 /*** 180 * Returns the date and time when this entry was modified. 181 * @return the date and time when this entry was modified. 182 */ 183 public Date getModifiedDate() { 184 return (Date) modifiedDate.clone(); 185 } 186 187 /*** 188 * Returns the configuration type of this entry. 189 * @return the configuration type of this entry. 190 */ 191 public ConfigurationType getConfigurationType() { 192 return this.configurationType; 193 } 194 195 public void setModifiedDate(final Date modifiedDate) { 196 this.modifiedDate = (Date) modifiedDate.clone(); 197 198 } 199 200 }