Content

GO TO THE GERMAN SITE

Softhema Group [www.softhema.de]

Introduction
Softhema JPA - Java Process Automation API Version 1.3

Introduction
JPA is a class library, that gives you access to running applications on the operating system. An application is thereby a process, which uses system resources. To system resources of a process also belongs established threads. A special Thread is the so-called user-interface-thread, in turn this thread creates and manages the windows and control elements of the user-interface. Further operating system resources are program modules. A special feature of the JPA-API is the ability to access user-interfaces of external processes. Like from a magic hand, the applications are operated by simulating keyboard inputs and mouse events for windows and control elements. The operation of an application can be automated with JPA-API. Complexe and frequent user inputs are accomplished through program control. From a visual point of view, JPA-API is like a software-robot, which simplify or extend the user-interface of an existing software system. To reduce frequent mouse and keyboard inputs, the JPA-API can accomplish this task for you. New functionalities can be added to existing applications without having to know details about their programming. There are many applications of JPA-API. More to this see use-cases.
In designing the JPA architecture, a lot of emphasis was made to make sure that the interfaces for processes, threads, windows and program modules are independent of the operating system. The JPA interfaces serves as common denominator for widely-used operating systems with a window-based user-interface. Implementing JPA-API in operating systems other than Microsoft Windows is principly feasible without big changes to the interfaces. The advantage for this neutral design is that the integrator doesn't have special programming knowledge of the operating system. Specific extensions of the operating system are nevertheless made available by extended classes/interfaces. The interface AppProcess represents for example a started application. The interface AppProcess_win32 is derived from the interface AppProcess. AppProcess_win32 makes specific functionalities of the operating system available. Another simular example is the interface AppWindow. Through the method getWindowHandle() of the extended interface AppWindow_win32, which represents a Win32-window, can the HWND of the window be requested. The HWND is a handle (number, integer value), which uniquely identifies a window.

The following is a list of some JPA features:

  • launching an application (program)
  • getting all running processes on the computer
  • getting the running threads of a process
  • requesting the toplevel windows(frame and dialogue windows) of an application
  • accessing and manipulating of user-interfaces
  • catching of events within user-interfaces
  • accessing control elements like pushbuttons, lists, dropdown lists, labels and input text fields
  • requesting program modules (executable file and libraries) loaded in the memory of a process
  • loading program libraries (in Windows: DLL) and calling functions from the library

The class AutomationManager and the interface ApplicationController play an important role in the JPA-API. AutomationManager is a class, which realizes the design pattern singleton. The design pattern singleton allows requesting a single instance of the class AutomationManager through the method getInstance() from anywhere in your source code. ApplicationController is an interface which can be requested from the AutomationManager. The ApplicationController represents the operating system (computer). The ApplicationController allows launching of applications and requesting of all running processes on a computer. Furthermore specific extensions of the operating system offered by a specialized interface. Under Microsoft Windows, the interface ApplicationController_win32 makes DDE (dynamic DATA Exchange) for interprocess communication and accessing the Windows Registry (configuration database) available. Basically, for integrating JPA-API in your program, the ApplicationController must be requested. The local ApplicationController refers to the computer, on which the JVM runs your program.
The introductory code snippet follows:
  import softhema.system.automation.*;
  import softhema.system.automation.win32.*;
  .
  .
  .
  AutomationManager manager = AutomationManager.getInstance();
  ApplicationController controller = manager.getApplicationControllerLocal();
  
  //Specialized for windows:
  ApplicationController_win32 controller_win32 = (ApplicationController_win32) controller;
		   

Through the instance of the ApplicationController one can request an array with current running processes:
  AppProcess[] processes = controller.getAppProcesses();  
  

An important task of ApplicationController is starting an application:
  AppProcess process = controller.exec("calc.exe");
  
The return value is an object, which represents the started application(process). Through the process-object one can request the main window. After the start of application, the main window is not started immediately. For this reason one should insert a short delay period. The source code section follows:
   Thread.sleep(1000);
   AppWindowTopLevel windowMain = process.getWindowMain();
  
The returned object represents the frame-window of the application.

Ein spezieller Anwendungsbereich eröffnet sich durch den entfernten Zugriff auf Rechner. Dies wird durch eine RMI Client/Server-Architektur realisiert. Der Server, genannt Remote-Application-Controller, muss dazu auf dem Hostrechner gestartet werden. Mehr dazu unter Start des Remote-Application-Controllers. Über die IP-Addresse des Hostrechners kann man nun mittels einer Internet/Intranet-Verbindung den Remote-Application-Controller ansprechen. Der Rechner von dem die Verbindung aufgebaut wird, wird als RMI-Client bezeichnet. Bei dem RMI-Client handelt es sich um ein ganz normales Java-Programm, welches die JPA-API einbindet und auf die Betriebssystem-Objekte des ApplicationController zugreift. Programmtechnisch kann derselbe Quellcode wiederverwendet werden, wie beim lokalen ApplicationController. Unterscheiden tut sich nur die Quellcodezeile zur Abfrage des ApplicationController zu vorherigen Quellcodebeispielen:
   ApplicationController controller = manager.getApplicationControllerRemote( "hostname or ip-address" );	 
	 
Beim Aufruf der Methode getApplicationControllerRemote muss die IP-Adresse oder der Hostname des Rechners angegeben werden, auf welchem der Remote-Application-Controller gestartet wurde. Bevor der Remote-Application-Controller über das Internet/Intranet verfügbar ist, muss dieser noch Online geschaltet werden.

Die Core-API von Java stellt bereits eine Möglichkeit zum Starten von externen Anwendungen bereit. Dies geschieht über die Methode exec(...) der Klasse Runtime.

Die gleiche Funktionalität wird über die Methode exec(...) der Klasse ApplicationController der JPA-API bereitgestellt. Zusätzlich wurde exec um einige Parameter erweitert. Jetzt ist es möglich auf das Erscheinungsbild des Hauptfensters nach dem Starten der Anwendung Einfluss zu nehmen. Die gewünschte Position und der Anzeigestatus (maximiert, normal und minimiert) des Hauptfensters kann angegeben werden. Allerdings ist anzumerken, dass einige Anwendungen manche Optionen ignorieren und diese deshalb ohne Wirkung sind.
Die Kernfunktionalität der JPA-API ist es Ihnen den Zugriff auf die Benutzeroberfläche des neugestarteten Prozesses zu ermöglichen. Dazu wurde die JPA-API als objekt-orientierte Klassenbibliothek entworfen. Jedes Betriebssystem-Objekt wird durch spezialisierte Java-Klassen repräsentiert. Im folgenden werden die Zusammenhänge näher erläutert.

In einem Prozess arbeiten ein oder mehrere Threads den Programmcode ab. Threads sind als leichtgewichtige Prozesse anzusehen. Alle Threads eines Prozesses teilen sich den gleichen virtuellen Arbeitsspeicher und haben Zugriff auf die Ressourcen des Programms. Nach dem Start eines Prozesses wird zunächst ein sogenannter Hauptthread (main thread) in der main()-Funtion mit der Ausführung des Programms beginnen. Handelt es sich um eine Anwendung mit einer Benutzeroberfläche, dann wird zumeist der Hauptthread für das Erzeugen von Dialogen und Fenstern zuständig sein. Während der Ausführung eines Prozesses werden Module in den Speicher geladen.
Das obige Bild zeigt nun die Abhängigkeiten zwischen diesen Betriebssystem-Objekten. Ein Prozess hat ein oder mehrere Threads. Beim ersten Thread handelt es sich um den Hauptthread. Ein Thread wiederum kann Fenster einer Benutzeroberfläche besitzen. Falls Ereignisse in den Fenstern auftreten, wird der Thread entsprechend reagieren. Unter UML-Klassendiagramm der JPA-API werden einige wichtige Klassen und deren Beziehungen zueinander aufgezeigt.

Content