1 /**
2  *  Copyright 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 package net.sf.ehcache.config;
17
18 import net.sf.ehcache.Element;
19 import net.sf.ehcache.store.compound.CopyStrategy;
20 import net.sf.ehcache.store.compound.LegacyCopyStrategyAdapter;
21 import net.sf.ehcache.store.compound.ReadWriteCopyStrategy;
22 import net.sf.ehcache.util.ClassLoaderUtil;
23
24 /**
25  * @author Alex Snaps
26  */

27 public class CopyStrategyConfiguration {
28
29     private volatile String className = "net.sf.ehcache.store.compound.ReadWriteSerializationCopyStrategy";
30     private ReadWriteCopyStrategy<Element> strategy;
31
32     /**
33      * Returns the fully qualified class name for the CopyStrategy to use
34      * 
35      * @return FQCN to the CopyStrategy implementation to use
36      */

37     public String getClassName() {
38         return className;
39     }
40
41     /**
42      * Sets the fully qualified class name for the CopyStrategy to use
43      * 
44      * @param className
45      *            FQCN
46      */

47     public void setClass(final String className) {
48         this.className = className;
49     }
50
51     /**
52      * Sets the CopyStrategy instance to use
53      *
54      * @param copyStrategy the copy strategy
55      */

56     public synchronized void setCopyStrategyInstance(ReadWriteCopyStrategy<Element> copyStrategy) {
57         this.strategy = copyStrategy;
58     }
59
60     /**
61      * Get (and potentially) instantiate the instance
62      * 
63      * @return the instance
64      */

65     public synchronized ReadWriteCopyStrategy<Element> getCopyStrategyInstance() {
66         if (strategy == null) {
67             Class copyStrategy = null;
68             try {
69                 copyStrategy = ClassLoaderUtil.loadClass(className);
70                 Object strategyObject = copyStrategy.newInstance();
71                 if (strategyObject instanceof CopyStrategy) {
72                     strategy = new LegacyCopyStrategyAdapter((CopyStrategy) strategyObject);
73                 } else {
74                     strategy = (ReadWriteCopyStrategy<Element>) strategyObject;
75                 }
76             } catch (ClassNotFoundException e) {
77                 throw new RuntimeException("Couldn't find the CopyStrategy class!", e);
78             } catch (InstantiationException e) {
79                 throw new RuntimeException("Couldn't instantiate the CopyStrategy instance!", e);
80             } catch (IllegalAccessException e) {
81                 throw new RuntimeException("Couldn't instantiate the CopyStrategy instance!", e);
82             } catch (ClassCastException e) {
83                 throw new RuntimeException(copyStrategy != null ? copyStrategy.getSimpleName()
84                         + " doesn't implement net.sf.ehcache.store.compound.CopyStrategy" : "Error with CopyStrategy", e);
85             }
86         }
87         return strategy;
88     }
89
90     /**
91      * Make copy of this configuration
92      * @return a copy of this configuration
93      */

94     protected CopyStrategyConfiguration copy() {
95         CopyStrategyConfiguration clone = new CopyStrategyConfiguration();
96         clone.setClass(getClassName());
97         return clone;
98     }
99
100     /**
101      * {@inheritDoc}
102      */

103     @Override
104     public int hashCode() {
105         final int prime = 31;
106         int result = 1;
107         result = prime * result + ((className == null) ? 0 : className.hashCode());
108         return result;
109     }
110
111     /**
112      * {@inheritDoc}
113      */

114     @Override
115     public boolean equals(Object obj) {
116         if (this == obj) {
117             return true;
118         }
119         if (obj == null) {
120             return false;
121         }
122         if (getClass() != obj.getClass()) {
123             return false;
124         }
125         CopyStrategyConfiguration other = (CopyStrategyConfiguration) obj;
126         if (className == null) {
127             if (other.className != null) {
128                 return false;
129             }
130         } else if (!className.equals(other.className)) {
131             return false;
132         }
133         return true;
134     }
135
136 }
137