Skip to content

Commit

Permalink
Add support for ManagedProperties to Java SE targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
martiell committed Mar 14, 2012
1 parent 64b7e4a commit dd3b30a
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ public interface Configuration {
* @return All sources producing maps.
*/
Set<MapSource> getMapSources();


/**
* Returns all sources producing Properties instances.
*
* @return All sources producing Properties instances.
*/
Set<PropertiesSource> getPropertiesSources();

/**
* Return the an instance by its name.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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;

import java.util.List;

/**
* A type of {@link Source} producing a Properties instance.
*/
public interface PropertiesSource extends Source {

List<MapSource.Entry> getEntries();

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ enum SourceType {
/**
* A map of entries.
*/
Map;
Map,

/**
* A Properties map.
*/
Properties;

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import me.springframework.di.Instance;
import me.springframework.di.ListSource;
import me.springframework.di.MapSource;
import me.springframework.di.PropertiesSource;
import me.springframework.di.SetSource;
import me.springframework.di.Sink;
import me.springframework.di.Source;
Expand All @@ -57,6 +58,7 @@ public class MutableConfiguration implements Configuration {
private Set<Instance> instanceSources = new HashSet<Instance>();
private Set<ListSource> listSources = new HashSet<ListSource>();
private Set<SetSource> setSources = new HashSet<SetSource>();
private Set<PropertiesSource> propertiesSources = new HashSet<PropertiesSource>();
private Set<Instance> publicInstances = new HashSet<Instance>();
private Map<String, ? extends Instance> instancesByName;
private Set<MapSource> mapSources = new HashSet<MapSource>();
Expand Down Expand Up @@ -85,6 +87,10 @@ public Set<MapSource> getMapSources() {
return mapSources;
}

public Set<PropertiesSource> getPropertiesSources() {
return propertiesSources;
}

public Set<Instance> getPublicInstances() {
return publicInstances;
}
Expand Down Expand Up @@ -129,6 +135,11 @@ private void processSource(Source source) {
processSource(entry.getKey());
processSource(entry.getValue());
}
break;
case Properties:
PropertiesSource propsSource = (PropertiesSource) source;
propertiesSources.add(propsSource);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* 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.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import me.springframework.di.MapSource;
import me.springframework.di.PropertiesSource;
import me.springframework.di.Sink;

/**
* A mutable implementation of {@link PropertiesSource}.
*/
public class MutablePropertiesSource extends AbstractTyped implements PropertiesSource, MutableSource {

/**
* The {@link Sink} to which the source is connected.
*/
private Sink sink;

/**
* The id of the {@link MapSource}.
*/
private String id;

/**
* The entries in the map.
*/
private List<MapSource.Entry> entries;

/**
* Constructs a new instance.
*/
public MutablePropertiesSource(Sink sink, Properties properties) {
this.sink = sink;
this.entries = new ArrayList<MapSource.Entry>();
}

public List<MapSource.Entry> getEntries() {
return entries;
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "a Properties injected in " + sink.toString();
}

/*
* (non-Javadoc)
*
* @see me.springframework.di.Source#getId()
*/
public String getId() {
return id;
}

/**
* Sets the id to a new value.
*
* @param id The new id.
*/
public void setId(String id) {
this.id = id;
}

/*
* (non-Javadoc)
*
* @see me.springframework.di.Source#getSourceType()
*/
public SourceType getSourceType() {
return SourceType.Properties;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public static void generate(Destination destination, Configuration definitions,
throw new IllegalArgumentException(
"Sets are not supported by BeanFactoryType: " + beanFactoryType);
}
if (!definitions.getPropertiesSources().isEmpty()
&& beanFactoryType.getPropertiesImplementationName() == null) {
throw new IllegalArgumentException(
"Properties are not supported by BeanFactoryType" + beanFactoryType);
}

Writer writer = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public interface BeanFactoryType {
*/
String getMapImplementationName();

/**
* Returns the fully qualified name of the Properties implementation backing
* the <code>&lt;props/&gt;</code> configuration.
*
* @return The fully qualified name of the Properties implementation backing
* <code>&lt;props/&gt;</code> configuration.
*/
String getPropertiesImplementationName();

/**
* Returns the fully qualified name of the list implementation backing
* <code>&lt;list/&gt;</code> configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public enum BeanFactoryTypes implements BeanFactoryType {
"java.util.Vector",
null,
"java.util.Hashtable",
null,
"addElement"),

/**
Expand All @@ -60,6 +61,7 @@ public enum BeanFactoryTypes implements BeanFactoryType {
"java.util.ArrayList",
"java.util.LinkedHashSet",
"java.util.HashMap",
"java.util.Properties",
"add"),

/**
Expand All @@ -69,6 +71,7 @@ public enum BeanFactoryTypes implements BeanFactoryType {
"java.util.ArrayList",
"java.util.LinkedHashSet",
"java.util.HashMap",
"java.util.Properties",
"add",
"me.springframework.beans.factory.MinimalBeanFactory");

Expand All @@ -94,6 +97,8 @@ public enum BeanFactoryTypes implements BeanFactoryType {
*/
private String mapImplementationName;

private String propertiesImplementationName;

/**
* @see BeanFactoryType#getInterfaceNames()
*/
Expand All @@ -103,12 +108,14 @@ private BeanFactoryTypes(String beansExceptionName,
String listImplementationName,
String setImplementationName,
String mapImplementationName,
String propertiesImplementationName,
String listAppendMethodName,
String... interfaceNames) {
this.beansExceptionName = beansExceptionName;
this.listImplementationName = listImplementationName;
this.setImplementationName = setImplementationName;
this.mapImplementationName = mapImplementationName;
this.propertiesImplementationName = propertiesImplementationName;
this.listAppendMethodName = listAppendMethodName;
this.interfaceNames = interfaceNames;
}
Expand Down Expand Up @@ -144,6 +151,10 @@ public String getMapImplementationName() {
return mapImplementationName;
}

public String getPropertiesImplementationName() {
return propertiesImplementationName;
}

/*
* (non-Javadoc)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import me.springframework.di.Configuration;
import me.springframework.di.Instance;
import me.springframework.di.MapSource;
import me.springframework.di.Scope;
import me.springframework.di.Sink;
import me.springframework.di.Source;
Expand All @@ -57,6 +59,8 @@
import me.springframework.di.base.MutableInstanceReference;
import me.springframework.di.base.MutableListSource;
import me.springframework.di.base.MutableMapSource;
import me.springframework.di.base.MutableMapSource.MapSourceEntry;
import me.springframework.di.base.MutablePropertiesSource;
import me.springframework.di.base.MutablePropertySetter;
import me.springframework.di.base.MutableSetSource;
import me.springframework.di.base.MutableSource;
Expand Down Expand Up @@ -264,7 +268,7 @@ private static MutableSource loadSource(MutableContext context, Sink sink, Objec
} else if (value instanceof ManagedMap) {
result = load(context, sink, (ManagedMap) value);
} else if (value instanceof ManagedProperties) {
result = load(sink, (ManagedProperties) value);
result = load(context, sink, (ManagedProperties) value);
} else {
System.err.println("No support for " + value.getClass().getName());
return null;
Expand All @@ -277,25 +281,35 @@ private static MutableSource load(Sink sink, String s) {
return new MutableStringValueSource(sink, s, "java.lang.String");
}

private static MutableSource load(Sink sink, ManagedProperties managedProperties) {
throw new UnsupportedOperationException("No support for managed properties yet.");
private static MutableSource load(MutableContext context, Sink sink, ManagedProperties properties) {
MutablePropertiesSource source = new MutablePropertiesSource(sink, properties);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
MapSourceEntry entrySource = createEntry(context, source, entry);
source.getEntries().add(entrySource);
}
return source;
}

private static MutableSource load(MutableContext context, Sink sink,
ManagedMap value) {
private static MutableSource load(MutableContext context, Sink sink, ManagedMap value) {
MutableMapSource source = new MutableMapSource(sink);
for (Object element : value.entrySet()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) element;
MutableMapSource.MapSourceEntry created = new MutableMapSource.MapSourceEntry();
Sink keySink = new EntrySink(EntrySink.Type.Key, source);
Sink valueSink = new EntrySink(EntrySink.Type.Value, source);
created.setKey(loadSource(context, keySink, entry.getKey()));
created.setValue(loadSource(context, valueSink, entry.getValue()));
for (Object element: value.entrySet()) {
Map.Entry<?, ?> entry = (Entry<?, ?>) element;
MapSourceEntry created = createEntry(context, source, entry);
source.getEntries().add(created);
}
return source;
}

private static MapSourceEntry createEntry(MutableContext context,
MutableSource source, Map.Entry<?, ?> entry) {
MapSourceEntry created = new MapSourceEntry();
Sink keySink = new EntrySink(EntrySink.Type.Key, source);
Sink valueSink = new EntrySink(EntrySink.Type.Value, source);
created.setKey(loadSource(context, keySink, entry.getKey()));
created.setValue(loadSource(context, valueSink, entry.getValue()));
return created;
}

/**
* Returns a {@link MutableSource} from a source providing a set of values.
*
Expand Down
Loading

0 comments on commit dd3b30a

Please sign in to comment.