propertyhandler.java
来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,648 行 · 第 1/5 页
JAVA
1,648 行
// **********************************************************************// // <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/PropertyHandler.java,v $// $RCSfile: PropertyHandler.java,v $// $Revision: 1.20.2.8 $// $Date: 2008/02/28 23:36:26 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.util.Collections;import java.util.Enumeration;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.Properties;import java.util.Set;import java.util.TreeMap;import java.util.Vector;import com.bbn.openmap.event.ProgressEvent;import com.bbn.openmap.event.ProgressListener;import com.bbn.openmap.event.ProgressSupport;import com.bbn.openmap.gui.ProgressListenerGauge;import com.bbn.openmap.gui.WindowSupport;import com.bbn.openmap.plugin.PlugIn;import com.bbn.openmap.proj.ProjectionFactory;import com.bbn.openmap.util.ComponentFactory;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.PropUtils;/** * The PropertyHandler object is the organizer of properties, looking for * settings on how to configure OpenMap components. It is designed to look * through a series of locations to find properties files, loading them in * order. If there is a name conflict for a property, the last version of the * property set is the one that gets used. This object isn't really interested * in hooking up with other objects. It's assumed that many objects will want to * contact this object, and find the properties that apply to them. There is one * exception this: When components start implementing the PropertyProvider * interface, and the PropertyHandler becomes capable of creating an properties * file, then the PropertyHandler will be able to use the BeanContext to query * PropertyProviders to get their properties to put in the properties file. * <P> * * The PropertyHandler looks in several places for an openmap.properties file: * <UL> * <LI>as a resource in the code base. * <LI>in the configDir set as a system property at runtime. * <LI>in the user's home directory. * </UL> * * For each properties file, a check is performed to look within for an include * property containing a marker name list. That list is parsed, and each item is * checked (markerName.URL) for an URL to another properties file. * <P> * * Also significant, the PropertyHandler can be given a BeanContext to load * components. For this, the openmap.components property contains a marker name * list for openmap objects. Each member of the list is then used to look for * another property (markername.class) which specifies which class names are to * be instantiated and added to the BeanContext. Intelligent components are * smart enough to wire themselves together. Order does matter for the * openmap.components property, especially for components that get added to * lists and menus. Place the components in the list in the order that you want * components added to the MapHandler. * <P> * * If the debug.showprogress environment variable is set, the PropertyHandler * will display a progress bar when it is creating components. If the * debug.properties file is set, the steps that the PropertyHandler takes in * looking for property files will be displayed. * <P> * * If the PropertyHandler is created with an empty constructor or with a null * Properties object, it will do the search for an openmap.properties file. If * you don't want it to do that search, create it with an empty Properties * object. */public class PropertyHandler extends MapHandlerChild implements SoloMapComponent { /** * All components can have access to an I18n object, which is provided by * the Environment. */ protected transient I18n i18n = Environment.getI18n(); /** * The propertyPrefix can be set to reflect a particular set of properties, * or for an application. If this variable is not set, 'openmap' will be * used. This prefix will be placed in front of the default properties file * that will be sought if a specific properties file is not specified, and * will also be placed in front of the standard application component * properties. */ protected String propertyPrefix; /** * The appendix for the name of the properties file to read. The * propertyPrefix will be prepended to this string for the default property * file search. */ public final static String propsFileName = "properties"; /** * The name of the system directory containing a properties file. The * propertyPrefix.configDir property will be checked for a possible location * for properties. */ public final static String configDirProperty = "configDir"; /** * The property name used to hold a list of marker names. Each marker name * is used to create another property to look for to create a component to * add to a BeanContext. For example: * <P> * * <PRE> * # if 'openmap' is the PropertyHandler property prefix... * openmap.components=name1 name2 name3 * name1.class=com.bbn.openmap.InformationDelegator * name2.class=com.bbn.openmap.MouseDelegator * name3.class=com.bbn.openmap.LayerHandler * * </PRE> */ public final static String componentProperty = "components"; /** * The property name used to hold a list of marker names. Each marker name * is used to create another property to look for to connect to a URL to * load a properties file. For example: * <P> * * <PRE> * * openmap.include=name1 name2 * name1.URL=http://openmap.bbn.com/props/link.properties * name2.URL=file:///usr/local/openmap/props/shape.properties * * </PRE> */ public final static String includeProperty = "include"; /** * The property name used to hold a file, resource or URL of a file to use * containing localized properties, like layer names. This is optional, if * it's not in the openmap.properties file or the properties file being read * in, an openmap_<localization string>.properties file will be searched * for in the classpath (i.e. openmap.localized=openmap_en_US.properties). */ public final static String localizedProperty = "localized"; /** Final openmap properties object. */ protected Properties properties = new Properties(); /** * Container to hold prefixes for components that have been created, in * order to determine if duplicates might have been made. Important if * properties are going to be written out, so that property scoping can * occur properly. This collection holds prefixes of objects that have been * created by this PropertyHandler, and also prefixes that have been given * out on request. */ protected Set usedPrefixes = Collections.synchronizedSet(new HashSet()); protected ProgressSupport progressSupport; /** * Flag to set whether the PropertyHandler should provide status updates to * any progress listeners, when it is building components. */ protected boolean updateProgress = false; /** * A hashtable to keep track of property prefixes and the objects that were * created for them. */ protected Hashtable prefixLibrarian = new Hashtable(); protected boolean DEBUG = false; /** * Create a PropertyHandler object that checks in the default order for * openmap.properties files. It checks for the openmap.properties file as a * resource, in the configDir if specified as a system property, and lastly, * in the user's home directory. If you want an empty PropertyHandler that * doesn't do the search, use the constructor that takes a * java.util.Properties object and provide it with empty Properties. */ public PropertyHandler() { this(false); } /** * Create a PropertyHandler object that checks in the default order for * openmap.properties files. It checks for the openmap.properties file as a * resource, in the configDir if specified as a system property, and lastly, * in the user's home directory. * * @param provideProgressUpdates if true, a progress bar will be displayed * to show the progress of building components. */ public PropertyHandler(boolean provideProgressUpdates) { DEBUG = Debug.debugging("properties"); updateProgress = provideProgressUpdates; searchForAndLoadProperties(); } /** * Constructor to take resource name, file path or URL string as argument, * to create context for a particular map. */ public PropertyHandler(String urlString) throws MalformedURLException, IOException { this(PropUtils.getResourceOrFileOrURL(urlString)); } /** * Constructor to take path (URL) as argument, to create context for a * particular map. */ public PropertyHandler(URL url) throws IOException { DEBUG = Debug.debugging("properties"); InputStream is = null; if (url != null) { // Open URL to read in properties is = url.openStream(); } Properties tmpProperties = new Properties(); if (is != null) { tmpProperties.load(is); } init(tmpProperties, "URL"); Environment.init(getProperties()); } /** * Constructor to take Properties object as argument, to create context for * a particular map. */ public PropertyHandler(Properties props) { DEBUG = Debug.debugging("properties"); init(props, null); Environment.init(getProperties()); } /** * Look for openmap.properties files as a resource in the classpath, in the * config directory, and in the user's home directory, in that order. If any * property is duplicated in any version, last one wins. */ protected void searchForAndLoadProperties() { Properties tmpProperties = new Properties(); Properties includeProperties; Properties localizedProperties; boolean foundProperties = false; boolean showDebugMessages = false; if (Debug.debugging("locale")) { java.util.Locale.setDefault(new java.util.Locale("pl", "PL")); } if (DEBUG) { showDebugMessages = true; } if (Debug.debugging("showprogress")) { updateProgress = true; } if (showDebugMessages) { Debug.output("***** Searching for properties ****"); } String propertyPrefix = getPropertyPrefix(); String propsFileName = propertyPrefix + PropertyHandler.propsFileName; // look for openmap.properties file in jar archive(of course // only in same package as this class) or wherever this // object's class file lives. if (showDebugMessages) { Debug.output("PropertyHandler: Looking for " + propsFileName + " in Resources"); } InputStream propsIn = getClass().getResourceAsStream(propsFileName); // Look in the codebase for applets... if (propsIn == null && Environment.isApplet()) { URL[] cba = new URL[1]; cba[0] = Environment.getApplet().getCodeBase(); URLClassLoader ucl = URLClassLoader.newInstance(cba); propsIn = ucl.getResourceAsStream(propsFileName); } if (propsIn == null) { propsIn = ClassLoader.getSystemResourceAsStream(propsFileName); if (propsIn != null && showDebugMessages) { Debug.output("Loading properties from System Resources: "
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?