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 <cs@schulte.it>, 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 5061 2015-05-31 13:20:40Z schulte $
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.ModelObjectException;
41  import org.jomc.model.Property;
42  import org.jomc.model.PropertyException;
43  import org.jomc.util.WeakIdentityHashMap;
44  import static org.jomc.ri.model.RuntimeModelObjects.BOOTSTRAP_CLASSLOADER_KEY;
45  
46  // SECTION-START[Documentation]
47  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
48  /**
49   * Runtime {@code Property}.
50   *
51   * <dl>
52   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeProperty</dd>
53   *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeProperty</dd>
54   *   <dt><b>Specifications:</b></dt>
55   *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
56   *   <dt><b>Abstract:</b></dt><dd>No</dd>
57   *   <dt><b>Final:</b></dt><dd>No</dd>
58   *   <dt><b>Stateless:</b></dt><dd>No</dd>
59   * </dl>
60   *
61   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
62   * @version 1.2
63   */
64  // </editor-fold>
65  // SECTION-END
66  // SECTION-START[Annotations]
67  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
68  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
69  // </editor-fold>
70  // SECTION-END
71  public class RuntimeProperty extends Property implements RuntimeModelObject
72  {
73      // SECTION-START[RuntimeProperty]
74  
75      /**
76       * Java values by class loader cache.
77       */
78      @XmlTransient
79      private final Map<ClassLoader, Object> javaValuesByClassLoaderCache =
80          new WeakIdentityHashMap<ClassLoader, Object>();
81  
82      /**
83       * Creates a new {@code RuntimeProperty} instance by deeply copying a given {@code Property} instance.
84       *
85       * @param property The instance to copy.
86       *
87       * @throws NullPointerException if {@code property} is {@code null}.
88       */
89      public RuntimeProperty( final Property property )
90      {
91          super( property );
92  
93          if ( this.getAuthors() != null )
94          {
95              this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
96          }
97          if ( this.getDocumentation() != null )
98          {
99              this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
100         }
101     }
102 
103     /**
104      * Gets the Java value of the property.
105      * <p>
106      * This method queries an internal cache for a result object to return for the given argument values. If no
107      * cached result object is available, this method queries the super-class for a result object to return and caches
108      * the outcome of that query for use on successive calls.
109      * </p>
110      * <p>
111      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
112      * state of the instance, should the state of the instance change.
113      * </p>
114      *
115      * @param classLoader The class loader to use for getting the Java value; {@code null} to use the platform's
116      * bootstrap class loader.
117      *
118      * @return The Java value of the property or {@code null}.
119      *
120      * @throws ModelObjectException if parsing the name of the type of the property fails.
121      * @throws PropertyException if getting the Java value of the property fails unexpectedly.
122      *
123      * @see #clear()
124      */
125     @Override
126     public Object getJavaValue( final ClassLoader classLoader ) throws ModelObjectException, PropertyException
127     {
128         ClassLoader classLoaderKey = classLoader;
129         if ( classLoaderKey == null )
130         {
131             classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
132         }
133 
134         synchronized ( this.javaValuesByClassLoaderCache )
135         {
136             Object javaValue = this.javaValuesByClassLoaderCache.get( classLoaderKey );
137 
138             if ( javaValue == null )
139             {
140                 javaValue = super.getJavaValue( classLoader );
141 
142                 if ( javaValue != null )
143                 {
144                     this.javaValuesByClassLoaderCache.put( classLoaderKey, javaValue );
145                 }
146             }
147 
148             return javaValue;
149         }
150     }
151 
152     // SECTION-END
153     // SECTION-START[RuntimeModelObject]
154     public void gc()
155     {
156         synchronized ( this.javaValuesByClassLoaderCache )
157         {
158             this.javaValuesByClassLoaderCache.size();
159         }
160 
161         this.gcOrClear( true, false );
162     }
163 
164     public void clear()
165     {
166         synchronized ( this.javaValuesByClassLoaderCache )
167         {
168             this.javaValuesByClassLoaderCache.clear();
169         }
170 
171         this.gcOrClear( false, true );
172     }
173 
174     private void gcOrClear( final boolean gc, final boolean clear )
175     {
176         if ( this.getAuthors() instanceof RuntimeModelObject )
177         {
178             if ( gc )
179             {
180                 ( (RuntimeModelObject) this.getAuthors() ).gc();
181             }
182             if ( clear )
183             {
184                 ( (RuntimeModelObject) this.getAuthors() ).clear();
185             }
186         }
187         if ( this.getDocumentation() instanceof RuntimeModelObject )
188         {
189             if ( gc )
190             {
191                 ( (RuntimeModelObject) this.getDocumentation() ).gc();
192             }
193             if ( clear )
194             {
195                 ( (RuntimeModelObject) this.getDocumentation() ).clear();
196             }
197         }
198     }
199 
200     // SECTION-END
201     // SECTION-START[Constructors]
202     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
203     /** Creates a new {@code RuntimeProperty} instance. */
204     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
205     public RuntimeProperty()
206     {
207         // SECTION-START[Default Constructor]
208         super();
209         // SECTION-END
210     }
211     // </editor-fold>
212     // SECTION-END
213     // SECTION-START[Dependencies]
214     // SECTION-END
215     // SECTION-START[Properties]
216     // SECTION-END
217     // SECTION-START[Messages]
218     // SECTION-END
219 
220 }