1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jdo.impl.model.java;
18
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.io.InputStream;
24
25 import org.apache.jdo.model.ModelException;
26 import org.apache.jdo.model.java.JavaModel;
27 import org.apache.jdo.model.java.JavaType;
28 import org.apache.jdo.model.jdo.JDOModel;
29
30
31 /***
32 * Abstract super class for JavaModel implementations.
33 * It implements the jdoModel property and the parent/child relationship
34 * between javaModels. It also provides a map of types managed by this
35 * JavaModel (see {@link #types}). The AbstractJavaModel constructor
36 * automatically adds all the predefined types to this map.
37 * <p>
38 * A non-abstract subclass must implement methods
39 * {@link #getJavaType(String name)} and
40 * {@link #getInputStreamForResource(String resourceName)}.
41 *
42 * @author Michael Bouschen
43 * @since JDO 1.0.1
44 */
45 abstract public class AbstractJavaModel
46 implements JavaModel
47 {
48 /*** Map of known JavaTypes. Key is the type name as a string. */
49 protected Map types;
50
51 /*** The parent JavaModel. */
52 protected JavaModel parent;
53
54 /*** The child JavaModels. */
55 protected Set children = new HashSet();
56
57 /*** The corresponding JDOModel instance. */
58 protected JDOModel jdoModel;
59
60 /***
61 * Constructor. It adds all predefined types to the cache of types
62 * known by this model instance.
63 * @see PredefinedType
64 */
65 protected AbstractJavaModel()
66 {
67 types = new HashMap(PredefinedType.getPredefinedTypes());
68 }
69
70 /***
71 * The method returns the JavaType instance for the specified type
72 * name. A type name is unique within one JavaModel instance. The
73 * method returns <code>null</code> if this model instance does not
74 * know a type with the specified name.
75 * @return a JavaType instance for the specified name or
76 * <code>null</code> if not present in this model instance.
77 */
78 abstract public JavaType getJavaType(String name);
79
80 /***
81 * The method returns the JavaType instance for the type name of the
82 * specified class object. This is a convenience method for
83 * <code>getJavaType(clazz.getName())</code>. The major difference
84 * between this method and getJavaType taking a type name is that this
85 * method is supposed to return a non-<code>null<code> value. The
86 * specified class object describes an existing type.
87 * @param clazz the Class instance representing the type
88 * @return a JavaType instance for the name of the specified class
89 * object.
90 */
91 public JavaType getJavaType(Class clazz)
92 {
93 return (clazz == null) ? null : getJavaType(clazz.getName());
94 }
95
96 /***
97 * Finds a resource with a given name. A resource is some data that can
98 * be accessed by class code in a way that is independent of the
99 * location of the code. The name of a resource is a "/"-separated path
100 * name that identifies the resource. The method method opens the
101 * resource for reading and returns an InputStream. It returns
102 * <code>null</code> if no resource with this name is found or if the
103 * caller doesn't have adequate privileges to get the resource.
104 * @param resourceName the resource name
105 * @return an input stream for reading the resource, or <code>null</code>
106 * if the resource could not be found or if the caller doesn't have
107 * adequate privileges to get the resource.
108 */
109 abstract public InputStream getInputStreamForResource(String resourceName);
110
111 /***
112 * Returns the parent JavaModel instance of this JavaModel.
113 * @return the parent JavaModel
114 */
115 public JavaModel getParent()
116 {
117 return parent;
118 }
119
120 /***
121 * Set the parent JavaModel for this JavaModel. The method
122 * automatically adds this JavaModel to the collection of children
123 * of the specified parent JavaModel.
124 * @param parent the parent JavaModel
125 * @exception ModelException if impossible
126 */
127 public void setParent(JavaModel parent)
128 throws ModelException
129 {
130 if (this.parent == parent) {
131
132 return;
133 }
134 if (this.parent != null) {
135
136 ((AbstractJavaModel)this.parent).children.remove(this);
137
138 ((AbstractJavaModel)parent).children.add(this);
139 }
140 this.parent = parent;
141 }
142
143 /***
144 * Returns a collection of child JavaModel instances in the form
145 * of an array. All instances from the returned array have this
146 * JavaModel instance as parent.
147 * @return the child JavaModel instances
148 */
149 public JavaModel[] getChildren()
150 {
151 return (JavaModel[])children.toArray(new JavaModel[children.size()]);
152 }
153
154 /***
155 * Returns the corresponding JDOModel instance.
156 * @return the corresponding JDOModel.
157 */
158 public JDOModel getJDOModel()
159 {
160 return jdoModel;
161 }
162
163 /***
164 * Sets the corresponding JDOModel instance.
165 * @param jdoModel the JDOModel instance
166 * @exception ModelException if impossible
167 */
168 public void setJDOModel(JDOModel jdoModel)
169 throws ModelException
170 {
171 this.jdoModel = jdoModel;
172 }
173
174 }
175