001: package org.apache.ojb.broker.metadata;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.Serializable;
019: import java.util.Properties;
020:
021: import org.apache.commons.lang.SystemUtils;
022: import org.apache.commons.lang.builder.ToStringBuilder;
023: import org.apache.commons.lang.builder.ToStringStyle;
024: import org.apache.ojb.broker.cache.ObjectCacheEmptyImpl;
025: import org.apache.ojb.broker.util.XmlHelper;
026:
027: /**
028: * Encapsulates a {@link org.apache.ojb.broker.cache.ObjectCache} implementation class
029: * and its proprietary configuration properties.
030: * <br/>
031: * All ObjectCache implementation specific configuration
032: * attributes are represented by key/value pairs in a
033: * <code>Properties</code> object and could be reached via
034: * {@link #getConfigurationProperties} or {@link #getAttribute(String key)}.
035: *
036: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
037: * @version $Id: ObjectCacheDescriptor.java,v 1.6.2.3 2005/04/26 03:41:36 mkalen Exp $
038: */
039: public class ObjectCacheDescriptor implements Serializable, XmlCapable,
040: AttributeContainer {
041: private static final long serialVersionUID = 2583853027407750053L;
042: private static final Class DEF_OBJECT_CACHE = ObjectCacheEmptyImpl.class;
043: private Class objectCache;
044: private Properties configurationProperties;
045:
046: public ObjectCacheDescriptor() {
047: this .configurationProperties = new Properties();
048: this .objectCache = DEF_OBJECT_CACHE;
049: }
050:
051: public ObjectCacheDescriptor(Class objectCacheClass) {
052: this ();
053: this .objectCache = objectCacheClass;
054: }
055:
056: public Class getObjectCache() {
057: return objectCache;
058: }
059:
060: public void setObjectCache(Class objectCache) {
061: this .objectCache = objectCache;
062: }
063:
064: public void addAttribute(String attributeName, String attributeValue) {
065: configurationProperties.setProperty(attributeName,
066: attributeValue);
067: }
068:
069: public String getAttribute(String key) {
070: return getAttribute(key, null);
071: }
072:
073: public String getAttribute(String attributeName, String defaultValue) {
074: String result = configurationProperties
075: .getProperty(attributeName);
076: if (result == null)
077: result = defaultValue;
078: return result;
079: }
080:
081: public Properties getConfigurationProperties() {
082: return configurationProperties;
083: }
084:
085: public void setConfigurationProperties(
086: Properties configurationProperties) {
087: this .configurationProperties = configurationProperties;
088: }
089:
090: public String toString() {
091: ToStringBuilder buf = new ToStringBuilder(this ,
092: ToStringStyle.DEFAULT_STYLE);
093: buf.append("ObjectCache", getObjectCache()).append(
094: "Properties", getConfigurationProperties());
095: return buf.toString();
096: }
097:
098: public String toXML() {
099: RepositoryTags tags = RepositoryTags.getInstance();
100: String eol = SystemUtils.LINE_SEPARATOR;
101: StringBuffer buf = new StringBuffer(1024);
102: //opening tag + attributes
103: buf.append(" ");
104: buf.append(tags.getOpeningTagNonClosingById(OBJECT_CACHE));
105: buf.append(eol);
106: buf.append(" ");
107: buf.append(tags.getAttribute(CLASS_NAME,
108: "" + getObjectCache() != null ? getObjectCache()
109: .getName() : ""));
110: buf.append(" >");
111: buf.append(eol);
112: buf.append(" <!-- ");
113: buf.append(eol);
114: buf
115: .append(" Add proprietary ObjectCache implementation properties here, using custom attributes");
116: buf.append(eol);
117: buf
118: .append(" e.g. <attribute attribute-name=\"timeout\" attribute-value=\"2000\"/>");
119: buf.append(eol);
120: buf.append(" -->");
121: buf.append(eol);
122: XmlHelper.appendSerializedAttributes(buf, " ",
123: getConfigurationProperties());
124: buf.append(" ");
125: buf.append(tags.getClosingTagById(OBJECT_CACHE));
126: buf.append(eol);
127:
128: return buf.toString();
129: }
130:
131: }
|