1 /*
2 * Copyright 2011-2020 the original author or authors.
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 * https://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 org.springframework.data.repository.core.support;
17
18 import lombok.NonNull;
19 import lombok.RequiredArgsConstructor;
20
21 import java.util.Properties;
22
23 import org.springframework.data.repository.core.NamedQueries;
24 import org.springframework.util.Assert;
25
26 /**
27 * {@link NamedQueries} implementation backed by a {@link Properties} instance.
28 *
29 * @author Oliver Gierke
30 */
31 @RequiredArgsConstructor
32 public class PropertiesBasedNamedQueries implements NamedQueries {
33
34 private static final String NO_QUERY_FOUND = "No query with name %s found! Make sure you call hasQuery(…) before calling this method!";
35
36 public static final NamedQueries EMPTY = new PropertiesBasedNamedQueries(new Properties());
37
38 private final @NonNull Properties properties;
39
40 /*
41 * (non-Javadoc)
42 * @see org.springframework.data.repository.core.NamedQueries#hasNamedQuery(java.lang.String)
43 */
44 public boolean hasQuery(String queryName) {
45
46 Assert.hasText(queryName, "Query name must not be null or empty!");
47
48 return properties.containsKey(queryName);
49 }
50
51 /*
52 * (non-Javadoc)
53 * @see org.springframework.data.repository.core.NamedQueries#getNamedQuery(java.lang.String)
54 */
55 public String getQuery(String queryName) {
56
57 Assert.hasText(queryName, "Query name must not be null or empty!");
58
59 String query = properties.getProperty(queryName);
60
61 if (query == null) {
62 throw new IllegalArgumentException(String.format(NO_QUERY_FOUND, queryName));
63 }
64
65 return query;
66 }
67 }
68