📄 poolmanconfiguration.java
字号:
/* * PoolMan Java Object Pooling and Caching Library * Copyright (C) 1999-2001 The Code Studio * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * The full license is located at the root of this distribution * in the LICENSE file. */package com.codestudio.management;import com.codestudio.PoolManConstants;import org.xml.sax.InputSource;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import java.io.InputStream;import java.net.URL;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Properties;/** * This service configures ObjectPools by first looking for * 'poolman.xml' in the CLASSPATH. If it finds it, it parses * the database entries and generic Class entries and creates * MBeans and pools for them, adding each one to an appropriate * PoolManager and the MBeanServer. If it * cannot find this xml file, it looks for 'poolman.props' and * loads those properties in order to create the pools (with * no MBeans). If it cannot find poolman.props, it looks for * the deprecated pool.props file and treats it as if it * were named 'poolman.props'. If it cannot find any of these * files, it throws an Exception. */public class PoolManConfiguration { private String configFile; private ConfigParser handler; private ArrayList datasources; private ArrayList genericObjects; private boolean useJMX = PoolManConstants.DEFAULT_USE_JMX; public PoolManConfiguration(String configFile) { this.configFile = configFile; this.datasources = new ArrayList(); this.genericObjects = new ArrayList(); } public boolean isUsingJMX() { return handler.isManagementJMX(); } /** Load DataSource info from XML and create a Service for each entry set. */ public void loadConfiguration() throws Exception { // first try XML try { parseXML(); } catch (NullPointerException ne) { // then try deprecated properties System.out.println("\n** ERROR: Unable to find XML file " + configFile + ": " + ne); // don't try the props files anymore, it's been over a year //System.out.println("** WARNING: Attempting to use deprecated properties files\n"); //this.datasources = parseProperties(PoolManConstants.PROPS_CONFIG_FILE); } catch (Exception e) { System.out.println("\n** ERROR: Unable to parse XML file " + configFile + ": " + e); } } public ArrayList getDataSources() { return this.datasources; } public ArrayList getGenericPools() { return this.genericObjects; } public Properties getAdminProperties() { return handler.getAdminProps(); } private void parseXML() throws Exception { /* CHANGED TO USE JAXP */ this.handler = new ConfigParser(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); SAXParser parser = factory.newSAXParser(); URL confURL = PoolManConfiguration.class.getClassLoader().getResource(configFile); if (confURL == null) PoolManConfiguration.class.getClassLoader().getResource("/" + configFile); if (confURL == null) Thread.currentThread().getContextClassLoader().getResource(configFile); if (confURL == null) confURL = ClassLoader.getSystemResource(configFile); parser.parse(confURL.toString(), handler); this.datasources = handler.getDataSourceProperties(); this.genericObjects = handler.getGenericProperties(); } private ArrayList parseProperties(String propsfilename) throws Exception { // backwards compatibility is a b**** Hashtable datasources = new Hashtable(); InputStream is = null; Properties p = null; try { is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsfilename); p = new Properties(); p.load(is); } catch (Exception e) { if (propsfilename.equals(PoolManConstants.PROPS_CONFIG_FILE)) { try { is = Thread.currentThread().getContextClassLoader().getResourceAsStream(PoolManConstants.OLDPROPS_CONFIG_FILE); p = new Properties(); p.load(is); } catch (Exception e2) { throw new Exception("Unable to find and read a valid poolman.xml config file. " + "Please ensure that '" + PoolManConstants.XML_CONFIG_FILE + "' is in a directory that is in your CLASSPATH.\n"); } System.out.println("\nPLEASE NOTE: You should replace the pool.props file with a valid poolman.xml file\n"); } else { throw new Exception("ERROR: Unable to find and read a valid PoolMan properties file.\n" + "Please ensure that " + PoolManConstants.XML_CONFIG_FILE + " or at least " + propsfilename + " is in a directory that is in your CLASSPATH.\n"); } } // now have props with each set of datasource entries delimited by a number // split those into an ArrayList of Properties objects Properties theseProps = null; String entrySetNumber = null; for (Enumeration enum = p.keys(); enum.hasMoreElements();) { String key = (String) enum.nextElement(); // get rid of the "_" String adjustedKey = key; if (key.indexOf("_") != -1) { StringBuffer sb = new StringBuffer(key.substring(0, key.indexOf("_"))); sb.append(key.substring(key.indexOf("_") + 1, key.length())); adjustedKey = sb.toString(); if ((!adjustedKey.toLowerCase().equals("dbname")) && (adjustedKey.startsWith("db"))) { adjustedKey = adjustedKey.substring(2, adjustedKey.length()); } } if (adjustedKey.toLowerCase().startsWith("cacherefresh")) { adjustedKey = "cacherefreshinterval" + adjustedKey.substring(12, adjustedKey.length()); } try { entrySetNumber = key.substring(key.indexOf('.') + 1, key.length()); } catch (StringIndexOutOfBoundsException sbe) { throw new Exception("Unnumbered property in poolman.props: " + key); } if (datasources.containsKey(entrySetNumber)) theseProps = (Properties) datasources.get(entrySetNumber); else theseProps = new Properties(); theseProps.setProperty(adjustedKey.substring(0, adjustedKey.indexOf('.')), p.getProperty(key)); datasources.put(entrySetNumber, theseProps); } ArrayList finalDataSourceList = new ArrayList(); for (Iterator iter = datasources.keySet().iterator(); iter.hasNext();) { Properties testp = (Properties) datasources.get(iter.next()); finalDataSourceList.add(testp); } return finalDataSourceList; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -