| 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 4795 2013-04-21 09:09:02Z 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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" ) |
| 69 | // </editor-fold> |
| 70 | // SECTION-END |
| 71 | public class RuntimeProperty extends Property implements RuntimeModelObject |
| 72 | { |
| 73 | // SECTION-START[RuntimeProperty] |
| 74 | |
| 75 | /** Java values by class loader cache.*/ |
| 76 | @XmlTransient |
| 77 | private final Map<ClassLoader, Object> javaValuesByClassLoaderCache = |
| 78 | new WeakIdentityHashMap<ClassLoader, Object>(); |
| 79 | |
| 80 | /** |
| 81 | * Creates a new {@code RuntimeProperty} instance by deeply copying a given {@code Property} instance. |
| 82 | * |
| 83 | * @param property The instance to copy. |
| 84 | * |
| 85 | * @throws NullPointerException if {@code property} is {@code null}. |
| 86 | */ |
| 87 | public RuntimeProperty( final Property property ) |
| 88 | { |
| 89 | super( property ); |
| 90 | |
| 91 | if ( this.getAuthors() != null ) |
| 92 | { |
| 93 | this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) ); |
| 94 | } |
| 95 | if ( this.getDocumentation() != null ) |
| 96 | { |
| 97 | this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) ); |
| 98 | } |
| 99 | } |
| 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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" ) |
| 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 | } |