📄 systemvariable.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/8/25
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.common.System;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import eti.bi.common.MLPropertyResourceBundle;
public class SystemVariable {
private static String HOME;
private static MLPropertyResourceBundle property;
private static String[] fields = new String[0];
private static boolean[] register = new boolean[0];
public static void initialize() {
HOME = SysConfig.getProperty("SYSTEM_VARIABLE_HOME");
File homedir = new File(HOME);
if (!homedir.exists()) {
if (!homedir.mkdirs()) {
System.err.println("Can't create system variable dir: " + HOME);
SysLog.warn("Can't create system variable dir: " + HOME);
return;
}
}
// load system variable
String filepath = HOME + File.separator + "system.properties";
File file = new File(filepath);
if (!file.exists()) {
System.err.println("SysTem property file: " + filepath + " dosen't exist!");
SysLog.warn("SysTem property file: " + filepath + " dosen't exist!");
return;
}
try {
FileInputStream is = new FileInputStream(file);
property = new MLPropertyResourceBundle();
property.addResource(is);
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
SysLog.warn("Registering the system variable fail!", e);
}
// load other variable
File[] files = homedir.listFiles();
if (files == null) {
SysLog.info("No other fields of system variable!");
return;
}
String temp;
String[] temp_fileds = new String[files.length];
int i,index = 0;
for (i = 0; i < files.length; i++) {
temp = files[i].getName();
if ((!temp.endsWith("system.properties") && temp.endsWith(".properties"))) {
temp_fileds[index] = temp.replaceAll(".properties", "");
try {
InputStream in = new FileInputStream(files[i]);
Properties properties = new Properties();
properties.load(in);
in.close();
Enumeration enu = properties.keys();
String key;
while (enu.hasMoreElements()) {
key = (String) enu.nextElement();
try {
property.addProperty(temp_fileds[index] + "_" + key, properties.getProperty(key));
} catch (Exception e) {
e.printStackTrace();
SysLog.warn("Registering the key: " + key + " of " + temp_fileds[index] + "from file: "
+ files[i].getAbsolutePath() + " fail!");
}
}
} catch (Exception e) {
e.printStackTrace();
SysLog.warn("Registering the property file: "+files[i].getAbsolutePath()+" fail!",e);
}
index++;
}
}
fields = new String[index];
register = new boolean[index];
for(i=0;i<index;i++) {
fields[i] = temp_fileds[i];
register[i] = false;
}
}
private static void registerFileProperty(Properties properties, String field) {
Enumeration enu = properties.keys();
String key;
while (enu.hasMoreElements()) {
key = (String) enu.nextElement();
try {
property.addProperty( field + "_" + key, properties.getProperty(key));
} catch (Exception e) {
e.printStackTrace();
SysLog.warn("Registering the key: " + key + " of " + field + " fail!");
}
}
String[] temp_fields = new String[fields.length+1];
boolean[] temp_register = new boolean[fields.length+1];
for(int i=0;i<fields.length;i++) {
temp_fields[i] = fields[i];
temp_register[i]= register[i];
}
temp_fields[fields.length] = field;
temp_register[fields.length] = true;
fields = temp_fields;
register = temp_register;
}
private static boolean registerField(String field) {
int len = fields.length;
for(int i=0;i<len;i++) {
if(fields[i].equals(field)) {
register[i] = true;
return true;
}
}
return false;
}
/**
* add a system variable properties to root system viariable property file
*
* @param afile
* the Properties file added
* @param field
* the field of system viariable Property, null for the system
* variable. If this file is in a plug-in, the name should be the
* file name of the plug-in.
* @throws Exception
*/
public static void addViariableProperty(String afile, String field) throws Exception {
if(hasField(field)) {
registerField(field);
return;
}
if (field != null) {
File file = new File(afile);
if (!file.exists()) {
throw new FileNotFoundException("File propertyfile not found!");
}
if (!file.isFile()) {
throw new FileNotFoundException(afile + " should be a file!");
}
File newFile = copyFile(file, field);
Properties properties = openProperty(newFile);
registerFileProperty(properties, field);
} else {
property.addResource(afile);
}
}
/**
* add a system variable property file to root system viariable property
* file
*
* @param in
* input stream of property file to add
* @param field
* the field of system viariable Property, null for the system
* variable. If this input stream in is from a plug-in, the name
* should be the file name of the plug-in.
* @throws Exception
*/
public static void addViariableProperty(InputStream in, String field) throws Exception {
if(hasField(field)) {
registerField(field);
return;
}
if (field != null) {
File newFile = copyInputStream(in, field);
Properties properties = openProperty(newFile);
registerFileProperty(properties, field);
} else {
property.addResource(in);
}
}
/**
* add a system variable properties to root system viariable property file
*
* @param properties
* the Properties added
* @param field
* the field of system viariable Property, null for the system
* variable. If the properties is form a plug-in, the name should
* be the file name of the plug-in.
* @throws Exception
*/
public static void addVariableProperty(Properties properties, String field) throws Exception {
if(hasField(field)) {
registerField(field);
return;
}
if (field != null) {
File newFile = copyProperty(properties, field);
Properties propert = openProperty(newFile);
registerFileProperty(propert, field);
} else {
property.addResource(properties);
}
}
/**
* add a system variable properties to root system viariable property file
*
* @param url
* the Properties URL added.
* @param field
* the field of system viariable Property, null for the system
* variable. If the url is from a plug-in, the name should be the
* file name of the plug-in.
* @throws Exception
*/
public static void addVariableProperty(URL url, String field) throws Exception {
if(hasField(field)) {
registerField(field);
return;
}
if (field != null) {
File newFile = copyURL(url, field);
Properties properties = openProperty(newFile);
registerFileProperty(properties, field);
} else {
property.addResource(url);
}
}
public static File copyFile(File file, String field) throws Exception{
File newFile = new File(HOME+File.separator+field+".properties");
if(newFile.exists()) {
throw new IOException("The variable file of field "+field+" already exists!");
}
InputStream in = null;
OutputStream ou = null;
try {
in = new FileInputStream(file);
ou = new FileOutputStream(newFile);
byte[] bytes = new byte[5000];
int read = 0;
int size = in.available();
int hasread = 0;
int step = 0;
while(hasread<size) {
read=0;
step = 5000>(size-hasread)?(size-hasread):5000;
while(read<step) {
read += in.read(bytes, read, step-read);
}
hasread += read;
ou.write(bytes, 0, read);
}
}
catch(Exception e) {
e.printStackTrace();
return null;
}
finally {
try{
if(in!=null) {
in.close();
}
if(ou!=null) {
ou.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return newFile;
}
public static File copyInputStream(InputStream in, String field) throws Exception{
File newFile = new File(HOME+File.separator+field+".properties");
if(newFile.exists()) {
throw new IOException("The variable file of field "+field+" already exists!");
}
OutputStream ou = new FileOutputStream(newFile);
try {
ou = new FileOutputStream(newFile);
byte[] bytes = new byte[5000];
int read = 0;
int size = in.available();
int hasread = 0;
int step = 0;
while(hasread<size) {
read=0;
step = 5000>(size-hasread)?(size-hasread):5000;
while(read<step) {
read += in.read(bytes, read, step-read);
}
hasread += read;
ou.write(bytes, 0, read);
}
}
catch(Exception e) {
e.printStackTrace();
return null;
}
finally {
try{
if(ou!=null) {
ou.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return newFile;
}
public static File copyProperty(Properties properties, String field) throws Exception{
File newFile = new File(HOME+File.separator+field+".properties");
if(newFile.exists()) {
throw new IOException("The variable file of field "+field+" already exists!");
}
OutputStream ou = new FileOutputStream(newFile);
properties.store(ou, null);
ou.close();
return newFile;
}
private static File copyURL(URL url, String field) throws Exception{
File newFile = new File(HOME+File.separator+field+".properties");
if(newFile.exists()) {
throw new IOException("The variable file of field "+field+" already exists!");
}
InputStream in = null;
OutputStream ou = new FileOutputStream(newFile);
try {
in = url.openStream();
ou = new FileOutputStream(newFile);
byte[] bytes = new byte[5000];
int read = 0;
int size = in.available();
int hasread = 0;
int step = 0;
while(hasread<size) {
read=0;
step = 5000>(size-hasread)?(size-hasread):5000;
while(read<step) {
read += in.read(bytes, read, step-read);
}
hasread += read;
ou.write(bytes, 0, read);
}
}
catch(Exception e) {
e.printStackTrace();
return null;
}
finally {
try{
if(in!=null) {
in.close();
}
if(ou!=null) {
ou.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return newFile;
}
private static Properties openProperty(File file) throws Exception{
InputStream in = new FileInputStream(file);
Properties properties = new Properties();
properties.load(in);
in.close();
return properties;
}
/**
* Get the system property for a give key.
*
* @param key
* the key for the desired string
* @param field
* the field of system viariable Property, null for the system
* variable.
* @return the system property for a give key.
*/
public static String getSystemProperty(String key ) {
if (property == null) {
return null;
} else {
try {
return property.getString(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
/**
* Get the field property for a give key.
*
* @param key
* the key for the desired string
* @param field
* the field of system viariable Property, null for the system
* variable.
* @return the system property for a give key.
*/
public static String getFieldProperty(String key, String field) {
if (property == null) {
return null;
} else {
try {
if (field != null) {
return property.getString(field + "_" + key);
}
return property.getString(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public static String getAllProperty(String key) {
String value = property.getString(key);
if(value!=null) {
return value;
}
for(int i=0;i<fields.length;i++) {
value = property.getString(fields[i] + "_" + key);
if(value!=null) {
return value;
}
}
return null;
}
public static Properties getAllProperties() {
return property.getProperties();
}
/**
* get the property of the specific field
* @param field
* the field of system viariable Property, null for the system
* variable. If the url is from a plug-in, the name should be the
* file name of the plug-in.
* @return the properties of the specific field, null if no properties of this field
* */
public static Properties getFieldProperties(String field) {
if(!hasField(field)) {
return null;
}
Properties prop = new Properties();
String file = HOME+File.separator+field+".properties";
InputStream in = null;
try {
File temp = new File(file);
in = new FileInputStream(temp);
prop.load(in);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return prop;
}
public static boolean hasField(String field) {
int len = fields.length;
for(int i=0;i<len;i++) {
if(fields[i].equals(field)) {
return true;
}
}
return false;
}
public static void Clean() {
if(fields==null) {
return;
}
int len = register.length;
for(int i=0;i<len;i++) {
if(!register[i]) {
String file = HOME+File.separator+fields[i]+".properties";
File deleteFile = new File(file);
deleteFile.delete();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -