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 java.util.Properties;
20
21 import net.sf.ehcache.config.generator.model.NodeElement;
22 import net.sf.ehcache.config.generator.model.SimpleNodeAttribute;
23 import net.sf.ehcache.config.generator.model.SimpleNodeElement;
24 import net.sf.ehcache.search.attribute.AttributeExtractor;
25 import net.sf.ehcache.search.attribute.JavaBeanAttributeExtractor;
26 import net.sf.ehcache.search.attribute.ReflectionAttributeExtractor;
27 import net.sf.ehcache.util.ClassLoaderUtil;
28 import net.sf.ehcache.util.PropertyUtil;
29
30 /***
31 * A cache search attribute. Search attributes must have a name and optionally an expression or class set (if neither is set then this
32 * implies java bean style)
33 *
34 * @author teck
35 */
36 public class SearchAttribute {
37
38 private String name;
39 private String className;
40 private String expression;
41 private String properties;
42 private String propertySeparator;
43
44 /***
45 * Set the attribute name
46 *
47 * @param name
48 */
49 public void setName(String name) {
50 this.name = name;
51 }
52
53 /***
54 * Set the extractor class for this attribute. This class must be available at runtime and must implement {@link AttributeExtractor}
55 *
56 * @param className
57 */
58 public void setClass(String className) {
59 if (expression != null) {
60 throw new InvalidConfigurationException("Cannot set both class and expression for a search attribute");
61 }
62 this.className = className;
63 }
64
65 /***
66 * Set the attribute expression. See {@link ReflectionAttributeExtractor} for more information
67 *
68 * @param expression
69 */
70 public void setExpression(String expression) {
71 if (className != null) {
72 throw new InvalidConfigurationException("Cannot set both class and expression for a search attribute");
73 }
74 this.expression = expression;
75 }
76
77 /***
78 * Get the extractor class name
79 */
80 public String getClassName() {
81 return className;
82 }
83
84 /***
85 * Get the attribute expression
86 */
87 public String getExpression() {
88 return expression;
89 }
90
91 /***
92 * Get the attribute name
93 */
94 public String getName() {
95 return name;
96 }
97
98 /***
99 * Construct the extractor for this attribute configuration
100 */
101 public AttributeExtractor constructExtractor() {
102 if (name == null) {
103 throw new InvalidConfigurationException("search attribute has no name");
104 }
105
106 if (expression != null) {
107 return new ReflectionAttributeExtractor(expression);
108 } else if (className != null) {
109 if (properties != null) {
110 return (AttributeExtractor) ClassLoaderUtil.createNewInstance(className, new Class[] {Properties.class},
111 new Object[] {PropertyUtil.parseProperties(properties, propertySeparator)});
112 } else {
113 return (AttributeExtractor) ClassLoaderUtil.createNewInstance(className);
114 }
115 } else {
116 return new JavaBeanAttributeExtractor(name);
117 }
118 }
119
120 /***
121 * Set the attribute name
122 *
123 * @param name
124 * @return this
125 */
126 public SearchAttribute name(String name) {
127 setName(name);
128 return this;
129 }
130
131 /***
132 * Set the attribute extractor class name
133 *
134 * @param className
135 * attribute extractor class
136 * @return this
137 */
138 public SearchAttribute className(String className) {
139 setClass(className);
140 return this;
141 }
142
143 /***
144 * Set the attribute expression
145 *
146 * @param expression
147 * attribute expression
148 * @return this
149 */
150 public SearchAttribute expression(String expression) {
151 setExpression(expression);
152 return this;
153 }
154
155 /***
156 * Set the extractor properties
157 *
158 * @param props
159 */
160 public void setProperties(String props) {
161 this.properties = props;
162 }
163
164 /***
165 * Set the extractor properties separator
166 *
167 * @param sep
168 */
169 public void setPropertySeparator(String sep) {
170 this.propertySeparator = sep;
171 }
172
173 /***
174 * Set the extractor properties separator
175 *
176 * @param sep
177 * @return this
178 */
179 public SearchAttribute propertySeparator(String sep) {
180 setPropertySeparator(sep);
181 return this;
182 }
183
184 /***
185 * Set the extractor properties
186 *
187 * @param props
188 * @return this
189 */
190 public SearchAttribute properties(String props) {
191 setProperties(props);
192 return this;
193 }
194
195 /***
196 * Create a generated config element node for this search attribute definition
197 *
198 * @param parent the enclosing parent config element
199 * @return generated config element for this search attribute
200 */
201 public NodeElement asConfigElement(NodeElement parent) {
202 SimpleNodeElement rv = new SimpleNodeElement(parent, "searchAttribute");
203
204 rv.addAttribute(new SimpleNodeAttribute("name", name));
205
206 if (expression != null) {
207 rv.addAttribute(new SimpleNodeAttribute("expression", expression));
208 } else if (className != null) {
209 rv.addAttribute(new SimpleNodeAttribute("class", className));
210 if (properties != null) {
211 rv.addAttribute(new SimpleNodeAttribute("properties", properties));
212 }
213 if (propertySeparator != null) {
214 rv.addAttribute(new SimpleNodeAttribute("propertySeperator", propertySeparator));
215 }
216 }
217
218 return rv;
219 }
220
221 }