View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.config;
18  
19  import net.sf.ehcache.CacheException;
20  
21  /***
22   * Holds the Terracotta configuration for a particular client
23   *
24   * @author amiller@terracotta.org
25   * @author Abhishek Sanoujam
26   */
27  public class TerracottaClientConfiguration implements Cloneable {
28  
29      /***
30       * Default value of rejoin attribute
31       */
32      public static final boolean DEFAULT_REJOIN_VALUE = false;
33  
34      private static final String TC_CONFIG_HEADER = "<tc:tc-config xmlns:tc=\"http://www.terracotta.org/config\">";
35      private static final String TC_CONFIG_FOOTER = "</tc:tc-config>";
36  
37      private String url;
38      private String embeddedConfig;
39      private boolean rejoin = DEFAULT_REJOIN_VALUE;
40      private volatile boolean configFrozen;
41  
42      /***
43       * Clones this object, following the usual contract.
44       *
45       * @return a copy, which independent other than configurations than cannot change.
46       * @throws CloneNotSupportedException
47       */
48      @Override
49      public TerracottaClientConfiguration clone() throws CloneNotSupportedException {
50          return (TerracottaClientConfiguration) super.clone();
51      }
52  
53      /***
54       * Builder method to set the URL.
55       *
56       * @param url
57       *            the URL to set
58       * @return this configuration instance
59       */
60      public final TerracottaClientConfiguration url(String url) {
61          setUrl(url);
62          return this;
63      }
64  
65      /***
66       * Builder method to set the URL for a host and a port.
67       *
68       * @param host
69       *            the host where to get the Terracotta configuration from
70       * @param port
71       *            the port on that host
72       * @return this configuration instance
73       */
74      public final TerracottaClientConfiguration url(String host, String port) {
75          setUrl(host + ":" + port);
76          return this;
77      }
78  
79      /***
80       * Set url
81       */
82      public final void setUrl(String url) {
83          this.url = url;
84          validateConfiguration();
85      }
86  
87      /***
88       * Get url string
89       */
90      public final String getUrl() {
91          return this.url;
92      }
93  
94      /***
95       * Tell the BeanHandler to extract the entire subtree xml as text at element <tc-config/>. Expects
96       * to receive the contents of the <tc-config/> tag and will wrap it in a proper tc-config header / footer.
97       */
98      public final void extractTcconfig(String text) {
99          this.embeddedConfig = text;
100         validateConfiguration();
101     }
102 
103     /***
104      * Get the embedded config read as <tc-config/>
105      */
106     public final String getEmbeddedConfig() {
107         return TC_CONFIG_HEADER + embeddedConfig + TC_CONFIG_FOOTER;
108     }
109 
110     /***
111      * Get the original embedded config
112      *
113      * @return original embedded config
114      */
115     public final String getOriginalEmbeddedConfig() {
116         return embeddedConfig;
117     }
118 
119     /***
120      * Helper to check whether this is url config or embedded config
121      */
122     public final boolean isUrlConfig() {
123         return this.url != null;
124     }
125 
126     private void validateConfiguration() {
127         if (this.url != null && this.embeddedConfig != null) {
128             throw new InvalidConfigurationException("It is invalid to specify both a config url and "
129                     + "an embedded config in the <terracottaConfig> element.");
130         }
131     }
132 
133     /***
134      * Returns true if rejoin is enabled
135      *
136      * @return the rejoin
137      */
138     public boolean isRejoin() {
139         return rejoin;
140     }
141 
142     /***
143      * Set rejoin value
144      *
145      * @param rejoin the rejoin to set
146      */
147     public void setRejoin(boolean rejoin) {
148         if (configFrozen) {
149             throw new CacheException("Cannot enable/disable rejoin once config has been frozen");
150         }
151         this.rejoin = rejoin;
152     }
153 
154     /***
155      * Builder method to set rejoin
156      *
157      * @param rejoin
158      * @return this instance
159      */
160     public TerracottaClientConfiguration rejoin(boolean rejoin) {
161         this.setRejoin(rejoin);
162         return this;
163     }
164 
165     /***
166      * Freezes the config
167      */
168     public void freezeConfig() {
169         configFrozen = true;
170     }
171 
172 }