Browse Source
Added NamedQueries abstraction to hide a map of names to manually defined queries. Added PropertiesBasedNamedQueries to hold the named queries in a Properties instance. Extended resolveQuery(…) method in QueryLookupStrategy to take a NamedQuery instance to allow query creation callback use the named queries. Added optional 'named-queries-location' attribute to the repositories namespace the bean definition parser loads the properties from to populate a PropertiesBasedNamedQueries instance and pipe it into the repository factory. Store implementations have to default the name to lookup in their RepositoryConfiguraion class (see CommonRepositoryConfigInformation.getNamedQueriesLocation()).pull/4/head
12 changed files with 271 additions and 14 deletions
@ -0,0 +1,99 @@
@@ -0,0 +1,99 @@
|
||||
/* |
||||
* Copyright 2011 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.repository.config; |
||||
|
||||
import java.util.Properties; |
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.beans.factory.config.PropertiesFactoryBean; |
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser; |
||||
import org.springframework.beans.factory.xml.ParserContext; |
||||
import org.springframework.data.repository.core.NamedQueries; |
||||
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.StringUtils; |
||||
import org.w3c.dom.Element; |
||||
|
||||
/** |
||||
* {@link BeanDefinitionParser} to create {@link BeanDefinition}s of {@link NamedQueries} instances looking up a |
||||
* {@link Properties} file fom the given location. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class NamedQueriesBeanDefinitionParser implements BeanDefinitionParser { |
||||
|
||||
private static final String ATTRIBUTE = "named-queries-location"; |
||||
private final String defaultLocation; |
||||
|
||||
/** |
||||
* Creates a new {@link NamedQueriesBeanDefinitionParser} using the given default location. |
||||
* |
||||
* @param defaultLocation must be non-empty |
||||
*/ |
||||
public NamedQueriesBeanDefinitionParser(String defaultLocation) { |
||||
Assert.hasText(defaultLocation); |
||||
this.defaultLocation = defaultLocation; |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext) |
||||
*/ |
||||
public BeanDefinition parse(Element element, ParserContext parserContext) { |
||||
|
||||
BeanDefinitionBuilder properties = BeanDefinitionBuilder.rootBeanDefinition(PropertiesFactoryBean.class); |
||||
properties.addPropertyValue("locations", getDefaultedLocation(element)); |
||||
|
||||
if (isDefaultLocation(element)) { |
||||
properties.addPropertyValue("ignoreResourceNotFound", true); |
||||
} |
||||
|
||||
AbstractBeanDefinition propertiesDefinition = properties.getBeanDefinition(); |
||||
propertiesDefinition.setSource(parserContext.extractSource(element)); |
||||
|
||||
BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder.rootBeanDefinition(PropertiesBasedNamedQueries.class); |
||||
namedQueries.addConstructorArgValue(propertiesDefinition); |
||||
|
||||
AbstractBeanDefinition namedQueriesDefinition = namedQueries.getBeanDefinition(); |
||||
namedQueriesDefinition.setSource(parserContext.extractSource(element)); |
||||
|
||||
return namedQueriesDefinition; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether we should use the default location. |
||||
* |
||||
* @param element |
||||
* @return |
||||
*/ |
||||
private boolean isDefaultLocation(Element element) { |
||||
return !StringUtils.hasText(element.getAttribute(ATTRIBUTE)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the location to look for {@link Properties} if configured or the default one if not. |
||||
* |
||||
* @param element |
||||
* @return |
||||
*/ |
||||
private String getDefaultedLocation(Element element) { |
||||
|
||||
String locations = element.getAttribute(ATTRIBUTE); |
||||
return StringUtils.hasText(locations) ? locations : defaultLocation; |
||||
} |
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2011 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.repository.core; |
||||
|
||||
/** |
||||
* Abstraction of a map of {@link NamedQueries} that can be looked up by their names. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public interface NamedQueries { |
||||
|
||||
/** |
||||
* Returns whether the map contains a named query for the given name. If this method returns {@literal true} you can |
||||
* expect {@link #getQuery(String)} to return a non-{@literal null} query for the very same name. |
||||
* |
||||
* @param queryName |
||||
* @return |
||||
*/ |
||||
boolean hasQuery(String queryName); |
||||
|
||||
/** |
||||
* Returns the named query with the given name or {@literal null} if none exists. |
||||
* |
||||
* @param queryName |
||||
* @return |
||||
*/ |
||||
String getQuery(String queryName); |
||||
} |
||||
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* Copyright 2011 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.repository.core.support; |
||||
|
||||
import java.util.Properties; |
||||
|
||||
import org.springframework.data.repository.core.NamedQueries; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* {@link NamedQueries} implementation backed by a {@link Properties} instance. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class PropertiesBasedNamedQueries implements NamedQueries { |
||||
|
||||
public static NamedQueries EMPTY = new PropertiesBasedNamedQueries(new Properties()); |
||||
|
||||
private final Properties properties; |
||||
|
||||
/** |
||||
* Creates a new {@link PropertiesBasedNamedQueries} for the given {@link Properties} instance. |
||||
* |
||||
* @param properties |
||||
*/ |
||||
public PropertiesBasedNamedQueries(Properties properties) { |
||||
Assert.notNull(properties); |
||||
this.properties = properties; |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see org.springframework.data.repository.core.NamedQueries#hasNamedQuery(java.lang.String) |
||||
*/ |
||||
public boolean hasQuery(String queryName) { |
||||
return properties.containsKey(queryName); |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see org.springframework.data.repository.core.NamedQueries#getNamedQuery(java.lang.String) |
||||
*/ |
||||
public String getQuery(String queryName) { |
||||
return properties.getProperty(queryName); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue