1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
* @(#)AutoDoc.java
*
* Copyright (C) 2002-2003 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Part of the GroboUtils package at:
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.autodoc.v1;
import net.sourceforge.groboutils.util.classes.v1.SPISingletonStore;
import net.sourceforge.groboutils.autodoc.v1.defimpl.DefaultAutoDocFactory;
import net.sourceforge.groboutils.autodoc.v1.defimpl.AutoDocTPSet;
import net.sourceforge.groboutils.autodoc.v1.defimpl.AutoDocLogSet;
import net.sourceforge.groboutils.autodoc.v1.defimpl.AutoDocITSet;
import net.sourceforge.groboutils.autodoc.v1.spi.AutoDocFactory;
import java.util.Vector;
/**
* Entry class for loading the AutoDoc pieces for a particular class. As
* this class is always instantiated, it does not need an interface. This
* acts as a central factory for generating all the AutoDoc instances.
* <P>
* This default implementation uses a static factory to load each instance's
* data. That factory may be set by a using class. Since initialization of
* the inner data is lazy-loaded (only loaded when needed, but when needed it
* is all loaded), the factory may be set at any time after construction, and
* before a data access member function is called. If no factory is ever
* specified, it will load the factory from the fully-qualified class name
* in the value of the System property specified by the key given by
* <tt>FACTORY_PROPERTY_NAME</tt>.
* <P>
* This class follows a combination of the Abstract Factory and Proxy patterns.
* AutoDoc acts as a Proxy for an Abstract Factory, allowing the particular
* implemented factory to be hidden from the user. However, a "hole" is open
* to each instance with the method <tt>setFactory()</tt>, allowing the owner
* to setup the factory to their needs.
* <P>
* NOTE: need to update the documentation. I've since replaced the single
* factory with an SPI fleet. The proxied objects are delegators to collections
* of sub-proxies, which are loaded as late as possible.
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @version $Date: 2003/02/10 22:52:11 $
* @since March 16, 2002
*/
public class AutoDoc
{
private static final SPISingletonStore s_factoryStore =
new SPISingletonStore( AutoDocFactory.class,
SPISingletonStore.NO_MULTIPLES_SILENT );
private volatile boolean notLoaded = true;
private Vector factories = null;
private final Class owner;
private AutoDocLogSet log = new AutoDocLogSet();
private AutoDocITSet it = new AutoDocITSet();
private AutoDocTPSet tp = new AutoDocTPSet();
/**
* Base constructor. Pass in the Class instance which will AutoDoc itself.
*
* @param c the class which will be AutoDoced.
* @exception IllegalArgumentException if the passed-in class is
* <tt>null</tt>.
*/
public AutoDoc( Class c )
{
if (c == null)
{
throw new IllegalArgumentException("no null arguments");
}
this.owner = c;
// lazy load data.
}
/**
* Get the log implementation. Each call will return the same object.
*
* @return the log for the owning class.
*/
public AutoDocLog getLog()
{
checkLoad();
return this.log;
}
/**
* Get the Issue Tracker implementation. Each call will return the
* same object.
*
* @return the Issue Tracker for the owning class.
*/
public AutoDocIT getIT()
{
checkLoad();
return this.it;
}
/**
* Get the Test Procedure Marker implementation. Each call will return
* the same object.
*/
public AutoDocTP getTP()
{
checkLoad();
return this.tp;
}
//-------------------------------------------------------------------------
// Protected methods
/**
* Check if the data instances have been loaded. If they have not, then
* load the instances, and ensure initialization is not performed again.
* This lazy-loading is thread-safe.
*/
protected final void checkLoad()
{
if (this.notLoaded)
{
// double-check locking pattern.
// since we're doing this on an atomicly volitile variable,
// this is fine. If we did this on a non-atomic variable, this
// might fail.
synchronized( this.getClass() )
{
if (this.notLoaded)
{
loadInstances();
this.notLoaded = false;
}
}
}
}
/**
* Retrieves the owning class. Will never return <tt>null</tt>.
*
* @return the class which is being AutoDoced.
*/
protected final Class getOwner()
{
return this.owner;
}
/**
* Loads all class
*/
protected void loadInstances()
{
AutoDocFactory adf[] = getFactories();
for (int i = 0; i < adf.length; ++i)
{
loadFromFactory( adf[i] );
}
cleanUpFactories();
}
/**
* Adds to all inner instances from the given factory.
*/
protected void loadFromFactory( AutoDocFactory adf )
{
this.log.addLog( adf.createLog( getOwner() ) );
this.it.addIT( adf.createIT( getOwner() ) );
this.tp.addTP( adf.createTP( getOwner() ) );
}
/**
* Returns the factory this implementation knows. By default, this is the
* static factory instance. This is only called at AutoDoc creation time.
* This is not static, because subclasses may need to re-implement this
* method. The factory may be changed through invocation of
* <tt>setFactory( AutoDocFactory )</tt>.
*
* @return the inner factory, or the static singleton if it was never
* assigned.
*/
protected AutoDocFactory[] getFactories()
{
AutoDocFactory adf[];
synchronized( this )
{
if (this.factories == null)
{
this.factories = new Vector();
java.util.Enumeration enum = getFactoryStore().getSingletons();
while (enum.hasMoreElements())
{
addFactory( (AutoDocFactory)enum.nextElement() );
}
}
adf = new AutoDocFactory[ this.factories.size() ];
this.factories.copyInto( adf );
}
return adf;
}
/**
* After loading the instances, we have no need to keep the memory
* of the original factories around - they may pollute our loaded
* proxied objects anyway. But since we've loaded, we don't want
* to allow another load. So keep the vector, but clean it out.
*/
protected void cleanUpFactories()
{
this.factories.removeAllElements();
}
/**
* Sets the AutoDoc factory instance. If this method is never invoked, then
* the default static store will be used instead.
* <P>
* The standard factory/singleton pattern used in this framework normally
* does not allow direct setting of the framework for an instance, but
* rather for the classloader's class. Since AutoDoc (or a subclass)
* is directly instantiated as a central processing point, opening a
* "hole" into the class allows for an easier method to setup a particular
* AutoDoc style. However, this is not the "recommended" usage, since,
* in general, AutoDoc instances are instantiated independently throughout
* many different classes, causing independent factory setting to be
* more difficult.
*
* @param adf the new factory to assign.
* @exception IllegalArgumentException if <tt>adf</tt> is <tt>null</tt>.
* @exception IllegalStateException if the inner factory has already been
* assigned. This usually indicates that all the objects have already
* been loaded.
*/
protected void addFactory( AutoDocFactory adf )
{
if (adf == null)
{
throw new IllegalArgumentException("no null args");
}
synchronized (this)
{
if (!this.notLoaded)
{
throw new IllegalStateException("factories already loaded.");
}
if (this.factories == null)
{
// allow this - it means we will not be using the defaults.
this.factories = new Vector();
}
this.factories.addElement( adf );
}
}
//-------------------------------------------------------------------------
// Static methods
/**
* Retrieve the AutoDocFactory singleton store for setting up the factory
* to be used for all uninitialized or uncreated AutoDoc instances.
*/
public static SPISingletonStore getFactoryStore()
{
return s_factoryStore;
}
}
|