PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1382 lines | 575 code | 137 blank | 670 comment | 82 complexity | defa30f3d1796bc87fa29815070dace6 MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // JTOpen (IBM Toolbox for Java - OSS version)
  4. //
  5. // Filename: JavaApplicationCall.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-2004 International Business Machines Corporation and
  10. // others. All rights reserved.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. package com.ibm.as400.access;
  14. import java.beans.PropertyChangeListener;
  15. import java.beans.PropertyChangeSupport;
  16. import java.beans.PropertyVetoException;
  17. import java.beans.VetoableChangeListener;
  18. import java.beans.VetoableChangeSupport;
  19. import java.net.ServerSocket;
  20. import java.net.Socket;
  21. import java.net.UnknownHostException;
  22. import java.io.InputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import java.io.Serializable;
  26. import java.util.Enumeration;
  27. import java.util.Properties;
  28. import java.util.StringTokenizer;
  29. import java.util.Vector;
  30. /**
  31. * <P>
  32. * Provides an easy way to run Java
  33. * applications on the IBM i system's Java Virtual Machine from a client.
  34. * The client Java program specifies the environment, the program to run,
  35. * and the program parameters. The program then runs on the IBM i system's
  36. * Java Virtual Machine. Text-based input/output is provided by
  37. * JavaApplicationCall. Input can be sent to the Java program which
  38. * will receive the input via standard input (<b><tt>stdin</tt></b>).
  39. * Standard output (<b><tt>stdout</tt></b>) and
  40. * standard error (<b><tt>stderr</tt></b>) text generated by the Java program are received
  41. * by JavaApplicationCall and made available to the calling program.
  42. * JavaApplicationCall does not support displaying the graphical user
  43. * interface of the IBM i system's Java program on the client. Other Java
  44. * facilities, such as remote AWT must be used to display graphical
  45. * interfaces on the client.
  46. *
  47. * <P>
  48. * Sockets are used to send stdin, stdout and stderr between
  49. * client and IBM i system. The port used can be set via setPort(). The
  50. * default port sequence is 2850, 2851 and 2852. If the port is in use,
  51. * this class searches for available ports if findPort is true.
  52. * stdin, stdout and stderr are <i>not</i> transported
  53. * across a secure connection even when the rest of the Toolbox
  54. * is using SSL.
  55. *
  56. * <P>
  57. * Note: The presence of a firewall, in either direction between the IBM i system
  58. * and the client, may prevent JavaApplicationCall from
  59. * opening the necessary additional ports (for stdin, stdout, and stderr).
  60. * The administrator may need to authenticate the application through
  61. * the firewall in <i>both</i> directions:
  62. * From client to IBM i system; and from IBM i system to client.
  63. *
  64. * <P>
  65. * For example, suppose the Java class <tt>HelloWorld</tt> resides in directory
  66. * <tt>/javatest</tt> on the system. The following calls this program and
  67. * receives program output written to stdout and stderr.
  68. *
  69. * <a name="ex"> </a>
  70. * <pre>
  71. * import com.ibm.as400.access.*;
  72. *
  73. * public class test implements Runnable
  74. * {
  75. * JavaApplicationCall jaCall;
  76. *
  77. * public static void main(String[] args)
  78. * {
  79. * test me = new test();
  80. * me.Main(args);
  81. * }
  82. *
  83. *
  84. * void Main(String[] args)
  85. * {
  86. *
  87. * try
  88. * {
  89. * // The IBM i system where the Java program to run, is located.
  90. * AS400 as400 = new AS400();
  91. *
  92. * // Construct a JavaApplicationCall object.
  93. * jaCall = new JavaApplicationCall(as400);
  94. *
  95. * // Set the name of the Java application to be run.
  96. * jaCall.setJavaApplication("HelloWorld");
  97. *
  98. * // Set the classpath environment variable used by the IBM i system's
  99. * // JVM so it can find the class to run.
  100. * jaCall.setClassPath("/javatest");
  101. *
  102. * // Start the thread that will receive stdout
  103. * Thread outputThread = new Thread(this);
  104. * outputThread.start();
  105. *
  106. * // Start the program. The call to run() will not return
  107. * // until the IBM i system Java program completes. If the Toolbox
  108. * // cannot start the Java program, false is returned with
  109. * // a list of AS400Message objects indicating why the program
  110. * // could not start.
  111. * if (jaCall.run() != true)
  112. * {
  113. * AS400Message[] messageList = jaCall.getMessageList();
  114. * for (int msg = 0; msg < messageList.length; msg++)
  115. * System.out.println(messageList[msg].toString());
  116. * }
  117. * }
  118. * catch (Exception e) { e.printStackTrace(); }
  119. *
  120. * System.exit(0);
  121. * }
  122. *
  123. * // This thread will get the output from the IBM i system Java program,
  124. * // and print it. Note the call to sleep().
  125. * // The getXxxString() methods return immediately even if there is no data.
  126. * public void run()
  127. * {
  128. * while (true)
  129. * {
  130. * String stdOut = jaCall.getStandardOutString();
  131. * if (stdOut != null)
  132. * System.out.println(stdOut);
  133. *
  134. * String stdErr = jaCall.getStandardErrorString();
  135. * if (stdErr != null)
  136. * System.out.println(stdErr);
  137. *
  138. * try { Thread.sleep(100); } catch (Exception e) {}
  139. * }
  140. * }
  141. * }
  142. *
  143. *
  144. * </pre>
  145. *
  146. **/
  147. public class JavaApplicationCall implements Serializable
  148. {
  149. static final long serialVersionUID = 4L;
  150. // The variables represent the default stdin, stdout, and stderr port.
  151. private static int DEFAULT_STANDARD_ERROR_PORT = 2852;
  152. private static int DEFAULT_STANDARD_IN_PORT = 2850;
  153. private static int DEFAULT_STANDARD_OUT_PORT = 2851;
  154. private static int PORT_SEARCH_TIMES = 1000;
  155. private AS400 as400_ = null;
  156. private CommandCall command_ = null;
  157. private AS400Message[] messageList_ = null;
  158. // lists of listeners and event catchers
  159. private transient PropertyChangeSupport propertyChange_ = new PropertyChangeSupport(this);
  160. private transient VetoableChangeSupport vetoableChange_ = new VetoableChangeSupport(this);
  161. private transient Vector completedListeners_ = new Vector();
  162. private transient JavaApplicationCallThread openSocketsThread_ = null;
  163. // The variables represent connections built in the subsidiary threads.
  164. private transient ServerSocket error_ = null;
  165. private transient ServerSocket read_ = null;
  166. private transient ServerSocket write_ = null;
  167. // The variables represent the properties of JavaApplicationCall.
  168. private String classPath_ = "";
  169. private int defaultPort_ = DEFAULT_STANDARD_IN_PORT;
  170. private boolean findPort_ = true;
  171. private int garbageCollectionInitialSize_ = 2048;
  172. private String garbageCollectionMaximumSize_ = "*NOMAX";
  173. private int garbageCollectionFrequency_ = 50;
  174. private int garbageCollectionPriority_ = 20;
  175. private String interpret_ = "*OPTIMIZE";
  176. private String jobname_ = "QJVACMDSRV"; // @F0A
  177. private String javaApplication_ = null;
  178. private String optimization_ = "10";
  179. private String[] options_ = {"*NONE"};
  180. private String[] parameters_ = {};
  181. private Properties properties_ = null;
  182. private String securityCheckLevel_ = "*WARN";
  183. private int standardErrorPort_ = DEFAULT_STANDARD_ERROR_PORT;
  184. private int standardInPort_ = DEFAULT_STANDARD_IN_PORT;
  185. private int standardOutPort_ = DEFAULT_STANDARD_OUT_PORT;
  186. /**
  187. * Constructs a JavaApplicationCall object.
  188. **/
  189. public JavaApplicationCall()
  190. {
  191. }
  192. /**
  193. * Constructs a JavaApplicationCall object. The Java program
  194. * is on an IBM i <i>system</i>.
  195. *
  196. * @param system The system on which contains the Java program.
  197. **/
  198. public JavaApplicationCall(AS400 system)
  199. {
  200. if (system == null)
  201. throw new NullPointerException("system");
  202. as400_ = system;
  203. }
  204. /**
  205. * Constructs a JavaApplicationCall object. The Java program is namee
  206. * <i>application</i> and runs on system <i>system</i>.
  207. *
  208. * @param system The system on which contains the Java program.
  209. * @param application The name of Java program.
  210. **/
  211. public JavaApplicationCall(AS400 system, String application)
  212. {
  213. this(system);
  214. if (application == null)
  215. throw new NullPointerException("application");
  216. javaApplication_ = application;
  217. }
  218. /**
  219. * Constructs a JavaApplicationCall object. The Java program is named
  220. * <i>application</i> and runs on system <i>system</i>. <i>
  221. * classPath</i> is passed to the system as the value of the
  222. * CLASSPATH environment variable.
  223. *
  224. * @param system The system on which contains the Java program.
  225. * @param application The name of Java program.
  226. * @param classPath The value of the environment variable CLASSPATH.
  227. **/
  228. public JavaApplicationCall(AS400 system, String application, String classPath)
  229. {
  230. this(system, application);
  231. if (classPath == null)
  232. throw new NullPointerException("classPath");
  233. this.classPath_ = classPath;
  234. }
  235. /**
  236. * Adds an ActionCompletedListener to be notified when the Java application ends.
  237. * The specified ActionCompletedListeners <b>actionCompleted</b> method will
  238. * be called each time an application runs.
  239. *
  240. * @see #removeActionCompletedListener
  241. * @param listener The ActionCompletedListener.
  242. */
  243. public void addActionCompletedListener(ActionCompletedListener listener)
  244. {
  245. if (listener != null)
  246. completedListeners_.addElement(listener);
  247. else
  248. throw new NullPointerException("addActionCompletedListener");
  249. }
  250. /**
  251. * Adds a listener to be notified when the value of any bound property
  252. * changes.
  253. *
  254. * @see #removePropertyChangeListener
  255. * @param listener The listener.
  256. **/
  257. public void addPropertyChangeListener(PropertyChangeListener listener )
  258. {
  259. if (listener != null)
  260. propertyChange_.addPropertyChangeListener(listener);
  261. else
  262. throw new NullPointerException("addPropertyChangeListener");
  263. }
  264. /**
  265. * Adds a listener to be notified when the value of any constrained
  266. * property changes.
  267. *
  268. * @see #removeVetoableChangeListener
  269. * @param listener The listener.
  270. **/
  271. public void addVetoableChangeListener(VetoableChangeListener listener )
  272. {
  273. if (listener != null)
  274. vetoableChange_.addVetoableChangeListener(listener);
  275. else
  276. throw new NullPointerException("addVetoableChangeListener");
  277. }
  278. /**
  279. * Returns the value of the CLASSPATH environment variable when running
  280. * the Java program. Use the forward slash to separate path elements
  281. * and a colon to separate the elements of CLASSPATH. For example,
  282. * /dir1:/dir1/dir2/myClasses.jar.
  283. * <p>Valid values are:
  284. * <UL>
  285. * <li>*ENVVAR No setting is sent from the client to the IBM i system so the
  286. * IBM i system's default value of CLASSPATH is used.
  287. * <li>string The value that is set before the program is run.
  288. * </UL>
  289. *
  290. * @return The value of CLASSPATH.
  291. **/
  292. public String getClassPath()
  293. {
  294. return classPath_;
  295. }
  296. /**
  297. Returns the CommandCall object that was used in the most recent invocation of {@link #run() run()}.
  298. @return The CommandCall object; null if run() has not been called.
  299. **/
  300. public CommandCall getCommandCall()
  301. {
  302. return command_;
  303. }
  304. /**
  305. * Returns the default port used to transfer stdin, stdout,
  306. * and stderr between the client and the IBM i system. Three
  307. * ports are used. The port returned by this method is used for
  308. * stdin; (port + 1) is used for stdout; and (port + 2)
  309. * is used for stderr. If not otherwise specified, the default port is 2850.
  310. *
  311. * @return The default port.
  312. **/
  313. public int getDefaultPort()
  314. {
  315. return defaultPort_;
  316. }
  317. /**
  318. Gets stdin, stdout and stderr port.
  319. Also creates serversockets for stdin, stdout and stderr.
  320. **/
  321. private void getFreePort()
  322. {
  323. read_ = null;
  324. write_ = null;
  325. error_ = null;
  326. if (!isFindPort())
  327. {
  328. standardInPort_ = defaultPort_;
  329. standardOutPort_ = defaultPort_ + 1;
  330. standardErrorPort_ = defaultPort_ + 2;
  331. try
  332. {
  333. read_ = new ServerSocket(standardInPort_);
  334. }
  335. catch (Exception e)
  336. {
  337. Trace.log(Trace.ERROR, e.toString(), e);
  338. }
  339. try
  340. {
  341. write_ = new ServerSocket(standardOutPort_);
  342. }
  343. catch (Exception e)
  344. {
  345. Trace.log(Trace.ERROR, e.toString(), e);
  346. }
  347. try
  348. {
  349. error_ = new ServerSocket(standardErrorPort_);
  350. }
  351. catch (Exception e)
  352. {
  353. Trace.log(Trace.ERROR, e.toString(), e);
  354. }
  355. }
  356. else
  357. {
  358. standardInPort_ = searchPort(defaultPort_ , DEFAULT_STANDARD_IN_PORT);
  359. standardOutPort_ = searchPort(standardInPort_+1, DEFAULT_STANDARD_OUT_PORT);
  360. standardErrorPort_ = searchPort(standardOutPort_+1, DEFAULT_STANDARD_ERROR_PORT);
  361. }
  362. if (read_ == null || write_ == null || error_ == null)
  363. {
  364. try
  365. {
  366. if (read_ != null) read_.close();
  367. read_ = null;
  368. }
  369. catch(Exception e){}
  370. try
  371. {
  372. if (write_ != null) write_.close();
  373. write_ = null;
  374. }
  375. catch(Exception e){}
  376. try
  377. {
  378. if (error_ != null) error_.close();
  379. error_ = null;
  380. }
  381. catch(Exception e){}
  382. String msg = "Setup of connection with IBM i system failed.";
  383. Trace.log(Trace.ERROR, msg);
  384. throw new RuntimeException(msg);
  385. }
  386. }
  387. /**
  388. * Returns the relative frequency that garbage collection runs. This
  389. * value applies only to OS/400 V4R2 and V4R3. It is
  390. * ignored in V4R4 and later versions.
  391. *
  392. * @return The relative frequency.
  393. **/
  394. public int getGarbageCollectionFrequency()
  395. {
  396. return garbageCollectionFrequency_;
  397. }
  398. /**
  399. * Returns the initial size, in kilobytes, of the garbage collection heap.
  400. * A large size can keep the garbage collector from starting when
  401. * the Java program is small, improving performance.
  402. *
  403. * <p>Possible values are:
  404. * <UL>
  405. * <li>2048 The default initial size is 2048 kilobytes.
  406. *
  407. * <li>heapSize The initial value of the garbage collection heap in
  408. * kilobytes. We recommend the initial heap size be
  409. * 2048 kilobytes (the default) or larger.
  410. * </UL>
  411. * @return The initial size of the garbage collection heap.
  412. **/
  413. public int getGarbageCollectionInitialSize()
  414. {
  415. return garbageCollectionInitialSize_;
  416. }
  417. /**
  418. * Returns the maximum size, in kilobytes, that the garbage
  419. * collection heap can grow to. This value is used to
  420. * prevent runaway programs from consuming all available
  421. * storage.
  422. *
  423. * <p>Possible values are:
  424. * <UL>
  425. * <li>*NOMAX The default maximum size is not specified by the user. The
  426. * maximum is determined by the system. The heap will grow until
  427. * all system resources are depleted. Then a synchronous garbage
  428. * collection is started to reclaim resources no longer in use.
  429. *
  430. * <li>heapSize The value in kilobytes that the garbage collection
  431. * heap can grow to.
  432. * </UL>
  433. * @return The maximum size that the garbage collection heap can grow to.
  434. **/
  435. public String getGarbageCollectionMaximumSize()
  436. {
  437. return garbageCollectionMaximumSize_;
  438. }
  439. /**
  440. * Returns the priority of the tasks running garbage collection. This
  441. * value applies only to OS/400 V4R2 and V4R3. It is
  442. * ignored in V4R4 and later versions.
  443. *
  444. * @return The priority of the tasks.
  445. **/
  446. public int getGarbageCollectionPriority()
  447. {
  448. return garbageCollectionPriority_;
  449. }
  450. /**
  451. * Returns whether all Java class files should be run interpretively.
  452. *
  453. * <p>Possible values are:
  454. * <UL>
  455. * <li>*OPTIMIZE Whether all Java classes are run interpretively depends on the
  456. * value of the optimize property. If optimize is
  457. * *INTERPRET, all Java classes will be run
  458. * interpretively. If any other value was specified for
  459. * optimize, only Java classes with Java programs created
  460. * using CRTJVAPGM command and specifying OPTIMIZE(*INTERPRET) will
  461. * be run interpretively.
  462. *
  463. * <li>*NO Only Java classes with Java programs created using CRTJVAPGM
  464. * command and specifying OPTIMIZE(*INTERPRET) will be run
  465. * interpretively. Java classes that need a Java program created
  466. * will be created with the optimization level specified in the
  467. * optimize property.
  468. *
  469. * <li>*YES All Java classes will be run interpretively regardless of the
  470. * OPTIMIZE value associated Java program. Java classes that need
  471. * a Java program created will use the optimization level specified
  472. * in the optimize property.
  473. * </UL>
  474. *
  475. * @return Whether all Java class files should be run interpretively.
  476. **/
  477. public String getInterpret()
  478. {
  479. return interpret_;
  480. }
  481. /**
  482. * Returns the name of Java application to be run.
  483. * If the Java application is not be set, null is returned.
  484. *
  485. * @return The name of Java application.
  486. **/
  487. public String getJavaApplication()
  488. {
  489. return javaApplication_;
  490. }
  491. /**
  492. * Returns the name that this job will run under.
  493. * The maximum length returned is 10 characters.
  494. *
  495. * <p>Possible values are:
  496. * <UL>
  497. * <li>QJVACMDSRV - The default job name.
  498. *
  499. * <li>*GEN - The job name will be generated from the class name.
  500. *
  501. * <li>string - Any generic string value.
  502. * </UL>
  503. *
  504. * @return The value of the job name.
  505. * @since i5/OS V5R3M0
  506. **/
  507. public String getJobName() // @F0A
  508. {
  509. return jobname_;
  510. }
  511. /**
  512. *Returns the list of IBM i system messages generated if the Java program
  513. *cannot be started. Before run() is called and if the Java program
  514. *can be started, an empty list is returned.
  515. *
  516. *@return The array of AS400Message objects.
  517. */
  518. public AS400Message[] getMessageList()
  519. {
  520. return messageList_;
  521. }
  522. /**
  523. * Returns the optimization level of IBM i system Java programs that
  524. * will be created if no Java program is associated with the Java
  525. * class.
  526. *
  527. * *INTERPRET means the resulting Java program interprets the
  528. * class byte codes when invoked. For other optimization levels, the
  529. * Java program contains machine instruction sequences that are run
  530. * when the Java program is invoked.
  531. *
  532. * *INTERPRET Java programs are smaller but run slower than
  533. * Java programs created with higher optimization levels. As you
  534. * increase the optimization level beyond 10, the Java program
  535. * performance generally improves, but the time required to create the
  536. * Java program increases, and debugging is more difficult.
  537. *
  538. * <p>Possible values are:
  539. * <UL>
  540. * <li>10 The Java program contains a compiled version of the class file
  541. * byte codes and has only minimal additional compiler
  542. * optimization. Variables can be displayed and modified while
  543. * debugging.
  544. *
  545. * <li>*INTERPRET The Java program created does not contain machine specific
  546. * instructions. It will be interpreted when the program is
  547. * started. Variables can be displayed and modified while
  548. * debugging.
  549. *
  550. * If the INTERPRET property has a value of *OPTIMIZE, all of the
  551. * classes that run will be run interpreted even if there is an
  552. * optimized Java program associated with the class.
  553. *
  554. * <li>20 The Java program contains a compiled version of the class file
  555. * byte codes and has some additional compiler optimization.
  556. * Variables can be displayed but not modified while debugging.
  557. *
  558. * <li>30 The Java program contains a compiled version of the class file
  559. * byte codes and has more compiler optimization than optimization
  560. * level 20. During a debug session, user variables cannot be
  561. * changed, but can be displayed. The presented values may not be
  562. * the current values of the variables.
  563. *
  564. * <li>40 The Java program contains a compiled version of the class file
  565. * byte codes and has more compiler optimization than optimization
  566. * level 30. All call and instruction tracing is disabled.
  567. * </UL>
  568. * @return The optimization level of the IBM i system Java application.
  569. **/
  570. public String getOptimization()
  571. {
  572. return optimization_;
  573. }
  574. /**
  575. * Returns a list of special options used when running the Java class.
  576. *
  577. * <p>The possible values are:
  578. * <UL>
  579. * <li>*NONE No special options are used when running the Java class.
  580. *
  581. * <li>*VERBOSE A message is displayed each time a class file is loaded.
  582. *
  583. * <li>*DEBUG Allows the system debugger to be used for this Java program.
  584. *
  585. * <li>*VERBOSEGC A message is displayed for each garbage collection sweep.
  586. *
  587. * <li>*NOCLASSGC Unused classes are not reclaimed when garbage collection is run.
  588. * </UL>
  589. *
  590. * @return The options used when running the Java class.
  591. **/
  592. public String[] getOptions()
  593. {
  594. return options_;
  595. }
  596. /**
  597. * Returns parameter values that are passed to the Java
  598. * application. A maximum of 200 parameter values can be passed.
  599. *
  600. * @return The parameters when running the Java application.
  601. **/
  602. public String[] getParameters()
  603. {
  604. return parameters_;
  605. }
  606. /**
  607. * Returns the properties set on the IBM i system's JVM before running
  608. * the Java program.
  609. *
  610. * @return The properties.
  611. **/
  612. public Properties getProperties()
  613. {
  614. return properties_;
  615. }
  616. /**
  617. * Returns the level of warnings given for directories in the
  618. * CLASSPATH that have public write authority. A directory in the
  619. * CLASSPATH that has public write authority is a security exposure
  620. * because it may contain a class file with the same name as the one
  621. * you want to run. Whichever class file is found first is run.
  622. *
  623. * <p>The possible values are:
  624. * <UL>
  625. * <li>*WARN Warning message is sent for each directory in the CLASSPATH
  626. * that has public write authority.
  627. *
  628. * <li>*SECURE A warning message is sent for each directory in the CLASSPATH
  629. * that has public write authority. If one or more warning
  630. * messages are sent, an escape message is sent and the Java
  631. * program is not run.
  632. *
  633. * <li>*IGNORE Ignore the fact that directories in the CLASSPATH may have
  634. * public write authority. No warnings messages are sent.
  635. * </UL>
  636. * @return The level of warnings given for directories in the
  637. * CLASSPATH that have public write authority.
  638. **/
  639. public String getSecurityCheckLevel()
  640. {
  641. return securityCheckLevel_;
  642. }
  643. /**
  644. * Returns the stderr port.
  645. * If not changed, the default port 2852 is returned.
  646. *
  647. * @return The stderr port.
  648. **/
  649. private int getStandardErrorPort()
  650. {
  651. return standardErrorPort_;
  652. }
  653. /**
  654. * Returns the next string written to stderr by the program
  655. * running on the system.
  656. *
  657. * @return the next stderr string from the system.
  658. **/
  659. public String getStandardErrorString()
  660. {
  661. if (openSocketsThread_ != null)
  662. return openSocketsThread_.getStandardErrorString();
  663. else
  664. return null;
  665. }
  666. /**
  667. * Returns the stdin port.
  668. * If not set, the default port 2850 is returned.
  669. *
  670. * @return The stdin port.
  671. **/
  672. private int getStandardInPort()
  673. {
  674. return standardInPort_;
  675. }
  676. /**
  677. * Returns the stdout port.
  678. * If not set, the default port 2851 is returned.
  679. *
  680. * @return The stdout port.
  681. **/
  682. private int getStandardOutPort()
  683. {
  684. return standardOutPort_;
  685. }
  686. /**
  687. * Returns the next string written to stdout by the application.
  688. *
  689. * @return the string written to stdout.
  690. **/
  691. public String getStandardOutString()
  692. {
  693. if (openSocketsThread_ != null)
  694. return openSocketsThread_.getStandardOutString();
  695. else
  696. return null;
  697. }
  698. /**
  699. Returns the system which contains the Java program.
  700. @return The system.
  701. **/
  702. public AS400 getSystem()
  703. {
  704. return as400_;
  705. }
  706. /**
  707. Indicates if this class should search for a free port.
  708. @return true if this class will search for a free port; false otherwise.
  709. The default value is <tt>true</tt>.
  710. **/
  711. public boolean isFindPort()
  712. {
  713. return findPort_;
  714. }
  715. /**
  716. Deserializes and initializes the transient data.
  717. **/
  718. private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
  719. {
  720. in.defaultReadObject();
  721. propertyChange_ = new PropertyChangeSupport(this);
  722. vetoableChange_ = new VetoableChangeSupport(this);
  723. completedListeners_ = new Vector();
  724. openSocketsThread_ = null;
  725. read_ = null;
  726. write_ = null;
  727. error_ = null;
  728. }
  729. /**
  730. * Removes this ActionCompletedListener from the list of listeners.
  731. * If the ActionCompletedListener is not on the list, nothing is done.
  732. *
  733. * @see #addActionCompletedListener
  734. * @param listener The ActionCompletedListener.
  735. */
  736. public synchronized void removeActionCompletedListener(ActionCompletedListener listener)
  737. {
  738. if (listener != null)
  739. completedListeners_.removeElement(listener);
  740. else
  741. throw new NullPointerException("removeActionCompletedListener");
  742. }
  743. /**
  744. * Removes a property change listener from the list of listeners.
  745. *
  746. * @see #addPropertyChangeListener
  747. * @param listener The listener.
  748. **/
  749. public void removePropertyChangeListener(PropertyChangeListener listener )
  750. {
  751. if (listener != null)
  752. propertyChange_.removePropertyChangeListener(listener);
  753. else
  754. throw new NullPointerException("removePropertyChangeListener");
  755. }
  756. /**
  757. * Removes a vetoable change listener from the list of listeners.
  758. *
  759. * @see #addVetoableChangeListener
  760. * @param listener The listener.
  761. **/
  762. public void removeVetoableChangeListener(VetoableChangeListener listener )
  763. {
  764. if (listener != null)
  765. vetoableChange_.removeVetoableChangeListener(listener );
  766. else
  767. throw new NullPointerException("removeVetoableChangeListener");
  768. }
  769. /**
  770. Run the Java application. Control will not be returned to the
  771. calling application until the program completes. If the program
  772. does not start, a list of AS400Message object containing information
  773. about is failure is available.
  774. @return true if the program can be started, false otherwise.
  775. @exception AS400SecurityException If a security or authority error occurs.
  776. @exception ConnectionDroppedException If the connection is dropped unexpectedly.
  777. @exception ErrorCompletingRequestException If an error occurs before the request is completed.
  778. @exception InterruptedException If this thread is interrupted.
  779. @exception IOException If an error occurs while communicating with the system.
  780. @exception ServerStartupException If the host server cannot be started.
  781. @exception UnknownHostException If the system cannot be located.
  782. **/
  783. public boolean run() throws AS400SecurityException,
  784. ConnectionDroppedException,
  785. ErrorCompletingRequestException,
  786. InterruptedException,
  787. IOException,
  788. ServerStartupException,
  789. UnknownHostException
  790. {
  791. if (getSystem() == null)
  792. throw new ExtendedIllegalStateException("system",
  793. ExtendedIllegalStateException.PROPERTY_NOT_SET);
  794. if (getJavaApplication() == null)
  795. throw new ExtendedIllegalStateException("javaApplication",
  796. ExtendedIllegalStateException.PROPERTY_NOT_SET);
  797. // force signon so sign-on dialog up before
  798. // we create more threads.
  799. getSystem().getVRM(); // @D1A
  800. getFreePort();
  801. openSocketsThread_ = new JavaApplicationCallThread(read_, write_, error_);
  802. openSocketsThread_.start();
  803. command_ = new CommandCall(getSystem());
  804. command_.suggestThreadsafe(false); // JAVA cmd isn't threadsafe. @A1A
  805. // Construct commandstring
  806. // 1. CLASS
  807. // 2. PARM
  808. // 3. CLASSPATH
  809. // 4. CHKPATH
  810. // 5. OPTIMIZE
  811. // 6. INTERPRET
  812. // 7. PROP
  813. // 8. GCHINL
  814. // 9. GCHMAX
  815. // 10. GCFRQ
  816. // 11. GCPTY
  817. // 12. OPTION
  818. // 1.CLASS
  819. StringBuffer commandString = new StringBuffer("JAVA CLASS(" + getJavaApplication() + ") ");
  820. // 2.PARM
  821. String[] parm = getParameters();
  822. if (parm.length > 0)
  823. {
  824. commandString.append("PARM(");
  825. for (int i = 0 ; i < parm.length; i++) {
  826. commandString.append("'" + parm[i] + "' ");
  827. }
  828. // delete last space
  829. commandString.delete(commandString.length()-1, commandString.length());
  830. commandString.append(") ");
  831. }
  832. // 3.CLASSPATH
  833. if (getClassPath().length() > 0)
  834. commandString.append("CLASSPATH('" + getClassPath() + "') ");
  835. // 4.CHKPATH
  836. commandString.append("CHKPATH(" + getSecurityCheckLevel() +") ");
  837. // 5.OPTIMIZE
  838. commandString.append("OPTIMIZE(" + getOptimization() + ") ");
  839. // 6.INTERPRET
  840. commandString.append("INTERPRET(" + getInterpret() + ") ");
  841. // 7.PROP
  842. String localAddress = null; // @E1c
  843. // @E1a
  844. // Because of bugs in Java, cannot get the local // @E1a
  845. // address until a socket (not a server socket) // @E1a
  846. // is connected. // @E1a
  847. try // @E1a
  848. { // @E1a
  849. Socket pmSocket = new Socket(getSystem().getSystemName(), 449); // @E1a
  850. localAddress = pmSocket.getLocalAddress().getHostAddress(); // @E1a
  851. pmSocket.close(); // @E1a
  852. } // @E1a
  853. catch (Exception e) { localAddress = null; } // @E1a
  854. // @E1a
  855. // if new method didn't work, try the old technique. // @E1a
  856. if (localAddress == null) // @E1a
  857. {
  858. localAddress = "localhost";
  859. try
  860. {
  861. localAddress = java.net.InetAddress.getLocalHost().getHostAddress();
  862. }
  863. catch (java.net.UnknownHostException e)
  864. {
  865. Trace.log(Trace.ERROR , e.toString());
  866. }
  867. } // @E1a
  868. commandString.append("PROP("
  869. +"(os400.stdin 'port:"+localAddress+":"+new Integer(getStandardInPort()).toString()+"') "
  870. +"(os400.stdout 'port:"+localAddress+":"+new Integer(getStandardOutPort()).toString()+"') "
  871. +"(os400.stderr 'port:"+localAddress+":"+new Integer(getStandardErrorPort()).toString()+"') ");
  872. commandString.append("(os400.stdio.convert N)");
  873. if (getProperties() != null)
  874. {
  875. Enumeration e = properties_.keys();
  876. //add all other properties here
  877. while (e.hasMoreElements()) {
  878. String key = (String) e.nextElement();
  879. String value = (String)properties_.get(key);
  880. commandString.append(" (" + key + " " + value + ")");
  881. }
  882. }
  883. commandString.append(") ");
  884. // 8.GCHINL
  885. commandString.append("GCHINL(" + getGarbageCollectionInitialSize() + ") ");
  886. // 9.GCHMAX
  887. commandString.append("GCHMAX(" + getGarbageCollectionMaximumSize() + ") ");
  888. // 10.GCFRQ
  889. commandString.append("GCFRQ(" + getGarbageCollectionFrequency() + ") ");
  890. // 11.GCPTY
  891. commandString.append("GCPTY(" + getGarbageCollectionPriority() + ") ");
  892. // 12.OPTION
  893. String[] opt = getOptions();
  894. if (opt.length > 0)
  895. {
  896. commandString.append("OPTION(");
  897. for (int i = 0 ; i < opt.length; i++) {
  898. commandString.append(opt[i] + " ");
  899. }
  900. // delete last space
  901. commandString.delete(commandString.length()-1, commandString.length());
  902. commandString.append(") ");
  903. }
  904. // 13.JOB @F0A
  905. if (as400_.getVRM() >= 0x00050300) // New in V5R3M0 @F0A
  906. commandString.append("JOB(" + getJobName() +") "); // @F0A
  907. //run the command
  908. boolean success = false;
  909. // System.out.println(commandString);
  910. try
  911. {
  912. success = command_.run(commandString.toString());
  913. }
  914. catch (PropertyVetoException e) {}
  915. finally
  916. {
  917. openSocketsThread_.closeSockets();
  918. openSocketsThread_ = null;
  919. parameters_ = new String[0];
  920. properties_ = null;
  921. read_ = null;
  922. write_ = null;
  923. error_ = null;
  924. }
  925. if (success)
  926. {
  927. // fire ActionCompletedEvent
  928. Vector targets;
  929. targets = (Vector) completedListeners_.clone();
  930. ActionCompletedEvent actionEvt = new ActionCompletedEvent(this);
  931. for (int i = 0; i < targets.size(); i++)
  932. {
  933. ActionCompletedListener target = (ActionCompletedListener)targets.elementAt(i);
  934. target.actionCompleted(actionEvt);
  935. }
  936. }
  937. messageList_ = command_.getMessageList();
  938. return success;
  939. }
  940. /**
  941. Find a free port. If the port passed as a parameter is in use, try port+=3.
  942. If that is busy keep trying for up to 1000 times.
  943. If we cannot find a free port, return the original port.
  944. **/
  945. private int searchPort(int port, int whichPort)
  946. {
  947. ServerSocket test = null;
  948. int curport = port;
  949. boolean found = false;
  950. int times = 0;
  951. while (times <= PORT_SEARCH_TIMES)
  952. {
  953. try
  954. {
  955. test = new ServerSocket(curport);
  956. }
  957. catch (Exception e)
  958. {
  959. curport += 3;
  960. times++;
  961. continue;
  962. }
  963. found = true;
  964. break;
  965. }
  966. if (found)
  967. {
  968. if (whichPort == DEFAULT_STANDARD_IN_PORT)
  969. read_ = test;
  970. else if (whichPort == DEFAULT_STANDARD_OUT_PORT)
  971. write_ = test;
  972. else
  973. error_ = test;
  974. }
  975. else
  976. curport = port;
  977. return curport;
  978. }
  979. /**
  980. Sends the stdin to the application running on the system.
  981. @param data The stdin to the system.
  982. **/
  983. public void sendStandardInString(String data)
  984. {
  985. if (openSocketsThread_ != null)
  986. openSocketsThread_.sendStandardInString(data);
  987. }
  988. /** Sets the value of the CLASSPATH environment variable when running
  989. * the Java program. Use the forward slash to separate path elements
  990. * and a colon to separate the elements of CLASSPATH. For example,
  991. * /dir1:/dir1/dir2/myClasses.jar.
  992. * <p>Valid values are:
  993. * <UL>
  994. * <li>*ENVVAR No setting is sent from the client to the IBM i system so the
  995. * IBM i system's default value of CLASSPATH is used.
  996. * <li>string The value that is set before the program is run.
  997. * </UL>
  998. *
  999. * @param classPath The value of the classpath.
  1000. * @exception PropertyVetoException If the change is vetoed.
  1001. **/
  1002. public void setClassPath(String classPath) throws PropertyVetoException
  1003. {
  1004. if (classPath != null)
  1005. {
  1006. String old = this.classPath_;
  1007. vetoableChange_.fireVetoableChange("classPath",old,classPath);
  1008. this.classPath_ = classPath;
  1009. propertyChange_.firePropertyChange("classPath",old,classPath);
  1010. }
  1011. else
  1012. throw new NullPointerException("classPath");
  1013. }
  1014. /**
  1015. Sets the default port. This is the port for stdin.
  1016. stdout is (port + 1) and stderr is (port + 2). {@link #setFindPort setFindPort()}
  1017. can be used to tell this class to search for a free port if these
  1018. ports are in use.
  1019. @param port The default port.
  1020. @exception PropertyVetoException If the change is vetoed.
  1021. **/
  1022. public void setDefaultPort(int port) throws PropertyVetoException
  1023. {
  1024. if (port > 0)
  1025. {
  1026. Integer old = new Integer(this.defaultPort_);
  1027. vetoableChange_.fireVetoableChange("defaultPort", old, new Integer(port));
  1028. this.defaultPort_ = port;
  1029. propertyChange_.firePropertyChange("defaultPort", old, new Integer(port));
  1030. }
  1031. else
  1032. throw new IllegalArgumentException("defaultPort");
  1033. }
  1034. /**
  1035. Sets searching for a free port.
  1036. @param search true to search for a port that is not in use; false to not search.
  1037. The default value is <tt>true</tt>.
  1038. @exception PropertyVetoException If the change is vetoed.
  1039. **/
  1040. public void setFindPort(boolean search) throws PropertyVetoException
  1041. {
  1042. Boolean old = new Boolean(findPort_);
  1043. vetoableChange_.fireVetoableChange("findPort",old,new Boolean(search));
  1044. findPort_ = search;
  1045. propertyChange_.firePropertyChange("findPort",old,new Boolean(search));
  1046. }
  1047. /**
  1048. Sets the relative frequency that garbage collection runs.
  1049. This parameter is valid only for OS/400 V4R2 and V4R3 versions.
  1050. It is ignored for V4R4 and later versions.
  1051. @param frequency The relative frequency that garbage collection runs.
  1052. @exception PropertyVetoException If the change is vetoed.
  1053. **/
  1054. public void setGarbageCollectionFrequency(int frequency) throws PropertyVetoException
  1055. {
  1056. if ((frequency >= 0) && (frequency <= 100))
  1057. {
  1058. int old = this.garbageCollectionFrequency_;
  1059. vetoableChange_.fireVetoableChange("garbageCollectionFrequency",new Integer(old),new Integer(frequency));
  1060. this.garbageCollectionFrequency_ = frequency;
  1061. propertyChange_.firePropertyChange("garbageCollectionFrequency",new Integer(old),new Integer(frequency));
  1062. }
  1063. else
  1064. throw new IllegalArgumentException("garbageCollectionFrequency");
  1065. }
  1066. /**
  1067. Sets the initial size, in kilobytes, of the garbage collection
  1068. heap. This is used to prevent garbage collection from starting on
  1069. small programs.
  1070. <p>The possible values are:
  1071. <UL>
  1072. <li>2048 The default initial size is 2048 kilobytes.
  1073. <li>heap-initial-size The initial value of the garbage collection heap, in
  1074. kilobytes. We recommend that the initial heap size be
  1075. set to 2048 kilobytes (the default) or larger.
  1076. </UL>
  1077. @param size The initial size of the garbage collection heap.
  1078. @exception PropertyVetoException If the change is vetoed.
  1079. **/
  1080. public void setGarbageCollectionInitialSize(int size) throws PropertyVetoException
  1081. {
  1082. if (size > 0)
  1083. {
  1084. if (Trace.traceOn_)
  1085. {
  1086. int sizeValue = -1;
  1087. boolean nomax = false;
  1088. try
  1089. {
  1090. sizeValue = Integer.parseInt(getGarbageCollectionMaximumSize());
  1091. }
  1092. catch(NumberFormatException e)
  1093. {
  1094. nomax = true;
  1095. }
  1096. if (!nomax && size > sizeValue)
  1097. {
  1098. Trace.log(Trace.WARNING , "Initial size ("+size+") exceeds specified maximum size (" + sizeValue + ")");
  1099. }
  1100. }
  1101. int old = this.garbageCollectionInitialSize_;
  1102. vetoableChange_.fireVetoableChange("garbageCollectionInitialSize",new Integer(old),new Integer(size));
  1103. this.garbageCollectionInitialSize_ = size;
  1104. propertyChange_.firePropertyChange("garbageCollectionInitialSize",new Integer(old),new Integer(size));
  1105. }
  1106. else
  1107. throw new IllegalArgumentException("garbageCollectionInitialSize");
  1108. }
  1109. /**
  1110. Sets the maximum size, in kilobytes, that the garbage
  1111. collection heap can grow to. This is used to prevent runaway
  1112. programs that consume all of the available storage. Normally,
  1113. garbage collection runs as an asynchronous thread in parallel with
  1114. other threads. If the maximum size is reached, all other threads
  1115. are stopped while garbage collection takes place. This may
  1116. adversely affect performance.
  1117. <p>The possible values are:
  1118. <UL>
  1119. <li>*NOMAX The default maximum size is not specified by the user. The
  1120. maximum is determined by the system. The heap will grow until
  1121. all system resources are depleted. Then a synchronous garbage
  1122. collection is started to reclaim resources no longer in use.
  1123. <li>heap-maximum-size The value in kilobytes that the garbage collection
  1124. heap can grow to.
  1125. </UL>
  1126. @param size The maximum size that the garbage collection heap can grow to.
  1127. @exception PropertyVetoException If the change is vetoed.
  1128. **/
  1129. public void setGarbageCollectionMaximumSize(String size) throws PropertyVetoException
  1130. {
  1131. if (size != null)
  1132. {
  1133. if (!size.toUpperCase().equalsIgnoreCase("*NOMAX"))
  1134. {
  1135. try
  1136. {
  1137. Integer.parseInt(size);
  1138. }
  1139. catch(NumberFormatException e)
  1140. {
  1141. throw new IllegalArgumentException("garbageCollectionMaximumSize");
  1142. }
  1143. }
  1144. String old = this.garbageCollectionMaximumSize_;
  1145. vetoableChange_.fireVetoableChange("garbageCollectionMaximumSize",old,size);
  1146. this.garbageCollectionMaximumSize_ = size;
  1147. propertyChange_.firePropertyChange("garbageCollectionMaximumSize",old,size);
  1148. }
  1149. else
  1150. throw new NullPointerException("garbageCollectionMaximumSize");
  1151. }
  1152. /**
  1153. Sets the priority of the tasks running garbage collection.
  1154. This parameter is valid only for OS/400 V4R3 and V4R2 versions.
  1155. It is ignored for V4R4 and later versions.
  1156. @param priority The priority of the tasks running garbage collection.
  1157. @exception PropertyVetoException If the change is vetoed.
  1158. **/
  1159. public void setGarbageCollectionPriority(int priority) throws PropertyVetoException
  1160. {
  1161. if (priority == 10 || priority == 20 || priority == 30)
  1162. {
  1163. int old = this.garbageCollectionPriority_;
  1164. vetoableChange_.fireVetoableChange("garbageCollectionPriority",new Integer(old),new Integer(priority));
  1165. this.garbageCollectionPriority_ = priority;
  1166. propertyChange_.firePropertyChange("garbageCollectionPriority",new Integer(old),new Integer(priority));
  1167. }
  1168. else
  1169. throw new IllegalArgumentException("garbageCollectionPriority");
  1170. }
  1171. /**
  1172. Sets whether all Java class files should be run interpretively.
  1173. <p>The possible values are:
  1174. <UL>
  1175. <li>*OPTIMIZE Whether all Java classes are run interpretively depends on the
  1176. value specified for the OPTIMIZE property. If
  1177. *INTERPRET, all Java classes will be run
  1178. interpretively. If any other value was specified for the
  1179. OPTIMIZE property, only Java classes with Java programs created
  1180. using CRTJVAPGM command and specifying OPTIMIZE(*INTERPRET) will
  1181. be run interpretively.
  1182. <li>*NO Only Java classes with Java programs created using CRTJVAPGM
  1183. command and specifying OPTIMIZE(*INTERPRET) will be run
  1184. interpretively. Java classes that need a Java program created
  1185. will be created with the optimization level specified in the
  1186. OPTIMIZE parameter.
  1187. <li>*YES All Java classes will be run interpretively regardless of the
  1188. OPTIMIZE value associated Java program. Java classes that need
  1189. a Java program created will use the optimization level specified
  1190. in the OPTIMIZE parameter.
  1191. <li>*JIT All Java class files will be run using the just in time
  1192. compiler (JIT) regardless of the OPTIMIZE value
  1193. used when the associated Java program was created.
  1194. @param interpret How all Java class files should be run interpretively.
  1195. @exception PropertyVetoException If the change is vetoed.
  1196. **/
  1197. public void setInterpret(String interpret) throws PropertyVetoException
  1198. {
  1199. if (interpret != null)
  1200. {
  1201. if (interpret.equalsIgnoreCase("*OPTIMIZE") ||
  1202. interpret.equalsIgnoreCase("*NO") ||
  1203. interpret.equalsIgnoreCase("*YES") ||
  1204. interpret.equalsIgnoreCase("*JIT")) // @D2a
  1205. {
  1206. String old = this.interpret_;
  1207. vetoableChange_.fireVetoableChange("interpret",old,interpret);
  1208. this.interpret_ = interpret;
  1209. propertyChange_.firePropertyChange("interpret",old,interpret);
  1210. }
  1211. else
  1212. throw new IllegalArgumentException("interpret");
  1213. }
  1214. else
  1215. throw new NullPointerException("interpret");
  1216. }
  1217. /**
  1218. Sets the Java application to be run.
  1219. @param application The Java application to be run.
  1220. @exception PropertyVetoException If the change is vetoed.
  1221. **