~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/AbstractClassDescriptor.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.util.*;
 
35
import com.sun.grid.jgdi.configuration.GEObject;
 
36
import com.sun.grid.jgdi.configuration.Util;
 
37
 
 
38
 
 
39
/**
 
40
 * Abstract Base class for <code>ClassDescriptor</code> subclasses
 
41
 *
 
42
 */
 
43
public abstract class AbstractClassDescriptor implements ClassDescriptor {
 
44
    
 
45
    private ArrayList<PropertyDescriptor> propertyList = new ArrayList<PropertyDescriptor>();
 
46
    private Map<String, PropertyDescriptor> propertyMap = new HashMap<String, PropertyDescriptor>();
 
47
    private Class beanClass;
 
48
    private Class implClass;
 
49
    private String cullName;
 
50
    
 
51
    protected AbstractClassDescriptor(Class beanClass, String cullName) {
 
52
        this.beanClass = beanClass;
 
53
        this.cullName = cullName;
 
54
    }
 
55
    
 
56
    public List getProperties() {
 
57
        return Collections.unmodifiableList(propertyList);
 
58
    }
 
59
    
 
60
    protected void add(PropertyDescriptor property) {
 
61
        propertyList.add(property);
 
62
        propertyMap.put(property.getPropertyName(), property);
 
63
    }
 
64
    
 
65
    protected SimplePropertyDescriptor addSimple(String name, Class type, String cullType, int cullFieldName, boolean required, boolean readOnly, boolean configurable) {
 
66
        SimplePropertyDescriptor prop = new SimplePropertyDescriptor(beanClass, name, type, cullType, cullFieldName, required, readOnly, configurable);
 
67
        add(prop);
 
68
        return prop;
 
69
    }
 
70
    
 
71
    protected ListPropertyDescriptor addList(String name, Class type, String cullType, int cullFieldName, boolean browsable, boolean readOnly, boolean configurable) {
 
72
        ListPropertyDescriptor prop = new DefaultListPropertyDescriptor(beanClass, name, type, cullType, cullFieldName, browsable, readOnly, configurable);
 
73
        add(prop);
 
74
        return prop;
 
75
    }
 
76
    
 
77
    protected MapPropertyDescriptor addMap(String name, Class type, String cullType, Class keyType, int cullFieldName, int keyCullFieldName, int valueCullFieldName, Object defaultKey, boolean readOnly, boolean configurable) {
 
78
        MapPropertyDescriptor prop = new DefaultMapPropertyDescriptor(beanClass, name, type, cullType, keyType, cullFieldName, keyCullFieldName, valueCullFieldName, defaultKey, readOnly, configurable);
 
79
        add(prop);
 
80
        return prop;
 
81
    }
 
82
    
 
83
    protected MapListPropertyDescriptor addMapList(String name, Class type, String cullType, Class keyType, String cullListType, int cullFieldName, int keyCullFieldName, int valueCullFieldName, Object defaultKey, boolean readOnly, boolean configurable) {
 
84
        MapListPropertyDescriptor prop = new DefaultMapListPropertyDescriptor(beanClass, name, type, cullType, keyType, cullListType, cullFieldName, keyCullFieldName, valueCullFieldName, defaultKey, readOnly, configurable);
 
85
        add(prop);
 
86
        return prop;
 
87
    }
 
88
    
 
89
    public PropertyDescriptor getProperty(String name) {
 
90
        return propertyMap.get(name);
 
91
    }
 
92
    
 
93
    public String[] getPropertyNames() {
 
94
        String[] ret = new String[propertyMap.size()];
 
95
        propertyMap.keySet().toArray(ret);
 
96
        return ret;
 
97
    }
 
98
    
 
99
    public int getPropertyCount() {
 
100
        return propertyList.size();
 
101
    }
 
102
    
 
103
    public PropertyDescriptor getProperty(int index) {
 
104
        return propertyList.get(index);
 
105
    }
 
106
    
 
107
    public PropertyDescriptor getPropertyByCullFieldName(int cullFieldName) {
 
108
        for(PropertyDescriptor pd: propertyList) {
 
109
            if (pd.getCullFieldName() == cullFieldName) {
 
110
                return pd;
 
111
            }
 
112
        }
 
113
        return null;
 
114
    }
 
115
    
 
116
    public Class getBeanClass() {
 
117
        return beanClass;
 
118
    }
 
119
    
 
120
    public Object newInstance() {
 
121
        try {
 
122
            if (implClass != null) {
 
123
                return implClass.newInstance();
 
124
            } else {
 
125
                return beanClass.newInstance();
 
126
            }
 
127
        } catch (IllegalAccessException ile) {
 
128
            IllegalStateException ilse = new IllegalStateException("Constructor" + beanClass.getName() + " is not accessible");
 
129
            ilse.initCause(ile);
 
130
            throw ilse;
 
131
        } catch (InstantiationException iae) {
 
132
            IllegalStateException ilse = new IllegalStateException("Can\'t create instanceof of " + beanClass.getName());
 
133
            ilse.initCause(iae);
 
134
            throw ilse;
 
135
        }
 
136
    }
 
137
    
 
138
    public Object clone(Object bean) {
 
139
        
 
140
        ClassDescriptor classDescr = Util.getDescriptor(bean.getClass());
 
141
        Object ret = classDescr.newInstance();
 
142
        PropertyDescriptor propDescr = null;
 
143
        
 
144
        for (int i = 0; i < classDescr.getPropertyCount(); i++) {
 
145
            propDescr = classDescr.getProperty(i);
 
146
            if (propDescr.getPropertyName().equals("parent")) {
 
147
                SimplePropertyDescriptor parentPropDescr = (SimplePropertyDescriptor) propDescr;
 
148
                parentPropDescr.setValue(ret, parentPropDescr.getValue(bean));
 
149
            } else {
 
150
                propDescr.clone(bean, ret);
 
151
            }
 
152
        }
 
153
        
 
154
        return ret;
 
155
    }
 
156
    
 
157
    public void registryObjects(Object root) {
 
158
        
 
159
        ClassDescriptor cd = Util.getDescriptor(root.getClass());
 
160
        int count = cd.getPropertyCount();
 
161
        PropertyDescriptor pd = null;
 
162
        
 
163
        for (int i = 0; i < count; i++) {
 
164
            pd = cd.getProperty(i);
 
165
            if (!pd.getPropertyName().equals("parent") && GEObject.class.isAssignableFrom(pd.getPropertyType())) {
 
166
                GEObject value = null;
 
167
                if (pd instanceof SimplePropertyDescriptor) {
 
168
                    value = (GEObject) ((SimplePropertyDescriptor)pd).getValue(root);
 
169
                } else if (pd instanceof ListPropertyDescriptor) {
 
170
                    ListPropertyDescriptor lpd = (ListPropertyDescriptor) pd;
 
171
                    int valueCount = lpd.getCount(root);
 
172
                    for (int ii = 0; ii < valueCount; ii++) {
 
173
                        value = (GEObject) lpd.get(root, ii);
 
174
                    }
 
175
                }
 
176
            }
 
177
        }
 
178
    }
 
179
    
 
180
    public String getCullName() {
 
181
        return cullName;
 
182
    }
 
183
    
 
184
    public String toString(Object bean) {
 
185
        return bean.toString();
 
186
    }
 
187
    
 
188
    public Class getImplClass() {
 
189
        return implClass;
 
190
    }
 
191
    
 
192
    public void setImplClass(Class implClass) {
 
193
        this.implClass = implClass;
 
194
    }
 
195
}