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

📄 utils.java.svn-base

📁 this is example use EJB with jboss.
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
		} else {
			return strIPAddress.substring(0, colon);
		}
	}

	/**
	 * Takes an array of tokens and converts into separator-separated string.
	 * 
	 * @param array
	 *            [] The array of strings input.
	 * @param separators
	 *            The string separator.
	 * @return String String A string containing tokens separated by seperator.
	 */
	public static final String arrayToString(String[] array, String separators) {
		StringBuffer sb = new StringBuffer("");
		String empty = "";

		if (array == null){
			return empty;
		}
		
		if (separators == null){
			separators = ",";
		}	
		
		int len = array.length;
		
		for (int ix = 0; ix < len; ix++) {
			if ((array[ix] != null) && (!array[ix].equals(""))) {
				sb.append(array[ix] + separators);
			}
		}
		
		String str = sb.toString();
		
		if (!str.equals("")) {
			str = str.substring(0, (str.length() - separators.length()));
		}
		return str;
	}

	/**
	 * Converts a delimited string into an array of string tokens.
	 * 
	 * @param strValue
	 *            The string separator.
	 * @param separators
	 *            [] The 'separator' separated string.
	 * @return String A string array of the original tokens.
	 */
	public static final String[] stringToArray(String strValue,
			String separators) {
		StringTokenizer tokenizer;
		String[] array = null;
		int count = 0;

		if (strValue == null){
			return array;
		}
		
		if (separators == null){
			separators = ",";
		}
		
		tokenizer = new StringTokenizer(strValue, separators);
		if ((count = tokenizer.countTokens()) <= 0) {
			return array;
		}

		array = new String[count];

		int ix = 0;
		
		while (tokenizer.hasMoreTokens()) {
			array[ix] = tokenizer.nextToken();
			ix++;
		}

		return array;
	}
	
	/**
	 * Parse exception to string
	 * @param e
	 * @return String
	 */
	public static String parseException(Throwable e) {
		String strStackTrace = e.toString() + Constants.LINE_SEP;
		StackTraceElement[] stacktraces = e.getStackTrace();
		for (StackTraceElement trace : stacktraces) {
			strStackTrace += "\tat " + trace.toString() + Constants.LINE_SEP;
		}
		return strStackTrace;
	}

	/**
	 * Get the extension of a file.
	 * @param file
	 * @return String
	 */
	public static String getExtension(File file) {
		if (file == null) {
			return null;
		}
		String ext = null;
		String fileName = file.getName();
		int i = fileName.lastIndexOf('.');

		if ((i > 0) && (i < (fileName.length() - 1))) {
			ext = fileName.substring(i + 1).toLowerCase();
		}
		return ext;
	}
	
	
	/**
	 * Check file exist
	 * 
	 * @param fileName
	 * @return boolean
	 */
	public static boolean isFile(String fileName) {
		try {
			File theFile = new File(fileName);
			// Check if a file exists.
			if (theFile.exists()) {
				return true;
			}
			return false;
		} catch (Exception e) {
			LogWriter.getSNMPLogger().error(e.toString());
			return false;
		}
	}
	
	/**
	 * Check configuration files
	 * 
	 * @return boolean
	 */
	public static boolean checkConfigFiles() {
		String tempPath = null;
	
		tempPath = Setting.getJBossHome() + Constants.SERVER_CONFIG_FILE;
		if (!isFile(tempPath)) {
			LogWriter.getSNMPLogger().fatal("[awgview.server.properties] file does not exist in " + tempPath);
			return false;
		}
	
		tempPath = Setting.getJBossHome() + Constants.ALARM_TYPE_CONFIG_FILE;
		if (!isFile(tempPath)) {
			LogWriter.getSNMPLogger().fatal("[alarm_type.ini] file does not exist in " + tempPath);
			return false;
		}
	
		tempPath = Setting.getJBossHome() + Constants.ALARM_LEVEL_CONFIG_FILE;
		if (!isFile(tempPath)) {
			LogWriter.getSNMPLogger().fatal("[alarm_level.ini] file does not exist in " + tempPath);
			return false;
		}
	
		tempPath = Setting.getJBossHome() + Constants.LOG4J_CONFIG_FILE;
		if (!isFile(tempPath)) {
			LogWriter.getSNMPLogger().fatal("[log4j.properties] file does not exist in " + tempPath);
			return false;
		}
	
		tempPath = Setting.getJBossHome() + Constants.LICENSE_PATH;
		if (!isFile(tempPath)) {
			LogWriter.getSNMPLogger().fatal("[awgview.lic] file does not exist in " + tempPath);
			return false;
		}
	
		return true;
	}

	/**
	 * Check valid license of AWGView
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean checkLicense() throws Exception {
		
		String macAddress = Utils.getMacAddress();
		
		if (macAddress == null) {
			LogWriter.getSNMPLogger().error("Cannot generate license file because getting Mac Address failt. "
							+ "Pls contact with admin.");
			return false;
		}
	
		String genlicense = Encryption.genLicense(macAddress, Encryption.SHA_256) + Constants.LINE_SEP;
	
		String pathLicense = Setting.getJBossHome() + Constants.LICENSE_PATH;
		LogWriter.getSNMPLogger().info("Read license file with path: " + pathLicense);
		
		if (isFile(pathLicense)) {
			String userLicense = readFile2String(pathLicense);
			// LogWriter.getSNMPLogger().debug("userLicense:\t" + userLicense);
			// LogWriter.getSNMPLogger().debug("genlicense:\t" + genlicense);
			if ((genlicense != null) && (genlicense.trim().length() > 0)
					&& (userLicense != null)
					&& (userLicense.trim().length() > 0)
					&& (userLicense.equals(genlicense))) {
				return true;
			}
		} else {
			throw new Exception("Not found license file in " + pathLicense);
		}
		return false;
	}

	/**
	 * Get Mac Address
	 * 
	 * @return String
	 * @throws IOException
	 */
	public final static String getMacAddress() throws IOException {
		
		String os = System.getProperty(OS_NAME);

		try {
			if (os.startsWith(OS_LINUX)) {
				return linuxParseMacAddress(linuxRunIfConfigCommand());
			} else {
				throw new IOException("No support operating system: " + os  + LINE_SEP 
						+ "Only support generation license on Linux Operating System");
			}
		} catch (ParseException ex) {			
			throw new IOException(ex.getMessage());
		}
	}
	
	/**
	 * Parse Mac Address on Linux
	 * 
	 * @param ipConfigResponse
	 * @return String
	 * @throws ParseException
	 */
	private final static String linuxParseMacAddress(String ipConfigResponse) throws ParseException {
		
		StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, LINE_SEP);
		String eth0MacAddress = null;
		
		while (tokenizer.hasMoreTokens()) {
			String line = tokenizer.nextToken().trim();		
			
			// see if line contains eth0 MAC address
			int macEth0AddressPosition = line.indexOf(ETH_0);
			
			if (macEth0AddressPosition < 0){
				continue;
			}else{
				// see if line contains MAC address
				int macAddressPosition = line.indexOf(HARD_WARE_ADDR);
				if (macAddressPosition <= 0){
					continue;
				}
				
				String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
				if (linuxIsMacAddress(macAddressCandidate)) {
					eth0MacAddress = macAddressCandidate;
					// see if line contains IP address
					if (eth0MacAddress != null) {
						return eth0MacAddress;
					}
				}
			}
		}
		
		ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigResponse + "]", 0);		
		throw ex;
	}
	
	/**
	 * Linux run ifconfig command
	 * 
	 * @return String
	 * @throws IOException
	 */
	private final static String linuxRunIfConfigCommand() throws IOException {
		Process p = Runtime.getRuntime().exec(LINUX_IFCONFIG_CMD);
		InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

		StringBuffer buffer = new StringBuffer();
		
		for (;;) {
			int c = stdoutStream.read();
			if (c == -1){
				break;
			}	
			buffer.append((char) c);
		}
		
		String outputText = buffer.toString();

		stdoutStream.close();

		return outputText;
	}

	/**
	 * Check Mac Address on Linux
	 * @param macAddressCandidate
	 * @return boolean
	 */
	private final static boolean linuxIsMacAddress(String macAddressCandidate) {		
		if (macAddressCandidate.length() != 17){
			return false;
		}	
		return true;
	}
}

⌨️ 快捷键说明

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