View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.mapping.loader;
36  
37  import java.util.HashMap;
38  import java.util.HashSet;
39  import java.util.Map;
40  import java.util.Set;
41  
42  import org.exolab.castor.mapping.ClassDescriptor;
43  import org.exolab.castor.mapping.FieldDescriptor;
44  import org.exolab.castor.mapping.xml.ClassMapping;
45  
46  /**
47   * The standard {@link ClassDescriptor} implementation, holding general OO information about the
48   * class <i>described</i>.
49   * <p/>
50   * 
51   * Engines will use {@link Nature}s to augment this class with engine-specific knowledge and
52   * functionality, using {@link #addNature(String)} to register these views with this class.
53   * <p/>
54   * 
55   * Once a Nature has been registered with this {@link ClassDescriptor}, the nature can be applied to
56   * the {@link ClassDescriptor} and nature-specific properties can be accessed in a type-safe way.
57   *
58   * @see Nature
59   * @see #addNature(String)
60   * @see #hasNature(String)
61   * 
62   * @author <a href="mailto:arkin AT intalio DOT com">Assaf Arkin</a>
63   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
64   * @author <a href="mailto:wguttmn AT codehaus DOT org">Werner Guttmann</a>
65   * @version $Revision$ $Date: 2006-01-07 15:48:31 -0700 (Sat, 07 Jan 2006) $
66   */
67  public class ClassDescriptorImpl implements ClassDescriptor {
68  
69    /**
70     * {@link ClassMapping} instance holding class mapping information required during initialisation
71     * of e.g. (JDO) ClassMolder.
72     */
73    private ClassMapping _mapping;
74  
75    /**
76     * The Java class for this descriptor.
77     */
78    private Class<?> _javaClass;
79  
80    /**
81     * The descriptor of the class which this class extends, or null if this is a top-level class.
82     */
83    private ClassDescriptor _extends;
84  
85    /**
86     * The {@link ClassDescriptor} of the class which this class depends upon.
87     */
88    private ClassDescriptor _depends;
89  
90    /**
91     * # The fields described for this class.
92     */
93    private FieldDescriptor[] _fields;
94  
95    /**
96     * Map holding the properties set and read by natures.
97     */
98    private Map<String, Object> _properties = new HashMap<String, Object>();
99  
100   /**
101    * Map holding the available natures.
102    */
103   private Set<String> _natures = new HashSet<String>();
104 
105   /**
106    * Identity {@link FieldDescriptor}s.
107    */
108   private FieldDescriptor[] _identities;
109 
110   /**
111    * Sets the {@link ClassMapping} instance.
112    * 
113    * @param mapping The {@link ClassMapping} instance to be used.
114    */
115   public void setMapping(final ClassMapping mapping) {
116     _mapping = mapping;
117   }
118 
119   /**
120    * Returns the {@link ClassMapping} instance used.
121    * 
122    * @return The {@link ClassMapping} instance used.
123    */
124   public ClassMapping getMapping() {
125     return _mapping;
126   }
127 
128   /**
129    * Sets the Java {@link Class} as described by this descriptor.
130    * 
131    * @param javaClass The Java {@link Class} instance as described by this descriptor.
132    */
133   public void setJavaClass(final Class<?> javaClass) {
134     _javaClass = javaClass;
135   }
136 
137   /**
138    * {@inheritDoc}
139    * 
140    * @see org.exolab.castor.mapping.ClassDescriptor#getJavaClass()
141    */
142   public Class<?> getJavaClass() {
143     return _javaClass;
144   }
145 
146   /**
147    * Sets the descriptor of the class which this class extends.
148    * 
149    * @param extend the descriptor of the class which this class extends.
150    */
151   public void setExtends(final ClassDescriptor extend) {
152     _extends = extend;
153   }
154 
155   /**
156    * {@inheritDoc}
157    * 
158    * @see org.exolab.castor.mapping.ClassDescriptor#getExtends()
159    */
160   public ClassDescriptor getExtends() {
161     return _extends;
162   }
163 
164   /**
165    * Sets the {@link ClassDescriptor} of the class which this class depends upon.
166    * 
167    * @param depends the {@link ClassDescriptor} of the class which this class depends upon
168    */
169   public void setDepends(final ClassDescriptor depends) {
170     _depends = depends;
171   }
172 
173   /**
174    * Returns the {@link ClassDescriptor} of the class which this class depends upon.
175    * 
176    * @return the {@link ClassDescriptor} of the class which this class depends upon.
177    */
178   public ClassDescriptor getDepends() {
179     return _depends;
180   }
181 
182   /**
183    * Sets the {@link FieldDescriptor}s that describe the fields defined for this class.
184    * 
185    * @param fields the {@link FieldDescriptor}s that describe the fields defined for this class.
186    */
187   public void setFields(final FieldDescriptor[] fields) {
188     _fields = fields;
189   }
190 
191   /**
192    * {@inheritDoc}
193    * 
194    * @see org.exolab.castor.mapping.ClassDescriptor#getFields()
195    */
196   public FieldDescriptor[] getFields() {
197     return _fields;
198   }
199 
200   /**
201    * {@inheritDoc}
202    * 
203    * @see java.lang.Object#toString()
204    */
205   public String toString() {
206     return _javaClass.getName() + '[' + _natures + ']';
207   }
208 
209   /**
210    * @see org.exolab.castor.builder.info.nature.PropertyHolder# getProperty(java.lang.String)
211    * @param name of the property
212    * @return value of the property
213    */
214   public Object getProperty(final String name) {
215     return _properties.get(name);
216   }
217 
218   /**
219    * @see org.exolab.castor.builder.info.nature.PropertyHolder# setProperty(java.lang.String,
220    *      java.lang.Object)
221    * @param name of the property
222    * @param value of the property
223    */
224   public void setProperty(final String name, final Object value) {
225     _properties.put(name, value);
226   }
227 
228   /**
229    * @see org.exolab.castor.builder.info.nature.NatureExtendable# addNature(java.lang.String)
230    * @param nature ID of the Nature
231    */
232   public void addNature(final String nature) {
233     _natures.add(nature);
234   }
235 
236   /**
237    * @see org.exolab.castor.builder.info.nature.NatureExtendable# hasNature(java.lang.String)
238    * @param nature ID of the Nature
239    * @return true if the Nature ID was added.
240    */
241   public boolean hasNature(final String nature) {
242     return _natures.contains(nature);
243 
244   }
245 
246   /**
247    * Sets the {@link FieldDescriptor}s that describe the identities as defined for this class.
248    * 
249    * @param identities the {@link FieldDescriptor}s that describe the identities as defined for this
250    *        class.
251    */
252   public void setIdentities(final FieldDescriptor[] identities) {
253     _identities = identities;
254   }
255 
256   /**
257    * Returns the {@link FieldDescriptor}s that describe the identities as defined for this class.
258    * 
259    * @return the {@link FieldDescriptor}s that describe the identities as defined for this class.
260    */
261   public FieldDescriptor[] getIdentities() {
262     return _identities;
263   }
264 
265   /**
266    * Returns the first {@link FieldDescriptor} instance.
267    * 
268    * @return the first {@link FieldDescriptor} instance
269    */
270   public FieldDescriptor getIdentity() {
271     FieldDescriptor[] identities = getIdentities();
272     if (identities == null) {
273       return null;
274     }
275     return identities[0];
276   }
277 
278 
279 }
280 
281