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.1 2007-08-24 22:17:29 ewestfal Exp $
038     */
039    public class ObjectCacheDescriptor implements Serializable, XmlCapable, AttributeContainer
040    {
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        {
048            this.configurationProperties = new Properties();
049            this.objectCache = DEF_OBJECT_CACHE;
050        }
051    
052        public ObjectCacheDescriptor(Class objectCacheClass)
053        {
054            this();
055            this.objectCache = objectCacheClass;
056        }
057    
058        public Class getObjectCache()
059        {
060            return objectCache;
061        }
062    
063        public void setObjectCache(Class objectCache)
064        {
065            this.objectCache = objectCache;
066        }
067    
068        public void addAttribute(String attributeName, String attributeValue)
069        {
070            configurationProperties.setProperty(attributeName, attributeValue);
071        }
072    
073        public String getAttribute(String key)
074        {
075            return getAttribute(key, null);
076        }
077    
078        public String getAttribute(String attributeName, String defaultValue)
079        {
080            String result = configurationProperties.getProperty(attributeName);
081            if(result == null) result = defaultValue;
082            return result;
083        }
084    
085        public Properties getConfigurationProperties()
086        {
087            return configurationProperties;
088        }
089    
090        public void setConfigurationProperties(Properties configurationProperties)
091        {
092            this.configurationProperties = configurationProperties;
093        }
094    
095        public String toString()
096        {
097            ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
098            buf.append("ObjectCache", getObjectCache()).
099            append("Properties", getConfigurationProperties());
100            return buf.toString();
101        }
102    
103        public String toXML()
104        {
105            RepositoryTags tags = RepositoryTags.getInstance();
106            String eol = SystemUtils.LINE_SEPARATOR;
107            StringBuffer buf = new StringBuffer(1024);
108            //opening tag + attributes
109            buf.append("      ");
110            buf.append(tags.getOpeningTagNonClosingById(OBJECT_CACHE));
111            buf.append(eol);
112            buf.append("         ");
113            buf.append(tags.getAttribute(CLASS_NAME, "" + getObjectCache() != null ? getObjectCache().getName() : ""));
114            buf.append("      >");
115            buf.append(eol);
116            buf.append("         <!-- ");
117            buf.append(eol);
118            buf.append("         Add proprietary ObjectCache implementation properties here, using custom attributes");
119            buf.append(eol);
120            buf.append("         e.g. <attribute attribute-name=\"timeout\" attribute-value=\"2000\"/>");
121            buf.append(eol);
122            buf.append("         -->");
123            buf.append(eol);
124            XmlHelper.appendSerializedAttributes(buf, "         ", getConfigurationProperties());
125            buf.append("      ");
126            buf.append(tags.getClosingTagById(OBJECT_CACHE));
127            buf.append(eol);
128    
129            return buf.toString();
130        }
131    
132    }