You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2006/11/28 02:55:56 UTC
svn commit: r479843 [2/2] - in /tapestry/tapestry5/tapestry-ioc/trunk/src:
main/java/org/apache/tapestry/ioc/
main/java/org/apache/tapestry/ioc/internal/
main/java/org/apache/tapestry/ioc/internal/services/
main/java/org/apache/tapestry/ioc/internal/ut...
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImpl.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImpl.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImpl.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,217 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc.internal.services;
+
+import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newLinkedList;
+import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newThreadSafeMap;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tapestry.ioc.services.SymbolProvider;
+import org.apache.tapestry.ioc.services.SymbolSource;
+
+public class SymbolSourceImpl implements SymbolSource
+{
+ private final List<SymbolProvider> _providers;
+
+ /** Cache of symbol name to fully expanded symbol value. */
+ private final Map<String, String> _cache = newThreadSafeMap();
+
+ /**
+ * Contains execution data needed when performing an expansion (largely, to check for endless
+ * recursion).
+ */
+ private class SymbolExpansion
+ {
+ private final LinkedList<String> _expandingSymbols = newLinkedList();
+
+ String expandSymbols(String input)
+ {
+ StringBuilder builder = null;
+
+ int startx = 0;
+
+ while (true)
+ {
+ int symbolx = input.indexOf("${", startx);
+
+ // Special case: if the string contains no symbols then return it as is.
+
+ if (startx == 0 && symbolx < 0) return input;
+
+ // The string has at least one symbol, so its OK to create the StringBuilder
+
+ if (builder == null) builder = new StringBuilder();
+
+ // No more symbols found, so add in the rest of the string.
+
+ if (symbolx < 0)
+ {
+ builder.append(input.substring(startx));
+ break;
+ }
+
+ builder.append(input.substring(startx, symbolx));
+
+ int endx = input.indexOf("}", symbolx);
+
+ if (endx < 0)
+ {
+ String message = _expandingSymbols.isEmpty() ? ServiceMessages
+ .missingSymbolCloseBrace(input) : ServiceMessages
+ .missingSymbolCloseBraceInPath(input, path());
+
+ throw new RuntimeException(message);
+ }
+
+ String symbolName = input.substring(symbolx + 2, endx);
+
+ builder.append(valueForSymbol(symbolName));
+
+ // Restart the search after the '}'
+
+ startx = endx + 1;
+ }
+
+ return builder.toString();
+ }
+
+ String valueForSymbol(String symbolName)
+ {
+ String value = _cache.get(symbolName);
+
+ if (value == null)
+ {
+ value = expandSymbol(symbolName);
+
+ _cache.put(symbolName, value);
+ }
+
+ return value;
+ }
+
+ String expandSymbol(String symbolName)
+ {
+ if (_expandingSymbols.contains(symbolName))
+ {
+ _expandingSymbols.add(symbolName);
+ throw new RuntimeException(ServiceMessages.recursiveSymbol(
+ symbolName,
+ pathFrom(symbolName)));
+ }
+
+ _expandingSymbols.addLast(symbolName);
+
+ String value = null;
+
+ for (SymbolProvider provider : _providers)
+ {
+ value = provider.valueForSymbol(symbolName);
+
+ if (value != null) break;
+ }
+
+ if (value == null)
+ {
+
+ String message = _expandingSymbols.size() == 1 ? ServiceMessages
+ .symbolUndefined(symbolName) : ServiceMessages.symbolUndefinedInPath(
+ symbolName,
+ path());
+
+ throw new RuntimeException(message);
+ }
+
+ // The value may have symbols that need expansion.
+
+ String result = expandSymbols(value);
+
+ // And we're done expanding this symbol
+
+ _expandingSymbols.removeLast();
+
+ return result;
+
+ }
+
+ String path()
+ {
+ StringBuilder builder = new StringBuilder();
+
+ boolean first = true;
+
+ for (String symbolName : _expandingSymbols)
+ {
+ if (!first) builder.append(" --> ");
+
+ builder.append(symbolName);
+
+ first = false;
+ }
+
+ return builder.toString();
+ }
+
+ String pathFrom(String startSymbolName)
+ {
+ StringBuilder builder = new StringBuilder();
+
+ boolean first = true;
+ boolean match = false;
+
+ for (String symbolName : _expandingSymbols)
+ {
+ if (!match)
+ {
+ if (symbolName.equals(startSymbolName))
+ match = true;
+ else
+ continue;
+ }
+
+ if (!first) builder.append(" --> ");
+
+ builder.append(symbolName);
+
+ first = false;
+ }
+
+ return builder.toString();
+ }
+ }
+
+ public SymbolSourceImpl(final List<SymbolProvider> providers)
+ {
+ _providers = providers;
+ }
+
+ public String expandSymbols(String input)
+ {
+ return new SymbolExpansion().expandSymbols(input);
+ }
+
+ public String valueForSymbol(String symbolName)
+ {
+ String value = _cache.get(symbolName);
+
+ // If already in the cache, then return it. Otherwise, let the SE find the value and
+ // update the cache.
+
+ return value != null ? value : new SymbolExpansion().valueForSymbol(symbolName);
+ }
+
+}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SystemPropertiesSymbolProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SystemPropertiesSymbolProvider.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SystemPropertiesSymbolProvider.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/services/SystemPropertiesSymbolProvider.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,29 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc.internal.services;
+
+import org.apache.tapestry.ioc.services.SymbolProvider;
+
+/**
+ * Obtains symbol values from JVM System properties. This implementation is usually ordered first,
+ * so that explicit overrides, provided as JVM system properties, can take effect.
+ */
+public class SystemPropertiesSymbolProvider implements SymbolProvider
+{
+ public String valueForSymbol(String symbolName)
+ {
+ return System.getProperty(symbolName);
+ }
+}
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/InternalUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/InternalUtils.java?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/InternalUtils.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/InternalUtils.java Mon Nov 27 17:55:54 2006
@@ -69,8 +69,7 @@
for (int i = 0; i < method.getParameterTypes().length; i++)
{
- if (i > 0)
- buffer.append(", ");
+ if (i > 0) buffer.append(", ");
String name = method.getParameterTypes()[i].getSimpleName();
@@ -104,8 +103,7 @@
{
char ch = builder.charAt(0);
- if (InternalUtils.NAME_PREFIX.indexOf(ch) < 0)
- break;
+ if (InternalUtils.NAME_PREFIX.indexOf(ch) < 0) break;
builder.deleteCharAt(0);
}
@@ -156,8 +154,7 @@
{
for (Annotation a : annotations)
{
- if (annotationClass.isInstance(a))
- return annotationClass.cast(a);
+ if (annotationClass.isInstance(a)) return annotationClass.cast(a);
}
return null;
@@ -192,8 +189,7 @@
// This will return a non-null value, or throw an exception
- if (result == null)
- result = locator.getService(parameterType);
+ if (result == null) result = locator.getService(parameterType);
// ... so the result is never null
@@ -241,8 +237,7 @@
for (Object o : elements)
{
- if (!first)
- buffer.append(", ");
+ if (!first) buffer.append(", ");
buffer.append(String.valueOf(o));
@@ -283,8 +278,7 @@
/** Capitalizes a string, converting the first character to uppercase. */
public static String capitalize(String input)
{
- if (input.length() == 0)
- return input;
+ if (input.length() == 0) return input;
return input.substring(0, 1).toUpperCase() + input.substring(1);
}
@@ -296,14 +290,11 @@
public static Location locationOf(Object location)
{
- if (location == null)
- return null;
+ if (location == null) return null;
- if (location instanceof Location)
- return (Location) location;
+ if (location instanceof Location) return (Location) location;
- if (location instanceof Locatable)
- return ((Locatable) location).getLocation();
+ if (location instanceof Locatable) return ((Locatable) location).getLocation();
return null;
}
@@ -319,8 +310,7 @@
public static List<String> sortedKeys(Map map)
{
- if (map == null)
- return Collections.emptyList();
+ if (map == null) return Collections.emptyList();
List<String> keys = newList();
@@ -345,8 +335,7 @@
public static <K, V> V get(Map<K, V> map, K key)
{
- if (map == null)
- return null;
+ if (map == null) return null;
return map.get(key);
}
@@ -405,8 +394,7 @@
boolean upperCase = Character.isUpperCase(ch);
- if (upperCase && !postSpace)
- builder.append(' ');
+ if (upperCase && !postSpace) builder.append(' ');
builder.append(ch);
@@ -414,5 +402,11 @@
}
return builder.toString();
+ }
+
+ /** Return true if the input string contains the marker for symbols that must be expanded. */
+ public static boolean containsSymbols(String input)
+ {
+ return input.contains("${");
}
}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolProvider.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolProvider.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolProvider.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,28 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc.services;
+
+/** A provider of values for symbols. */
+public interface SymbolProvider
+{
+ /**
+ * Returns the value for the symbol, or null if this provider can not provide a value. The value
+ * itself may contain symbols that will be recursively expanded.
+ *
+ * @param symbolName
+ * @return the value or null
+ */
+ String valueForSymbol(String symbolName);
+}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolSource.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolSource.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolSource.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/SymbolSource.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,47 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc.services;
+
+import org.apache.tapestry.ioc.annotations.Inject;
+
+/**
+ * Used to manage <em>symbols</em>, configuration properties whose value is evaluated at runtime.
+ * Symbols use the Ant syntax: <code>${foo.bar.baz}</code> where <code>foo.bar.baz</code> is the
+ * name of the symbol. The symbol may appear inside annotations such as {@link Inject}. Values for
+ * symbols are provided by {@link SymbolProvider}.
+ */
+public interface SymbolSource
+{
+ /**
+ * Expands the value for a particular symbol. This may involve recursive expansion, if the
+ * immediate value for the symbol itself contains symbols.
+ *
+ * @param symbolName
+ * @return the expanded string
+ * @throws RuntimeException
+ * if the symbol name can not be expanded (no {@link SymbolProvider} can provide its
+ * value), or if an expansion is directly or indirectly recursive
+ */
+ String valueForSymbol(String symbolName);
+
+ /**
+ * Given an input string that <em>may</em> contain symbols, returns the string with any and
+ * all symbols fully expanded.
+ *
+ * @param input
+ * @return expanded input
+ */
+ String expandSymbols(String input);
+}
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java Mon Nov 27 17:55:54 2006
@@ -24,6 +24,7 @@
import org.apache.tapestry.ioc.Configuration;
import org.apache.tapestry.ioc.MappedConfiguration;
import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.OrderedConfiguration;
import org.apache.tapestry.ioc.ServiceLifecycle;
import org.apache.tapestry.ioc.annotations.Id;
import org.apache.tapestry.ioc.annotations.InjectService;
@@ -33,6 +34,7 @@
import org.apache.tapestry.ioc.internal.services.ExceptionAnalyzerImpl;
import org.apache.tapestry.ioc.internal.services.ExceptionTrackerImpl;
import org.apache.tapestry.ioc.internal.services.LoggingDecoratorImpl;
+import org.apache.tapestry.ioc.internal.services.MapSymbolProvider;
import org.apache.tapestry.ioc.internal.services.MasterObjectProvider;
import org.apache.tapestry.ioc.internal.services.PerThreadServiceLifecycle;
import org.apache.tapestry.ioc.internal.services.PipelineBuilderImpl;
@@ -40,6 +42,8 @@
import org.apache.tapestry.ioc.internal.services.PropertyShadowBuilderImpl;
import org.apache.tapestry.ioc.internal.services.ServiceObjectProvider;
import org.apache.tapestry.ioc.internal.services.StrategyBuilderImpl;
+import org.apache.tapestry.ioc.internal.services.SymbolSourceImpl;
+import org.apache.tapestry.ioc.internal.services.SystemPropertiesSymbolProvider;
import org.apache.tapestry.ioc.internal.services.ThreadLocaleImpl;
import org.apache.tapestry.ioc.internal.services.TypeCoercerImpl;
import org.apache.tapestry.ioc.util.StrategyRegistry;
@@ -154,9 +158,12 @@
* @param configuration
* map of ObjectProviders, keyed on prefix
*/
- public static ObjectProvider buildMasterObjectProvider(Map<String, ObjectProvider> configuration)
+ public static ObjectProvider buildMasterObjectProvider(
+ Map<String, ObjectProvider> configuration, @InjectService("SymbolSource")
+ SymbolSource symbolSource, @InjectService("TypeCoercer")
+ TypeCoercer typeCoercer)
{
- return new MasterObjectProvider(configuration);
+ return new MasterObjectProvider(configuration, symbolSource, typeCoercer);
}
/**
@@ -383,8 +390,33 @@
}
@Lifecycle("perthread")
- public ThreadLocale buildThreadLocale()
+ public static ThreadLocale buildThreadLocale()
{
return new ThreadLocaleImpl();
+ }
+
+ public static SymbolSource buildSymbolSource(List<SymbolProvider> configuration)
+ {
+ return new SymbolSourceImpl(configuration);
+ }
+
+ public static void contributeSymbolSource(OrderedConfiguration<SymbolProvider> configuration,
+ @InjectService("ApplicationDefaults")
+ SymbolProvider applicationDefaults, @InjectService("FactoryDefaults")
+ SymbolProvider factoryDefaults)
+ {
+ configuration.add("SystemProperties", new SystemPropertiesSymbolProvider());
+ configuration.add("ApplicationDefaults", applicationDefaults, "after:SystemProperties");
+ configuration.add("FactoryDefaults", factoryDefaults, "after:ApplicationDefaults");
+ }
+
+ public static SymbolProvider buildApplicationDefaults(Map<String, String> configuration)
+ {
+ return new MapSymbolProvider(configuration);
+ }
+
+ public static SymbolProvider buildFactoryDefaults(Map<String, String> configuration)
+ {
+ return new MapSymbolProvider(configuration);
}
}
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java Mon Nov 27 17:55:54 2006
@@ -36,6 +36,7 @@
import org.apache.tapestry.ioc.def.ModuleDef;
import org.apache.tapestry.ioc.def.ServiceDef;
import org.apache.tapestry.ioc.services.ClassFactory;
+import org.apache.tapestry.ioc.services.SymbolSource;
import org.apache.tapestry.ioc.services.TypeCoercer;
/** Add factory and trainer methods for the public interfaces of Tapestry IOC. */
@@ -253,6 +254,22 @@
protected final <K, V> MappedConfiguration<K, V> newMappedConfiguration()
{
return newMock(MappedConfiguration.class);
+ }
+
+ protected final void train_expandSymbols(SymbolSource source, String input)
+ {
+ train_expandSymbols(source, input, input);
+
+ }
+
+ protected final void train_expandSymbols(SymbolSource source, String input, String expanded)
+ {
+ expect(source.expandSymbols(input)).andReturn(expanded);
+ }
+
+ protected final SymbolSource newSymbolSource()
+ {
+ return newMock(SymbolSource.class);
}
}
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/resources/org/apache/tapestry/ioc/internal/services/ServiceStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/resources/org/apache/tapestry/ioc/internal/services/ServiceStrings.properties?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/resources/org/apache/tapestry/ioc/internal/services/ServiceStrings.properties (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/resources/org/apache/tapestry/ioc/internal/services/ServiceStrings.properties Mon Nov 27 17:55:54 2006
@@ -29,7 +29,12 @@
property-type-mismatch=Property '%s' of class %s is of type %s, which is not assignable to type %s.
extra-filter-method=Method %s of filter interface %s does not have a matching method in %s.
unmatched-service-method=Method %s has no match in filter interface %s.
-object-reference-format-error=Object reference '%s' does not contain an provider prefix (such as 'service:').
unknown-object-provider=Object provider '%s' does not exist (in object reference '%s').
shutdown-listener-error=Error notifying %s of registry shutdown: %s
-no-coercion-found=Could not find a coercion from type %s to type %s. Available coercions: %s.
+no-coercion-found=Could not find a coercion from type %s to type %s. Available coercions: %s.
+recursive-symbol=Symbol '%s' is defined in terms of itself (%s).
+symbol-undefined=Symbol '%s' is not defined.
+symbol-undefined-in-path=Symbol '%s' is not defined (in %s).
+missing-symbol-close-brace=Input string '%s' is missing a symbol closing brace.
+missing-symbol-close-brace-in-path=Input string '%s' is missing a symbol closing brace (in %s).
+
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/index.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/index.apt?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/index.apt (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/index.apt Mon Nov 27 17:55:54 2006
@@ -18,8 +18,8 @@
specifically for use as the IoC container for Tapestry 4. Tapestry 4 has met its goals for extensibility and configurability, largely
because of HiveMind's flexibility.
- Tapestry 5 extends on this, replacing HiveMind with a new container directly built into the framework,
- designed for greater ease of use, expressiveness and performance.
+ Tapestry 5 extends on this, replacing HiveMind with a new container specifically build for Tapestry,
+ designed for greater ease of use, expressiveness and performance. And it can be used seperately from the rest of Tapestry!
* Why Not Spring?
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/provider.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/provider.apt?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/provider.apt (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/provider.apt Mon Nov 27 17:55:54 2006
@@ -29,8 +29,13 @@
* infrastructure provider
The tapestry module provides the
- {{{../guide/infrastructure.html}infrastructure}} object provider, which exists
+ {{{..tapestry-core/guide/infrastructure.html}infrastructure}} object provider, which exists
to provide shorter names for injecting services and to support overrides.
+
+* default provider
+
+ If the string does not contain a prefix, then the value is treated as a literal string.
+ It will be coerced to the proper type (such as int or long).
Defining New Providers
@@ -52,4 +57,9 @@
Of course, this is a simplified example. In a real scenario, the provider is most likely
a service with its own dependencies.
-
\ No newline at end of file
+
+Providers and Symbols
+
+ It is occasionally useful to add a level of indirection, to allow control over exactly
+ what service (or other object) is injected. Both the Inject and InjectService
+ annotations support the use of {{{symbols.html}symbols}}.
\ No newline at end of file
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/symbols.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/symbols.apt?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/symbols.apt (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/site/apt/symbols.apt Mon Nov 27 17:55:54 2006
@@ -0,0 +1,110 @@
+ ----
+ Symbols
+ ----
+
+Symbols
+
+ Tapestry IOC makes use of runtime-evaluated <symbols> to handle certains types of configuration tasks.
+
+ The syntax of symbols is based on Ant expression, that is, a leading <<<$\{>>> before the symbol name,
+ and a trailing <<<\}>>> after. The value on the inside is the <symbol name>. By convention, the symbol
+ name is segmented with periods.
+
+ These symbols are used inside the
+ {{{apidocs/org/apache/tapestry/ioc/annoations/Inject.html}Inject}} and
+ {{{apidocs/org/apache/tapestry/ioc/annoations/InjectService.html}InjectService}}
+ annoations.
+
+ For example:
+
++----+
+ public static MyService buildMyService(@Inject("${mymodule.some-reference}") CollaboratorA collabA,
+ @InjectService("${mymodule.some-service-id}") CollaboratorB collabB)
+ {
+ return . . . ;
+ }
++---+
+
+ Here, the first symbol is <<<mymodule.some-reference>>> whose value should be an
+ {{{provider.html}object reference}} such as <<<service:CollaboratorA>>>. The second symbol,
+ <<<mymodule.some-service-id>>> is just a service id, such as <<<CollaboratorB>>>.
+
+ Although not shown here, it is possible to use multple symbols inside the string, or mix literal text with
+ symbols.
+
+Symbol Resolution
+
+ Symbols are resolved by the
+ {{{apidocs/org/apache/tapestry/ioc/services/SymbolSource.html}SymbolSource}} service. The SymbolSource
+ checks against an ordered list of
+ {{{apidocs/org/apache/tapestry/ioc/services/SymbolProvider.html}SymbolProvider}} objects.
+
+ Additional symbol providers may be employed by contributing to the tapestry.ioc.SymbolSource service configuration,
+ which is an ordered list of SymbolProviders.
+
+ By default, there are three providers:
+
+* SystemProperties
+
+ The first provider allows JVM System Properties to provide symbol values. This allows the use
+ of the <<java>> command's <<-D>> option to provide runtime overrides. This is most often used
+ when testing code, rather than in production.
+
+* ApplicationDefaults
+
+ Values not found as System Properties are searched for in the ApplicationDefaults. This
+ service, tapestry.ioc.ApplicationDefaults may be configured using a mapped configuration to provide values.
+
+ From the previous example:
+
++----+
+ @Contribute("tapestry.ioc.ApplicationDefaults")
+ public void contributeDefaults(MappedConfiguration<String, String> configuration)
+ {
+ configuration.add("mymodule.some-reference", "service:CollaboratorA");
+ configuration.add("mymodule.some-service-id", "CollaboratorB");
+ }
++----+
+
+* FactoryDefaults
+
+ The same as ApplicationDefaults, but checked only if a value is not satisfied by SystemProperties
+ or ApplicationDefaults.
+
+ Libraries will typically set reasonable defaults as contributions to the FactoryDefaults service configuration.
+ Individual applications may hard code overrides of those defaults using ApplicationDefaults. Individual developers
+ may override even those defaults by setting JVM System Properties.
+
+Recursive Symbols
+
+ It is possible and valid to define one symbol in terms of one ore more other symbols.
+
+
++----+
+ @Contribute("tapestry.ioc.FactoryDefaults")
+ public void contributeDefaults(MappedConfiguration<String, String> configuration)
+ {
+ configuration.add("report.url", "http://${report.host}:${report.port}/${report.path}");
+ configuration.add("report.host", "www.myreportsite.com");
+ configuration.add("report.port", "80");
+ configuration.add("report.path", "/report.cgi");
+ }
++----+
+
+ The ordinary default for <<<report.url>>> will be <<<http://www.myreportsite.com:80/report.cgi>>>.
+
+ However, this can be changed by making an overriding contribution to the tapestry.ioc.ApplicationDefaults configuration.
+
+ Tapestry checks that no symbol is directly or indirectly dependent on itself. For example, the following contribution is
+ illegal:
+
++----+
+ @Contribute("tapestry.ioc.ApplicationDefaults")
+ public void contributeOverrides(MappedConfiguration<String, String> configuration)
+ {
+ configuration.add("report.path", "${report.url}/report.cgi");
+ }
++----+
+
+ When the <<<report.url>>> is referenced, an exception will be thrown with the message: <<
+ Symbol 'report.path' is defined in terms of itself (report.path --> report.util --> report.path).>>
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/site/site.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/site/site.xml?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/site/site.xml (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/site/site.xml Mon Nov 27 17:55:54 2006
@@ -36,10 +36,9 @@
<body>
<links>
- <item name="Tapestry" href="http://tapestry.apache.org/" />
- <item name="Apache" href="http://www.apache.org/" />
-
- </links>
+ <item name="Apache" href="http://www.apache.org/" />
+ </links>
+
<head>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">_uacct = "UA-400821-1"; urchinTracker();</script>
@@ -51,6 +50,7 @@
<item name="Services" href="service.html"/>
<item name="Decorators" href="decorator.html"/>
<item name="Configuration" href="configuration.html"/>
+ <item name="Symbols" href="symbols.html"/>
<item name="Starting the Registry" href="run.html"/>
<item name="Object Providers" href="provider.html"/>
<item name="Ordering" href="order.html"/>
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/Indirection.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/Indirection.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/Indirection.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/Indirection.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,21 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc;
+
+/** Used to demonstrate indirection via symbols. */
+public interface Indirection
+{
+ String getName();
+}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IndirectionModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IndirectionModule.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IndirectionModule.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IndirectionModule.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,70 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc;
+
+import org.apache.tapestry.ioc.annotations.Contribute;
+import org.apache.tapestry.ioc.annotations.Id;
+import org.apache.tapestry.ioc.annotations.Inject;
+import org.apache.tapestry.ioc.annotations.InjectService;
+
+@Id("indirection")
+public class IndirectionModule
+{
+ public Indirection buildInner()
+ {
+ return new Indirection()
+ {
+ public String getName()
+ {
+ return "INNER";
+ }
+ };
+ }
+
+ public Indirection buildOuter(final @InjectService("${indirection.inner}")
+ Indirection inner)
+ {
+ return new Indirection()
+ {
+ public String getName()
+ {
+ return String.format("OUTER[%S]", inner.getName());
+ }
+ };
+ }
+
+ public Indirection buildOuter2(final @Inject("${indirection.object-inner}")
+ Indirection inner)
+ {
+ return new Indirection()
+ {
+ public String getName()
+ {
+ return String.format("OUTER2[%S]", inner.getName());
+ }
+ };
+ }
+
+ @Contribute("tapestry.ioc.FactoryDefaults")
+ public void contributeDefaults(MappedConfiguration<String, String> configuration)
+ {
+ // You tend to want to use the fully qualified service id, since you can't tell under which
+ // context the
+ // symbols will be expanded.
+
+ configuration.add("indirection.inner", "indirection.Inner");
+ configuration.add("indirection.object-inner", "service:indirection.Inner");
+ }
+}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/InjectLiteralModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/InjectLiteralModule.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/InjectLiteralModule.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/InjectLiteralModule.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,35 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc;
+
+import org.apache.tapestry.ioc.annotations.Contribute;
+import org.apache.tapestry.ioc.annotations.Id;
+import org.apache.tapestry.ioc.annotations.Inject;
+
+@Id("inject.literal")
+public class InjectLiteralModule
+{
+ public static IntHolder buildIntHolder(@Inject("${value}")
+ int value)
+ {
+ return new IntHolderImpl(value);
+ }
+
+ @Contribute("tapestry.ioc.FactoryDefaults")
+ public static void contributeDefaults(MappedConfiguration<String, String> configuration)
+ {
+ configuration.add("value", "42");
+ }
+}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolder.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolder.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolder.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,20 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc;
+
+public interface IntHolder
+{
+ int getValue();
+}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolderImpl.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolderImpl.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntHolderImpl.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,31 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc;
+
+public class IntHolderImpl implements IntHolder
+{
+ private final int _value;
+
+ public IntHolderImpl(final int value)
+ {
+ _value = value;
+ }
+
+ public int getValue()
+ {
+ return _value;
+ }
+
+}
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java Mon Nov 27 17:55:54 2006
@@ -366,4 +366,54 @@
r = null;
}
+
+ @Test
+ public void inject_service_annotation_with_symbol()
+ {
+ Registry r = buildRegistry(IndirectionModule.class);
+
+ Indirection outer = r.getService("indirection.Outer", Indirection.class);
+
+ assertEquals(outer.getName(), "OUTER[INNER]");
+ }
+
+ @Test
+ public void inject_annotation_with_symbol()
+ {
+ Registry r = buildRegistry(IndirectionModule.class);
+
+ Indirection outer = r.getService("indirection.Outer2", Indirection.class);
+
+ assertEquals(outer.getName(), "OUTER2[INNER]");
+ }
+
+ @Test
+ public void registry_get_service_with_symbol()
+ {
+ Registry r = buildRegistry(IndirectionModule.class);
+
+ Indirection inner = r.getService("${indirection.inner}", Indirection.class);
+
+ assertEquals(inner.getName(), "INNER");
+ }
+
+ @Test
+ public void registry_get_object_with_symbol()
+ {
+ Registry r = buildRegistry(IndirectionModule.class);
+
+ Indirection inner = r.getObject("${indirection.object-inner}", Indirection.class);
+
+ assertEquals(inner.getName(), "INNER");
+ }
+
+ @Test
+ public void inject_annotation_literal_value()
+ {
+ Registry r = buildRegistry(InjectLiteralModule.class);
+
+ IntHolder holder = r.getService(IntHolder.class);
+
+ assertEquals(holder.getValue(), 42);
+ }
}
Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/ModuleImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/ModuleImplTest.java?view=diff&rev=479843&r1=479842&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/ModuleImplTest.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/ModuleImplTest.java Mon Nov 27 17:55:54 2006
@@ -253,6 +253,8 @@
log.warn(contains("contains more than one public constructor"));
+ train_expandSymbols(registry, "tapestry.ioc.ClassFactory", "tapestry.ioc.ClassFactory");
+
train_getService(registry, "tapestry.ioc.ClassFactory", ClassFactory.class, module, factory);
replay();
@@ -260,6 +262,11 @@
assertTrue(module.getModuleBuilder() instanceof ExtraPublicConstructorsModule);
verify();
+ }
+
+ protected void train_expandSymbols(InternalRegistry registry, String input, String expanded)
+ {
+ expect(registry.expandSymbols(input)).andReturn(expanded);
}
private Registry buildRegistry()
Copied: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProviderTest.java (from r479717, tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProvideTest.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProviderTest.java?view=diff&rev=479843&p1=tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProvideTest.java&r1=479717&p2=tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProviderTest.java&r2=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProvideTest.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/MasterObjectProviderTest.java Mon Nov 27 17:55:54 2006
@@ -21,9 +21,11 @@
import org.apache.tapestry.ioc.ServiceLocator;
import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
import org.apache.tapestry.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry.ioc.services.SymbolSource;
+import org.apache.tapestry.ioc.services.TypeCoercer;
import org.testng.annotations.Test;
-public class MasterObjectProvideTest extends IOCInternalTestCase
+public class MasterObjectProviderTest extends IOCInternalTestCase
{
private Map<String, ObjectProvider> newMap(String prefix, ObjectProvider provider)
{
@@ -36,12 +38,14 @@
ObjectProvider provider = newObjectProvider();
ServiceLocator locator = newServiceLocator();
Runnable r = newRunnable();
+ SymbolSource source = newSymbolSource();
+ train_expandSymbols(source, "prefix:expression");
train_provide(provider, "expression", Runnable.class, locator, r);
replay();
- ObjectProvider master = new MasterObjectProvider(newMap("prefix", provider));
+ ObjectProvider master = new MasterObjectProvider(newMap("prefix", provider), source, null);
Runnable actual = master.provide("prefix:expression", Runnable.class, locator);
@@ -51,15 +55,40 @@
}
@Test
+ public void symbols_are_expanded()
+ {
+ ObjectProvider provider = newObjectProvider();
+ ServiceLocator locator = newServiceLocator();
+ Runnable r = newRunnable();
+ SymbolSource source = newSymbolSource();
+
+ train_expandSymbols(source, "${symbol}", "prefix:expression");
+ train_provide(provider, "expression", Runnable.class, locator, r);
+
+ replay();
+
+ ObjectProvider master = new MasterObjectProvider(newMap("prefix", provider), source, null);
+
+ Runnable actual = master.provide("${symbol}", Runnable.class, locator);
+
+ assertSame(actual, r);
+
+ verify();
+ }
+
+ @Test
public void unknown_object_provider_prefix()
{
ServiceLocator locator = newServiceLocator();
+ SymbolSource source = newSymbolSource();
+
+ train_expandSymbols(source, "prefix:expression");
replay();
Map<String, ObjectProvider> configuration = CollectionFactory.newMap();
- ObjectProvider master = new MasterObjectProvider(configuration);
+ ObjectProvider master = new MasterObjectProvider(configuration, source, null);
try
{
@@ -80,24 +109,19 @@
public void no_prefix_in_object_reference()
{
ServiceLocator locator = newServiceLocator();
+ SymbolSource source = newSymbolSource();
+ TypeCoercer coercer = newTypeCoercer();
+
+ train_expandSymbols(source, "${value}", "55");
+ train_coerce(coercer, "55", Integer.class, 55);
replay();
Map<String, ObjectProvider> configuration = CollectionFactory.newMap();
- ObjectProvider master = new MasterObjectProvider(configuration);
+ ObjectProvider master = new MasterObjectProvider(configuration, source, coercer);
- try
- {
- master.provide("just-expression", Runnable.class, locator);
- unreachable();
- }
- catch (RuntimeException ex)
- {
- assertEquals(
- ex.getMessage(),
- "Object reference 'just-expression' does not contain an provider prefix (such as 'service:').");
- }
+ assertEquals(master.provide("${value}", Integer.class, locator), new Integer(55));
verify();
}
Added: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImplTest.java?view=auto&rev=479843
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImplTest.java (added)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/services/SymbolSourceImplTest.java Mon Nov 27 17:55:54 2006
@@ -0,0 +1,307 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc.internal.services;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
+import org.apache.tapestry.ioc.services.SymbolProvider;
+import org.apache.tapestry.ioc.services.SymbolSource;
+import org.testng.annotations.Test;
+
+public class SymbolSourceImplTest extends IOCInternalTestCase
+{
+ @Test
+ public void expand_symbols_no_symbols_present()
+ {
+ String input = "A jolly good time.";
+
+ List<SymbolProvider> providers = Collections.emptyList();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ // Not just equal, but the same.
+
+ assertSame(source.expandSymbols(input), input);
+ }
+
+ @Test
+ public void simple_expansions()
+ {
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "barney", "Barney");
+ train_valueForSymbol(provider, "dino", "Dino");
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ assertEquals(
+ source.expandSymbols("Fred's friends are ${barney} and ${dino}."),
+ "Fred's friends are Barney and Dino.");
+
+ verify();
+ }
+
+ @Test
+ public void undefined_symbol()
+ {
+
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "barney", null);
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ try
+ {
+ source.valueForSymbol("barney");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertEquals(ex.getMessage(), "Symbol 'barney' is not defined.");
+ }
+
+ verify();
+ }
+
+ @Test
+ public void missing_brace()
+ {
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ try
+ {
+ source.expandSymbols("Unmatched ${this");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertEquals(
+ ex.getMessage(),
+ "Input string 'Unmatched ${this' is missing a symbol closing brace.");
+ }
+
+ verify();
+ }
+
+ @Test
+ public void indirect_expansions()
+ {
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "fred.friends", "${barney} and ${dino}");
+ train_valueForSymbol(provider, "barney", "Barney");
+ train_valueForSymbol(provider, "dino", "Dino");
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ assertEquals(
+ source.expandSymbols("Fred's friends are ${fred.friends}."),
+ "Fred's friends are Barney and Dino.");
+
+ verify();
+ }
+
+ @Test
+ public void undefined_symbol_in_path()
+ {
+
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "barney", "Barney (whose friends are ${barney.friends})");
+ train_valueForSymbol(provider, "barney.friends", "${fred} and ${betty}");
+ train_valueForSymbol(provider, "fred", null);
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ try
+ {
+ source.valueForSymbol("barney");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertEquals(
+ ex.getMessage(),
+ "Symbol 'fred' is not defined (in barney --> barney.friends --> fred). ");
+ }
+
+ verify();
+ }
+
+ @Test
+ public void missing_brace_in_path()
+ {
+
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "barney", "Barney (whose friends are ${barney.friends})");
+ train_valueForSymbol(provider, "barney.friends", "${fred} and ${betty");
+ train_valueForSymbol(provider, "fred", "Fred");
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ try
+ {
+ source.valueForSymbol("barney");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertEquals(
+ ex.getMessage(),
+ "Input string '${fred} and ${betty' is missing a symbol closing brace (in barney --> barney.friends).");
+ }
+
+ verify();
+ }
+
+ @Test
+ public void providers_searched_in_order()
+ {
+ SymbolProvider provider1 = newSymbolProvider();
+ SymbolProvider provider2 = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider1, provider2);
+
+ train_valueForSymbol(provider1, "fred.friends", "${barney} and ${dino}");
+ train_valueForSymbol(provider1, "barney", null);
+ train_valueForSymbol(provider2, "barney", "Barney");
+ train_valueForSymbol(provider1, "dino", null);
+ train_valueForSymbol(provider2, "dino", "Dino");
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ assertEquals(
+ source.expandSymbols("Fred's friends are ${fred.friends}."),
+ "Fred's friends are Barney and Dino.");
+
+ verify();
+ }
+
+ @Test
+ public void symbols_are_cached()
+ {
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "fred", "Fred's friends are ${barney} and ${dino}.");
+ train_valueForSymbol(provider, "barney", "Barney");
+ train_valueForSymbol(provider, "dino", "Dino");
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ assertEquals(source.valueForSymbol("fred"), "Fred's friends are Barney and Dino.");
+
+ verify();
+
+ replay();
+
+ // This time, comes out of the cache.
+
+ assertEquals(source.valueForSymbol("fred"), "Fred's friends are Barney and Dino.");
+
+ verify();
+ }
+
+ @Test
+ public void recursive_symbols_fail()
+ {
+ SymbolProvider provider = newSymbolProvider();
+
+ List<SymbolProvider> providers = Arrays.asList(provider);
+
+ train_valueForSymbol(provider, "fred", "Fred (whose friends are ${fred.friends})");
+ train_valueForSymbol(provider, "fred.friends", "${barney} and ${dino}");
+ train_valueForSymbol(provider, "barney", "Barney (whose friends are ${barney.friends})");
+ train_valueForSymbol(provider, "barney.friends", "${fred} and ${betty}");
+
+ replay();
+
+ SymbolSource source = new SymbolSourceImpl(providers);
+
+ try
+ {
+ source.valueForSymbol("fred");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertEquals(
+ ex.getMessage(),
+ "Symbol 'fred' is defined in terms of itself (fred --> fred.friends --> barney --> barney.friends --> fred).");
+ }
+
+ verify();
+ }
+
+ @Test
+ public void integration_test()
+ {
+ SymbolSource source = getService(SymbolSource.class);
+
+ // SystemPropertiesSymbolProvider is available by default
+
+ String userName = System.getProperty("user.name");
+
+ assertEquals(source.valueForSymbol("user.name"), userName);
+ }
+
+ protected final void train_valueForSymbol(SymbolProvider provider, String symbolName,
+ String value)
+ {
+ expect(provider.valueForSymbol(symbolName)).andReturn(value);
+ }
+
+ protected final SymbolProvider newSymbolProvider()
+ {
+ return newMock(SymbolProvider.class);
+ }
+
+}