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: RuntimeProperty.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.util.Map;
039import javax.xml.bind.annotation.XmlTransient;
040import org.jomc.model.ModelObjectException;
041import org.jomc.model.Property;
042import org.jomc.model.PropertyException;
043import org.jomc.util.WeakIdentityHashMap;
044import static org.jomc.ri.model.RuntimeModelObjects.BOOTSTRAP_CLASSLOADER_KEY;
045
046// SECTION-START[Documentation]
047// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
048/**
049 * Runtime {@code Property}.
050 *
051 * <dl>
052 *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeProperty</dd>
053 *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeProperty</dd>
054 *   <dt><b>Specifications:</b></dt>
055 *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
056 *   <dt><b>Abstract:</b></dt><dd>No</dd>
057 *   <dt><b>Final:</b></dt><dd>No</dd>
058 *   <dt><b>Stateless:</b></dt><dd>No</dd>
059 * </dl>
060 *
061 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
062 * @version 1.2
063 */
064// </editor-fold>
065// SECTION-END
066// SECTION-START[Annotations]
067// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
068@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
069// </editor-fold>
070// SECTION-END
071public class RuntimeProperty extends Property implements RuntimeModelObject
072{
073    // SECTION-START[RuntimeProperty]
074
075    /** Java values by class loader cache.*/
076    @XmlTransient
077    private final Map<ClassLoader, Object> javaValuesByClassLoaderCache =
078        new WeakIdentityHashMap<ClassLoader, Object>();
079
080    /**
081     * Creates a new {@code RuntimeProperty} instance by deeply copying a given {@code Property} instance.
082     *
083     * @param property The instance to copy.
084     *
085     * @throws NullPointerException if {@code property} is {@code null}.
086     */
087    public RuntimeProperty( final Property property )
088    {
089        super( property );
090
091        if ( this.getAuthors() != null )
092        {
093            this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
094        }
095        if ( this.getDocumentation() != null )
096        {
097            this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
098        }
099    }
100
101    /**
102     * Gets the Java value of the property.
103     * <p>This method queries an internal cache for a result object to return for the given argument values. If no
104     * cached result object is available, this method queries the super-class for a result object to return and caches
105     * the outcome of that query for use on successive calls.</p>
106     * <p><b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
107     * state of the instance, should the state of the instance change.</p>
108     *
109     * @param classLoader The class loader to use for getting the Java value; {@code null} to use the platform's
110     * bootstrap class loader.
111     *
112     * @return The Java value of the property or {@code null}.
113     *
114     * @throws ModelObjectException if parsing the name of the type of the property fails.
115     * @throws PropertyException if getting the Java value of the property fails unexpectedly.
116     *
117     * @see #clear()
118     */
119    @Override
120    public Object getJavaValue( final ClassLoader classLoader ) throws ModelObjectException, PropertyException
121    {
122        ClassLoader classLoaderKey = classLoader;
123        if ( classLoaderKey == null )
124        {
125            classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
126        }
127
128        synchronized ( this.javaValuesByClassLoaderCache )
129        {
130            Object javaValue = this.javaValuesByClassLoaderCache.get( classLoaderKey );
131
132            if ( javaValue == null )
133            {
134                javaValue = super.getJavaValue( classLoader );
135
136                if ( javaValue != null )
137                {
138                    this.javaValuesByClassLoaderCache.put( classLoaderKey, javaValue );
139                }
140            }
141
142            return javaValue;
143        }
144    }
145
146    // SECTION-END
147    // SECTION-START[RuntimeModelObject]
148    public void gc()
149    {
150        synchronized ( this.javaValuesByClassLoaderCache )
151        {
152            this.javaValuesByClassLoaderCache.size();
153        }
154
155        this.gcOrClear( true, false );
156    }
157
158    public void clear()
159    {
160        synchronized ( this.javaValuesByClassLoaderCache )
161        {
162            this.javaValuesByClassLoaderCache.clear();
163        }
164
165        this.gcOrClear( false, true );
166    }
167
168    private void gcOrClear( final boolean gc, final boolean clear )
169    {
170        if ( this.getAuthors() instanceof RuntimeModelObject )
171        {
172            if ( gc )
173            {
174                ( (RuntimeModelObject) this.getAuthors() ).gc();
175            }
176            if ( clear )
177            {
178                ( (RuntimeModelObject) this.getAuthors() ).clear();
179            }
180        }
181        if ( this.getDocumentation() instanceof RuntimeModelObject )
182        {
183            if ( gc )
184            {
185                ( (RuntimeModelObject) this.getDocumentation() ).gc();
186            }
187            if ( clear )
188            {
189                ( (RuntimeModelObject) this.getDocumentation() ).clear();
190            }
191        }
192    }
193
194    // SECTION-END
195    // SECTION-START[Constructors]
196    // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
197    /** Creates a new {@code RuntimeProperty} instance. */
198    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
199    public RuntimeProperty()
200    {
201        // SECTION-START[Default Constructor]
202        super();
203        // SECTION-END
204    }
205    // </editor-fold>
206    // SECTION-END
207    // SECTION-START[Dependencies]
208    // SECTION-END
209    // SECTION-START[Properties]
210    // SECTION-END
211    // SECTION-START[Messages]
212    // SECTION-END
213}