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