~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jgdi/src/com/sun/grid/jgdi/configuration/reflect/PropertyDescriptor.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.jgdi.configuration.reflect;
 
33
 
 
34
import java.lang.reflect.*;
 
35
 
 
36
/**
 
37
 *
 
38
 */
 
39
public abstract class PropertyDescriptor {
 
40
    
 
41
    private Class  beanClass;
 
42
    private String propertyName;
 
43
    Class  propertyType;
 
44
    private int    cullFieldName;
 
45
    private String cullType;
 
46
    
 
47
    private boolean readOnly;
 
48
    private int hashCode;
 
49
    private boolean browsable;
 
50
    private boolean configurable;
 
51
    private Method isSetMethod;
 
52
    
 
53
    /** Some cull type are mapped to a primitive java class
 
54
     *  e.g HR_Type -> String
 
55
     *  For all these type the <code>hasCullWrapper</code> flag
 
56
     *  is set to <code>true</code>.
 
57
     */
 
58
    private boolean hasCullWrapper;
 
59
    
 
60
    /**
 
61
     *  If a cull type is mapped to a primitive java class the
 
62
     *  <code>cullContentField</code> specifies the cull attribute
 
63
     *  which contains the content.
 
64
     */
 
65
    private int     cullContentField;
 
66
    
 
67
    
 
68
    
 
69
    /** Creates a new instance of PropertyDescriptor */
 
70
    protected PropertyDescriptor(Class beanClass, String propertyName,
 
71
            Class propertyType, String cullType,
 
72
            int cullFieldName, boolean readOnly,
 
73
            boolean configurable) {
 
74
        this.beanClass = beanClass;
 
75
        this.propertyName = propertyName;
 
76
        this.propertyType = propertyType;
 
77
        this.cullType = cullType;
 
78
        this.cullFieldName = cullFieldName;
 
79
        this.readOnly = readOnly;
 
80
        this.configurable = configurable;
 
81
        this.hashCode = beanClass.hashCode() * 31 + propertyName.hashCode();
 
82
        
 
83
        try {
 
84
            this.isSetMethod = findMethod("isSet", null);
 
85
        } catch (IllegalArgumentException e) {
 
86
            // ignore error if isSet* is not present for bean class -> webqmon
 
87
        }
 
88
    }
 
89
    
 
90
    public boolean equals(Object obj) {
 
91
        return obj instanceof PropertyDescriptor &&
 
92
                beanClass.equals(((PropertyDescriptor)obj).beanClass) &&
 
93
                propertyName.equals(((PropertyDescriptor)obj).propertyName);
 
94
    }
 
95
    
 
96
    
 
97
    public int hashCode() {
 
98
        return hashCode;
 
99
    }
 
100
    
 
101
    protected Method findMethod(String prefix, String suffix, Class [] argTypes) {
 
102
        
 
103
        StringBuilder buf = new StringBuilder();
 
104
        if (prefix != null) {
 
105
            buf.append(prefix);
 
106
        }
 
107
        buf.append(Character.toUpperCase(propertyName.charAt(0)));
 
108
        buf.append(propertyName.substring(1));
 
109
        if (suffix != null) {
 
110
            buf.append(suffix);
 
111
        }
 
112
        String methodName = buf.toString();
 
113
        
 
114
        try {
 
115
            return beanClass.getMethod(methodName, argTypes);
 
116
        } catch (NoSuchMethodException nse) {
 
117
            StringBuilder buffer = new StringBuilder();
 
118
            if (argTypes != null) {
 
119
                for (int i=0; i<argTypes.length; i++) {
 
120
                    if (i>0) {
 
121
                        buffer.append(", ");
 
122
                    }
 
123
                    buffer.append(argTypes[i].getName());
 
124
                }
 
125
            } else {
 
126
                buffer.append("");
 
127
            }
 
128
            IllegalArgumentException ilse = new IllegalArgumentException(
 
129
                    "Method " + methodName + "(" + buffer + ")"
 
130
                    + " not found in class " + beanClass );
 
131
            ilse.initCause(nse);
 
132
            throw ilse;
 
133
        }
 
134
    }
 
135
    
 
136
    protected Method findMethod(String prefix, Class [] argTypes) {
 
137
        return findMethod(prefix, null, argTypes);
 
138
    }
 
139
    
 
140
    protected Object invoke(Method method, Object bean, Object[] args) {
 
141
        try {
 
142
            return method.invoke(bean, args);
 
143
        } catch (IllegalAccessException iae) {
 
144
            IllegalStateException ilse = new IllegalStateException( method.getName()  + " of "
 
145
                    + getBeanClass().getName() + "." + getPropertyName()
 
146
                    + " is not accessible");
 
147
            ilse.initCause( iae );
 
148
            throw ilse;
 
149
        } catch( InvocationTargetException ite ) {
 
150
            IllegalStateException ilse = new IllegalStateException(
 
151
                    "error in method " + method.getName() + " of "
 
152
                    + getBeanClass().getName() + "." + getPropertyName() );
 
153
            ilse.initCause( ite );
 
154
            throw ilse;
 
155
        }
 
156
    }
 
157
    
 
158
    
 
159
    /**
 
160
     * Getter for property propertyType.
 
161
     * @return Value of property propertyType.
 
162
     */
 
163
    public java.lang.Class getPropertyType() {
 
164
        return propertyType;
 
165
    }
 
166
    
 
167
    public String getCullType() {
 
168
        return cullType;
 
169
    }
 
170
    
 
171
    public String getJNIPropertyType() {
 
172
        
 
173
        String name = propertyType.getName();
 
174
        if( !propertyType.isPrimitive() ) {
 
175
            name = "L" + name.replace('.','/') + ";";
 
176
        }
 
177
        return name;
 
178
    }
 
179
    
 
180
    /**
 
181
     * Getter for property propertyName.
 
182
     * @return Value of property propertyName.
 
183
     */
 
184
    public java.lang.String getPropertyName() {
 
185
        return propertyName;
 
186
    }
 
187
    
 
188
    public int getCullFieldName() {
 
189
        return this.cullFieldName;
 
190
    }
 
191
    /**
 
192
     * Getter for property beanClass.
 
193
     * @return Value of property beanClass.
 
194
     */
 
195
    public java.lang.Class getBeanClass() {
 
196
        return beanClass;
 
197
    }
 
198
    
 
199
    public abstract void clone( Object srcBean, Object targetBean );
 
200
    
 
201
    public boolean isReadOnly() {
 
202
        return readOnly;
 
203
    }
 
204
    
 
205
    public void setReadOnly(boolean readOnly) {
 
206
        this.readOnly = readOnly;
 
207
    }
 
208
    
 
209
    public boolean isConfigurable() {
 
210
        return configurable;
 
211
    }
 
212
    
 
213
    public void setConfigurable(boolean configurable) {
 
214
        this.configurable = configurable;
 
215
    }
 
216
    
 
217
    
 
218
    /**
 
219
     * Getter for property browsable.
 
220
     * @return Value of property browsable.
 
221
     */
 
222
    public boolean isBrowsable() {
 
223
        return browsable;
 
224
    }
 
225
    
 
226
    /**
 
227
     * Setter for property browsable.
 
228
     * @param browsable New value of property browsable.
 
229
     */
 
230
    public void setBrowsable(boolean browsable) {
 
231
        this.browsable = browsable;
 
232
    }
 
233
    
 
234
    public boolean hasCullWrapper() {
 
235
        return hasCullWrapper;
 
236
    }
 
237
    
 
238
    public void setHasCullWrapper(boolean hasCullWrapper) {
 
239
        this.hasCullWrapper = hasCullWrapper;
 
240
    }
 
241
    
 
242
    public int getCullContentField() {
 
243
        return cullContentField;
 
244
    }
 
245
    
 
246
    public void setCullContentField(int cullContentField) {
 
247
        this.cullContentField = cullContentField;
 
248
    }
 
249
    
 
250
    public boolean isSet(Object bean) {
 
251
        return ((Boolean)invoke(isSetMethod,  bean , null)).booleanValue();
 
252
    }
 
253
    
 
254
}