⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 property.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        return value;      String origValue = value;      for (int ii = 0;; ii++) {         int from = value.indexOf("$_{");         if (from != -1) {            int to = value.indexOf("}", from);            if (to == -1) {               throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME + ".InvalidVariable", "Invalid variable '" + value.substring(from) + "', expecting ${} syntax.");            }            String sub = value.substring(from, to + 1); // "$_{XY}"            String subKey = sub.substring(3, sub.length() - 1); // "XY"            String subValue = get_(subKey);            if (subValue == null) {               if (verbose>=2) System.out.println("Property: Unknown variable " + sub + " is not replaced");               return value;            }            value = org.xmlBlaster.util.ReplaceVariable.replaceAll(value, sub, subValue);         }         else {            if (ii > 0 && verbose>=2) {               System.out.println("Property: Replacing '" + key + "=" + origValue + "' to '" + value + "'");            }            return value;         }         if (ii > MAX_NEST) {            if (verbose>=1) System.out.println("Property: Maximum nested depth of " + MAX_NEST + " reached for variable '" + get_(key) + "'.");            return value;         }      }   }   /**   * Parse a string to boolean.   * <p />   * @param token for example "false"   * @return true for one of "true", "yes", "1", "ok"<br />   *         false for "false", "0", "no"   * @exception if none of the above strings   */   static public final boolean toBool(String token) throws XmlBlasterException {      if (token == null)        throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Can't parse <null> to true or false");      if (token.equalsIgnoreCase("true") || token.equalsIgnoreCase("1") || token.equalsIgnoreCase("ok") || token.equalsIgnoreCase("yes"))        return true;      if (token.equalsIgnoreCase("false") || token.equalsIgnoreCase("0") || token.equalsIgnoreCase("no"))        return false;      throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Can't parse <" + token + "> to true or false");   }   /********************************************************************************************    * Look for properties file.    * <p />    * The property file is searched in the specific order as described above    *    * @param      fileName    *             e.g. "cool.properties".    * @return     the path to file or null if not found    *******************************************************************************************/   public final FileInfo findPath(String fileName) {      if (fileName == null)        return null;      File f = null;      FileInfo info = new FileInfo(fileName);      f = new File(currentPath, fileName);      if (f.exists()) {         if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + currentPath);         info.path = currentPath;         return info;      }      else {         if (verbose>=2)            System.out.println("Property: File '" + fileName + "' is not in current directory " + currentPath);      }      f = new File(fileName); // if given with full name      if (f.exists()) {         if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + currentPath);         info.path = f.getName(); // The file name without path         info.fullPath = fileName;         return info;      }      else {         if (verbose>=2)            System.out.println("Property: File '" + fileName + "' is not in current directory " + currentPath);      }      if (projectHomePath == null) {         if (verbose>=2) {            System.out.println("Property: File '" + fileName + "' is not in PROJECT_HOME, 'java -DPROJECT_HOME=...' is not set ...");         }      }      else {         if (!projectHomePath.endsWith(separator))            projectHomePath += separator;         f = new File(projectHomePath, fileName);         if(f.exists()) {            if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + projectHomePath);            info.path = projectHomePath;            return info;         }         else {            if (verbose>=2)               System.out.println("Property: File '" + fileName + "' is not in directory PROJECT_HOME=" + projectHomePath);         }      }      f = new File(userHome, fileName);      if (f.exists()) {        if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + userHome);        info.path = userHome;        return info;      }      else {        if (verbose>=2)           System.out.println("Property: File '" + fileName + "' is not in user.home directory " + userHome);      }      ClassLoader loader = Property.class.getClassLoader();      if (loader != null) {        java.net.URL url = loader.getResource(fileName);        if (url != null ) {           info.in = loader.getResourceAsStream(fileName);           info.fullPath = url.getFile(); // path and filename           if (verbose>=1) System.out.println("Property: Loading " + fileName + " from CLASSPATH " + info.getFullPath());         return info;        }      }      if (verbose>=2)         System.out.println("Property: File '" + fileName + "' is not in CLASSPATH");      f = new File(javaHomeExt, fileName);      if (f.exists()) {        if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + javaHomeExt);        info.path = javaHomeExt;        return info;      }      else {         if (verbose>=2)           System.out.println("Property: File '" + fileName + "' is not in java.home/lib/ext directory " + javaHomeExt);      }      f = new File(javaHome, fileName);      if (f.exists()) {        if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + javaHome);        info.path = javaHome;        return info;      }      else {         if (verbose>=2) {           System.out.println("Property: File '" + fileName + "' is not in java.home directory " + javaHome);         }      }      String guess;      if (separator.equals("/"))        guess = "/usr/local/jutils/";      else        guess = "\\jutils\\";      f = new File(guess, fileName);      if(f.exists()) {        if (verbose>=1) System.out.println("Property: Loading " + fileName + " from directory " + guess);        info.path = guess;        return info;      }      else {        if(verbose>=2)           System.out.println("Property: File '" + fileName + "' is not in directory " + guess);      }      return null;   }   /**   * Find properties file.   * @param args Only "-propertyFile /tmp/xy.properties" is evaluated (if given)   * @return The property file name or null if not found of not given.      */   public final FileInfo findArgsFile(Properties argsProps) {      String argsLocation = (String)argsProps.get("propertyFile");      if (argsLocation != null) {         FileInfo info = findPath(argsLocation);         if(info != null) {            propertyFileName = argsLocation;         }         else {            if (verbose>=0) System.out.println("Property: ERROR: File '-propertyFile " + argsLocation + "' not found!");            // TODO: throw Exception?         }         return info;      }      return null;   }   /**   * Find properties file which was given with the constructor.   * <p />   * See findPath() for search - logic   *   * @param fileName e.g. "cool.properties"   * @param args Only "-propertyFile /tmp/xy.properties" is evaluated (if given)   *             this has precedence over the given fileName!   * @return The path to file, e.g. "\jutils\"      */   public final FileInfo findGivenFile(String fileName) {      if (fileName == null) {        if (verbose>=2) System.out.println("Property: No property file specified.");        return null;      }      // The method 'findPath' now return the full path including the filename      FileInfo info = findPath(fileName);      if(info == null) {        if (verbose>=1) System.err.println("Property: File '" + fileName + "' not found");      }      return info;   }   /**   * Find properties file.   * <p />   * See findPath() for search - logic   *   * @param fileName e.g. "cool.properties"   * @param args Only "-propertyFile /tmp/xy.properties" is evaluated (if given)   *             this has precedence over the given fileName!   * @return The path to file or null      */   public final FileInfo findFile(String fileName, Properties argsProps) {      FileInfo info = findArgsFile(argsProps);      if (info != null) return info;      return findGivenFile(fileName);   }   /**   * Add key/values, for example from startup command line args to   * the property variable.   * <p />   * Args parameters are stronger and overwrite the property file variables.   * <p />   * The arg key must have a leading - or -- (as usual on command line).<br />   * The leading - are stripped (to match the property variable)   * args must be a tuple (key / value pairs).   * <p />   */   public void addArgs2Props(String[] args) throws XmlBlasterException {      if (args == null)         return;      if (System.getProperty("property.verbose") != null) {         try { verbose = Integer.parseInt(System.getProperty("property.verbose").trim()); } catch(NumberFormatException e) { System.err.println("-property.verbose expects a number"); }      }      if (properties == null) properties = new Properties();      Properties pp = argsToProps(args);      if (pp != null && pp.get("---help") != null) {         wantsHelp = true;      }      addArgs2Props(properties, pp);      replaceVariables();      if (supportArrays == true) scanArrays(); // Buggy: scans everything again      if (verbose>=1) System.out.println("Property: Added " + args.length/2 + " property pairs");   }   /**   * The same as addArgs2Props(String[] args) but passing the args in a Properties hashtable.   */   public void addArgs2Props(Properties argsProps) throws XmlBlasterException {      addArgs2Props(propsToArgs(argsProps));   }   /**    * Convert a properties hashtable to a String array with leading "-" in front of the keys    */   public static String[] propsToArgs(Map props)   {      if (props == null) return new String[0];      String[] args = new String[props.size()*2];      Iterator e = props.keySet().iterator();      int ii = 0;      while (e.hasNext()) {         String key = (String)e.next();         args[ii] = "-"+key;         ii++;         args[ii] = (String)props.get(key);         //System.out.println("ii=" + ii + ": " + args[ii] + " key=" + key);         ii++;      }      return args;   }   /**    * See method addArgs2Props(args)    */   private void addArgs2Props(Properties props, Properties argsProps)       throws XmlBlasterException   {      if(argsProps == null) {         return;      }      // 1. Load property file if given on command line      FileInfo info = findArgsFile(argsProps);      if (info != null) {         InputStream inputStream = info.getInputStream();         if (inputStream != null) {            try {               props.load(inputStream);               if (verbose>=2) System.out.println("Property: Loaded file " + info.getFullPath());            } catch(IOException e) {               if (verbose>=0) System.out.println("Property: ERROR loading file " + info.getFullPath());            } finally {               info.closeInputStream();            }         }         else {            if (verbose>=1) System.out.println("Property: No property file given.");         }      }      // 2. Load other command line properties      Enumeration e = argsProps.keys();      while (e.hasMoreElements()) {         String key = (String)e.nextElement();         String value = (String)argsProps.get(key);         String oldValue = (String)props.put(key, value);         fireChangeEvent(key, oldValue, value);      }   }   /**    * Scan args, correct and check them and return the result in a Properties hash table    * @parameter null if nothing found    */   public static Properties argsToProps(String[] args) throws XmlBlasterException {      Properties props = null;      if (args == null) return null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -