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