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.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
018:
019: import java.util.Formatter;
020:
021: import org.apache.tapestry.ioc.internal.util.InternalUtils;
022:
023: /**
024: * A contribution into the {@link Alias} or AliasOverride service configuration.
025: */
026: public final class AliasContribution<T> {
027: private final Class<T> _contributionType;
028:
029: private final String _mode;
030:
031: private final T _object;
032:
033: /**
034: * Simplifies the creation of an AliasContribution around a known type and instance of that
035: * type.
036: */
037: public static <X> AliasContribution<X> create(
038: Class<X> contributionType, X object) {
039: return new AliasContribution<X>(contributionType, object);
040: }
041:
042: /**
043: * Simplifies the creation of an AliasContribution around a known type, mode, and an instance of
044: * that type.
045: */
046: public static <X> AliasContribution<X> create(
047: Class<X> contributionType, String mode, X object) {
048: return new AliasContribution<X>(contributionType, mode, object);
049: }
050:
051: /**
052: * Conntributes the object with a blank mode.
053: */
054: public AliasContribution(Class<T> contributionType, T object) {
055: this (contributionType, "", object);
056: }
057:
058: public AliasContribution(Class<T> contributionType, String mode,
059: T object) {
060: _contributionType = notNull(contributionType,
061: "contributionClass");
062: _mode = notNull(mode, "mode");
063: _object = notNull(object, "object");
064: }
065:
066: /**
067: * Returns the mode of operation for this instance of Tapestry. Most of the time, this will be
068: * the empty string, meaning that the contribution applies to Tapestry is any mode. In other
069: * cases, the mode will be "servlet" but may be other modes via add on modules, such as
070: * "portlet" or "offline".
071: */
072: public String getMode() {
073: return _mode;
074: }
075:
076: public Class<T> getContributionType() {
077: return _contributionType;
078: }
079:
080: /** The contributed object, which will be made available. */
081: public T getObject() {
082: return _object;
083: }
084:
085: @Override
086: public String toString() {
087: StringBuilder builder = new StringBuilder();
088: Formatter formatter = new Formatter(builder);
089:
090: formatter.format("<AliasContribution: %s", _contributionType
091: .getName());
092:
093: if (InternalUtils.isNonBlank(_mode))
094: formatter.format(" mode:%s", _mode);
095:
096: formatter.format(" %s>", _object);
097:
098: return builder.toString();
099: }
100:
101: }
|