001//
002// Name
003//  $RCSfile: ManagedObjectStreamClass.java,v $
004// 
005// Copyright
006//  Copyright 2011-2015 Cloud Software Group, Inc. ALL RIGHTS RESERVED.
007//  Cloud Software Group, Inc. Confidential Information
008//
009// History
010//  $Revision: 1.1.2.4.2.1 $ $Date: 2015/10/08 22:35:50 $
011//
012package com.kabira.platform;
013
014import java.io.ObjectStreamField;
015import java.io.ObjectOutput;
016import java.io.IOException;
017
018/**
019 * Class used to manage managed object streams.
020 */
021public final class ManagedObjectStreamClass
022{
023    ManagedObjectStreamClass(long offset)
024        throws IOException
025    {
026        m_td = TypeDescriptor.getDescriptor(offset);
027        loadStreamFields(m_td.m_fields.size());
028    } 
029
030    ManagedObjectStreamClass(int domainId, int typeId)
031        throws IOException
032    {
033        m_td = TypeDescriptor.getDescriptor(domainId, typeId);
034        loadStreamFields(m_td.m_fields.size());
035    } 
036
037    //
038    // load a subset of the stream fields, starting at the first field.
039    //
040    ManagedObjectStreamClass(int domainId, int typeId, int numFields)
041        throws IOException
042    {
043        m_td = TypeDescriptor.getDescriptor(domainId, typeId);
044        loadStreamFields(numFields);
045    } 
046
047    /**
048     * Get a field of this managed class by name.
049     * @param name Field name to get.
050     * @return ObjectStreamField instance or null if name not found.
051     */
052    public ObjectStreamField getField(String name)
053    {
054        for (int i = 0; i < m_streamFields.length; i++)
055        {
056            if (m_streamFields[i].getName().equals(name))
057            {
058                return m_streamFields[i];
059            }
060        }
061        return null;
062    }
063
064    /**
065     * Return an array of the fields of this managed class.
066     * @return Array of ObjectStreamField instances.
067     */
068    public ObjectStreamField[] getFields()
069    {
070        return m_streamFields;
071    }
072
073    /**
074     * Returns the name of the class described by this descriptor.
075     * @return Name of class.
076     */
077    public String getName() 
078    {
079        return m_td.m_className;
080    }
081
082    /**
083     * Return the serialVersionUID for this class.
084     * <p>
085     * If there was no serialVersionUID defined in
086     * the class a value of 0L is returned.
087     * @return Serial version id.
088     */
089    public long getSerialVersionUID()
090    {
091        return m_td.m_serialVersionUID;
092    }
093
094    //
095    // FIX THIS: If we want to support offsets, we need to subclass
096    // ObjectStreamField and implement setOffset().
097    //
098    private void loadStreamFields(int numFields) throws IOException
099    {
100        m_streamFields = new ObjectStreamField[numFields];
101
102        int idx = 0;
103
104        try
105        {
106            for (TypeDescriptor.FieldDescriptor fd : m_td.m_fields)
107            {
108                if (idx == numFields)
109                {
110                    break;
111                }
112
113                Class<?> cls = TypeDescriptor.forName(fd);
114                m_streamFields[idx] = new ObjectStreamField(
115                                            fd.m_name,
116                                            TypeDescriptor.forName(fd));
117                idx++;
118            }
119        }
120        catch (ClassNotFoundException ex)
121        {
122            throw new IOException(ex);
123        }
124    }
125
126    TypeDescriptor m_td;
127    ObjectStreamField [] m_streamFields;
128}