This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 21111
Collapse All | Expand All

(-)src/org/netbeans/mdr/NBMDRepositoryImpl.java (+5 lines)
Lines 276-283 Link Here
276
    private void boot() {
276
    private void boot() {
277
        Log.out.println( "Booting repository ..." );
277
        Log.out.println( "Booting repository ..." );
278
        Log.out.indent();
278
        Log.out.indent();
279
        mdrStorage.setBooting(true);
280
        try {
279
        installFakeMof();
281
        installFakeMof();
280
        installPureMof();   
282
        installPureMof();   
283
        } finally {
284
            mdrStorage.setBooting(false);
285
        }
281
        Log.out.unindent();
286
        Log.out.unindent();
282
    }    
287
    }    
283
288
(-)src/org/netbeans/mdr/handlers/EnumImpl.java (-40 / +202 lines)
Lines 16-24 Link Here
16
import java.lang.reflect.*;
16
import java.lang.reflect.*;
17
import java.io.*;
17
import java.io.*;
18
18
19
import javax.jmi.model.Classifier;
20
import javax.jmi.model.EnumerationType;
21
import javax.jmi.model.TypedElement;
19
import javax.jmi.reflect.*;
22
import javax.jmi.reflect.*;
20
23
21
import org.netbeans.mdr.util.*;
24
import org.netbeans.mdr.util.*;
25
import org.netbeans.mdr.persistence.StorageException;
22
import org.netbeans.mdr.storagemodel.*;
26
import org.netbeans.mdr.storagemodel.*;
23
27
24
/**
28
/**
Lines 27-37 Link Here
27
 * @version 
31
 * @version 
28
 */
32
 */
29
public abstract class EnumImpl extends ImplClass implements RefEnum {
33
public abstract class EnumImpl extends ImplClass implements RefEnum {
34
    private static final String ENUMIMPL_SUFFIX = "Enum";
35
    private static final Hashtable enumCache = new Hashtable(50);
36
30
    private final String label;
37
    private final String label;
31
    private final List qualifiedName;
38
    private final List qualifiedName;
32
    
39
    
33
    private static final Hashtable enumCache = new Hashtable(50);
34
35
    private static Class getEnumClass(ClassLoader loader, Class ifc) throws IllegalArgumentException {
40
    private static Class getEnumClass(ClassLoader loader, Class ifc) throws IllegalArgumentException {
36
        check(loader, ifc);
41
        check(loader, ifc);
37
        Map cache = getLoaderCache(loader);
42
        Map cache = getLoaderCache(loader);
Lines 40-47 Link Here
40
        
45
        
41
        if (result == null) {
46
        if (result == null) {
42
            try {
47
            try {
43
                byte[] enumClassFile = EnumGenerator.generateEnum(className, ifc);
48
                String rawName = ifc.getName() + ENUMIMPL_SUFFIX;
44
                result = defineClass(loader, className, enumClassFile);
49
                result = loader.loadClass(rawName);
50
            } catch (ClassNotFoundException ex) {
51
                throw new IllegalArgumentException("Invalid inteface: " + ifc);
45
            } finally {
52
            } finally {
46
                releaseCache(cache, result, className);
53
                releaseCache(cache, result, className);
47
            }
54
            }
Lines 49-64 Link Here
49
        
56
        
50
        return result;
57
        return result;
51
    }
58
    }
52
/*    
59
    
53
    public static RefEnum newInstance(Descriptor.DataType dataType, int value) {
54
        try {
55
            return newInstance(Class.forName(dataType.getIfcName()), value, (String) dataType.getMembers().get(value), dataType.getTypeName());
56
        } catch (ClassNotFoundException e) {
57
            e.printStackTrace();
58
            throw new DebugException(e.toString());
59
        }
60
    }
61
*/    
62
    public static RefEnum newInstance(DatatypeDescriptor dataType, String label) {
60
    public static RefEnum newInstance(DatatypeDescriptor dataType, String label) {
63
        try {
61
        try {
64
            return newInstance(BaseObjectHandler.resolveInterface(dataType.getIfcName()), label, dataType.getTypeName());
62
            return newInstance(BaseObjectHandler.resolveInterface(dataType.getIfcName()), label, dataType.getTypeName());
Lines 77-97 Link Here
77
            Class cl = getEnumClass(loader, ifc);
75
            Class cl = getEnumClass(loader, ifc);
78
76
79
            try {
77
            try {
80
                Constructor cons = cl.getConstructor(new Class[] {String.class, List.class});
78
                Field f = cl.getField(label.toUpperCase());
81
                enum = (RefEnum) cons.newInstance(new Object[] {label, qualifiedName});
79
                enum = (RefEnum) f.get(null);
82
                enumCache.put(enumKey, enum);
80
                enumCache.put(enumKey, enum);
83
            } catch (NoSuchMethodException e) {
81
            } catch (NoSuchFieldException e) {
84
                e.printStackTrace();
82
                e.printStackTrace();
85
                throw new DebugException(e.toString());
83
                throw new DebugException(e.toString());
86
            } catch (IllegalAccessException e) {
84
            } catch (IllegalAccessException e) {
87
                e.printStackTrace();
85
                e.printStackTrace();
88
                throw new DebugException(e.toString());
86
                throw new DebugException(e.toString());
89
            } catch (InstantiationException e) {
90
                e.printStackTrace();
91
                throw new DebugException(e.toString());
92
            } catch (InvocationTargetException e) {
93
                e.printStackTrace();
94
                throw new DebugException(e.toString());
95
            }
87
            }
96
        }
88
        }
97
        
89
        
Lines 131-159 Link Here
131
        return label.equals(other.toString());
123
        return label.equals(other.toString());
132
    }
124
    }
133
    
125
    
134
    public static RefEnum decode(InputStream inputStream, StorableBaseObject storable) throws IOException {
126
    /**
135
        if (storable == null) Thread.dumpStack();
127
     * Maps enumeration's interface name to an integer index within the package
136
        String className = (String) IOUtils.read(inputStream);
128
     */
137
        List qualifiedName = (List) IOUtils.read(inputStream);
129
    private static Map enumerationMap = new HashMap(17);
138
        String label = (String) IOUtils.read(inputStream);
139
        
130
        
140
        Class clazz;
131
    /**
132
     * The first-level index is the enumeration index within the package.
133
     * The second-level arrays are indexed by literal positions within
134
     * the enumeration.
135
     */
136
    private static List   enumValues = new ArrayList(15);
137
    
138
    /** 
139
     * Maps enumeration instances (labels) to integer indices
140
     */
141
    private static  Map identityToValueMap = new HashMap(23);
142
    
143
    /**
144
     * Another map of enum instances to integer values, this time
145
     * keyed by the enum instance itself, so equals() from JMI spec is
146
     * used.
147
     */
148
    private static  Map enumToValueMap = new HashMap(23);  
149
150
    /**
151
     * A little performance hack: we don't want allocate identity key every
152
     * time a value of an enum needs to be looked up. Access to this field must
153
     * be SYNCHRONIZED!
154
     */
155
    private static IdentityKey   sharedEnumKey;
156
    
157
    /**
158
     * Registers an enumeration type with the package. It only puts the
159
     * enumeration into a map and assings it an integer number to have fast
160
     * access to values later.
161
     * @param typeIndex index of typename in the MDR storage.
162
     */
163
    public static int addEnumerationType(String typeName) {
164
        int index;
165
        synchronized (enumerationMap) {
166
             index = enumerationMap.size();
167
168
            Integer old = (Integer)enumerationMap.put(typeName, new Integer(index));
169
            if (old != null) {
170
                enumerationMap.put(typeName, old);
171
                index = old.intValue();
172
            } else {
173
                enumValues.add(null);
174
            }
175
        }
176
        return index;
177
    }
178
179
    /**
180
     * Initializes an enumeration for the given typeIndex (= interface FQN).
181
     * If the enumeration was not seen yet, typedMetaMOFID is used to get 
182
     * the metaclass of the element, then the enumeration type. MOF reflective
183
     * APIs are used to compute the translation tables.
184
     *
185
     * @param typeIndex index of the interface typename, key to the cache.
186
     * @param typedMetaMOFID MOFID of a TypedElement, which uses the enumeration.
187
     * @return handle to the enumeration to be used as a quick reference.
188
     */
189
    public static int initEnumeration(String typeName, MdrStorage storage, 
190
        String typedMetaMOFID) {
191
        Integer ivalue = (Integer)enumerationMap.get(typeName);
192
        int index;
193
        
194
        if (ivalue == null) {
195
            index = addEnumerationType(typeName);
196
        } else {
197
            index = ivalue.intValue();
198
        }
199
        
200
        RefEnum[] vals;
201
        
202
        synchronized (enumValues) {
203
            vals = (RefEnum[])enumValues.get(index);
204
            if (vals != null)
205
                return index;
206
        }
141
        
207
        
142
        try {
208
        try {
143
            clazz = Class.forName(className);
209
            RefBaseObject te = BaseObjectHandler.getHandler(storage.getObject(typedMetaMOFID));
144
        } catch (ClassNotFoundException e) {
210
            Classifier c = ((TypedElement)te).getType();
145
            e.printStackTrace();
211
            
146
            throw new DebugException();
212
            // c can be BootMOF instance; in that case, it won't contain reliable information:
213
            String typeMOFID = c.refMofId();
214
            /*
215
            StorableObject sto = (StorableObject)storage.getObject(typeMOFID);
216
            StorableClass stoProxy = sto.getClassProxy();
217
            StorableClass.AttributeDescriptor desc = stoProxy.getAttrDesc(0);
218
            if (desc.getType() == Object.class) {
219
                // It IS BootMOF => Go away.
220
                return -1;
147
        }
221
        }
222
             */
223
            EnumerationType t = (EnumerationType)c;
148
        
224
        
149
        return newInstance(clazz, label, qualifiedName);
225
            // Try to acquire the Enum's implementation class
226
            Class enumImpl = BaseObjectHandler.resolveInterface(typeName + ENUMIMPL_SUFFIX);
227
            if (enumImpl == null) {
228
                throw new DebugException("Cannot get enum class for " + typeName);
229
            }
230
            List labels = t.getLabels();
231
            vals = new RefEnum[labels.size()];
232
            int i = 0;
233
            for (Iterator it = labels.iterator(); it.hasNext(); i++) {
234
                String lab = (String)it.next();
235
                java.lang.reflect.Field f = enumImpl.getField(lab.toUpperCase());
236
                RefEnum v = (RefEnum)f.get(null);
237
                vals[i] = v;
238
                Integer ival = new Integer(i + 1);
239
                identityToValueMap.put(new IdentityKey(v), ival);
240
                enumToValueMap.put(v, ival);
241
            }
242
        } catch (ClassNotFoundException ex) {
243
        } catch (NoSuchFieldException ex) {
244
        } catch (IllegalAccessException ex) {
245
        } catch (StorageException ex) {
246
        }
247
        if (sharedEnumKey == null) {
248
            sharedEnumKey = new IdentityKey();
249
        }
250
        synchronized (enumValues) {
251
            enumValues.set(index, vals);
252
        }
253
        return index;
150
    }
254
    }
151
    
255
    
152
    public void write(OutputStream outputStream, MdrStorage storage) throws IOException {
256
    /**
153
        if (storage == null) Thread.dumpStack();
257
     * Maps the passed enum instance to a one-based label code. Null instance
154
        String className = this.getClass().getName().substring(0, this.getClass().getName().length() - getClassNameSuffix().length());
258
     * will be always mapped to 0.
155
        IOUtils.write(outputStream, className);
259
     * @param enumValue a value of the enumerated type
156
        IOUtils.write(outputStream, qualifiedName);
260
     * @return encoded value.
157
        IOUtils.write(outputStream, label);
261
     */
262
    public static int encodeEnumInstance(RefEnum enumValue) {
263
        if (enumValue == null)
264
            return 0;
265
        synchronized (sharedEnumKey) {
266
            sharedEnumKey.setEnum(enumValue);
267
            Object o = identityToValueMap.get(sharedEnumKey);
268
            if (o == null) {
269
                o = enumToValueMap.get(enumValue);
270
                if (o == null) {
271
                    throw new IllegalArgumentException("Unknown enumeration label: " +
272
                        enumValue.getClass().getName() + ":" + enumValue.toString());
273
                }
274
            }
275
            return ((Integer)o).intValue();
276
        }
277
    }
278
    
279
    /**
280
     * Decodes a value of an enumerated type to a RefEnum instance.
281
     * The method requires that the enum was previously properly initialized
282
     * by calling {@link #initEnumeration}.
283
     * The method always maps value 0 to <code>null</code>
284
     * @param enumHandle handle returned by {@link #initEnumeration} call
285
     * @param value int code of the label.
286
     */
287
    public static RefEnum decodeEnumInstance(int enumHandle, int value) {
288
        if (value == 0)
289
            return null;
290
        return ((RefEnum[])enumValues.get(enumHandle))[value -1];
291
    }
292
    
293
    /**
294
     * This class is hack around ugly equals() mandated by the JMI spec.
295
     * If/after JMI spec changes to a less stupid formula, this class may
296
     * be removed and RefEnums may be put into the hashmap directly.
297
     */
298
    private static final class IdentityKey {
299
        private RefEnum     enumValue;
300
        private int         hashCode;
301
302
        IdentityKey() {}
303
        
304
        IdentityKey(RefEnum en) {
305
            setEnum(en);
306
        }
307
        
308
        void setEnum(RefEnum en) {
309
            this.enumValue = en;
310
            this.hashCode = en.hashCode();
311
        }
312
        
313
        public boolean equals(Object o) {
314
            return (o instanceof IdentityKey) && ((IdentityKey)o).enumValue == this.enumValue;
315
        }
316
        
317
        public int hashCode() {
318
            return hashCode;
319
        }
158
    }
320
    }
159
}
321
}
(-)src/org/netbeans/mdr/storagemodel/AttrImmutableList.java (+14 lines)
Lines 22-27 Link Here
22
import org.netbeans.mdr.util.IOUtils;
22
import org.netbeans.mdr.util.IOUtils;
23
23
24
/**
24
/**
25
 * [svata] This class is a candidate to be package private -- as soon as
26
 * referencies to it are removed from util.IOUtils class.
25
 *
27
 *
26
 * @author  mm109185
28
 * @author  mm109185
27
 */
29
 */
Lines 296-301 Link Here
296
    public void write(OutputStream stream) throws IOException {
298
    public void write(OutputStream stream) throws IOException {
297
        IOUtils.writeInt(stream, attrIndex);
299
        IOUtils.writeInt(stream, attrIndex);
298
        IOUtils.write(stream, innerList);
300
        IOUtils.write(stream, innerList);
301
    }
302
    
303
    final void read(InputStream in, StorableFeatured owner, StorableClass.AttributeDescriptor desc,
304
        int index) throws IOException, StorageException {
305
        this.mdrObject = owner;
306
        this.attrIndex = index;
307
        this.innerList = AttributeIOHelper.readList(in, owner, desc);
308
    }
309
    
310
    final void write(OutputStream out, StorableClass.AttributeDescriptor desc, StorableFeatured owner) 
311
        throws IOException {
312
        AttributeIOHelper.writeList(out, innerList, owner, desc);
299
    }
313
    }
300
    
314
    
301
    protected class AttrIterator implements ListIterator {
315
    protected class AttrIterator implements ListIterator {
(-)src/org/netbeans/mdr/storagemodel/MdrStorage.java (+17 lines)
Lines 90-95 Link Here
90
    /** name of storage file */
90
    /** name of storage file */
91
    private final String storageFile;
91
    private final String storageFile;
92
    
92
    
93
    /**
94
     * Determines whether the storage is ready or not.
95
     */
96
    private boolean booting;
97
    
93
    // mutexes
98
    // mutexes
94
    private final RWMutex repositoryMutex = new RWMutex();
99
    private final RWMutex repositoryMutex = new RWMutex();
95
    public RWMutex getRepositoryMutex() {
100
    public RWMutex getRepositoryMutex() {
Lines 208-213 Link Here
208
        instances.put(storage, this);
213
        instances.put(storage, this);
209
        
214
        
210
        return result;
215
        return result;
216
    }
217
    
218
    /**
219
     * Announce that this storage is being booted. Only the storage which holds
220
     * the MOF metamodel should be called with `true' as argument.
221
     */
222
    public void setBooting(boolean flag) {
223
        this.booting = flag;
224
    }
225
    
226
    public boolean isBooting() {
227
        return this.booting;
211
    }
228
    }
212
229
213
    /** Commits changes to the storage.
230
    /** Commits changes to the storage.
(-)src/org/netbeans/mdr/storagemodel/StorableClass.java (-12 / +70 lines)
Lines 15-20 Link Here
15
import java.util.*;
15
import java.util.*;
16
import java.io.IOException;
16
import java.io.IOException;
17
17
18
import javax.jmi.reflect.RefEnum;
19
18
import org.netbeans.mdr.util.*;
20
import org.netbeans.mdr.util.*;
19
import org.netbeans.mdr.persistence.*;
21
import org.netbeans.mdr.persistence.*;
20
import org.netbeans.mdr.handlers.*;
22
import org.netbeans.mdr.handlers.*;
Lines 26-32 Link Here
26
 * @version 0.2
28
 * @version 0.2
27
 */
29
 */
28
public class StorableClass extends StorableFeatured {
30
public class StorableClass extends StorableFeatured {
29
    
30
    private AttributeDescriptor attrDescs[];
31
    private AttributeDescriptor attrDescs[];
31
    private List superclasses;
32
    private List superclasses;
32
    private List subclasses;
33
    private List subclasses;
Lines 40-47 Link Here
40
41
41
    // transient variables
42
    // transient variables
42
    private final Hashtable refCache = new Hashtable();
43
    private final Hashtable refCache = new Hashtable();
43
    private List attributeDescs = null;
44
44
    private List attributes = null;
45
    /**
46
     * Names of all attributes of this classproxy's instances, collected from
47
     * the metaclass and its superclasses. Order of the names match exactly
48
     * order of attribute descriptors in the attributeDecs array and the
49
     * array of attr values in instances.
50
     */
51
    private List attributes;
52
53
    /**
54
     * Collected attribute descriptors from this classproxy's metaclass and
55
     * its superclasses. They're stored as array because of convenient fast access
56
     * by index.
57
     */
58
    private AttributeDescriptor[] attributeDescs;
45
    
59
    
46
    // mutexes
60
    // mutexes
47
    private final Object attrMutex = new Object();
61
    private final Object attrMutex = new Object();
Lines 57-63 Link Here
57
        for (int i = 0; i < attrDescs.length; i++) {
71
        for (int i = 0; i < attrDescs.length; i++) {
58
            attrDescs[i].replaceValues(table);
72
            attrDescs[i].replaceValues(table);
59
        }
73
        }
60
        
74
        AttributeIOHelper.finalizeClassProxy(this, this.attrDescs);
61
        objectChanged();
75
        objectChanged();
62
    }
76
    }
63
77
Lines 90-95 Link Here
90
        this.singletonClass = isSingleton;
104
        this.singletonClass = isSingleton;
91
        this.abstractClass = isAbstract;
105
        this.abstractClass = isAbstract;
92
        
106
        
107
        // asks the IOHelper to precompute some things into the attributes
108
        AttributeIOHelper.finalizeClassProxy(this, this.attrDescs);
109
        
93
        getMdrStorage().addObject(this);
110
        getMdrStorage().addObject(this);
94
        initFinished = true;
111
        initFinished = true;
95
    }
112
    }
Lines 147-153 Link Here
147
        objectChanged();
164
        objectChanged();
148
    }
165
    }
149
    
166
    
150
    public int getAttrIndex(String attributeName) throws StorageException {
167
    public final int getAttrIndex(String attributeName) throws StorageException {
151
        checkAttributes();
168
        checkAttributes();
152
        
169
        
153
        int result = attributes.indexOf(attributeName);
170
        int result = attributes.indexOf(attributeName);
Lines 170-183 Link Here
170
        return result;
187
        return result;
171
    }
188
    }
172
    
189
    
173
    public int getAttrCount() throws StorageException {
190
    public final int getAttrCount() throws StorageException {
174
        checkAttributes();
191
        checkAttributes();
175
        return attributeDescs.size();
192
        return attributeDescs.length;
176
    }
193
    }
177
    
194
    
178
    protected AttributeDescriptor getAttrDesc(int attrIndex) throws StorageException {
195
    protected final AttributeDescriptor getAttrDesc(int attrIndex) throws StorageException {
179
        checkAttributes();
196
        checkAttributes();
180
        return (AttributeDescriptor) attributeDescs.get(attrIndex);
197
        return attributeDescs[attrIndex];
198
    }
199
    
200
    final AttributeDescriptor[] getAttrDescriptors() throws StorageException {
201
        checkAttributes();
202
        return attributeDescs;
181
    }
203
    }
182
    
204
    
183
    public ReferenceDescriptor getReferenceDescriptor(String referenceName) throws StorageException {
205
    public ReferenceDescriptor getReferenceDescriptor(String referenceName) throws StorageException {
Lines 206-213 Link Here
206
                if (attributes == null) {
228
                if (attributes == null) {
207
                    //System.out.println("collecting attributes for: " + getMofId());
229
                    //System.out.println("collecting attributes for: " + getMofId());
208
                    attributes = new ArrayList();
230
                    attributes = new ArrayList();
209
                    attributeDescs = new ArrayList();
231
                    List attribDescs = new ArrayList();
210
                    collectAttributes(attributes, attributeDescs, new HashSet());
232
                    collectAttributes(attributes, attribDescs, new HashSet());
233
                    this.attributeDescs = (AttributeDescriptor[])attribDescs.toArray(
234
                        new AttributeDescriptor[attribDescs.size()]);
211
                }
235
                }
212
            }
236
            }
213
        }
237
        }
Lines 345-351 Link Here
345
            if (attrDescs == null) {
369
            if (attrDescs == null) {
346
                IOUtils.write(outputStream, null);
370
                IOUtils.write(outputStream, null);
347
            } else {
371
            } else {
348
                IOUtils.writeInt(outputStream, attrDescs.length);
372
                IOUtils.write(outputStream, new Integer(attrDescs.length));
349
                for (int i = 0; i < attrDescs.length; i++) {
373
                for (int i = 0; i < attrDescs.length; i++) {
350
                    IOUtils.write(outputStream, attrDescs[i], getMdrStorage());
374
                    IOUtils.write(outputStream, attrDescs[i], getMdrStorage());
351
                }
375
                }
Lines 410-415 Link Here
410
            
434
            
411
            classSuperclass = (String) IOUtils.read(inputStream);
435
            classSuperclass = (String) IOUtils.read(inputStream);
412
            instanceSuperclass = (String) IOUtils.read(inputStream);
436
            instanceSuperclass = (String) IOUtils.read(inputStream);
437
            AttributeIOHelper.readResolveHelper(this, attrDescs);
413
        } catch (IOException e) {
438
        } catch (IOException e) {
414
            e.printStackTrace();
439
            e.printStackTrace();
415
            throw new DebugException("IOException: " + e);
440
            throw new DebugException("IOException: " + e);
Lines 460-465 Link Here
460
        private String mofId;
485
        private String mofId;
461
        private final String name;
486
        private final String name;
462
        private final boolean indexed;
487
        private final boolean indexed;
488
        /**
489
         * Hint used by the marshalling code.
490
         */
491
        private int serialCode;
492
493
        /**
494
         * Runtime ID of the enumeration in the immediate package
495
         */
496
        private transient int enumerationID;
463
        
497
        
464
        private final transient MdrStorage mdrStorage;
498
        private final transient MdrStorage mdrStorage;
465
        private transient Class type = null;
499
        private transient Class type = null;
Lines 469-474 Link Here
469
            this.type = type;
503
            this.type = type;
470
        }
504
        }
471
        
505
        
506
        public AttributeDescriptor(MdrStorage storage, String mofId, String name, int typeIndex, 
507
            int minSize, int maxSize, boolean isUnique, boolean isChangeable, boolean isIndexed,
508
            int serialCode) {
509
            this(storage, mofId, name, typeIndex, minSize, maxSize, isUnique, isChangeable,
510
                isIndexed);
511
            this.serialCode = serialCode;
512
        }
513
        
472
        public AttributeDescriptor(MdrStorage storage, String mofId, String name, int typeIndex, int minSize, int maxSize, boolean isUnique, boolean isChangeable, boolean isIndexed) {
514
        public AttributeDescriptor(MdrStorage storage, String mofId, String name, int typeIndex, int minSize, int maxSize, boolean isUnique, boolean isChangeable, boolean isIndexed) {
473
            this.mdrStorage = storage;
515
            this.mdrStorage = storage;
474
            this.typeIndex = typeIndex;
516
            this.typeIndex = typeIndex;
Lines 496-501 Link Here
496
                }
538
                }
497
            }
539
            }
498
            return type;
540
            return type;
541
        }
542
        
543
        public void setEnumerationID(int id) {
544
            this.enumerationID = id;
545
        }
546
        
547
        public int getEnumerationID() {
548
            return this.enumerationID;
549
        }
550
        
551
        public void setSerialCode(int code) {
552
            this.serialCode = code;
553
        }
554
        
555
        public int getSerialCode() {
556
            return serialCode;
499
        }
557
        }
500
        
558
        
501
        public int getTypeIndex() {
559
        public int getTypeIndex() {
(-)src/org/netbeans/mdr/storagemodel/StorableFeatured.java (-16 / +6 lines)
Lines 180-193 Link Here
180
        super.write(outputStream);
180
        super.write(outputStream);
181
        
181
        
182
        try {
182
        try {
183
            if (values == null) {
183
            AttributeIOHelper.writeAttributeValues(outputStream, this, values);
184
                IOUtils.write(outputStream, null);
184
        } catch (StorageException ex) {
185
            } else {
185
            ex.printStackTrace();
186
                IOUtils.writeInt(outputStream, values.length);
186
            throw new DebugException();
187
                for (int i = 0; i < values.length; i++) {
188
                    IOUtils.write(outputStream, values[i], getMdrStorage());
189
                }
190
            }
191
        } catch (java.io.IOException e) {
187
        } catch (java.io.IOException e) {
192
            e.printStackTrace();
188
            e.printStackTrace();
193
            throw new DebugException();
189
            throw new DebugException();
Lines 201-214 Link Here
201
        super.read (inputStream);
197
        super.read (inputStream);
202
        
198
        
203
        try {
199
        try {
204
            Integer objCount = (Integer) IOUtils.read(inputStream);
200
            values = AttributeIOHelper.readAttributeValues(inputStream,
205
            if (objCount != null) {
201
                getClassProxy());
206
                int count = objCount.intValue();
207
                values = new Object[count];
208
                for (int i = 0; i < count; i++) {
209
                    values[i] = IOUtils.read(inputStream, this, getClassProxy().getAttrDesc(i).getType().getName());
210
                }
211
            }
212
        } catch (StorageException e) {
202
        } catch (StorageException e) {
213
            e.printStackTrace();
203
            e.printStackTrace();
214
            throw new DebugException("IOException: " + e);
204
            throw new DebugException("IOException: " + e);
(-)src/org/netbeans/mdr/storagemodel/StorableObject.java (-4 / +4 lines)
Lines 197-204 Link Here
197
     */    
197
     */    
198
    public void write(java.io.OutputStream outputStream) {
198
    public void write(java.io.OutputStream outputStream) {
199
        try {
199
        try {
200
            IOUtils.write(outputStream, classProxyId);
200
            IOUtils.writeString(outputStream, classProxyId);
201
            IOUtils.write(outputStream, attribComposite);
201
            IOUtils.writeString(outputStream, attribComposite);
202
        } catch (java.io.IOException e) {
202
        } catch (java.io.IOException e) {
203
            e.printStackTrace();
203
            e.printStackTrace();
204
        }
204
        }
Lines 210-217 Link Here
210
     */    
210
     */    
211
    public void read(java.io.InputStream inputStream) {
211
    public void read(java.io.InputStream inputStream) {
212
        try {
212
        try {
213
            classProxyId = (String) IOUtils.read(inputStream);
213
            classProxyId = IOUtils.readString(inputStream);
214
            attribComposite = (String) IOUtils.read(inputStream);
214
            attribComposite = IOUtils.readString(inputStream);
215
            // init value of "meta" (not serialized in case of StorableObject)            
215
            // init value of "meta" (not serialized in case of StorableObject)            
216
        } catch (java.io.IOException e) {
216
        } catch (java.io.IOException e) {
217
            e.printStackTrace(Log.out);
217
            e.printStackTrace(Log.out);
(-)src/org/netbeans/mdr/util/IOUtils.java (-33 / +105 lines)
Lines 49-54 Link Here
49
    private static final int T_SHORT = 15;
49
    private static final int T_SHORT = 15;
50
    private static final int T_BYTE = 16;
50
    private static final int T_BYTE = 16;
51
    private static final int T_OBJECT = 17;
51
    private static final int T_OBJECT = 17;
52
    private static final int T_LONG = 18;
52
    
53
    
53
    private static final String ENUMIMPL_SUFFIX = "Enum";
54
    private static final String ENUMIMPL_SUFFIX = "Enum";
54
    
55
    
Lines 60-97 Link Here
60
        write(outputStream, object, null);
61
        write(outputStream, object, null);
61
    }
62
    }
62
    
63
    
64
    /**
65
     * Writes an integer to the output stream. It tries to optimize for space a little:
66
     * small values are stored just as one byte. Values <= 0xffff are stored as
67
     * 3 bytes, little-endian and other values as 5 bytes, little-endian
68
     */
63
    public static void writeInt(OutputStream outputStream, int val) throws IOException {
69
    public static void writeInt(OutputStream outputStream, int val) throws IOException {
64
        if ((int) ((byte) val & 0xFF) == val) {
70
        if ((int) ((byte) val & 0x7F) == val) {
65
            outputStream.write(T_BYTE);
66
            outputStream.write((byte)val);
71
            outputStream.write((byte)val);
67
        } else if ((int) ((byte) (val >> 8) & 0xFF) == val >> 8) {
72
        } else if ((int) ((byte) (val >>> 8) & 0xFF) == val >>> 8) {
68
            outputStream.write(T_SHORT);
73
            outputStream.write(T_SHORT | 0x80);
69
            outputStream.write((byte)(val & 0xff));
74
            outputStream.write((byte)(val & 0xff));
70
            outputStream.write((byte)((val & 0xff00) >> 8));
75
            outputStream.write((byte)((val & 0xff00) >>> 8));
71
        } else {
76
        } else {
72
            outputStream.write(T_INTEGER);
77
            outputStream.write(T_INTEGER | 0x80);
73
            outputStream.write((byte)(val & 0xff));
78
            outputStream.write((byte)(val & 0xff));
74
            outputStream.write((byte)((val & 0xff00) >> 8));
79
            outputStream.write((byte)((val & 0xff00) >>> 8));
75
            outputStream.write((byte)((val & 0xff0000) >> 16));
80
            outputStream.write((byte)((val & 0xff0000) >>> 16));
76
            val = (val >> 24) & 0xff;
81
            val = (val >> 24) & 0xff;
77
            outputStream.write((byte)val);
82
            outputStream.write((byte)val);
78
        }
83
        }
79
    }
84
    }
80
    
85
    
86
    public static void writeLong(OutputStream outputStream, long val) throws IOException {
87
        if ((int) ((byte) val & 0x7F) == val) {
88
            outputStream.write((byte)val);
89
        } else if ((int) ((byte) (val >>> 24) & 0xFF) == val >>> 24) {
90
            outputStream.write(T_INTEGER | 0x80);
91
            outputStream.write((byte)(val & 0xff));
92
            outputStream.write((byte)((val & 0xff00) >>> 8));
93
            outputStream.write((byte)((val & 0xff0000) >>> 16));
94
            outputStream.write((byte)((val >>> 24) & 0xff));
95
        } else {
96
            outputStream.write(T_LONG | 0x80);
97
            outputStream.write((byte)(val & 0xff));
98
            outputStream.write((byte)((val & 0xff00) >>> 8));
99
            outputStream.write((byte)((val >>> 16) & 0xff));
100
            outputStream.write((byte)((val >>> 24) & 0xff));
101
            outputStream.write((byte)((val >>> 32) & 0xff));
102
            outputStream.write((byte)((val >>> 48) & 0xff));
103
            outputStream.write((byte)((val >>> 56) & 0xff));
104
        }
105
    }
106
    
81
    public static void writeBoolean(OutputStream outputStream, boolean val) throws IOException {
107
    public static void writeBoolean(OutputStream outputStream, boolean val) throws IOException {
82
        outputStream.write(T_BOOLEAN);
83
        outputStream.write(val ? 1 : 0);
108
        outputStream.write(val ? 1 : 0);
84
    }
109
    }
85
    
110
    
86
    public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException {
111
    public static void writeString(OutputStream outputStream, String val) throws IOException {
87
        if (object == null) {
112
        if (val == null) {
88
            outputStream.write(T_NULL);
113
            writeInt(outputStream, 0);
89
114
            return;
90
        } else if (object instanceof String) {
115
        }
91
            String val = (String) object;
92
            outputStream.write(T_STRING);
93
            int len = val.length();
116
            int len = val.length();
94
            writeInt(outputStream, len);
117
        writeInt(outputStream, len + 1);
95
            for (int i = 0; i < len; i++) {
118
            for (int i = 0; i < len; i++) {
96
                char ch = val.charAt(i);
119
                char ch = val.charAt(i);
97
                if (ch <= 0x7f) {
120
                if (ch <= 0x7f) {
Lines 105-114 Link Here
105
                    outputStream.write((byte) (0x80 | (ch & 0x3f)));
128
                    outputStream.write((byte) (0x80 | (ch & 0x3f)));
106
                }
129
                }
107
            }
130
            }
131
    }
132
    
133
    public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException {
134
        if (object == null) {
135
            outputStream.write(T_NULL);
136
137
        } else if (object instanceof String) {
138
            outputStream.write(T_STRING);
139
            writeString(outputStream, (String)object);
108
        } else if (object instanceof Integer) {
140
        } else if (object instanceof Integer) {
109
            int val = ((Integer)object).intValue();
141
            int val = ((Integer)object).intValue();
142
            outputStream.write(T_INTEGER);
110
            writeInt(outputStream, val);
143
            writeInt(outputStream, val);
111
        } else if (object instanceof Boolean) {
144
        } else if (object instanceof Boolean) {
145
            outputStream.write(T_BOOLEAN);
112
            writeBoolean(outputStream, ((Boolean) object).booleanValue());
146
            writeBoolean(outputStream, ((Boolean) object).booleanValue());
113
147
114
        } else if (object instanceof Map) {
148
        } else if (object instanceof Map) {
Lines 160-166 Link Here
160
            writeBoolean(outputStream, desc.isUnique());
194
            writeBoolean(outputStream, desc.isUnique());
161
            writeBoolean(outputStream, desc.isChangeable());
195
            writeBoolean(outputStream, desc.isChangeable());
162
            writeBoolean(outputStream, desc.isIndexed());
196
            writeBoolean(outputStream, desc.isIndexed());
163
            
197
            writeInt(outputStream, desc.getSerialCode());
164
        } else if (object instanceof StorableClass.ReferenceDescriptor) {
198
        } else if (object instanceof StorableClass.ReferenceDescriptor) {
165
            outputStream.write(T_MOF_REFERENCE);
199
            outputStream.write(T_MOF_REFERENCE);
166
            write(outputStream, ((StorableClass.ReferenceDescriptor) object).getAssociationId());
200
            write(outputStream, ((StorableClass.ReferenceDescriptor) object).getAssociationId());
Lines 193-199 Link Here
193
    }
227
    }
194
    
228
    
195
    public static boolean readBoolean(InputStream is) throws IOException {
229
    public static boolean readBoolean(InputStream is) throws IOException {
196
        if (is.read() != T_BOOLEAN) throw new DebugException("Not a boolean value.");
197
        return (is.read() == 1);
230
        return (is.read() == 1);
198
    }
231
    }
199
    
232
    
Lines 201-218 Link Here
201
        return readInt(is, is.read());
234
        return readInt(is, is.read());
202
    }
235
    }
203
    
236
    
204
    public static int readInt(InputStream inputStream, int type) throws IOException {
237
    static int readInt(InputStream inputStream, int type) throws IOException {
238
        if (type <= 0x7f)
239
            return type;
205
        switch (type) {
240
        switch (type) {
206
            case T_BYTE: {
241
            case T_SHORT | 0x80: {
207
                return inputStream.read();
208
            }
209
            case T_SHORT: {
210
                int ch1, ch2;
242
                int ch1, ch2;
211
                ch1 = inputStream.read();
243
                ch1 = inputStream.read();
212
                ch2 = inputStream.read();
244
                ch2 = inputStream.read();
213
                return (ch2 << 8) | ch1;
245
                return (ch2 << 8) | ch1;
214
            }
246
            }
215
            case T_INTEGER: {
247
            case T_INTEGER | 0x80: {
216
                int ch1, ch2, ch3, ch4;
248
                int ch1, ch2, ch3, ch4;
217
                ch1 = inputStream.read();
249
                ch1 = inputStream.read();
218
                ch2 = inputStream.read();
250
                ch2 = inputStream.read();
Lines 225-238 Link Here
225
        }
257
        }
226
    }
258
    }
227
    
259
    
228
    public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException {
260
    public static long readLong(InputStream inputStream) throws IOException {
229
        int type;
261
        int t = inputStream.read();
230
        switch (type = inputStream.read()) {
262
        if (t <= 0x7f)
231
            case T_NULL: {
263
            return t;
232
                return null;
264
        switch (t) {
233
            } case T_STRING: {
265
            case T_INTEGER | 0x80: {
266
                int ch1, ch2, ch3, ch4;
267
                ch1 = inputStream.read();
268
                ch2 = inputStream.read();
269
                ch3 = inputStream.read();
270
                ch4 = inputStream.read();
271
                return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1;
272
            }
273
            case T_LONG | 0x80: {
274
                int ch1, ch2, ch3, ch4;
275
                long v;
276
                
277
                ch1 = inputStream.read();
278
                ch2 = inputStream.read();
279
                ch3 = inputStream.read();
280
                ch4 = inputStream.read();
281
                v = (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1;
282
                ch1 = inputStream.read();
283
                ch2 = inputStream.read();
284
                ch3 = inputStream.read();
285
                ch4 = inputStream.read();
286
                return v << 32 + ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1);
287
            }
288
            default:
289
                throw new IOException("Unknown int format: " + t);
290
        }
291
    }
292
    
293
    public static String readString(InputStream inputStream) throws IOException {
234
                int length = readInt(inputStream);
294
                int length = readInt(inputStream);
235
                if (length == 0)
295
                if (length == 0)
296
            return null;
297
        else if (--length == 0)
236
                    return "";
298
                    return "";
237
                StringBuffer sb = new StringBuffer(length);
299
                StringBuffer sb = new StringBuffer(length);
238
                do {
300
                do {
Lines 249-258 Link Here
249
                } while (--length > 0);
311
                } while (--length > 0);
250
                return new String(sb);
312
                return new String(sb);
251
            }
313
            }
314
    
315
    public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException {
316
        int type;
317
        switch (type = inputStream.read()) {
318
            case T_NULL:
319
                return null;
320
            case T_STRING:
321
                return readString(inputStream);
252
            case T_BYTE: 
322
            case T_BYTE: 
253
            case T_SHORT:
323
            case T_SHORT:
254
            case T_INTEGER:
324
            case T_INTEGER:
255
                return new Integer(readInt(inputStream, type));
325
                return new Integer(readInt(inputStream));
326
            case T_LONG:
327
                return new Long(readLong(inputStream));
256
            case T_BOOLEAN: {
328
            case T_BOOLEAN: {
257
                return new Boolean(inputStream.read() == 1);
329
                return new Boolean(inputStream.read() == 1);
258
            } case T_MAP: {
330
            } case T_MAP: {
Lines 296-302 Link Here
296
                }
368
                }
297
            } case T_MOF_ATTRIBUTE: {
369
            } case T_MOF_ATTRIBUTE: {
298
                if (storable == null) Thread.dumpStack();
370
                if (storable == null) Thread.dumpStack();
299
                return new StorableClass.AttributeDescriptor(storable.getMdrStorage(), (String) read(inputStream), (String) read(inputStream), readInt(inputStream), readInt(inputStream), readInt(inputStream), readBoolean(inputStream), readBoolean(inputStream), readBoolean(inputStream));
371
                return new StorableClass.AttributeDescriptor(storable.getMdrStorage(), (String) read(inputStream), (String) read(inputStream), readInt(inputStream), readInt(inputStream), readInt(inputStream), readBoolean(inputStream), readBoolean(inputStream), readBoolean(inputStream), readInt(inputStream));
300
            } case T_MOF_REFERENCE: {
372
            } case T_MOF_REFERENCE: {
301
                if (storable == null) Thread.dumpStack();
373
                if (storable == null) Thread.dumpStack();
302
                return ((StorableClass) storable).new ReferenceDescriptor((String) read(inputStream, storable), (String) read(inputStream, storable));
374
                return ((StorableClass) storable).new ReferenceDescriptor((String) read(inputStream, storable), (String) read(inputStream, storable));

Return to bug 21111