001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.tapestry.ioc.annotations.Match;
025: import org.apache.tapestry.ioc.annotations.Order;
026:
027: /**
028: * Module used to demonstrate decorator ordering.
029: */
030:
031: public class BarneyModule {
032: @Match({"UnorderedNames","Fred","PrivateFredAlias"})
033: @Order("after:Beta")
034: public Object decorateGamma(Object delegate, DecoratorList list) {
035: list.add("gamma");
036:
037: return null;
038: }
039:
040: public Sizer buildSizer(final Map<Class, Sizer> configuration) {
041: return new Sizer() {
042: public int size(Object object) {
043: if (object == null)
044: return 0;
045:
046: Sizer sizer = configuration.get(object.getClass());
047:
048: if (sizer != null)
049: return sizer.size(object);
050:
051: return 1;
052: }
053:
054: };
055: }
056:
057: public void contributeSizer(
058: MappedConfiguration<Class, Sizer> configuration) {
059: Sizer listSizer = new Sizer() {
060: public int size(Object object) {
061: List list = (List) object;
062:
063: return list.size();
064: }
065: };
066:
067: Sizer mapSizer = new Sizer() {
068: public int size(Object object) {
069: Map map = (Map) object;
070:
071: return map.size();
072: }
073: };
074:
075: // Have to work on concrete class, rather than type, until we move the StrategyFactory
076: // over from HiveMind.
077:
078: configuration.add(ArrayList.class, listSizer);
079: configuration.add(HashMap.class, mapSizer);
080: }
081:
082: /**
083: * Put DecoratorList in module barney, where so it won't accidentally be decorated (which
084: * recusively builds the service, and is caught as a failure).
085: */
086: public DecoratorList buildDecoratorList() {
087: return new DecoratorList() {
088: private List<String> _names = newList();
089:
090: public void add(String name) {
091: _names.add(name);
092: }
093:
094: public List<String> getNames() {
095: return _names;
096: }
097: };
098: }
099:
100: public void contributeUnorderedNames(
101: Configuration<String> configuration) {
102: configuration.add("Gamma");
103: }
104: }
|