📄 componentfactory.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/util/ComponentFactory.java,v $// $RCSfile: ComponentFactory.java,v $// $Revision: 1.9.2.5 $// $Date: 2005/08/11 21:03:29 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.util;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.util.Properties;import java.util.Vector;import com.bbn.openmap.BasicI18n;import com.bbn.openmap.PropertyConsumer;import com.bbn.openmap.event.ProgressEvent;import com.bbn.openmap.event.ProgressSupport;/** * The OpenMap ComponentFactory is a class that can construct objects * from class names, with the added capability of passing the new * object a Properties object to initialize itself. The new object may * also receive a property prefix to use to scope its properties from * the Properties object. It is sensitive to the OpenMap paradigm of * marker names in a list: That a list of objects can be defined as a * space separated names (marker names) within a String. Those marker * names can serve as a prefix for other properties within a * Properties object, as well as the prefix for a '.class' property to * define the class name for the new object. */public class ComponentFactory { /** * The property to use for the class name of new objects - * ".class". Expects that a prefix will be prepended to it. */ public static final String ClassNameProperty = ".class"; /** * The singleton instance of the ComponentFactory. */ private static ComponentFactory singleton; protected ComponentFactory() {} /** * Method call to retrieve the singleton instance of the * ComponentFactory. * * @return ComponentFactory. */ protected static ComponentFactory getInstance() { if (singleton == null) { singleton = new ComponentFactory(); } return singleton; } /** * Set the singleton instance of the ComponentFactory. * * @param cf */ protected static void setInstance(ComponentFactory cf) { singleton = cf; } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param properties Properties object containing the details. * @return Vector containing the new Objects. */ public static Vector create(Vector markerNames, Properties properties) { return getInstance()._create(markerNames, null, properties, null, false); } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param prefix The prefix that should be prepended to the marker * names. * @param properties Properties object containing the details. * @return Vector containing the new Objects. */ public static Vector create(Vector markerNames, String prefix, Properties properties) { return getInstance()._create(markerNames, prefix, properties, null, false); } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param prefix The prefix that should be prepended to the marker * names. * @param properties Properties object containing the details. * @param progressSupport ProgressSupport object to provide * progress updates to. It's OK if this is null to not have * progress events sent. * @return Vector containing the new Objects. */ public static Vector create(Vector markerNames, String prefix, Properties properties, ProgressSupport progressSupport) { return getInstance()._create(markerNames, prefix, properties, progressSupport, false); } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param properties Properties object containing the details. * @param progressSupport ProgressSupport object to provide * progress updates to. It's OK if this is null to not have * progress events sent. * @return Vector containing the new Objects. */ public static Vector create(Vector markerNames, Properties properties, ProgressSupport progressSupport) { return getInstance()._create(markerNames, null, properties, progressSupport, false); } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param properties Properties object containing the details. * @param progressSupport ProgressSupport object to provide * progress updates to. It's OK if this is null to not have * progress events sent. * @param matchInOutVectorSize if true, then if there is any * trouble creating an object, it's marker name will be * placed in the returned vector instead of a component. If * false, only valid objects will be returned in the * vector. * @return Vector containing the new Objects. If a component could * not be created, the markerName is returned in its * place, so you can figure out which one couldn't be * created. In any case, the size of the returned vector * is the same size as the markerNames vector, so you can * figure out which markerNames go with which objects. */ public static Vector create(Vector markerNames, Properties properties, ProgressSupport progressSupport, boolean matchInOutVectorSize) { return getInstance()._create(markerNames, null, properties, progressSupport, matchInOutVectorSize); } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param prefix The prefix that should be prepended to the marker * names. * @param properties Properties object containing the details. * @param progressSupport ProgressSupport object to provide * progress updates to. It's OK if this is null to not have * progress events sent. * @param matchInOutVectorSize if true, then if there is any * trouble creating an object, it's marker name will be * placed in the returned vector instead of a component. If * false, only valid objects will be returned in the * vector. * @return Vector containing the new Objects. If a component could * not be created, the markerName is returned in its * place, so you can figure out which one couldn't be * created. In any case, the size of the returned vector * is the same size as the markerNames vector, so you can * figure out which markerNames go with which objects. */ public static Vector create(Vector markerNames, String prefix, Properties properties, ProgressSupport progressSupport, boolean matchInOutVectorSize) { return getInstance()._create(markerNames, prefix, properties, progressSupport, matchInOutVectorSize); } /** * Given a Vector of marker name Strings, and a Properties object, * look in the Properties object for the markerName.class property * to get a class name to create each object. Then, if the new * objects are PropertyConsumers, use the marker name as a * property prefix to get properties for that object out of the * Properties. * * @param markerNames String of space separated marker names. * @param prefix The prefix that should be prepended to the marker * names. * @param properties Properties object containing the details. * @param progressSupport ProgressSupport object to provide * progress updates to. It's OK if this is null to not have * progress events sent. * @param matchInOutVectorSize if true, then if there is any * trouble creating an object, it's marker name will be * placed in the returned vector instead of a component. If * false, only valid objects will be returned in the * vector. * @return Vector containing the new Objects. If a component could * not be created, the markerName is returned in its * place, so you can figure out which one couldn't be * created. In any case, the size of the returned vector * is the same size as the markerNames vector, so you can * figure out which markerNames go with which objects. */ protected Vector _create(Vector markerNames, String prefix, Properties properties, ProgressSupport progressSupport, boolean matchInOutVectorSize) { int size = markerNames.size(); Vector vector = new Vector(size); if (progressSupport != null) { progressSupport.fireUpdate(ProgressEvent.UPDATE, "Creating Components", 100, 0); } for (int i = 0; i < size; i++) { String componentName = PropUtils.getScopedPropertyPrefix(prefix) + (String) markerNames.elementAt(i); String classProperty = componentName + ClassNameProperty; String className = properties.getProperty(classProperty); if (className == null) { Debug.error("ComponentFactory.create: Failed to locate property \"" + componentName + "\" with class \"" + classProperty + "\"\n Skipping component \"" + componentName + "\""); if (matchInOutVectorSize) { vector.add(componentName); } continue; } if (progressSupport != null) { progressSupport.fireUpdate(ProgressEvent.UPDATE, "Creating Components", size, i); } Object component = create(className, componentName, properties); if (component != null) { vector.add(component); if (Debug.debugging("componentfactory")) { Debug.output("ComponentFactory: [" + className + "(" + i + ")] created"); } } else { if (matchInOutVectorSize) { vector.add(componentName); } Debug.output("ComponentFactory: [" + componentName + " : " + className + "(" + i + ")] NOT created. -- " + "Set 'componentfactory' debug flag for details."); } } if (progressSupport != null) { progressSupport.fireUpdate(ProgressEvent.UPDATE, "Configuring...", size, size); } return vector; } /** * Create a single object. * * @param className Class name to instantiate, empty constructor. * @return object if all goes well, null if not. */ public static Object create(String className) { return create(className, (Object[]) null, null, null); } /** * Create a single object. * * @param className Class name to instantiate. * @param properties Properties to use to initalize the object, if * the object is a PropertyConsumer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -