3737import org .python .google .common .base .Throwables ;
3838import org .robotframework .javalib .annotation .Autowired ;
3939import org .robotframework .javalib .library .AnnotationLibrary ;
40- import org .robotframework .remoteserver .RemoteServer ;
4140
4241import javax .script .ScriptEngine ;
4342import javax .script .ScriptEngineManager ;
@@ -90,12 +89,81 @@ public static String loadRobotLibraryVersion() {
9089 @ Autowired
9190 protected RunOnFailure runOnFailure ;
9291
92+ @ Override
93+ public Object runKeyword (String keywordName , List args , Map kwargs ) {
94+
95+ if (kwargs == null ) {
96+ kwargs = new HashMap ();
97+ }
98+
99+ List finalArgs ;
100+ Map finalKwargs ;
101+
102+ // JavalibCore changes arguments of Call Method keywords to Strings after this check, so they need to handle their own objectMapping
103+ if (!(keywordName .equals ("callObjectMethod" ) || keywordName .equals ("callObjectMethodInFxApplicationThread" ))) {
104+ finalArgs = HelperFunctions .useMappedObjects (args );
105+ finalKwargs = HelperFunctions .useMappedObjects (kwargs );
106+ } else {
107+ finalArgs = args ;
108+ finalKwargs = kwargs ;
109+ }
110+
111+
112+ AtomicReference <Object > retval = new AtomicReference <>();
113+ AtomicReference <RuntimeException > retExcep = new AtomicReference <>();
114+
115+ try {
116+ RobotLog .ignoreDuplicates ();
117+ // timeout + 500 ms so that underlying timeout has a chance to expire first
118+ waitFor (getWaitUntilTimeout (TimeUnit .MILLISECONDS ) + 500 , TimeUnit .MILLISECONDS , () -> {
119+
120+ try {
121+ retval .set (super .runKeyword (keywordName , finalArgs , finalKwargs ));
122+ return true ;
123+
124+ } catch (JavaFXLibraryTimeoutException jfxte ){
125+ // timeout already expired, catch exception and jump out
126+ retExcep .set (jfxte );
127+ throw jfxte ;
128+
129+ } catch (RuntimeException e ){
130+ // catch exception and continue trying
131+ retExcep .set (e );
132+ return false ;
133+ }
134+ });
135+ } catch (TimeoutException te ) {
136+ RobotLog .reset ();
137+ RuntimeException e = retExcep .get ();
138+ runOnFailure .runOnFailure ();
139+
140+ if (e .getCause () instanceof JavaFXLibraryFatalException ) {
141+ RobotLog .trace ("JavaFXLibrary: Caught JavaFXLibrary FATAL exception: \n " + Throwables .getStackTraceAsString (e ));
142+ throw e ;
143+ } else if (e .getCause () instanceof JavaFXLibraryNonFatalException ) {
144+ RobotLog .trace ("JavaFXLibrary: Caught JavaFXLibrary NON-FATAL exception: \n " + Throwables .getStackTraceAsString (e ));
145+ throw e ;
146+ } else {
147+ RobotLog .trace ("JavaFXLibrary: Caught JavaFXLibrary RUNTIME exception: \n " + Throwables .getStackTraceAsString (e ));
148+ throw e ;
149+ }
150+ } catch (JavaFXLibraryTimeoutException jfxte ) {
151+ RobotLog .reset ();
152+ RobotLog .trace ("JavaFXLibrary: Caught JavaFXLibrary TIMEOUT exception: \n " + Throwables .getStackTraceAsString (jfxte ));
153+ throw jfxte ;
154+ }
155+ RobotLog .reset ();
156+ return retval .get ();
157+ }
158+
93159 // overriding the run method to catch the control in case of failure, so that desired runOnFailureKeyword
94160 // can be executed in controlled manner.
95161 @ Override
96- public Object runKeyword (String keywordName , Object [] args ) {
162+ public Object runKeyword (String keywordName , List args ) {
163+ // TODO: Check if this is ever called anymore
164+ RobotLog .info ("runKeyword called with args ONLY" );
97165
98- Object [] finalArgs ;
166+ List finalArgs ;
99167 // JavalibCore changes arguments of Call Method keywords to Strings after this check, so they need to handle their own objectMapping
100168 if (!(keywordName .equals ("callObjectMethod" ) || keywordName .equals ("callObjectMethodInFxApplicationThread" )))
101169 finalArgs = HelperFunctions .useMappedObjects (args );
@@ -190,22 +258,23 @@ public static JavaFXLibrary getLibraryInstance() throws ScriptException {
190258 }
191259
192260 public static void main (String [] args ) throws Exception {
193- JavaFXLibraryRemoteServer .configureLogging ();
194- System .out .println ("---------------------------= JavaFXLibrary =---------------------------- " );
195- RemoteServer server = new JavaFXLibraryRemoteServer ();
196- server .putLibrary ("/RPC2" , new JavaFXLibrary ());
197261 int port = 8270 ;
198262 InetAddress ipAddr = InetAddress .getLocalHost ();
199263
200264 try {
201- if (args .length > 0 )
265+ JavaFXLibraryRemoteServer .configureLogging ();
266+ System .out .println ("----------------------------= JavaFXLibrary =-----------------------------" );
267+ if (args .length > 0 ) {
202268 port = Integer .parseInt (args [0 ]);
203- else
269+ }
270+ else {
204271 System .out .println ("RemoteServer for JavaFXLibrary will be started at default port of: " + port + ".\n " +
205272 "If you wish to use another port, restart the library and give port number\n " +
206273 "as an argument." );
274+ }
207275
208- server .setPort (port );
276+ JavaFXLibraryRemoteServer server = new JavaFXLibraryRemoteServer (port );
277+ server .putLibrary ("/RPC2" , new JavaFXLibrary ());
209278 server .start ();
210279 System .out .println ("\n JavaFXLibrary " + ROBOT_LIBRARY_VERSION + " is now available at: " +
211280 ipAddr .getHostAddress () + ":" + port + "\n " );
@@ -219,6 +288,9 @@ public static void main(String[] args) throws Exception {
219288 } catch (BindException be ) {
220289 System .out .println ("\n Error! " + be .getMessage () + ": " + ipAddr .getHostAddress () + ":" + port + "\n " );
221290 System .exit (1 );
291+ } catch (IOException ioe ) {
292+ System .out .println ("\n Error! " + ioe .getMessage () + ": " + ipAddr .getHostAddress () + ":" + port + "\n " );
293+ System .exit (1 );
222294 }
223295 }
224296}
0 commit comments