Core
piSmartService
package com.PowerIntegral;
import java.io.FileInputStream;
import java.util.*;
import javax.naming.Context;
/**
* <p>
* Title: piSmartService
* </p>
*
* <p>
* Description: piSmartService: 公用函式集
* </p>
*
*/
public class piSmartService {
private boolean _OK = false;
private String _ErrorMessage = "", _CallerInfo = "PowerIntegral";
private piLog _piLog = null;
public piValueList _piValueList = null;
public piBase64 _piBase64 = null;
private piDatabase _piDatabase = null;
private piJNDI _piJNDI = null;
private com.PowerIntegral.piSmartUserService _piSmartUserService = null;
private java.sql.Connection _jdbcConnection = null;
/**
* piSmartService: 建構子
*
* @param Log
* piLog => 日誌物件
*/
public piSmartService(piLog Log) {
if (Log == null) {
this._piLog = new piLog();
} else {
this._piLog = Log;
}
this.Init(this._piLog);
}
/**
* piSmartService: 建構子
*
* @param Log
* piLog => 日誌物件
* @param CallerInfo
* String => 調用者訊息
*/
public piSmartService(piLog Log, String CallerInfo) {
if (Log == null) {
this._piLog = new piLog();
} else {
this._piLog = Log;
}
this._CallerInfo = CallerInfo;
this.Init(this._piLog);
}
/**
* piSmartService: 建構子
*
* @param MyLogProperties
* Properties => 已預載 Log4J 設定屬性物件
*/
public piSmartService(java.util.Properties MyLogProperties) {
this._piLog = new piLog(MyLogProperties);
this.Init(this._piLog, MyLogProperties);
}
/**
* piSmartService: 建構子
*
* @param MyLogProperties
* Properties => 已預載 Log4J 設定屬性物件
* @param CallerInfo
* String => 調用者訊息
*/
public piSmartService(java.util.Properties MyLogProperties,
String CallerInfo) {
this._piLog = new piLog(MyLogProperties);
this._CallerInfo = CallerInfo;
this.Init(this._piLog, MyLogProperties);
}
/**
* Init: 初始物件, 預設 Base64 編碼為 UTF8
*
* @param Log
* piLog => 日誌物件
*/
private void Init(piLog Log, java.util.Properties MyLogProperties) {
this._piBase64 = new piBase64(MyLogProperties);
this._piLog.LogData.ModuleName = this.toString();
this._piValueList = new piValueList();
java.util.Properties _Configuration = new java.util.Properties();
_Configuration.put(Context.INITIAL_CONTEXT_FACTORY, MyLogProperties
.getProperty("INITIAL_CONTEXT_FACTORY"));
_Configuration.put(Context.PROVIDER_URL, MyLogProperties
.getProperty("PROVIDER_URL"));
_Configuration.put(Context.SECURITY_PRINCIPAL, MyLogProperties
.getProperty("SECURITY_PRINCIPAL"));
_Configuration.put(Context.SECURITY_CREDENTIALS, MyLogProperties
.getProperty("SECURITY_CREDENTIALS"));
String _CallerInfoTemp = MyLogProperties.getProperty("CallerInfo");
if (_CallerInfoTemp != null)
this._CallerInfo = _CallerInfoTemp;
this._piJNDI = new piJNDI(_Configuration);
try {
String _jdbcURL = MyLogProperties.getProperty("jdbcURL");
String _jdbcDriver = MyLogProperties.getProperty("jdbcDriver");
String jdbcUID = MyLogProperties.getProperty("jdbcUID");
String _jdbcPWD = MyLogProperties.getProperty("jdbcPWD");
this._piDatabase = new piDatabase();
this._piDatabase.setDbUrl(_jdbcURL);
this._piDatabase.setDbDriver(_jdbcDriver);
this._piDatabase.setDbUid(jdbcUID);
if (_jdbcPWD != null && !_jdbcPWD.equals("")) {
this._piDatabase.setDbPwd(_jdbcPWD);
}
this._jdbcConnection = this._piDatabase.OpenDatabase();
} catch (Exception ex) {
this._piDatabase = null;
this._jdbcConnection = null;
}
}
/**
* Init: 初始物件, 預設 base64 編碼為 UTF8
*
* @param Log
* piLog => 日誌物件
*/
private void Init(piLog Log) {
this._piBase64 = new piBase64(Log.getConfiguration());
this._piLog.LogData.ModuleName = this.toString();
this._piValueList = new piValueList();
java.util.Properties _Configuration = new java.util.Properties();
java.util.Properties _Prop = Log.getConfiguration();
_Configuration.put(Context.INITIAL_CONTEXT_FACTORY, _Prop
.getProperty("INITIAL_CONTEXT_FACTORY"));
_Configuration.put(Context.PROVIDER_URL, _Prop
.getProperty("PROVIDER_URL"));
_Configuration.put(Context.SECURITY_PRINCIPAL, _Prop
.getProperty("SECURITY_PRINCIPAL"));
_Configuration.put(Context.SECURITY_CREDENTIALS, _Prop
.getProperty("SECURITY_CREDENTIALS"));
String _CallerInfoTemp = _Configuration.getProperty("CallerInfo");
if (_CallerInfoTemp != null)
this._CallerInfo = _CallerInfoTemp;
this._piJNDI = new piJNDI(_Configuration);
this._piDatabase = new piDatabase();
try {
String _jdbcURL = _Prop.getProperty("jdbcURL");
String _jdbcDriver = _Prop.getProperty("jdbcDriver");
String jdbcUID = _Prop.getProperty("jdbcUID");
String _jdbcPWD = _Prop.getProperty("jdbcPWD");
this._piDatabase = new piDatabase();
this._piDatabase.setDbUrl(_jdbcURL);
this._piDatabase.setDbDriver(_jdbcDriver);
this._piDatabase.setDbUid(jdbcUID);
if (_jdbcPWD != null && !_jdbcPWD.equals("")) {
this._piDatabase.setDbPwd(_jdbcPWD);
}
this._jdbcConnection = this._piDatabase.OpenDatabase();
} catch (Exception ex) {
this._piDatabase = null;
this._jdbcConnection = null;
}
}
/**
* getSuccess: 取得最近函式調用狀態
*
* @return boolean
*/
public boolean getSuccess() {
return this._OK;
}
/**
* getErrorMessage: 取得最近函式調用錯誤訊息
*
* @return String
*/
public String getErrorMessage() {
return this._ErrorMessage;
}
/**
* getJNDI: 取得目錄服務物件
*
* @return com.PowerIntegral.piJNDI
*/
public piJNDI getJNDI() {
return this._piJNDI;
}
/**
* getLog: 取得日誌物件
*
* @return com.PowerIntegral.piLog
*/
public piLog getLog() {
return this._piLog;
}
/**
* getDatabase: 取得資料庫物件
*
* @return com.PowerIntegral.piDatabase
*/
public piDatabase getDatabase() {
return this._piDatabase;
}
/**
* getCallerInfo: 取得調用者訊息
*
* @return String
*/
public String getCallerInfo() {
return this._CallerInfo;
}
/**
* setCallerInfo: 設定調用者訊息
*/
public void setCallerInfo(String CallerInfo) {
this._CallerInfo = CallerInfo;
}
/**
* Now: 取得日期時間
*
* @return String
*/
public String Now() {
return this.runService(0, "", false, false, this._CallerInfo);
}
/**
* Echo: 取得回應
*
* @param TaskParam
* String => 回應值, 需經 Base64 編碼
* @return String => 需經 Base64 解碼
*/
public String Echo(String TaskParam) {
return this.runService(1, TaskParam, true, true, this._CallerInfo);
}
/**
* runShell: 執行外部程式 => 伺服器作業命令, 需經 Base64 編碼
*
* @param shellCmd
* String
* @return String
*/
public String runShell(String shellCmd) {
return this.runService(2, shellCmd, true, false, this._CallerInfo);
}
/**
* getURLContent: 取得網頁內容
*
* @param URL
* String => 網址, 需經 Base64 編碼
* @return String => 需經 Base64 解碼
*/
public String getURLContent(String URL) {
return this.runService(200, URL, true, true, this._CallerInfo);
}
/**
* saveURLContent: 下載網頁檔案
*
* @param URL
* String => 網址, 需經 Base64 編碼
* @param savedFolder
* String => 伺服器存檔目錄, 需經 Base64 編碼
* @return String
*/
public String saveURLContent(String URL, String savedFolder) {
return this.runService(201, "URL=" + this._piBase64.getFromBase64(URL)
+ "\tFOLDER=" + this._piBase64.getFromBase64(savedFolder)
+ "\n", false, false, this._CallerInfo);
}
/**
* getLDAPEntryValue: 取得 LDAP 屬性值
*
* @param ldapEntry
* String => LDAP 描述子
* @param AttributeName
* String => 屬性名
* @return String
*/
public String getLDAPEntryValue(String ldapEntry, String AttributeName) {
return this.runService(100, "ldapEntry=" + ldapEntry
+ "\tldapAttributeName=" + AttributeName, false, true,
this._CallerInfo);
}
/**
* searchLDAPEntries: 搜尋 LDAP 屬性物件
*
* @param ldapSearchBase
* String => LDAP 描述子
* @param ldapSeachFilter
* String => LDAP 搜尋條件
* @return String
*/
public String searchLDAPEntries(String ldapSearchBase,
String ldapSeachFilter) {
return this.runService(101, "ldapSearchBase=" + ldapSearchBase
+ "\tldapSearchFilter=" + ldapSeachFilter, false, true,
this._CallerInfo);
}
public boolean changeLDAPPassword(String ldapEntry, String ldapNewPassword) {
String _TaskResult = this.runService(102, "ldapEntry=" + ldapEntry
+ "\tldapNewPassword=" + ldapNewPassword, false, false,
this._CallerInfo);
this._OK = (_TaskResult.equals("1")) ? true : false;
return this._OK;
}
/**
* CallDynamicClassFunction: 動態調用 Class.CallUserFunction
*
* @param DynamicClassName
* String => Class 名
* @param TaskID
* int => 功能代號
* @param TaskParam
* String => 引數
* @param CallerInfo
* String => 調用者訊息
* @return String
*/
public String CallDynamicClassFunction(String DynamicClassName, int TaskID,
String TaskParam, String CallerInfo) {
return this.runService(300, "ClassName=" + DynamicClassName
+ "\tTaskID=" + String.valueOf(TaskID) + "\tTaskParam="
+ this._piBase64.getFromBase64(TaskParam), false, true,
CallerInfo);
}
/**
* CallDynamicClassFunction: 動態調用 Class.CallUserFunction
*
* @param DynamicClassName
* String => Class 名
* @param TaskID
* int => 功能代號
* @param TaskParam
* String => 引數
* @return String
*/
public String CallDynamicClassFunction(String DynamicClassName, int TaskID,
String TaskParam) {
return this.CallDynamicClassFunction(DynamicClassName, TaskID,
TaskParam, this._CallerInfo);
}
/**
* _InitStatus: 初始最近函式調用值
*/
private void _InitStatus() {
this._OK = false;
this._ErrorMessage = "";
}
/**
* setUserService: 指定擴充功能
*
* @param SmartUserService
* piSmartUserService =>
* @param CallerInfo
* String => 調用者訊息
*/
protected void setUserService(
com.PowerIntegral.piSmartUserService SmartUserService,
String CallerInfo) {
if (SmartUserService == null) {
this._piSmartUserService = new com.PowerIntegral.piSmartUserService(
this._piBase64, this._piValueList, this._piDatabase,
this._piJNDI, this._piLog, CallerInfo);
} else {
this._piSmartUserService = SmartUserService;
}
}
/**
* setUserService: 指定擴充功能
*
* @param SmartUserService
* piSmartUserService
*/
protected void setUserService(
com.PowerIntegral.piSmartUserService SmartUserService) {
if (SmartUserService == null) {
this._piSmartUserService = new com.PowerIntegral.piSmartUserService(
this._piBase64, this._piValueList, this._piDatabase,
this._piJNDI, this._piLog, this._CallerInfo);
} else {
this._piSmartUserService = SmartUserService;
}
}
/**
* UserService: 擴充功能
*
* @param TaskID
* int => 功能代號
* @param TaskParam
* String => 引數
* @param CallerInfo
* String => 調用者訊息
* @return String
*/
protected String UserService(int TaskID, String TaskParam, String CallerInfo) {
if (this._piSmartUserService == null) {
this.setUserService(null);
}
String _TaskResult = this._piSmartUserService.CallUserFunction(TaskID,
TaskParam, CallerInfo);
this._OK = this._piSmartUserService.getSuccess();
this._ErrorMessage = this._piSmartUserService.getErrorMessage();
return _TaskResult;
}
/**
* runService: 執行功能
*
* @param TaskID
* int => 功能代號
* @param TaskParam
* String => 引數
* @param TaskParamIsBase64
* boolean => 引數是否為 Base64 編碼
* @param TaskResultWantBase64
* boolean => 返回值是否需要 Base64 編碼
* @param CallerInfo
* String => 調用者訊息
* @return String
*/
public String runService(int TaskID, String TaskParam,
boolean TaskParamIsBase64, boolean TaskResultWantBase64,
String CallerInfo) {
this._InitStatus();
String _TaskID = "", _TaskParam = "", _TaskResult = "", _CallerInfo = CallerInfo, _Now = piDateTime
.DateTimeString();
this._piLog.Debug(10000, CallerInfo + ":" + _Now);
this._piLog.Debug(10000, CallerInfo + "\tID:" + TaskID);
if (!TaskParam.equals("")) {
this._piLog.Debug(10000, CallerInfo + "\tParam:" + TaskParam);
}
if (TaskParamIsBase64 && !TaskParam.equals("")) {
_TaskParam = this._piBase64.getFromBase64(TaskParam);
this._piLog.Debug(10000, CallerInfo + "\tParam:" + _TaskParam);
} else {
_TaskParam = TaskParam;
}
com.PowerIntegral.piSmartDownload _piSmartDownload = null;
switch (TaskID) {
case 0:
_TaskResult = _Now;
this._ErrorMessage = "";
this._OK = true;
break;
case 1:
_TaskResult = _TaskParam;
this._ErrorMessage = "";
this._OK = true;
break;
case 2:
try {
Process _Process = Runtime.getRuntime().exec(_TaskParam);
_TaskResult = String.valueOf(_Process.exitValue());
_TaskResult = "1";
this._OK = true;
} catch (Exception ex) {
this._piLog.Error(90000, CallerInfo + "\tResult:"
+ ex.getMessage());
_TaskResult = "0";
}
break;
case 100:
_TaskResult = "";
if (this._piValueList.Load(_TaskParam, "\n", "\t")) {
String _ldapEntry = (String) this._piValueList.getField(0,
"ldapEntry");
String _ldapAttributeName = (String) this._piValueList
.getField(0, "ldapAttributeName");
this._piLog.Debug(10100, "\tCallerInfo=" + CallerInfo);
this._piLog.Debug(10100, "\tldapEntry=" + _ldapEntry);
this._piLog.Debug(10100, "\tldapAttributeName="
+ _ldapAttributeName);
javax.naming.directory.Attributes _Attributes = null;
String _AttributeValue = "";
_Attributes = this._piJNDI.getAtributes(_ldapEntry);
if (_Attributes != null) {
_AttributeValue = this._piJNDI.getAttributeValue(
_Attributes, _ldapAttributeName);
_TaskResult = _AttributeValue;
}
this._OK = true;
}
break;
case 101:
_TaskResult = "";
if (this._piValueList.Load(_TaskParam, "\n", "\t")) {
String _ldapSearchBase = (String) this._piValueList.getField(0,
"ldapSearchBase");
String _ldapAttributeFilter = (String) this._piValueList
.getField(0, "ldapSearchFilter");
this._piLog.Debug(10101, "\tCallerInfo=" + CallerInfo);
this._piLog.Debug(10101, "\tldapSearchBase:" + _ldapSearchBase);
this._piLog.Debug(10101, "\tldapSearchFilter:"
+ _ldapAttributeFilter);
ArrayList _SearchResult = this._piJNDI.SearchEntries(
_ldapSearchBase, _ldapAttributeFilter);
StringBuffer _Result = new StringBuffer();
if (_SearchResult != null && _SearchResult.size() != 0) {
for (int i = 0; i < _SearchResult.size(); i++) {
_Result.append((String) _SearchResult.get(i) + "\n");
}
_TaskResult = _Result.toString();
}
this._OK = true;
}
break;
case 102:
_TaskResult = "";
if (this._piValueList.Load(_TaskParam, "\n", "\t")) {
String _ldapEntry = (String) this._piValueList.getField(0,
"ldapEntry");
String _ldapNewPassword = (String) this._piValueList.getField(
0, "ldapNewPassword");
this._piLog.Debug(10102, "\tCallerInfo=" + CallerInfo);
this._piLog.Debug(10102, "\tldapEntry=" + _ldapEntry);
this._piLog.Debug(10102, "\tldapNewPassword="
+ _ldapNewPassword);
this._OK = this._piJNDI.setAttributes(_ldapEntry,
"userpassword", _ldapNewPassword, 0);
this._ErrorMessage = this._piJNDI.ErrorMessage();
_TaskResult = (this._OK) ? "1" : "0";
}
break;
case 200:
_piSmartDownload = new com.PowerIntegral.piSmartDownload();
_TaskResult = _piSmartDownload.Download(_TaskParam);
this._ErrorMessage = _piSmartDownload.getErrorMessage();
this._OK = _piSmartDownload.getSuccess();
break;
case 201:
_piSmartDownload = new com.PowerIntegral.piSmartDownload();
_TaskResult = "";
if (this._piValueList.Load(_TaskParam, "\n", "\t")) {
String _URL = (String) this._piValueList.getField(0, "URL");
String _Folder = (String) this._piValueList.getField(0,
"FOLDER");
this._piLog.Debug(10201, CallerInfo + "\tURL:" + _URL);
this._piLog.Debug(10201, CallerInfo + "\tFOLDER:" + _Folder);
if (_piSmartDownload.Download(_URL, _Folder)) {
_TaskResult = (new Long(_piSmartDownload.getByteRead()))
.toString();
}
this._ErrorMessage = _piSmartDownload.getErrorMessage();
this._OK = _piSmartDownload.getSuccess();
}
break;
case 300: // piLog
if (this._piValueList.Load(TaskParam, "\n", "\t")) {
String _ClassName = (String) this._piValueList.getField(0,
"ClassName");
_TaskID = (String) this._piValueList.getField(0, "TaskID");
_TaskParam = (String) this._piValueList
.getField(0, "TaskParam");
_CallerInfo = (String) this._piValueList.getField(0,
"CallerInfo");
if (_CallerInfo == null)
_CallerInfo = CallerInfo;
this._piLog.Debug(10300, "\tCallerInfo=" + _CallerInfo);
this._piLog.Debug(10300, "\tClassName:" + _ClassName);
this._piLog.Debug(10300, "\tTaskID:" + _TaskID);
this._piLog.Debug(10300, "\tTaskParam:" + _TaskParam);
Class[] _ClassParams = { String.class, String.class,
com.PowerIntegral.piLog.class, String.class };
Object[] _MethodParams = { _TaskID, _TaskParam, this._piLog,
_CallerInfo };
try {
if ((_TaskResult = (String) piClassLoader.InvokeMethod2(
_ClassName, "CallUserFunction", _ClassParams,
_MethodParams)) == null) {
_TaskResult = "";
} else {
this._OK = true;
}
} catch (Exception ex) {
this._ErrorMessage = ex.getMessage();
}
}
break;
case 301: // piJNDI
if (this._piValueList.Load(TaskParam, "\n", "\t")) {
String _ClassName = (String) this._piValueList.getField(0,
"ClassName");
_TaskID = (String) this._piValueList.getField(0, "TaskID");
_TaskParam = (String) this._piValueList
.getField(0, "TaskParam");
_CallerInfo = (String) this._piValueList.getField(0,
"CallerInfo");
if (_CallerInfo == null)
_CallerInfo = CallerInfo;
this._piLog.Debug(10301, "\tCallerInfo=" + _CallerInfo);
this._piLog.Debug(10301, "\tClassName:" + _ClassName);
this._piLog.Debug(10301, "\tTaskID:" + _TaskID);
this._piLog.Debug(10301, "\tTaskParam:" + _TaskParam);
Class[] _ClassParams = { String.class, String.class,
com.PowerIntegral.piJNDI.class,
com.PowerIntegral.piLog.class, String.class };
Object[] _MethodParams = { _TaskID, _TaskParam, this._piJNDI,
this._piLog, _CallerInfo };
try {
if ((_TaskResult = (String) piClassLoader.InvokeMethod2(
_ClassName, "CallUserFunction", _ClassParams,
_MethodParams)) == null) {
_TaskResult = "";
} else {
this._OK = true;
}
} catch (Exception ex) {
this._ErrorMessage = ex.getMessage();
}
}
break;
case 302: // java.sql.Connection
if (this._piValueList.Load(TaskParam, "\n", "\t")) {
String _ClassName = (String) this._piValueList.getField(0,
"ClassName");
_TaskID = (String) this._piValueList.getField(0, "TaskID");
_TaskParam = (String) this._piValueList
.getField(0, "TaskParam");
_CallerInfo = (String) this._piValueList.getField(0,
"CallerInfo");
if (_CallerInfo == null)
_CallerInfo = CallerInfo;
this._piLog.Debug(10302, "\tCallerInfo=" + _CallerInfo);
this._piLog.Debug(10302, "\tClassName:" + _ClassName);
this._piLog.Debug(10302, "\tTaskID:" + _TaskID);
this._piLog.Debug(10302, "\tTaskParam:" + _TaskParam);
try {
Class[] _ClassParams = { String.class, String.class,
java.sql.Connection.class,
com.PowerIntegral.piLog.class, String.class };
Object[] _MethodParams = { _TaskID, _TaskParam,
this._jdbcConnection, this._piLog, _CallerInfo };
if ((_TaskResult = (String) piClassLoader.InvokeMethod2(
_ClassName, "CallUserFunction", _ClassParams,
_MethodParams)) == null) {
_TaskResult = "";
} else {
this._OK = true;
}
} catch (Exception ex) {
this._ErrorMessage = ex.getMessage();
}
}
break;
default:
_TaskResult = this.UserService(TaskID, _TaskParam, CallerInfo);
}
this._piLog.Debug(10000, CallerInfo + "\tResult:" + _TaskResult);
if (TaskResultWantBase64) {
_TaskResult = this._piBase64.getBase64(_TaskResult);
this._piLog.Debug(10000, CallerInfo + "\tResult:" + _TaskResult);
}
_Now = piDateTime.DateTimeString();
this._piLog.Debug(10000, CallerInfo + "\tDone:" + _Now);
return _TaskResult;
}
/**
* runService: 執行功能
*
* @param TaskID
* int => 功能代號
* @param TaskParam
* String => 引數
* @param TaskParamIsBase64
* boolean => 引數是否為 Base64 編碼
* @param TaskResultWantBase64
* boolean => 返回值是否需要 Base64 編碼
* @return String
*/
public String runService(int TaskID, String TaskParam,
boolean TaskParamIsBase64, boolean TaskResultWantBase64) {
return this.runService(TaskID, TaskParam, TaskParamIsBase64,
TaskResultWantBase64, this._CallerInfo);
}
}