01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.vote;
17:
18: import java.lang.reflect.Method;
19:
20: /**
21: * This is a very useful implementation of the LabelParameterStrategy. Data objects which are meant to be labeled
22: * should implement the LabeledData interface. This strategy will then castdown to that interface for either testing
23: * or retrieval of the label.
24: *
25: * @author Greg Turnquist
26: * @version $Id: InterfaceBasedLabelParameterStrategy.java 1750 2006-11-14 22:07:36Z benalex $
27: */
28: public class InterfaceBasedLabelParameterStrategy implements
29: LabelParameterStrategy {
30: //~ Instance fields ================================================================================================
31:
32: private String noLabel = "";
33:
34: //~ Methods ========================================================================================================
35:
36: /**
37: * Test if the argument is labeled, and if so, downcast to LabeledData and retrieve the domain object's
38: * labeled value. Otherwise, return an empty string. NOTE: The default for no label is an empty string. If somehow
39: * the user wants to make that a label itself, he or she must inject an alternate value to the noLabel property.
40: *
41: * @param method DOCUMENT ME!
42: * @param arg DOCUMENT ME!
43: *
44: * @return DOCUMENT ME!
45: */
46: public String getLabel(Method method, Object arg) {
47: if (isLabeled(method, arg)) {
48: return ((LabeledData) arg).getLabel();
49: } else {
50: return noLabel;
51: }
52: }
53:
54: public String getNoLabel() {
55: return noLabel;
56: }
57:
58: /**
59: * Test if the argument implemented the LabeledData interface. NOTE: The invoking method has no bearing for
60: * this strategy, only the argument itself.
61: *
62: * @param method DOCUMENT ME!
63: * @param arg DOCUMENT ME!
64: *
65: * @return DOCUMENT ME!
66: */
67: public boolean isLabeled(Method method, Object arg) {
68: return (arg instanceof LabeledData);
69: }
70:
71: public void setNoLabel(String noLabel) {
72: this.noLabel = noLabel;
73: }
74: }
|