Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes/improvements #5

Merged
merged 12 commits into from Apr 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-me-core/pom.xml
Expand Up @@ -56,7 +56,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
9 changes: 9 additions & 0 deletions spring-me-core/src/main/java/me/springframework/di/Sink.java
Expand Up @@ -71,4 +71,13 @@ public interface Sink extends Typed {
*/
String getCastTo();

/**
* Returns true if this sink requires an array value. Spring allows lists
* to be injected into locations that require an array, which requires the
* list to be copied into an array before it can be injected.
*
* @return True if this sink requires an array value.
*/
boolean isArray();

}
Expand Up @@ -49,6 +49,8 @@ public abstract class AbstractSink extends AbstractTyped implements Sink {

private String castTo;

private boolean array;

public Source getSource() {
return source;
}
Expand All @@ -73,4 +75,12 @@ public void setCastTo(String castTo) {
this.castTo = castTo;
}

public boolean isArray() {
return array;
}

public void setArray(boolean array) {
this.array = array;
}

}
Expand Up @@ -97,6 +97,14 @@ public interface BeanFactoryType {
*/
String getListAppendMethodName();

/**
* Returns the name of the method used to convert a list into an array.
* This is the copyInto method for Vector, and the toArray method for List.
*
* @return The method name used to copy list elements into an array.
*/
String getListToArrayMethod();

/**
* Returns a list of interfaces to be implemented by the bean factory.
*
Expand Down
Expand Up @@ -49,6 +49,7 @@ public enum BeanFactoryTypes implements BeanFactoryType {
*/
MINIMAL_JAVA_ME("java.lang.RuntimeException",
"java.util.Vector",
"copyInto",
null,
"java.util.Hashtable",
null,
Expand All @@ -59,6 +60,7 @@ public enum BeanFactoryTypes implements BeanFactoryType {
*/
MINIMAL_JAVA_SE("java.lang.RuntimeException",
"java.util.ArrayList",
"toArray",
"java.util.LinkedHashSet",
"java.util.HashMap",
"java.util.Properties",
Expand All @@ -69,6 +71,7 @@ public enum BeanFactoryTypes implements BeanFactoryType {
*/
JAVA_SE("me.springframework.beans.BeansException",
"java.util.ArrayList",
"toArray",
"java.util.LinkedHashSet",
"java.util.HashMap",
"java.util.Properties",
Expand All @@ -85,6 +88,8 @@ public enum BeanFactoryTypes implements BeanFactoryType {
*/
private String listImplementationName;

private String listToArrayMethod;

/**
* @see BeanFactoryType#getListAppendMethodName()
*/
Expand All @@ -106,13 +111,15 @@ public enum BeanFactoryTypes implements BeanFactoryType {

private BeanFactoryTypes(String beansExceptionName,
String listImplementationName,
String listToArrayMethod,
String setImplementationName,
String mapImplementationName,
String propertiesImplementationName,
String listAppendMethodName,
String... interfaceNames) {
this.beansExceptionName = beansExceptionName;
this.listImplementationName = listImplementationName;
this.listToArrayMethod = listToArrayMethod;
this.setImplementationName = setImplementationName;
this.mapImplementationName = mapImplementationName;
this.propertiesImplementationName = propertiesImplementationName;
Expand All @@ -138,6 +145,10 @@ public String getListImplementationName() {
return listImplementationName;
}

public String getListToArrayMethod() {
return listToArrayMethod;
}

public String getSetImplementationName() {
return setImplementationName;
}
Expand Down
@@ -0,0 +1,178 @@
/**
* Copyright (C) 2009 Original Authors
*
* This file is part of Spring ME.
*
* Spring ME is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* Spring ME is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Spring ME; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent
* modules, and to copy and distribute the resulting executable under
* terms of your choice, provided that you also meet, for each linked
* independent module, the terms and conditions of the license of that
* module. An independent module is a module which is not derived from or
* based on this library. If you modify this library, you may extend this
* exception to your version of the library, but you are not obligated to
* do so. If you do not wish to do so, delete this exception statement
* from your version.
*/
package me.springframework.di.spring;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.springframework.di.Configuration;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import com.agilejava.blammo.LoggingKitAdapter;
import com.thoughtworks.qdox.JavaDocBuilder;

/**
* Creates {@link Configuration} instances from Spring configurations.
* This builder is responsible for wiring the configuration loader and
* augmentations to their respective dependencies.
* Most methods return 'this', for chaining.
*/
public class ConfigurationBuilder {

private final JavaDocBuilder builder;

private ConfigurableListableBeanFactory beanFactory;

private boolean autowire;

private boolean withConversions;

private LoggingKitAdapter logAdapter;

/**
* Creates a new ConfigurationBuilder instance.
*/
public ConfigurationBuilder() {
this.builder = new JavaDocBuilder();
}

/**
* Creates a new ConfigurationBuilder instance, using the given
* JavaDocBuilder for querying types in the program.
*/
public ConfigurationBuilder(JavaDocBuilder builder) {
this.builder = builder;
}

public ConfigurationBuilder addSourceTree(File directory) {
builder.addSourceTree(directory);
return this;
}

/**
* Create a BeanFactory for the given resource. The configuration will be
* built by reading BeanDefinitions from this BeanFactory.
*/
public ConfigurationBuilder withBeanFactoryOf(Resource resource) {
assertNoBeanFactory();
this.beanFactory = new XmlBeanFactory(resource);
return this;
}

/**
* Sets the ApplicationContext from which bean definitions should be loaded.
*/
public ConfigurationBuilder withContext(ConfigurableApplicationContext ctxt) {
assertNoBeanFactory();
this.beanFactory = ctxt.getBeanFactory();
return this;
}

/**
* Sets the BeanFactory from which bean definitions should be loaded.
*/
public ConfigurationBuilder beanFactory(ConfigurableListableBeanFactory beanFactory) {
assertNoBeanFactory();
this.beanFactory = beanFactory;
return this;
}

/**
* Use the autowiring augmentation when building the Configuration.
*/
public ConfigurationBuilder withAutowiring() {
this.autowire = true;
return this;
}

/**
* Use information about injection sites when building the Configuration.
*/
public ConfigurationBuilder withConversions() {
this.withConversions = true;
return this;
}

/**
* Sets the LoggingKitAdapter to use for logging.
*/
public ConfigurationBuilder withLoggingKitAdapter(LoggingKitAdapter logAdapter) {
this.logAdapter = logAdapter;
return this;
}

private void assertNoBeanFactory() {
if (beanFactory != null) {
throw new RuntimeException("Only one BeanFactory or context can be used.");
}
}

/**
* Creates a Configuration. This method should be called after a BeanFactory
* or ApplicationContext has been set.
*/
public Configuration build() {
if (beanFactory == null) {
throw new RuntimeException("BeanFactory has not been set.");
}
List<Augmentation> steps = new ArrayList<Augmentation>();
QDoxAugmentation qdox = new QDoxAugmentation(builder);
if (logAdapter != null) {
qdox.setLoggingKitAdapter(logAdapter);
}
steps.add(qdox);
if (autowire) {
steps.add(new AutowiringAugmentation(builder));
}
if (withConversions) {
steps.add(new SinkAugmentation());
}
Augmentation[] augmentations = new Augmentation[steps.size()];
steps.toArray(augmentations);
SpringConfigurationLoader loader =
new SpringConfigurationLoader(beanFactory, augmentations);
Configuration config = loader.load();
return config;
}

}
Expand Up @@ -146,7 +146,7 @@ private void attribute(MutableInstance instance, MutableContext context) {
logger.logNoSuchProperty(setter.getName(), cl.getName());
it.remove();
} else {
setter.setType(property.getType().getValue());
setter.setType(property.getType().toString());
setter.setPrimitive(property.getType().isPrimitive());
attribute(setter.getSource(), context);
}
Expand Down