01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.controls.runtime.bean;
20:
21: import org.apache.beehive.controls.api.bean.ControlInterface;
22: import org.apache.beehive.controls.api.bean.ControlExtension;
23: import org.apache.beehive.controls.api.ControlException;
24:
25: /**
26: * Utilities used by the Controls runtime.
27: */
28: public final class ControlUtils {
29:
30: private ControlUtils() {
31: }
32:
33: /**
34: * Implements the default control implementation binding algorithm ( <InterfaceName> + "Impl" ). See
35: * documentation for the org.apache.beehive.controls.api.bean.ControlInterface annotation.
36: *
37: * @param implBinding the value of the defaultBinding attribute returned from a ControlInterface annotation
38: * @param controlClass the actual name of the interface decorated by the ControlInterface annotation
39: * @return the resolved defaultBinding value
40: */
41: public static String resolveDefaultBinding(String implBinding,
42: String controlClass) {
43: int intfIndex = implBinding
44: .indexOf(ControlInterface.INTERFACE_NAME);
45: if (intfIndex >= 0) {
46: implBinding = implBinding.substring(0, intfIndex)
47: + controlClass
48: + implBinding.substring(intfIndex
49: + ControlInterface.INTERFACE_NAME.length());
50: }
51: return implBinding;
52: }
53:
54: /**
55: * Returns the default binding based entirely upon annotations or naming conventions.
56: * @param controlIntf the control interface class
57: * @return the class name of the default control implementation binding
58: */
59: static String getDefaultControlBinding(Class controlIntf) {
60: controlIntf = getMostDerivedInterface(controlIntf);
61:
62: ControlInterface intfAnnot = (ControlInterface) controlIntf
63: .getAnnotation(ControlInterface.class);
64: String implBinding = intfAnnot.defaultBinding();
65: implBinding = resolveDefaultBinding(implBinding, controlIntf
66: .getName());
67:
68: return implBinding;
69: }
70:
71: /**
72: * Computes the most derived ControlInterface for the specified ControlExtension.
73: * @param controlIntf
74: * @return the most derived ControlInterface
75: */
76: static Class getMostDerivedInterface(Class controlIntf) {
77: while (controlIntf.isAnnotationPresent(ControlExtension.class)) {
78: Class[] intfs = controlIntf.getInterfaces();
79: boolean found = false;
80: for (int i = 0; i < intfs.length; i++) {
81: if (intfs[i]
82: .isAnnotationPresent(ControlExtension.class)
83: || intfs[i]
84: .isAnnotationPresent(ControlInterface.class)) {
85: controlIntf = intfs[i];
86: found = true;
87: break;
88: }
89: }
90: if (!found) {
91: throw new ControlException(
92: "Can't find base control interface for "
93: + controlIntf);
94: }
95: }
96: return controlIntf;
97: }
98: }
|