View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Java Object Management and Configuration
5    *   Copyright (C) Christian Schulte, 2011-313
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   *   $JOMC: RuntimeProperty.java 4588 2012-06-03 06:01:30Z schulte2005 $
32   *
33   */
34  // </editor-fold>
35  // SECTION-END
36  package org.jomc.ri.model;
37  
38  import java.util.Map;
39  import javax.xml.bind.annotation.XmlTransient;
40  import org.jomc.model.Property;
41  import org.jomc.model.PropertyException;
42  import org.jomc.util.WeakIdentityHashMap;
43  import static org.jomc.ri.model.RuntimeModelObjects.BOOTSTRAP_CLASSLOADER_KEY;
44  
45  // SECTION-START[Documentation]
46  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
47  /**
48   * Runtime {@code Property}.
49   *
50   * <dl>
51   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeProperty</dd>
52   *   <dt><b>Name:</b></dt><dd>JOMC RI RuntimeProperty</dd>
53   *   <dt><b>Specifications:</b></dt>
54   *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
55   *   <dt><b>Abstract:</b></dt><dd>No</dd>
56   *   <dt><b>Final:</b></dt><dd>No</dd>
57   *   <dt><b>Stateless:</b></dt><dd>No</dd>
58   * </dl>
59   *
60   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.2
61   * @version 1.2
62   */
63  // </editor-fold>
64  // SECTION-END
65  // SECTION-START[Annotations]
66  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
67  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
68  // </editor-fold>
69  // SECTION-END
70  public class RuntimeProperty extends Property implements RuntimeModelObject
71  {
72      // SECTION-START[RuntimeProperty]
73  
74      /** Java values by class loader cache.*/
75      @XmlTransient
76      private final Map<ClassLoader, Object> javaValuesByClassLoaderCache =
77          new WeakIdentityHashMap<ClassLoader, Object>();
78  
79      /**
80       * Creates a new {@code RuntimeProperty} instance by deeply copying a given {@code Property} instance.
81       *
82       * @param property The instance to copy.
83       *
84       * @throws NullPointerException if {@code property} is {@code null}.
85       */
86      public RuntimeProperty( final Property property )
87      {
88          super( property );
89  
90          if ( this.getAuthors() != null )
91          {
92              this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
93          }
94          if ( this.getDocumentation() != null )
95          {
96              this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
97          }
98      }
99  
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 }