PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jtopen-7.8/src/com/ibm/as400/access/JavaProgram.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 667 lines | 392 code | 48 blank | 227 comment | 40 complexity | 81920da70913b02095d311acf277d573 MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // JTOpen (IBM Toolbox for Java - OSS version)
  4. //
  5. // Filename: JavaProgram.java
  6. //
  7. // The source code contained herein is licensed under the IBM Public License
  8. // Version 1.0, which has been approved by the Open Source Initiative.
  9. // Copyright (C) 1997-2003 International Business Machines Corporation and
  10. // others. All rights reserved.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. package com.ibm.as400.access;
  14. import java.io.IOException;
  15. import java.io.Serializable;
  16. import java.io.UnsupportedEncodingException;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19. /**
  20. Represents an IBM i Java program. This class is supported
  21. only when connecting to systems running IBM i V5R1 or higher, and is not supported beyond IBM i 7.1.
  22. <p>
  23. In the context of this discussion, a "Java program" is the IBM i executable object that is created when the CRTJVAPGM (Create Java Program) CL command is run against a class, JAR, or ZIP file.
  24. <br>
  25. Using the JavaProgram class, you can obtain the following information about an IBM i Java program:
  26. <ul>
  27. <li>Adopted authority profile</li>
  28. <li>File change date</li>
  29. <li>File owner</li>
  30. <li>Java program creation date</li>
  31. <li>Release program was created for</li>
  32. <li>Licensed Internal Code options</li>
  33. <li>Number of attached java programs</li>
  34. <li>Number of classes</li>
  35. <li>Number of classes with current java programs</li>
  36. <li>Number of classes without current java programs</li>
  37. <li>Number of classes with errors</li>
  38. <li>Optimization level</li>
  39. <li>Path used</li>
  40. <li>Performance Collection Enabled flag</li>
  41. <li>Performance Collection type</li>
  42. <li>Profiling data status</li>
  43. <li>Size of attached java programs</li>
  44. <li>Use adopted authority</li>
  45. </ul>
  46. <br>
  47. <br>
  48. An Example using the JavaProgram class:
  49. <br>
  50. <blockquote><pre>
  51. // Create a JavaProgram object to refer to a specific Java program.
  52. AS400 system = new AS400("MYSYSTEM", "MYUSERID", "MYPASSWORD");
  53. JavaProgram javaProgram = new JavaProgram(system, "/home/mydir/HelloWorld.class");
  54. <br>
  55. // Get the optimization.
  56. int optimization = javaProgram.getOptimizationLevel();
  57. <br>
  58. // Get the file owner.
  59. String owner = javaProgram.getFileOwner();
  60. </pre></blockquote>
  61. **/
  62. public class JavaProgram implements Serializable
  63. {
  64. static final long serialVersionUID = -209990140140936884L;
  65. private boolean loaded_ = false; // Have we retrieved values from the system yet?
  66. private AS400 system_;
  67. private String path_;
  68. private String fileOwner_;
  69. private Date fileChangeDate_;
  70. private Date javaProgramCreationDate_;
  71. private int numberOfAttachedPrograms_;
  72. private int numberOfClassesWithJavaPrograms_;
  73. private int numberOfClassesWithoutPrograms_;
  74. private int numberOfClassesWithErrors_;
  75. private int numberOfClasses_;
  76. private int optimizationLevel_;
  77. private String performanceCollectionEnabledFlag_;
  78. private String performanceCollectionType_;
  79. private boolean useAdoptedAuthority_;
  80. private String adoptedAuthorityProfile_;
  81. private int sizeOfAttachedPrograms_;
  82. private String javaProgramVersion_;
  83. private String profilingDataStatus_;
  84. private String LICoptions_;
  85. /**
  86. Constant indicating that the profile to use when the use adopted authority field is set is *USER.
  87. **/
  88. public static final String ADOPTED_AUTHORITY_PROFILE_USER = "*USER";
  89. /**
  90. Constant indicating that the profile to use when the use adopted authority field is set is *OWNER.
  91. **/
  92. public static final String ADOPTED_AUTHORITY_PROFILE_OWNER = "*OWNER";
  93. /**
  94. Constant indicating the type of performance collection is *ENTRYEXIT.
  95. **/
  96. public static final String PERFORMANCE_COLLECTION_TYPE_ENTRYEXIT = "*ENTRYEXIT";
  97. /**
  98. Constant indicating the type of performance collection is *FULL.
  99. **/
  100. public static final String PERFORMANCE_COLLECTION_TYPE_FULL = "*FULL";
  101. /**
  102. Constant indicating that profile data collection is not enabled for the the Java program(s).
  103. **/
  104. public static final String PROFILING_DATA_STATUS_NOCOL = "*NOCOL";
  105. /**
  106. Constant indicating that profile data collection is enabled for the attached Java program(s).
  107. **/
  108. public static final String PROFILING_DATA_STATUS_COL = "*COL";
  109. /**
  110. Constant indicating that profile data has been applied to the attached Java program(s).
  111. **/
  112. public static final String PROFILING_DATA_STATUS_APY = "*APY";
  113. /**
  114. Creates a JavaProgram
  115. **/
  116. public JavaProgram()
  117. {
  118. }
  119. /**
  120. Creates a JavaProgram
  121. @param system The system.
  122. @param path The path. This can specify any class, jar, or zip file.
  123. **/
  124. public JavaProgram(AS400 system, String path)
  125. {
  126. setSystem(system);
  127. setPath(path);
  128. }
  129. private void checkVRM()
  130. throws UnsupportedOperationException
  131. {
  132. try
  133. {
  134. // See if the system VRM is higher than IBM i 7.1.
  135. if (getSystem() != null && getSystem().getVRM() > 0x00070100) {
  136. Trace.log(Trace.ERROR, "JavaProgram is not supported beyond IBM i 7.1.");
  137. throw new UnsupportedOperationException("JavaProgram");
  138. }
  139. }
  140. catch (UnsupportedOperationException e) { throw e; }
  141. catch (Exception e) {
  142. if (Trace.isTraceOn())
  143. Trace.log(Trace.ERROR, "Error when checking system VRM.", e);
  144. throw new UnsupportedOperationException(e.getMessage());
  145. }
  146. }
  147. /**
  148. Returns the name of the system.
  149. @return system name
  150. **/
  151. public AS400 getSystem()
  152. {
  153. return system_;
  154. }
  155. /**
  156. Returns the path to a class, jar, or zip file used to create the Java program.
  157. @return path
  158. **/
  159. public String getPath()
  160. {
  161. return path_;
  162. }
  163. /**
  164. Returns the profile to use when the "Use Adopted Authority" field is set.
  165. @return the profile to use. Possible values are:
  166. <ul>
  167. <li>{@link #ADOPTED_AUTHORITY_PROFILE_USER ADOPTED_AUTHORITY_PROFILE_USER}
  168. <li>{@link #ADOPTED_AUTHORITY_PROFILE_OWNER ADOPTED_AUTHORITY_PROFILE_OWNER}
  169. </ul>
  170. **/
  171. public String getAdoptedAuthorityProfile()
  172. throws AS400Exception,
  173. AS400SecurityException,
  174. ErrorCompletingRequestException,
  175. InterruptedException,
  176. IOException,
  177. ObjectDoesNotExistException
  178. {
  179. if (!loaded_) refresh();
  180. if(adoptedAuthorityProfile_.equals("0"))
  181. return ADOPTED_AUTHORITY_PROFILE_USER;
  182. else
  183. return ADOPTED_AUTHORITY_PROFILE_OWNER;
  184. }
  185. /**
  186. Returns the date and time the file was last modified or changed.
  187. @return the last-changed date and time
  188. **/
  189. public Date getFileChangeDate()
  190. throws AS400Exception,
  191. AS400SecurityException,
  192. ErrorCompletingRequestException,
  193. InterruptedException,
  194. IOException,
  195. ObjectDoesNotExistException
  196. {
  197. if(!loaded_) refresh();
  198. return (Date)fileChangeDate_.clone();
  199. }
  200. /**
  201. Returns the name of the owner of the file.
  202. The string is in job CCSID
  203. @return the file owner
  204. **/
  205. public String getFileOwner()
  206. throws AS400Exception,
  207. AS400SecurityException,
  208. ErrorCompletingRequestException,
  209. InterruptedException,
  210. IOException,
  211. ObjectDoesNotExistException
  212. {
  213. if(!loaded_) refresh();
  214. return fileOwner_;
  215. }
  216. /**
  217. Returns the date and time the Java program was created for the file.
  218. @return the creation date
  219. **/
  220. public Date getJavaProgramCreationDate()
  221. throws AS400Exception,
  222. AS400SecurityException,
  223. ErrorCompletingRequestException,
  224. InterruptedException,
  225. IOException,
  226. ObjectDoesNotExistException
  227. {
  228. if(!loaded_) refresh();
  229. return (Date)javaProgramCreationDate_.clone();
  230. }
  231. /**
  232. Returns the IBM i version the Java program was created for.
  233. @return the version
  234. **/
  235. public String getJavaProgramVersion()
  236. throws AS400Exception,
  237. AS400SecurityException,
  238. ErrorCompletingRequestException,
  239. InterruptedException,
  240. IOException,
  241. ObjectDoesNotExistException
  242. {
  243. if(!loaded_) refresh();
  244. return javaProgramVersion_;
  245. }
  246. /**
  247. Returns the number of classes.
  248. @return the number of classes.
  249. **/
  250. public int getNumberOfClasses()
  251. throws AS400Exception,
  252. AS400SecurityException,
  253. ErrorCompletingRequestException,
  254. InterruptedException,
  255. IOException,
  256. ObjectDoesNotExistException
  257. {
  258. if(!loaded_) refresh();
  259. return numberOfClasses_;
  260. }
  261. /**
  262. Returns the number of classes with representations up-to-date in the attached Java programs.
  263. @return the number of classes with current java programs.
  264. **/
  265. public int getNumberOfClassesWithCurrentJavaPrograms()
  266. throws AS400Exception,
  267. AS400SecurityException,
  268. ErrorCompletingRequestException,
  269. InterruptedException,
  270. IOException,
  271. ObjectDoesNotExistException
  272. {
  273. if(!loaded_) refresh();
  274. return numberOfClassesWithJavaPrograms_;
  275. }
  276. /**
  277. Returns the number of classes containing errors.
  278. @return the number of classes with errors.
  279. **/
  280. public int getNumberOfClassesWithErrors()
  281. throws AS400Exception,
  282. AS400SecurityException,
  283. ErrorCompletingRequestException,
  284. InterruptedException,
  285. IOException,
  286. ObjectDoesNotExistException
  287. {
  288. if(!loaded_) refresh();
  289. return numberOfClassesWithErrors_;
  290. }
  291. /**
  292. Returns the number of classes with representations out-of-date.
  293. @return the number of classes without current java programs.
  294. **/
  295. public int getNumberOfClassesWithoutCurrentJavaPrograms()
  296. throws AS400Exception,
  297. AS400SecurityException,
  298. ErrorCompletingRequestException,
  299. InterruptedException,
  300. IOException,
  301. ObjectDoesNotExistException
  302. {
  303. if(!loaded_) refresh();
  304. return numberOfClassesWithoutPrograms_;
  305. }
  306. /**
  307. Returns the number of Java prgroams attached to the .class or .jar/sip file.
  308. @return the number of programs
  309. **/
  310. public int getNumberOfAttachedPrograms()
  311. throws AS400Exception,
  312. AS400SecurityException,
  313. ErrorCompletingRequestException,
  314. InterruptedException,
  315. IOException,
  316. ObjectDoesNotExistException
  317. {
  318. if(!loaded_) refresh();
  319. return numberOfAttachedPrograms_;
  320. }
  321. /**
  322. Returns the optimization level used to create the java program.
  323. @return the optimization level. Possible values are:
  324. <ul>
  325. <li>0</li>
  326. <li>10</li>
  327. <li>20</li>
  328. <li>30</li>
  329. <li>40</li>
  330. </ul>
  331. **/
  332. public int getOptimizationLevel()
  333. throws AS400Exception,
  334. AS400SecurityException,
  335. ErrorCompletingRequestException,
  336. InterruptedException,
  337. IOException,
  338. ObjectDoesNotExistException
  339. {
  340. if(!loaded_) refresh();
  341. return optimizationLevel_;
  342. }
  343. /**
  344. Returns whether or not performance collection is enabled.
  345. @return the performance collection enabled flag. Possible values are:
  346. <ul>
  347. <li>0 - "NONE</li>
  348. <li>1 - on</li>
  349. </ul>
  350. **/
  351. public String getPerformanceCollectionEnabledFlag()
  352. throws AS400Exception,
  353. AS400SecurityException,
  354. ErrorCompletingRequestException,
  355. InterruptedException,
  356. IOException,
  357. ObjectDoesNotExistException
  358. {
  359. if(!loaded_) refresh();
  360. return performanceCollectionEnabledFlag_;
  361. }
  362. /**
  363. Returns the type of performance collection if the performance collection flag is set.
  364. @return the type of performance collection. Possible values are:
  365. <ul>
  366. <li>{@link #PERFORMANCE_COLLECTION_TYPE_ENTRYEXIT PERFORMANCE_COLLECTION_TYPE_ENTRYEXIT}</li>
  367. <li>{@link #PERFORMANCE_COLLECTION_TYPE_FULL PERFORMANCE_COLLECTION_TYPE_FULL}</li>
  368. </ul>
  369. **/
  370. public String getPerformanceCollectionType()
  371. throws AS400Exception,
  372. AS400SecurityException,
  373. ErrorCompletingRequestException,
  374. InterruptedException,
  375. IOException,
  376. ObjectDoesNotExistException
  377. {
  378. if(!loaded_) refresh();
  379. if(performanceCollectionType_.equals("0"))
  380. return PERFORMANCE_COLLECTION_TYPE_ENTRYEXIT;
  381. else
  382. return PERFORMANCE_COLLECTION_TYPE_FULL;
  383. }
  384. /**
  385. Returns whether or not the used adopted authority is set.
  386. @return true if the use adopted authority is set, false otherwise
  387. **/
  388. public boolean isUseAdoptedAuthority()
  389. throws AS400Exception,
  390. AS400SecurityException,
  391. ErrorCompletingRequestException,
  392. InterruptedException,
  393. IOException,
  394. ObjectDoesNotExistException
  395. {
  396. if(!loaded_) refresh();
  397. return useAdoptedAuthority_;
  398. }
  399. /**
  400. Returns the size in kilobytes of all the attached java programs.
  401. @return the size
  402. **/
  403. public int getSizeOfAttachedJavaPrograms()
  404. throws AS400Exception,
  405. AS400SecurityException,
  406. ErrorCompletingRequestException,
  407. InterruptedException,
  408. IOException,
  409. ObjectDoesNotExistException
  410. {
  411. if(!loaded_) refresh();
  412. return sizeOfAttachedPrograms_;
  413. }
  414. /**
  415. Returns whether profiling data is enabled or applied.
  416. @return whether profiling data is enabled or applied. Possible values are:
  417. <ul>
  418. <li>{@link #PROFILING_DATA_STATUS_NOCOL PROFILING_DATA_STATUS_NOCOL}</li>
  419. <li>{@link #PROFILING_DATA_STATUS_COL PROFILING_DATA_STATUS_COL}</li>
  420. <li>{@link #PROFILING_DATA_STATUS_APY PROFILING_DATA_STATUS_APY}</li>
  421. </ul>
  422. **/
  423. public String getProfilingDataStatus()
  424. throws AS400Exception,
  425. AS400SecurityException,
  426. ErrorCompletingRequestException,
  427. InterruptedException,
  428. IOException,
  429. ObjectDoesNotExistException
  430. {
  431. if(!loaded_) refresh();
  432. if(profilingDataStatus_.equals("0"))
  433. return PROFILING_DATA_STATUS_NOCOL;
  434. else if(profilingDataStatus_.equals("1"))
  435. return PROFILING_DATA_STATUS_COL;
  436. else
  437. return PROFILING_DATA_STATUS_APY;
  438. }
  439. /**
  440. Returns the LIC options string specified when the java program was last modified.
  441. @return the LIC options.
  442. **/
  443. public String getLICOptions()
  444. throws AS400Exception,
  445. AS400SecurityException,
  446. ErrorCompletingRequestException,
  447. InterruptedException,
  448. IOException,
  449. ObjectDoesNotExistException
  450. {
  451. if(!loaded_) refresh();
  452. return LICoptions_;
  453. }
  454. /**
  455. Refreshes all the values for this PTF by retrieving them from the system.
  456. **/
  457. public void refresh()
  458. throws AS400Exception,
  459. AS400SecurityException,
  460. ConnectionDroppedException,
  461. ErrorCompletingRequestException,
  462. InterruptedException,
  463. ObjectDoesNotExistException,
  464. IOException,
  465. UnsupportedEncodingException
  466. {
  467. checkVRM();
  468. int ccsid = system_.getCcsid();
  469. ConvTable conv = ConvTable.getTable(ccsid, null);
  470. int len=4096;
  471. ProgramParameter[] parms = new ProgramParameter[15];
  472. parms[0] = new ProgramParameter(len); // receiver variable
  473. parms[1] = new ProgramParameter(BinaryConverter.intToByteArray(len)); // length of receiver variable
  474. parms[2] = new ProgramParameter(conv.stringToByteArray("RJPI0100")); // format name
  475. parms[3] = new ProgramParameter(0); //Class list receiver variable
  476. parms[4] = new ProgramParameter(BinaryConverter.intToByteArray(0)); //Length of class list receiver variable
  477. parms[5] = new ProgramParameter(conv.stringToByteArray("RJPC0100")); //format of class list receiver variable
  478. parms[6] = new ProgramParameter(conv.stringToByteArray(path_)); //path name
  479. parms[7] = new ProgramParameter(BinaryConverter.intToByteArray(path_.length())); //length of path name
  480. parms[8] = new ProgramParameter(conv.stringToByteArray(" ")); //classpath
  481. parms[9] = new ProgramParameter(BinaryConverter.intToByteArray(0)); //length of classpath
  482. parms[10] = new ProgramParameter(0); //classpath used receiver variable
  483. parms[11] = new ProgramParameter(BinaryConverter.intToByteArray(0)); //length of classpath used receiver variable
  484. parms[12] = new ProgramParameter(BinaryConverter.intToByteArray(1)); //status of calsses to return in the class list
  485. parms[13] = new ProgramParameter(conv.stringToByteArray("*PGM")); //JDK version
  486. parms[14] = new ProgramParameter(BinaryConverter.intToByteArray(0)); // error code
  487. ProgramCall pc = new ProgramCall(system_, "/QSYS.LIB/QJVAMAT.PGM", parms);
  488. // Assumption of thread-safety defaults to false, or to the value of the "threadSafe" system property (if it has been set).
  489. //pc.setThreadSafe(false);
  490. if (!pc.run())
  491. {
  492. throw new AS400Exception(pc.getMessageList());
  493. }
  494. byte[] output = parms[0].getOutputData();
  495. //int bytesReturned = BinaryConverter.byteArrayToInt(output, 0);
  496. //int bytesAvailable = BinaryConverter.byteArrayToInt(output, 4);
  497. fileOwner_ = conv.byteArrayToString(output, 8, 10);
  498. String d = conv.byteArrayToString(output, 18, 13);
  499. // Parse the "file change" date
  500. if (d.trim().length() == 13)
  501. {
  502. Calendar cal = AS400Calendar.getGregorianInstance();
  503. cal.clear();
  504. cal.set(Integer.parseInt(d.substring(0,3)) + 1900, // year
  505. Integer.parseInt(d.substring(3,5))-1, // month is zero-based
  506. Integer.parseInt(d.substring(5,7)), // day
  507. Integer.parseInt(d.substring(7,9)), // hour
  508. Integer.parseInt(d.substring(9,11)), // minute
  509. Integer.parseInt(d.substring(11,13))); // second
  510. fileChangeDate_ = cal.getTime();
  511. }
  512. else
  513. {
  514. fileChangeDate_ = null;
  515. }
  516. d = conv.byteArrayToString(output, 31, 13);
  517. //Parse the "Java program creation" date
  518. if (d.trim().length() == 13)
  519. {
  520. Calendar cal = AS400Calendar.getGregorianInstance();
  521. cal.clear();
  522. cal.set(Integer.parseInt(d.substring(0,3)) + 1900, // year
  523. Integer.parseInt(d.substring(3,5))-1, // month is zero-based
  524. Integer.parseInt(d.substring(5,7)), // day
  525. Integer.parseInt(d.substring(7,9)), // hour
  526. Integer.parseInt(d.substring(9,11)), // minute
  527. Integer.parseInt(d.substring(11,13))); // second
  528. javaProgramCreationDate_ = cal.getTime();
  529. }
  530. else
  531. {
  532. javaProgramCreationDate_ = null;
  533. }
  534. numberOfAttachedPrograms_ = BinaryConverter.byteArrayToInt(output, 44);
  535. numberOfClassesWithJavaPrograms_ = BinaryConverter.byteArrayToInt(output, 48);
  536. numberOfClassesWithoutPrograms_ = BinaryConverter.byteArrayToInt(output, 52);
  537. numberOfClassesWithErrors_ = BinaryConverter.byteArrayToInt(output, 56);
  538. numberOfClasses_ = BinaryConverter.byteArrayToInt(output, 60);
  539. optimizationLevel_ = BinaryConverter.byteArrayToInt(output, 64);
  540. performanceCollectionEnabledFlag_ = conv.byteArrayToString(output, 68, 1);
  541. performanceCollectionType_ = conv.byteArrayToString(output, 69, 1);
  542. String useAdopAuthority = conv.byteArrayToString(output, 70, 1);
  543. adoptedAuthorityProfile_ = conv.byteArrayToString(output, 71, 1);
  544. sizeOfAttachedPrograms_ = BinaryConverter.byteArrayToInt(output, 72);
  545. String version = Integer.toString( (output[76] & 0xff ) + 0x100, 16 /* radix */ ) .substring( 1 ); //get version
  546. String release = Integer.toString( (output[77] & 0xff ) + 0x100, 16 /* radix */ ) .substring( 1 ); //get release and modification
  547. javaProgramVersion_ = getVersion((version + release).toCharArray());
  548. profilingDataStatus_ = Byte.toString(output[78]);
  549. int offsetToLICOptions = BinaryConverter.byteArrayToInt(output, 80);
  550. int lengthOfLIC = BinaryConverter.byteArrayToInt(output, 84);
  551. int lengthOfAvailableLIC = BinaryConverter.byteArrayToInt(output, 88);
  552. LICoptions_ = conv.byteArrayToString(output, offsetToLICOptions, lengthOfLIC);
  553. if(useAdopAuthority.equals("0"))
  554. useAdoptedAuthority_ = false;
  555. else
  556. useAdoptedAuthority_ = true;
  557. loaded_= true;
  558. }
  559. /**
  560. Gets the IBM i version.
  561. **/
  562. private String getVersion(char[] version)
  563. {
  564. String v = "V";
  565. String r = "R";
  566. String m = "M";
  567. if(version[0] != '0')
  568. v += version[0];
  569. v += version[1];
  570. r += version[2];
  571. m += version[3];
  572. return v+r+m;
  573. }
  574. /**
  575. Sets the name of the system to search for a Java Program.
  576. @param system The system
  577. **/
  578. public void setSystem(AS400 system)
  579. {
  580. if(loaded_)
  581. throw new ExtendedIllegalStateException("propertiesFrozen", ExtendedIllegalStateException.PROPERTY_NOT_CHANGED);
  582. if(system == null)
  583. throw new NullPointerException("system");
  584. system_ = system;
  585. }
  586. /**
  587. Sets the qualified path name to use.
  588. @param path - the qualified path name.
  589. **/
  590. public void setPath(String path)
  591. {
  592. if(loaded_)
  593. throw new ExtendedIllegalStateException("propertiesFrozen", ExtendedIllegalStateException.PROPERTY_NOT_CHANGED);
  594. if (path == null)
  595. throw new NullPointerException("path");
  596. path_ = path;
  597. }
  598. }