1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.core.meta;
20
21 import java.util.*;
22
23 import org.caleigo.core.*;
24 import org.caleigo.toolkit.log.Log;
25
26
27 /***
28 *
29 * @author Mattias Hagstrand
30 * @version 1.0
31 */
32
33
34
35
36
37
38 class CustomDataSourceDescriptor extends AbstractDataSourceDescriptor
39 {
40
41 private static HashMap sRegisteredDescriptors = new HashMap();
42
43
44
45 /*** Returns a CustomDataSourceDescriptor with the given code name if it has
46 * been created, otherwise null.
47 *
48 * All CustomDataSourceDescriptor that are created are automatically registered.
49 * This mehtod has package scope since it is only used to handle serialization
50 * of custom descriptors.
51 */
52 static CustomDataSourceDescriptor getRegisteredCustomDataSourceDescriptor(String codeName)
53 {
54 return (CustomDataSourceDescriptor) sRegisteredDescriptors.get(codeName);
55 }
56
57
58 private Hashtable mEntityDescriptorCodeNames = new Hashtable();
59 private Hashtable mEntityDescriptorSourceNames = new Hashtable();
60
61
62 public CustomDataSourceDescriptor(String codeName, String sourceName, String displayName, String version, boolean readOnly)
63 {
64 super(codeName, sourceName, displayName, version, readOnly, null);
65 sRegisteredDescriptors.put(codeName, this);
66 }
67
68
69 public int getEntityDescriptorCount()
70 {
71 return mEntityDescriptorCodeNames.size();
72 }
73
74 public IEntityDescriptor getEntityDescriptor(int index)
75 {
76 Enumeration entityDescriptors = mEntityDescriptorCodeNames.elements();
77 for (int i = 0; i < index && entityDescriptors.hasMoreElements(); i++)
78 entityDescriptors.nextElement();
79
80 if (entityDescriptors.hasMoreElements())
81 return (IEntityDescriptor) entityDescriptors.nextElement();
82 else
83 return null;
84 }
85
86 public IEntityDescriptor getEntityDescriptor(String codeName)
87 {
88 return (IEntityDescriptor) mEntityDescriptorCodeNames.get(codeName);
89 }
90
91 protected Object writeReplace() throws java.io.ObjectStreamException
92 {
93 return new Dezerializer(this.getCodeName());
94 }
95
96 protected void finalize()
97 {
98 sRegisteredDescriptors.remove(this.getCodeName());
99 }
100
101
102 protected void addEntityDescriptor(IEntityDescriptor entityDescriptor)
103 {
104 mEntityDescriptorCodeNames.put(entityDescriptor.getCodeName(), entityDescriptor);
105 mEntityDescriptorSourceNames.put(entityDescriptor.getSourceName(), entityDescriptor);
106 }
107
108 protected IEntityDescriptor getEntityDescriptorBySourceName(String sourceName)
109 {
110 return (IEntityDescriptor) mEntityDescriptorSourceNames.get(sourceName);
111 }
112
113
114 protected static class Dezerializer implements java.io.Serializable
115 {
116
117 protected String mCodeName;
118
119
120 public Dezerializer(String codeName)
121 {
122 mCodeName = codeName;
123 }
124
125
126 protected Object readResolve() throws java.io.ObjectStreamException
127 {
128 if (sRegisteredDescriptors.containsKey(mCodeName))
129 return sRegisteredDescriptors.get(mCodeName);
130 else
131 {
132 Log.printError(this, "CustomDataSourceDescriptor with code name " + mCodeName + " not found");
133 throw new java.io.InvalidObjectException("CustomDataSourceDescriptor with code name " + mCodeName + " not found");
134 }
135 }
136 }
137 }