提交 8d481f7a 编写于 作者: K kohsuke

Started integrating acegi security into Hudson.

The first step is to declare dependencies and put Groovy-based spring bean configuration code, so that we can compose components from Acegi little more nicely.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@6059 71c3de6d-444a-0410-be80-ed276b4c234a
上级 ed7a21e2
......@@ -381,155 +381,35 @@
<version>1.1-hudson-20071021</version>
</dependency>
<dependency>
<groupId>groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.0</version>
<groupId>org.acegisecurity</groupId>
<artifactId>acegi-security</artifactId>
<version>1.0.5</version>
<exclusions>
<!-- Groovy seems to mix mandatory dependencies and optional dependencies all in one, so
our approach here is to find ones that seem mandatory and exclude everything else
-->
<!--exclusion>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
</exclusion-->
<exclusion>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
</exclusion>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
<exclusion>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</exclusion>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant-junit</artifactId>
</exclusion>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant-launcher</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>jmock</groupId>
<artifactId>jmock</artifactId>
</exclusion>
<exclusion>
<groupId>jmock</groupId>
<artifactId>jmock-cglib</artifactId>
</exclusion>
<!--exclusion>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm-util</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm-attrs</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm-analysis</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm-tree</artifactId>
</exclusion-->
<exclusion>
<groupId>bsf</groupId>
<artifactId>bsf</artifactId>
</exclusion>
<exclusion>
<groupId>mx4j</groupId>
<artifactId>mx4j</artifactId>
</exclusion>
<exclusion>
<groupId>mockobjects</groupId>
<artifactId>mockobjects-core</artifactId>
</exclusion>
<exclusion>
<groupId>openejb</groupId>
<artifactId>openejb-loader</artifactId>
</exclusion>
<exclusion>
<groupId>axion</groupId>
<artifactId>axion</artifactId>
</exclusion>
<!--exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
<exclusion>
<groupId>commons-primitives</groupId>
<artifactId>commons-primitives</artifactId>
</exclusion-->
<exclusion>
<groupId>regexp</groupId>
<artifactId>regexp</artifactId>
</exclusion>
<!--exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion-->
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>radeox</groupId>
<artifactId>radeox</artifactId>
</exclusion>
<exclusion>
<groupId>radeox</groupId>
<artifactId>radeox-oro</artifactId>
</exclusion>
<exclusion>
<groupId>nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</exclusion>
<exclusion>
<groupId>qdox</groupId>
<artifactId>qdox</artifactId>
</exclusion>
<exclusion>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>xstream</groupId>
<artifactId>xstream</artifactId>
</exclusion>
<!--exclusion>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
</exclusioni-->
<exclusion>
<groupId>com.tonicsystems</groupId>
<artifactId>jarjar</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-remoting</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.1-rc-2</version>
</dependency>
<dependency>
<!--
for Grails spring bean builder.
Ideally we should be able to modify BeanBuilder so as not to depend on this.
-->
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
......
此差异已折叠。
/*
* Copyright 2004-2005 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 hudson.util.spring;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
/**
* An interface that represent a runtime bean configuration
*
* Credit must go to Solomon Duskis and the
* article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
*
* @author Graeme
* @since 0.3
*
*/
public interface BeanConfiguration {
String AUTOWIRE_BY_TYPE = "byType";
String AUTOWIRE_BY_NAME = "byName";
/**
*
* @return The name of the bean
*/
String getName();
/**
*
* @return True if the bean is singleton
*/
boolean isSingleton();
/**
*
* @return The Spring bean definition instance
*/
AbstractBeanDefinition getBeanDefinition();
/**
* Adds a property value to this bean
* @param propertyName The name of the property
* @param propertyValue The value of the property
*
* @return Returns this bean configuration
*/
BeanConfiguration addProperty(String propertyName, Object propertyValue);
/**
* Sets the name of the method to call when destroying the bean
*
* @param methodName The method name
* @return This bean configuration
*/
BeanConfiguration setDestroyMethod(String methodName);
/**
* Sets the names of the beans this bean configuration depends on
*
* @param dependsOn Bean names it depends on
* @return This bean configuration
*/
BeanConfiguration setDependsOn(String[] dependsOn);
/**
*
* @param beanName
* @return This BeanConfiguration
*/
BeanConfiguration setFactoryBean(String beanName);
/**
*
* @param methodName
* @return This BeanConfiguration
*/
BeanConfiguration setFactoryMethod(String methodName);
/**
* Sets the autowire type, either "byType" or "byName"
*
* @param type The type
* @return This BeanConfiguration
*/
BeanConfiguration setAutowire(String type);
/**
* Sets the name of the bean in the app ctx
* @param beanName The bean name
*/
void setName(String beanName);
/**
* Returns true if the bean config has the name property set
* @param name The name of the property
* @return True if it does have a property with the given name
*/
boolean hasProperty(String name);
/**
* Returns the value of the given property or throws a MissingPropertyException
*
* @param name The name of the property
* @return The value of the property
*/
Object getPropertyValue(String name);
/**
* Sets a property value on the bean configuration
*
* @param property The name of the property
* @param newValue The value
*/
void setPropertyValue(String property, Object newValue);
/**
* Sets the BeanConfiguration as an Abstract bean definition
* @param isAbstract Whether its abstract or not
* @return This BeanConfiguration object
*/
BeanConfiguration setAbstract(boolean isAbstract);
/**
* Sets the name of the parent bean
*
* @param name Either a string which is the name of the bean, a RuntimeBeanReference or a BeanConfiguration
*/
void setParent(Object name);
}
package hudson.util.spring;
import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.GroovyObject;
import groovy.lang.MissingMethodException;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
/**
* {@link Script} that performs method invocations and property access like {@link Closure} does.
*
* <p>
* For example, when the script is:
*
* <pre>
* a = 1;
* b(2);
* <pre>
*
* <p>
* Using {@link ClosureScript} as the base class would run it as:
*
* <pre>
* delegate.a = 1;
* delegate.b(2);
* </pre>
*
* ... whereas in plain {@link Script}, this will be run as:
*
* <pre>
* binding.setProperty("a",1);
* ((Closure)binding.getProperty("b")).call(2);
* </pre>
*
* @author Kohsuke Kawaguchi
*/
public abstract class ClosureScript extends Script {
private GroovyObject delegate;
protected ClosureScript() {
super();
}
protected ClosureScript(Binding binding) {
super(binding);
}
/**
* Sets the delegation target.
*/
public void setDelegate(GroovyObject delegate) {
this.delegate = delegate;
}
public Object invokeMethod(String name, Object args) {
try {
return delegate.invokeMethod(name,args);
} catch (MissingMethodException mme) {
return super.invokeMethod(name, args);
}
}
public Object getProperty(String property) {
try {
return delegate.getProperty(property);
} catch (MissingPropertyException e) {
return super.getProperty(property);
}
}
public void setProperty(String property, Object newValue) {
try {
delegate.setProperty(property,newValue);
} catch (MissingPropertyException e) {
super.setProperty(property,newValue);
}
}
}
/*
* Copyright 2004-2005 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 hudson.util.spring;
import groovy.lang.GroovyObjectSupport;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import java.util.*;
/**
* Default implementation of the BeanConfiguration interface
*
* Credit must go to Solomon Duskis and the
* article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
*
* @author Graeme
* @since 0.3
*
*/
class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfiguration {
private static final String AUTOWIRE = "autowire";
private static final String CONSTRUCTOR_ARGS = "constructorArgs";
private static final String DESTROY_METHOD = "destroyMethod";
private static final String FACTORY_BEAN = "factoryBean";
private static final String FACTORY_METHOD = "factoryMethod";
private static final String INIT_METHOD = "initMethod";
private static final String BY_NAME = "byName";
private static final String PARENT = "parent";
private static final String BY_TYPE = "byType";
private static final String BY_CONSTRUCTOR = "constructor";
private static final Set<String> DYNAMIC_PROPS = new HashSet<String>(Arrays.asList(AUTOWIRE, CONSTRUCTOR_ARGS, DESTROY_METHOD, FACTORY_BEAN, FACTORY_METHOD, INIT_METHOD, BY_NAME, BY_TYPE, BY_CONSTRUCTOR));
private String parentName;
public Object getProperty(String property) {
getBeanDefinition();
if(wrapper.isReadableProperty(property)) {
return wrapper.getPropertyValue(property);
}
else if(DYNAMIC_PROPS.contains(property)) {
return null;
}
return super.getProperty(property);
}
public void setProperty(String property, Object newValue) {
if(PARENT.equals(property)) {
setParent(newValue);
}
else {
AbstractBeanDefinition bd = getBeanDefinition();
if(AUTOWIRE.equals(property)) {
if(BY_NAME.equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
}
else if(BY_TYPE.equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
}
else if(Boolean.TRUE.equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
}
else if(BY_CONSTRUCTOR.equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
}
}
// constructorArgs
else if(CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
ConstructorArgumentValues cav = new ConstructorArgumentValues();
List args = (List)newValue;
for (Object e : args) {
cav.addGenericArgumentValue(e);
}
bd.setConstructorArgumentValues(cav);
}
// destroyMethod
else if(DESTROY_METHOD.equals(property)) {
if(newValue != null)
bd.setDestroyMethodName(newValue.toString());
}
// factoryBean
else if(FACTORY_BEAN.equals(property)) {
if(newValue != null)
bd.setFactoryBeanName(newValue.toString());
}
// factoryMethod
else if(FACTORY_METHOD.equals(property)) {
if(newValue != null)
bd.setFactoryMethodName(newValue.toString());
}
// initMethod
else if(INIT_METHOD.equals(property)) {
if(newValue != null)
bd.setInitMethodName(newValue.toString());
}
else if(wrapper.isWritableProperty(property)) {
wrapper.setPropertyValue(property, newValue);
}
// autowire
else {
super.setProperty(property, newValue);
}
}
}
private Class clazz;
private String name;
private boolean singleton = true;
private AbstractBeanDefinition definition;
private Collection constructorArgs = Collections.EMPTY_LIST;
private BeanWrapper wrapper;
public DefaultBeanConfiguration(String name, Class clazz) {
this.name = name;
this.clazz = clazz;
}
public DefaultBeanConfiguration(String name, Class clazz, boolean prototype) {
this(name,clazz,Collections.EMPTY_LIST);
this.singleton = !prototype;
}
public DefaultBeanConfiguration(String name) {
this(name,null,Collections.EMPTY_LIST);
}
public DefaultBeanConfiguration(Class clazz2) {
this.clazz = clazz2;
}
public DefaultBeanConfiguration(String name2, Class clazz2, Collection args) {
this.name = name2;
this.clazz = clazz2;
this.constructorArgs = args;
}
public DefaultBeanConfiguration(String name2, boolean prototype) {
this(name2,null,Collections.EMPTY_LIST);
this.singleton = !prototype;
}
public DefaultBeanConfiguration(Class clazz2, Collection constructorArguments) {
this.clazz = clazz2;
this.constructorArgs = constructorArguments;
}
public String getName() {
return this.name;
}
public boolean isSingleton() {
return this.singleton ;
}
public AbstractBeanDefinition getBeanDefinition() {
if (definition == null)
definition = createBeanDefinition();
return definition;
}
protected AbstractBeanDefinition createBeanDefinition() {
AbstractBeanDefinition bd;
if(constructorArgs.size() > 0) {
ConstructorArgumentValues cav = new ConstructorArgumentValues();
for (Object constructorArg : constructorArgs) {
cav.addGenericArgumentValue(constructorArg);
}
if(StringUtils.isBlank(parentName)) {
bd = new RootBeanDefinition(clazz,cav,null);
}
else {
bd = new ChildBeanDefinition(parentName,clazz,cav, null);
}
bd.setSingleton(singleton);
}
else {
if(StringUtils.isBlank(parentName)) {
bd = new RootBeanDefinition(clazz,singleton);
}
else {
bd = new ChildBeanDefinition(parentName,clazz, null,null);
bd.setSingleton(singleton);
}
}
wrapper = new BeanWrapperImpl(bd);
return bd;
}
public BeanConfiguration addProperty(String propertyName, Object propertyValue) {
if(propertyValue instanceof BeanConfiguration) {
propertyValue = ((BeanConfiguration)propertyValue).getBeanDefinition();
}
getBeanDefinition()
.getPropertyValues()
.addPropertyValue(propertyName,propertyValue);
return this;
}
public BeanConfiguration setDestroyMethod(String methodName) {
getBeanDefinition().setDestroyMethodName(methodName);
return this;
}
public BeanConfiguration setDependsOn(String[] dependsOn) {
getBeanDefinition().setDependsOn(dependsOn);
return this;
}
public BeanConfiguration setFactoryBean(String beanName) {
getBeanDefinition().setFactoryBeanName(beanName);
return this;
}
public BeanConfiguration setFactoryMethod(String methodName) {
getBeanDefinition().setFactoryMethodName(methodName);
return this;
}
public BeanConfiguration setAutowire(String type) {
if("byName".equals(type)) {
getBeanDefinition().setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
}
else if("byType".equals(type)){
getBeanDefinition().setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
}
return this;
}
public void setName(String beanName) {
this.name = beanName;
}
public Object getPropertyValue(String name) {
return getBeanDefinition()
.getPropertyValues()
.getPropertyValue(name)
.getValue();
}
public boolean hasProperty(String name) {
return getBeanDefinition().getPropertyValues().contains(name);
}
public void setPropertyValue(String property, Object newValue) {
getBeanDefinition().getPropertyValues().addPropertyValue(property, newValue);
}
public BeanConfiguration setAbstract(boolean isAbstract) {
getBeanDefinition().setAbstract(isAbstract);
return this;
}
public void setParent(Object obj) {
if(obj == null) throw new IllegalArgumentException("Parent bean cannot be set to a null runtime bean reference!");
if(obj instanceof String)
this.parentName = (String)obj;
else if(obj instanceof RuntimeBeanReference) {
this.parentName = ((RuntimeBeanReference)obj).getBeanName();
}
else if(obj instanceof BeanConfiguration) {
this.parentName = ((BeanConfiguration)obj).getName();
}
}
}
/*
* Copyright 2004-2005 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 hudson.util.spring;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StaticWebApplicationContext;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A programmable runtime Spring configuration that allows a spring ApplicationContext
* to be constructed at runtime
*
* Credit must go to Solomon Duskis and the
* article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
*
* @author Graeme
* @since 0.3
*
*/
class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration {
private static final Logger LOGGER = Logger.getLogger(DefaultRuntimeSpringConfiguration.class.getName());
private StaticWebApplicationContext context;
private Map<String,BeanConfiguration> beanConfigs = new HashMap<String,BeanConfiguration>();
private Map<String,BeanDefinition> beanDefinitions = new HashMap<String,BeanDefinition>();
private List<String> beanNames = new ArrayList<String>();
public DefaultRuntimeSpringConfiguration() {
super();
this.context = new StaticWebApplicationContext();
}
public DefaultRuntimeSpringConfiguration(ApplicationContext parent) {
super();
this.context = new StaticWebApplicationContext();
context.setParent(parent);
// if(parent != null){
// trySettingClassLoaderOnContextIfFoundInParent(parent);
// }
}
// private void trySettingClassLoaderOnContextIfFoundInParent(ApplicationContext parent) {
// try{
// Object classLoader = parent.getBean(GrailsRuntimeConfigurator.CLASS_LOADER_BEAN);
// if(classLoader instanceof ClassLoader){
// // this.context.setClassLoader((ClassLoader) classLoader);
// }
// }catch(NoSuchBeanDefinitionException nsbde){
// //ignore, we tried our best
// }
// }
public BeanConfiguration addSingletonBean(String name, Class clazz) {
BeanConfiguration bc = new DefaultBeanConfiguration(name,clazz);
registerBeanConfiguration(name, bc);
return bc;
}
public BeanConfiguration addPrototypeBean(String name, Class clazz) {
BeanConfiguration bc = new DefaultBeanConfiguration(name,clazz,true);
registerBeanConfiguration(name, bc);
return bc;
}
public WebApplicationContext getApplicationContext() {
registerBeansWithContext(context);
context.refresh();
return context;
}
public WebApplicationContext getUnrefreshedApplicationContext() {
return context;
}
public BeanConfiguration addSingletonBean(String name) {
BeanConfiguration bc = new DefaultBeanConfiguration(name);
registerBeanConfiguration(name, bc);
return bc;
}
public BeanConfiguration createSingletonBean(Class clazz) {
return new DefaultBeanConfiguration(clazz);
}
public BeanConfiguration addSingletonBean(String name, Class clazz, Collection args) {
BeanConfiguration bc = new DefaultBeanConfiguration(name,clazz,args);
registerBeanConfiguration(name, bc);
return bc;
}
public BeanConfiguration addPrototypeBean(String name) {
BeanConfiguration bc = new DefaultBeanConfiguration(name,true);
registerBeanConfiguration(name, bc);
return bc;
}
private void registerBeanConfiguration(String name, BeanConfiguration bc) {
beanConfigs.put(name,bc);
beanNames.add(name);
}
public BeanConfiguration createSingletonBean(Class clazz, Collection constructorArguments) {
return new DefaultBeanConfiguration(clazz, constructorArguments);
}
public void setServletContext(ServletContext context) {
this.context.setServletContext(context);
}
public BeanConfiguration createPrototypeBean(String name) {
return new DefaultBeanConfiguration(name,true);
}
public BeanConfiguration createSingletonBean(String name) {
return new DefaultBeanConfiguration(name);
}
public void addBeanConfiguration(String beanName, BeanConfiguration beanConfiguration) {
beanConfiguration.setName(beanName);
registerBeanConfiguration(beanName, beanConfiguration);
}
public void addBeanDefinition(String name, BeanDefinition bd) {
beanDefinitions.put(name,bd);
beanNames.add(name);
}
public boolean containsBean(String name) {
return beanNames .contains(name);
}
public BeanConfiguration getBeanConfig(String name) {
return beanConfigs.get(name);
}
public AbstractBeanDefinition createBeanDefinition(String name) {
if(containsBean(name)) {
if(beanDefinitions.containsKey(name))
return (AbstractBeanDefinition)beanDefinitions.get(name);
else if(beanConfigs.containsKey(name))
return beanConfigs.get(name).getBeanDefinition();
}
return null;
}
public void registerPostProcessor(BeanFactoryPostProcessor processor) {
this.context.addBeanFactoryPostProcessor(processor);
}
public List<String> getBeanNames() {
return beanNames;
}
public void registerBeansWithContext(StaticApplicationContext applicationContext) {
for (BeanConfiguration bc : beanConfigs.values()) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("[RuntimeConfiguration] Registering bean [" + bc.getName() + "]");
if (LOGGER.isLoggable(Level.FINEST)) {
PropertyValue[] pvs = bc.getBeanDefinition()
.getPropertyValues()
.getPropertyValues();
for (PropertyValue pv : pvs) {
LOGGER.finest("[RuntimeConfiguration] With property [" + pv.getName() + "] set to [" + pv.getValue() + "]");
}
}
}
if (applicationContext.containsBeanDefinition(bc.getName()))
applicationContext.removeBeanDefinition(bc.getName());
applicationContext.registerBeanDefinition(bc.getName(),
bc.getBeanDefinition());
}
for (String key : beanDefinitions.keySet()) {
BeanDefinition bd = beanDefinitions.get(key);
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("[RuntimeConfiguration] Registering bean [" + key + "]");
if (LOGGER.isLoggable(Level.FINEST)) {
for (PropertyValue pv : bd.getPropertyValues().getPropertyValues()) {
LOGGER.finest("[RuntimeConfiguration] With property [" + pv.getName() + "] set to [" + pv.getValue() + "]");
}
}
}
if (applicationContext.containsBean(key)) {
applicationContext.removeBeanDefinition(key);
}
applicationContext.registerBeanDefinition(key, bd);
}
}
/**
* Adds an abstract bean and returns the BeanConfiguration instance
*
* @param name The name of the bean
* @return The BeanConfiguration object
*/
public BeanConfiguration addAbstractBean(String name) {
BeanConfiguration bc = new DefaultBeanConfiguration(name);
bc.setAbstract(true);
registerBeanConfiguration(name, bc);
return bc;
}
}
/*
* Copyright 2004-2005 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 hudson.util.spring;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletContext;
import java.util.Collection;
import java.util.List;
/**
* A programmable runtime Spring configuration that allows a spring ApplicationContext
* to be constructed at runtime
*
* Credit must go to Solomon Duskis and the
* article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
*
* @author Graeme
* @since 0.3
*
*/
interface RuntimeSpringConfiguration extends ServletContextAware {
/**
* Adds a singleton bean definition
*
* @param name The name of the bean
* @param clazz The class of the bean
* @return A BeanConfiguration instance
*/
public BeanConfiguration addSingletonBean(String name, Class clazz);
public WebApplicationContext getUnrefreshedApplicationContext();
/**
* Adds a prototype bean definition
*
* @param name The name of the bean
* @param clazz The class of the bean
* @return A BeanConfiguration instance
*/
public BeanConfiguration addPrototypeBean(String name, Class clazz);
/**
* Retrieves the application context from the current state
*
* @return The ApplicationContext instance
*/
WebApplicationContext getApplicationContext();
/**
* Adds an empty singleton bean configuration
* @param name The name of the singleton bean
*
* @return A BeanConfiguration instance
*/
public BeanConfiguration addSingletonBean(String name);
/**
* Adds an empty prototype bean configuration
*
* @param name The name of the prototype bean
* @return A BeanConfiguration instance
*/
public BeanConfiguration addPrototypeBean(String name);
/**
* Creates a singleton bean configuration. Differs from addSingletonBean in that
* it doesn't add the bean to the list of bean references. Hence should be used for
* creating nested beans
*
* @param clazz
* @return A BeanConfiguration instance
*/
public BeanConfiguration createSingletonBean(Class clazz);
/**
* Creates a new singleton bean and adds it to the list of bean references
*
* @param name The name of the bean
* @param clazz The class of the bean
* @param args The constructor arguments of the bean
* @return A BeanConfiguration instance
*/
public BeanConfiguration addSingletonBean(String name, Class clazz, Collection args);
/**
* Creates a singleton bean configuration. Differs from addSingletonBean in that
* it doesn't add the bean to the list of bean references. Hence should be used for
* creating nested beans
*
* @param clazz The bean class
* @param constructorArguments The constructor arguments
* @return A BeanConfiguration instance
*/
public BeanConfiguration createSingletonBean(Class clazz, Collection constructorArguments);
/**
* Sets the servlet context
*
* @param context The servlet Context
*/
public void setServletContext(ServletContext context);
/**
* Creates a new prototype bean configuration. Differs from addPrototypeBean in that
* it doesn't add the bean to the list of bean references to be created via the getApplicationContext()
* method, hence can be used for creating nested beans
*
* @param name The bean name
* @return A BeanConfiguration instance
*
*/
public BeanConfiguration createPrototypeBean(String name);
/**
* Creates a new singleton bean configuration. Differs from addSingletonBean in that
* it doesn't add the bean to the list of bean references to be created via the getApplicationContext()
* method, hence can be used for creating nested beans
*
* @param name The bean name
* @return A BeanConfiguration instance
*
*/
public BeanConfiguration createSingletonBean(String name);
/**
* Adds a bean configuration to the list of beans to be created
*
* @param beanName The name of the bean in the context
* @param beanConfiguration The BeanConfiguration instance
*/
void addBeanConfiguration(String beanName, BeanConfiguration beanConfiguration);
/**
* Adds a Spring BeanDefinition. Differs from BeanConfiguration which is a factory class
* for creating BeanDefinition instances
* @param name The name of the bean
* @param bd The BeanDefinition instance
*/
public void addBeanDefinition(String name, BeanDefinition bd);
/**
* Returns whether the runtime spring config contains the specified bean
*
* @param name The bean name
* @return True if it does
*/
public boolean containsBean(String name);
/**
* Returns the BeanConfiguration for the specified name
* @param name The name of the bean configuration
* @return The BeanConfiguration
*/
public BeanConfiguration getBeanConfig(String name);
/**
* Creates and returns the BeanDefinition that is regsitered within the given name or returns null
*
* @param name The name of the bean definition
* @return A BeanDefinition
*/
public AbstractBeanDefinition createBeanDefinition(String name);
/**
* Registers a bean factory post processor with the context
*
* @param processor The BeanFactoryPostProcessor instance
*/
public void registerPostProcessor(BeanFactoryPostProcessor processor);
List<String> getBeanNames();
/**
* Registers the beans held within this RuntimeSpringConfiguration instance with the given ApplicationContext
*
* @param applicationContext The ApplicationContext instance
*/
void registerBeansWithContext(StaticApplicationContext applicationContext);
/**
* Adds an abstract bean definition to the bean factory and returns the BeanConfiguration object
*
* @param name The name of the bean
* @return The BeanConfiguration object
*/
BeanConfiguration addAbstractBean(String name);
}
<html>
<body>
Spring Bean Builder by using Groovy.
<p>
This code is <a href="http://grails.org/Spring+Bean+Builder">originally from Grails</a>
but modifications are made since then to make the syntax more consistent.
<h2>Changes to the original code</h2>
<p>
Our version has support for getting rid of surrounding "bb.beans { ... }" if the script
is parsed via the <tt>BeanBuilder.parse()</tt> method.
</p>
<p>
Anonymous bean definition syntax is changed to <tt>bean(CLASS) {...}</tt> from
<tt>{CLASS _ -> ...}</tt> to increase consistency with named bean definition.
</p>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册