001// SECTION-START[License Header]
002// <editor-fold defaultstate="collapsed" desc=" Generated License ">
003/*
004 *   Java Object Management and Configuration
005 *   Copyright (C) Christian Schulte, 2011-313
006 *   All rights reserved.
007 *
008 *   Redistribution and use in source and binary forms, with or without
009 *   modification, are permitted provided that the following conditions
010 *   are met:
011 *
012 *     o Redistributions of source code must retain the above copyright
013 *       notice, this list of conditions and the following disclaimer.
014 *
015 *     o Redistributions in binary form must reproduce the above copyright
016 *       notice, this list of conditions and the following disclaimer in
017 *       the documentation and/or other materials provided with the
018 *       distribution.
019 *
020 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 *
031 *   $JOMC: RuntimeSpecification.java 4712 2013-01-02 13:56:12Z schulte $
032 *
033 */
034// </editor-fold>
035// SECTION-END
036package org.jomc.ri.model;
037
038import java.lang.ref.Reference;
039import java.lang.ref.WeakReference;
040import java.util.Map;
041import javax.xml.bind.annotation.XmlTransient;
042import org.jomc.model.JavaTypeName;
043import org.jomc.model.ModelObjectException;
044import org.jomc.model.Specification;
045import static org.jomc.ri.model.RuntimeModelObjects.BOOTSTRAP_CLASSLOADER_KEY;
046import static org.jomc.ri.model.RuntimeModelObjects.classesByClassLoaderAndNameCache;
047import static org.jomc.ri.model.RuntimeModelObjects.createMap;
048
049// SECTION-START[Documentation]
050// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
051/**
052 * Runtime {@code Specification}.
053 *
054 * <dl>
055 *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeSpecification</dd>
056 *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeSpecification</dd>
057 *   <dt><b>Specifications:</b></dt>
058 *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
059 *   <dt><b>Abstract:</b></dt><dd>No</dd>
060 *   <dt><b>Final:</b></dt><dd>No</dd>
061 *   <dt><b>Stateless:</b></dt><dd>No</dd>
062 * </dl>
063 *
064 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
065 * @version 1.2
066 */
067// </editor-fold>
068// SECTION-END
069// SECTION-START[Annotations]
070// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
071@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
072// </editor-fold>
073// SECTION-END
074public class RuntimeSpecification extends Specification implements RuntimeModelObject
075{
076    // SECTION-START[RuntimeSpecification]
077
078    /** Java type name. */
079    @XmlTransient
080    private volatile JavaTypeName javaTypeName;
081
082    /**
083     * Creates a new {@code RuntimeSpecification} instance by deeply copying a given {@code Specification} instance.
084     *
085     * @param specification The instance to copy.
086     *
087     * @throws NullPointerException if {@code specification} is {@code null}.
088     */
089    public RuntimeSpecification( final Specification specification )
090    {
091        super( specification );
092
093        if ( this.getAuthors() != null )
094        {
095            this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
096        }
097        if ( this.getDocumentation() != null )
098        {
099            this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
100        }
101        if ( this.getProperties() != null )
102        {
103            this.setProperties( RuntimeModelObjects.getInstance().copyOf( this.getProperties() ) );
104        }
105    }
106
107    /**
108     * Gets the Java class of the specification for a given class loader.
109     * <p>This method queries an internal cache for a result object to return for the given argument values. If no
110     * cached result object is available, this method queries the super-class for a result object to return and caches
111     * the outcome of that query for use on successive calls.</p>
112     * <p><b>Note:</b><br/>Method {@code RuntimeModelObjects.clear()} must be used to synchronize the state of the
113     * internal cache with the state of the class loader, should the state of the class loader change.</p>
114     *
115     * @param classLoader The class loader to get the Java class from or {@code null}, to get the Java class from the
116     * platform's bootstrap class loader.
117     *
118     * @return The Java class of the specification or {@code null}, if the specification does not declare a class.
119     *
120     * @throws ClassNotFoundException if the Java class is not found.
121     * @throws ModelObjectException if parsing the name of the referenced type fails.
122     *
123     * @see #getClazz()
124     * @see RuntimeModelObjects#clear()
125     */
126    @Override
127    public Class<?> getJavaClass( final ClassLoader classLoader )
128        throws ModelObjectException, ClassNotFoundException
129    {
130        Class<?> javaClass = null;
131
132        if ( this.getJavaTypeName() != null )
133        {
134            synchronized ( classesByClassLoaderAndNameCache )
135            {
136                ClassLoader classLoaderKey = classLoader;
137                if ( classLoaderKey == null )
138                {
139                    classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
140                }
141
142                Map<String, Reference<Class<?>>> map = classesByClassLoaderAndNameCache.get( classLoaderKey );
143
144                if ( map == null )
145                {
146                    map = createMap();
147                    classesByClassLoaderAndNameCache.put( classLoaderKey, map );
148                }
149
150                final Reference<Class<?>> reference = map.get( this.getJavaTypeName().getClassName() );
151
152                if ( reference != null )
153                {
154                    javaClass = reference.get();
155                }
156
157                if ( javaClass == null )
158                {
159                    javaClass = super.getJavaClass( classLoader );
160                    map.put( this.getJavaTypeName().getClassName(), new WeakReference<Class<?>>( javaClass ) );
161                }
162            }
163        }
164
165        return javaClass;
166    }
167
168    /**
169     * Gets the Java type name of the type referenced by the specification.
170     * <p>This method queries an internal cache for a result object to return. If no cached result object is available,
171     * this method queries the super-class for a result object to return and caches the outcome of that query for use on
172     * successive calls.</p>
173     * <p><b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
174     * state of the instance, should the state of the instance change.</p>
175     *
176     * @return The Java type name of the type referenced by the specification or {@code null}, if the specification does
177     * not reference a type.
178     *
179     * @throws ModelObjectException if compiling the name of the referenced type to a {@code JavaTypeName} fails.
180     *
181     * @since 1.4
182     *
183     * @see #getJavaTypeName()
184     * @see #clear()
185     */
186    @Override
187    public JavaTypeName getJavaTypeName() throws ModelObjectException
188    {
189        if ( this.javaTypeName == null )
190        {
191            this.javaTypeName = super.getJavaTypeName();
192        }
193
194        return this.javaTypeName;
195    }
196
197    // SECTION-END
198    // SECTION-START[RuntimeModelObject]
199    public void gc()
200    {
201        this.gcOrClear( true, false );
202    }
203
204    public void clear()
205    {
206        this.javaTypeName = null;
207        this.gcOrClear( false, true );
208    }
209
210    private void gcOrClear( final boolean gc, final boolean clear )
211    {
212        if ( this.getAuthors() instanceof RuntimeModelObject )
213        {
214            if ( gc )
215            {
216                ( (RuntimeModelObject) this.getAuthors() ).gc();
217            }
218            if ( clear )
219            {
220                ( (RuntimeModelObject) this.getAuthors() ).clear();
221            }
222        }
223        if ( this.getDocumentation() instanceof RuntimeModelObject )
224        {
225            if ( gc )
226            {
227                ( (RuntimeModelObject) this.getDocumentation() ).gc();
228            }
229            if ( clear )
230            {
231                ( (RuntimeModelObject) this.getDocumentation() ).clear();
232            }
233        }
234        if ( this.getProperties() instanceof RuntimeModelObject )
235        {
236            if ( gc )
237            {
238                ( (RuntimeModelObject) this.getProperties() ).gc();
239            }
240            if ( clear )
241            {
242                ( (RuntimeModelObject) this.getProperties() ).clear();
243            }
244        }
245    }
246
247    // SECTION-END
248    // SECTION-START[Constructors]
249    // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
250    /** Creates a new {@code RuntimeSpecification} instance. */
251    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
252    public RuntimeSpecification()
253    {
254        // SECTION-START[Default Constructor]
255        super();
256        // SECTION-END
257    }
258    // </editor-fold>
259    // SECTION-END
260    // SECTION-START[Dependencies]
261    // SECTION-END
262    // SECTION-START[Properties]
263    // SECTION-END
264    // SECTION-START[Messages]
265    // SECTION-END
266}