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