如何於 java 環境中透過取得系統名稱?(RtvNetA.java)
RtvNetA.java -- Retrieve Network Attributes
於 CLP 中可執行 RTVNETA SYSNAME(&SYSNAME),於 java 需要呼叫 API QWCRNETA 來完成。
RtvNetA.java
/* ==================================================================*/
/* */
/* Program . . : RtvNetA.java */
/* Description : Retrieve Network Attributes */
/* Author . . : Vengoal Chang */
/* Published . : AS400ePaper */
/* Date . . . : May 23, 2014 */
/* */
/* ==================================================================*/
package com.free400.vengoal.as400api;
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Bin4;
import com.ibm.as400.access.AS400Exception;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.CharConverter;
import com.ibm.as400.access.ErrorCodeParameter;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;
public class RtvNetA {
private AS400 as400;
private final AS400Bin4 intConverter_ = new AS400Bin4();
public static final String NETATR_ALRBCKFP = "ALRBCKFP";
public static final String NETATR_ALRCTLD = "ALRCTLD";
public static final String NETATR_ALRDFTFP = "ALRDFTFP";
public static final String NETATR_ALRFTR = "ALRFTR";
public static final String NETATR_ALRHLDCNT = "ALRHLDCNT";
public static final String NETATR_ALRLOGSTS = "ALRLOGSTS";
public static final String NETATR_ALRPRIFP = "ALRPRIFP";
public static final String NETATR_ALRRQSFP = "ALRRQSFP";
public static final String NETATR_ALRSTS = "ALRSTS";
public static final String NETATR_ALWADDCLU = "ALWADDCLU";
public static final String NETATR_ALWANYNET = "ALWANYNET";
public static final String NETATR_ALWHPRTWR = "ALWHPRTWR";
public static final String NETATR_ALWVRTAPPN = "ALWVRTAPPN";
public static final String NETATR_VRTAUTODEV = "VRTAUTODEV";
public static final String NETATR_DDMACC = "DDMACC";
public static final String NETATR_DFTCNNLST = "DFTCNNLST";
public static final String NETATR_DFTMODE = "DFTMODE";
public static final String NETATR_DFTNETTYPE = "DFTNETTYPE";
public static final String NETATR_DTACPR = "DTACPR";
public static final String NETATR_DTACPRINM = "DTACPRINM";
public static final String NETATR_HPRPTHTMR = "HPRPTHTMR";
public static final String NETATR_JOBACN = "JOBACN";
public static final String NETATR_LCLCPNAME = "LCLCPNAME";
public static final String NETATR_LCLLOCNAME = "LCLLOCNAME";
public static final String NETATR_LCLNETID = "LCLNETID";
public static final String NETATR_MAXINTSSN = "MAXINTSSN";
public static final String NETATR_MAXHOP = "MAXHOP";
public static final String NETATR_MDMCNTRYID = "MDMCNTRYID";
public static final String NETATR_MSGQ = "MSGQ";
public static final String NETATR_NETSERVER = "NETSERVER";
public static final String NETATR_NODETYPE = "NODETYPE";
public static final String NETATR_NWSDOMAIN = "NWSDOMAIN";
public static final String NETATR_OUTQ = "OUTQ";
public static final String NETATR_PNDSYSNAME = "PNDSYSNAME";
public static final String NETATR_PCSACC = "PCSACC";
public static final String NETATR_RAR = "RAR";
public static final String NETATR_SYSNAME = "SYSNAME";
public RtvNetA(AS400 as400){
this.as400 = as400;
}
public String getNetAtr(String attribute) throws Exception{
String[] atrs = new String[1];
atrs[0] = attribute;
String[] rtnAtrs = getNetAtr(atrs);
return rtnAtrs[0];
}
public String[] getNetAtr(String[] attributes) throws Exception {
final CharConverter charConverter = new CharConverter(as400.getCcsid(), as400);
String[] rtnStr = new String[attributes.length];
ProgramParameter[] parameters = buildProgramParameters(attributes);
ProgramCall rtvNetAtr = new ProgramCall(as400, "/QSYS.LIB/QWCRNETA.PGM", parameters);
if (rtvNetAtr.run() != true) {
throw new AS400Exception(rtvNetAtr.getMessageList());
} else {
byte[] rcvVar = parameters[0].getOutputData();
Integer nbrOfAtrRtn = (Integer) intConverter_.toObject(rcvVar, 0);
int offsetOfNetAtrInfoTable = 0;
int offset;
String netAtr, typeOfData, infoStatus;
int lenOfData;
Object atrValue = null;
for (int i = 0; i < nbrOfAtrRtn; i++) {
offsetOfNetAtrInfoTable += 4;
offset = intConverter_.toInt(rcvVar, offsetOfNetAtrInfoTable);
netAtr = charConverter.byteArrayToString(rcvVar, offset, 10);
offset += 10;
typeOfData = charConverter.byteArrayToString(rcvVar, offset, 1);
offset += 1;
infoStatus = charConverter.byteArrayToString(rcvVar, offset, 1);
offset += 1;
lenOfData = intConverter_.toInt(rcvVar, offset);
offset += 4;
if (typeOfData.equalsIgnoreCase("C")) {
atrValue = charConverter.byteArrayToString(rcvVar, offset, lenOfData).trim();
rtnStr[i] = (String) atrValue;
} else if (typeOfData.equalsIgnoreCase("B")) {
atrValue = intConverter_.toInt(rcvVar, offset);
rtnStr[i] = Integer.toString((Integer) atrValue);
}
// System.out.println(netAtr + "=" + atrValue);
}
}
return rtnStr;
}
private ProgramParameter[] buildProgramParameters(String[] attributes) {
ProgramParameter[] parameterList = new ProgramParameter[5];
parameterList[0] = new ProgramParameter(2048);
parameterList[1] = new ProgramParameter(intConverter_.toBytes(2048));
parameterList[2] = new ProgramParameter(intConverter_.toBytes(attributes.length));
AS400Text text10 = new AS400Text(10, as400);
byte[] atrNameBytes = new byte[attributes.length * 10];
int atrNameOffset = 0;
for (int i = 0; i < attributes.length; i++) {
byte[] atrName = text10.toBytes(attributes[i]);
System.arraycopy(atrName, 0, atrNameBytes, atrNameOffset, 10);
atrNameOffset += 10;
}
parameterList[3] = new ProgramParameter(atrNameBytes);
parameterList[4] = new ErrorCodeParameter();
return parameterList;
}
public static void main(String[] args) {
try {
AS400 as400 = new AS400("AS400IP", "USER", "PASS");
RtvNetA rtvNetA = new RtvNetA(as400);
String[] atrName = { RtvNetA.NETATR_SYSNAME };
String[] rtnValues = rtvNetA.getNetAtr(atrName);
for (int i = 0; i < rtnValues.length; i++) {
System.out.println(atrName[i] + "=" + rtnValues[i]);
}
atrName[0] = RtvNetA.NETATR_MAXINTSSN;
rtnValues = rtvNetA.getNetAtr(atrName);
for (int i = 0; i < rtnValues.length; i++) {
System.out.println(atrName[i] + "=" + rtnValues[i]);
}
String[] atrName2 = { RtvNetA.NETATR_DDMACC, RtvNetA.NETATR_MAXHOP };
rtnValues = rtvNetA.getNetAtr(atrName2);
for (int i = 0; i < rtnValues.length; i++) {
System.out.println(atrName2[i] + "=" + rtnValues[i]);
}
System.out.println(NETATR_LCLNETID+ "=" + rtvNetA.getNetAtr(NETATR_LCLNETID));
as400.disconnectAllServices();
System.exit(0);
} catch (Exception excp) {
excp.printStackTrace();
}
}
}
A blog about IBM i (AS/400), MQ and other things developers or Admins need to know.
星期四, 11月 09, 2023
2014-05-23 2014-05-12 如何於 java 環境中透過取得系統名稱?(RtvNetA.java)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言