View Javadoc

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;
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   * 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.1 2007-08-24 22:17:29 ewestfal Exp $
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         //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 }