1
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
27 public class CopyStrategyConfiguration {
28
29 private volatile String className = "net.sf.ehcache.store.compound.ReadWriteSerializationCopyStrategy";
30 private ReadWriteCopyStrategy<Element> strategy;
31
32
37 public String getClassName() {
38 return className;
39 }
40
41
47 public void setClass(final String className) {
48 this.className = className;
49 }
50
51
56 public synchronized void setCopyStrategyInstance(ReadWriteCopyStrategy<Element> copyStrategy) {
57 this.strategy = copyStrategy;
58 }
59
60
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
94 protected CopyStrategyConfiguration copy() {
95 CopyStrategyConfiguration clone = new CopyStrategyConfiguration();
96 clone.setClass(getClassName());
97 return clone;
98 }
99
100
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
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