1 package org.apache.ojb.broker.metadata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.io.Serializable;
19 import java.util.Properties;
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
29
30
31
32
33
34
35
36
37
38
39 public class ObjectCacheDescriptor implements Serializable, XmlCapable, AttributeContainer
40 {
41 private static final long serialVersionUID = 2583853027407750053L;
42 private static final Class DEF_OBJECT_CACHE = ObjectCacheEmptyImpl.class;
43 private Class objectCache;
44 private Properties configurationProperties;
45
46 public ObjectCacheDescriptor()
47 {
48 this.configurationProperties = new Properties();
49 this.objectCache = DEF_OBJECT_CACHE;
50 }
51
52 public ObjectCacheDescriptor(Class objectCacheClass)
53 {
54 this();
55 this.objectCache = objectCacheClass;
56 }
57
58 public Class getObjectCache()
59 {
60 return objectCache;
61 }
62
63 public void setObjectCache(Class objectCache)
64 {
65 this.objectCache = objectCache;
66 }
67
68 public void addAttribute(String attributeName, String attributeValue)
69 {
70 configurationProperties.setProperty(attributeName, attributeValue);
71 }
72
73 public String getAttribute(String key)
74 {
75 return getAttribute(key, null);
76 }
77
78 public String getAttribute(String attributeName, String defaultValue)
79 {
80 String result = configurationProperties.getProperty(attributeName);
81 if(result == null) result = defaultValue;
82 return result;
83 }
84
85 public Properties getConfigurationProperties()
86 {
87 return configurationProperties;
88 }
89
90 public void setConfigurationProperties(Properties configurationProperties)
91 {
92 this.configurationProperties = configurationProperties;
93 }
94
95 public String 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 toXML()
104 {
105 RepositoryTags tags = RepositoryTags.getInstance();
106 String eol = SystemUtils.LINE_SEPARATOR;
107 StringBuffer buf = new StringBuffer(1024);
108
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 }