KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > ObjectCacheDescriptor


1 package org.apache.ojb.broker.metadata;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.lang.SystemUtils;
22 import org.apache.commons.lang.builder.ToStringBuilder;
23 import org.apache.commons.lang.builder.ToStringStyle;
24 import org.apache.ojb.broker.cache.ObjectCacheEmptyImpl;
25 import org.apache.ojb.broker.util.XmlHelper;
26
27 /**
28  * Encapsulates a {@link org.apache.ojb.broker.cache.ObjectCache} implementation class
29  * and its proprietary configuration properties.
30  * <br/>
31  * All ObjectCache implementation specific configuration
32  * attributes are represented by key/value pairs in a
33  * <code>Properties</code> object and could be reached via
34  * {@link #getConfigurationProperties} or {@link #getAttribute(String key)}.
35  *
36  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
37  * @version $Id: ObjectCacheDescriptor.java,v 1.6.2.3 2005/04/26 03:41:36 mkalen Exp $
38  */

39 public class ObjectCacheDescriptor implements Serializable JavaDoc, XmlCapable, AttributeContainer
40 {
41     private static final long serialVersionUID = 2583853027407750053L;
42     private static final Class JavaDoc DEF_OBJECT_CACHE = ObjectCacheEmptyImpl.class;
43     private Class JavaDoc objectCache;
44     private Properties JavaDoc configurationProperties;
45
46     public ObjectCacheDescriptor()
47     {
48         this.configurationProperties = new Properties JavaDoc();
49         this.objectCache = DEF_OBJECT_CACHE;
50     }
51
52     public ObjectCacheDescriptor(Class JavaDoc objectCacheClass)
53     {
54         this();
55         this.objectCache = objectCacheClass;
56     }
57
58     public Class JavaDoc getObjectCache()
59     {
60         return objectCache;
61     }
62
63     public void setObjectCache(Class JavaDoc objectCache)
64     {
65         this.objectCache = objectCache;
66     }
67
68     public void addAttribute(String JavaDoc attributeName, String JavaDoc attributeValue)
69     {
70         configurationProperties.setProperty(attributeName, attributeValue);
71     }
72
73     public String JavaDoc getAttribute(String JavaDoc key)
74     {
75         return getAttribute(key, null);
76     }
77
78     public String JavaDoc getAttribute(String JavaDoc attributeName, String JavaDoc defaultValue)
79     {
80         String JavaDoc result = configurationProperties.getProperty(attributeName);
81         if(result == null) result = defaultValue;
82         return result;
83     }
84
85     public Properties JavaDoc getConfigurationProperties()
86     {
87         return configurationProperties;
88     }
89
90     public void setConfigurationProperties(Properties JavaDoc configurationProperties)
91     {
92         this.configurationProperties = configurationProperties;
93     }
94
95     public String JavaDoc toString()
96     {
97         ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
98         buf.append("ObjectCache", getObjectCache()).
99         append("Properties", getConfigurationProperties());
100         return buf.toString();
101     }
102
103     public String JavaDoc toXML()
104     {
105         RepositoryTags tags = RepositoryTags.getInstance();
106         String JavaDoc eol = SystemUtils.LINE_SEPARATOR;
107         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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 }
133
Popular Tags