1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util.launchutils;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import net.java.sip.communicator.util.*;
/**
* The <tt>LauncherArgHandler</tt> class handles invocation arguments that have
* been passed to us when running SIP Communicator. The class supports a fixed
* set of options and also allows for registration of delegates.
*
* @author Emil Ivov <emcho at sip-communicator.org>
*/
public class LaunchArgHandler
{
/**
* Our class logger.
*/
private static final net.java.sip.communicator.util.Logger logger =
net.java.sip.communicator.util.Logger.getLogger(LaunchArgHandler.class);
/**
* The name of the property that contains the location of the SC
* configuration directory.
*/
private static final String PNAME_SC_HOME_DIR_LOCATION =
"net.java.sip.communicator.SC_HOME_DIR_LOCATION";
/**
* The name of the property that stores the home dir for cache data, such
* as avatars or spelling dictionaries.
*/
private static final String PNAME_SC_CACHE_DIR_LOCATION =
"net.java.sip.communicator.SC_CACHE_DIR_LOCATION";
/**
* The name of the property that stores the home dir for application logs
* (not history).
*/
private static final String PNAME_SC_LOG_DIR_LOCATION =
"net.java.sip.communicator.SC_LOG_DIR_LOCATION";
/**
* The name of the property that contains the name of the SC configuration
* directory.
*/
private static final String PNAME_SC_HOME_DIR_NAME =
"net.java.sip.communicator.SC_HOME_DIR_NAME";
/**
* Returned by the <tt>handleArgs</tt> methods when the arguments that have
* been parsed do not require for SIP Communicator to be started and the
* Launcher is supposed to exit. That could happen when "SIP Communicator"
* is launched with a --version argument for example or when trying to
* run the application after an instance was already launched.
*/
public static final int ACTION_EXIT = 0;
/**
* Returned by the <tt>handleArgs</tt> methods when all arguments have been
* parsed and the SIP Communicator launch can continue.
*/
public static final int ACTION_CONTINUE = 1;
/**
* Returned by the <tt>handleArgs</tt> method when parsing the arguments
* has failed or if no arguments were passed and an instance of SC was
* already launched. If this is the code returned by handleArgs, then the
* <tt>getErrorCode</tt> method would return an error code indicating what
* the error was.
*/
public static final int ACTION_ERROR = 2;
/**
* Returned by the <tt>handleArgs</tt> methods when all arguments have been
* successfully parsed and one of them indicates that the user has requested
* a multi instance launch.
*/
public static final int ACTION_CONTINUE_LOCK_DISABLED = 3;
/**
* The error code returned when we couldn't parse one of the options.
*/
public static final int ERROR_CODE_UNKNOWN_ARG = 1;
/**
* The error code returned when we try to launch SIP Communicator while
* there is already a running instance and there were no arguments that we
* forward to that instance.
*/
public static final int ERROR_CODE_ALREADY_STARTED = 2;
/**
* The error code that we return when we fail to create a directory that has
* been specified with the -c|--config option.
*/
public static final int ERROR_CODE_CREATE_DIR_FAILED = 3;
/**
* The property name containing the name of the application
* (e.g. SIP Communicator)
*/
private static final String PNAME_APPLICATION_NAME = "APPLICATION_NAME";
/**
* The package name of the applications (e.g. jitsi).
*/
private static final String PNAME_PACKAGE_NAME = "PACKAGE_NAME";
/**
* The property name containing the current version.
*/
private static final String PNAME_VERSION = "APPLICATION_VERSION";
/**
* The name of the file containing version properties for use with the
* argument handler.
*/
private static final String VERSION_PROPERTIES = "version.properties";
/**
* The errorCode identifying the error that occurred last time
* <tt>handleArgs</tt> was called.
*/
private int errorCode = 0;
/**
* A reference to the instance of the
*/
private ArgDelegator argDelegator = new ArgDelegator();
/**
* The singleton instance of this handler.
*/
private static LaunchArgHandler argHandler = null;
/**
* The properties where we load version info from our update location.
*/
private Properties versionProperties = new Properties();
/**
* Creates the sole instance of this class;
*/
private LaunchArgHandler()
{
InputStream versionPropertiesStream =
getClass().getResourceAsStream(VERSION_PROPERTIES);
boolean versionPropertiesAreLoaded = false;
if (versionPropertiesStream != null)
{
try
{
try
{
versionProperties.load(versionPropertiesStream);
versionPropertiesAreLoaded = true;
}
finally
{
versionPropertiesStream.close();
}
}
catch (IOException exc)
{
// no need to worry the user, so only print if we're in FINEST
}
}
if (!versionPropertiesAreLoaded)
{
if (logger.isTraceEnabled())
logger.trace("Couldn't open version.properties");
}
// Start url handler for Mac OS X.
/*
* XXX The detection of the operating systems is the responsibility of
* OSUtils. It used to reside in the util.jar which is in the classpath
* but it is now in libjitsi.jar which is not in the classpath.
*/
String osName = System.getProperty("os.name");
if ((osName != null) && osName.startsWith("Mac"))
new AEGetURLEventHandler(this);
}
/**
* Creates a singleton instance of the LauncherArgHandler if necessary and
* returns a reference to it.
*
* @return the singleton instance of the LauncherArgHandler.
*/
public static LaunchArgHandler getInstance()
{
if(argHandler == null)
{
argHandler = new LaunchArgHandler();
}
return argHandler;
}
/**
* Does the actual argument handling.
*
* @param args the arguments the way we have received them from the main()
* method.
*
* @return one of the ACTION_XXX fields defined here, intended to indicate
* to the caller they action that they are supposed as a result of the arg
* handling.
*/
public int handleArgs(String[] args)
{
int returnAction = ACTION_CONTINUE;
for(int i = 0; i < args.length; i++)
{
if (logger.isTraceEnabled())
logger.trace("handling arg " + i);
if (args[i].equals("--version") || args[i].equals("-v"))
{
handleVersionArg();
//we're supposed to exit after printing version info
returnAction = ACTION_EXIT;
break;
}
else if (args[i].equals("--help") || args[i].equals("-h"))
{
handleHelpArg();
//we're supposed to exit after printing the help message
returnAction = ACTION_EXIT;
break;
}
else if (args[i].equals("--debug") || args[i].equals("-d"))
{
handleDebugArg(args[i]);
continue;
}
else if (args[i].equals("--ipv6") || args[i].equals("-6"))
{
handleIPv6Enforcement();
break;
}
else if (args[i].equals("--ipv4") || args[i].equals("-4"))
{
handleIPv4Enforcement();
break;
}
else if (args[i].startsWith("--config="))
{
returnAction = handleConfigArg(args[i]);
if(returnAction == ACTION_ERROR)
break;
else
continue;
}
else if (args[i].equals("-c"))
{
//make sure we have at least one more argument left.
if( i == args.length - 1)
{
System.out.println(
"The \"-c\" option expects a directory parameter.");
returnAction = ACTION_ERROR;
break;
}
handleConfigArg(args[++i]);
continue;
}
else if (args[i].equals("--multiple") || args[i].equals("-m"))
{
returnAction = ACTION_CONTINUE_LOCK_DISABLED;
continue;
}
else if (args[i].startsWith("--splash="))
{
// do nothing already handled by startup script/binary
continue;
}
//if this is the last arg and it's not an option then it's probably
//an URI
else if ( i == args.length - 1
&& !args[i].startsWith("-"))
{
handleUri(args[i]);
}
else
{
handleUnknownArg(args[i]);
errorCode = ERROR_CODE_UNKNOWN_ARG;
returnAction = ACTION_ERROR;
break;
}
}
return returnAction;
}
/**
* Forces use of IPv6 addresses where possible. (This should one day
* become a default mode of operation.)
*/
private void handleIPv6Enforcement()
{
System.setProperty("java.net.preferIPv4Stack", "false");
System.setProperty("java.net.preferIPv6Addresses", "true");
}
/**
* Forces non-support for IPv6 and use of IPv4 only.
*/
private void handleIPv4Enforcement()
{
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
}
/**
* Passes <tt>uriArg</tt> to our uri manager for handling.
*
* @param uri the uri that we'd like to pass to
*/
private void handleUri(String uri)
{
if (logger.isTraceEnabled())
logger.trace("Handling uri "+ uri);
argDelegator.handleUri(uri);
}
/**
* Instructs SIP Communicator to print logging messages to the console.
*
* @param arg the debug arg which we are not really using in this method.
*/
private void handleDebugArg(String arg)
{
//first enable standard out printing
ScStdOut.setStdOutPrintingEnabled(true);
//then find a console handler (or create a new one) and set its level
//to FINEST
java.util.logging.Logger rootLogger
= java.util.logging.Logger.getAnonymousLogger().getParent();
ConsoleHandler conHan = null;
for (Handler handler : rootLogger.getHandlers())
{
if(handler instanceof ConsoleHandler)
{
conHan = (ConsoleHandler) handler;
break;
}
}
if(conHan == null)
{
conHan = new ConsoleHandler();
rootLogger.addHandler(conHan);
}
//conHan.setLevel(Level.SEVERE);
}
/**
* Instructs SIP Communicator change the location of its home dir.
*
* @param configArg the arg containing the location of the new dir.
*
* @return either ACTION_ERROR or ACTION_CONTINUE depending on whether or
* not parsing the option went fine.
*/
private int handleConfigArg(String configArg)
{
if (configArg.startsWith("--config="))
{
configArg = configArg.substring("--config=".length());
}
File configDir = new File(configArg);
configDir.mkdirs();
if(!configDir.isDirectory())
{
System.out.println("Failed to create directory " + configArg);
errorCode = ERROR_CODE_CREATE_DIR_FAILED;
return ACTION_ERROR;
}
System.setProperty(PNAME_SC_HOME_DIR_LOCATION, configDir.getParent());
System.setProperty(PNAME_SC_CACHE_DIR_LOCATION, configDir.getParent());
System.setProperty(PNAME_SC_LOG_DIR_LOCATION, configDir.getParent());
System.setProperty(PNAME_SC_HOME_DIR_NAME, configDir.getName());
//we instantiated our class logger before we had a chance to change
//the dir so we need to reset it now.
logger.reset();
return ACTION_CONTINUE;
}
/**
* Prints the name and the version of this application. This method uses the
* version.properties file which is created by ant during the build process.
* If this file does not exist the method would print a default name and
* version string.
*/
private void handleVersionArg()
{
String name = getApplicationName();
String version = getVersion();
if (name == null || name.trim().length() == 0)
{
name = "Jitsi";
}
if (version == null || version.trim().length() == 0)
{
version = "build.by.SVN";
}
System.out.println(name + " " + version);
}
/**
* Returns the version of the SIP Communicator instance that we are
* currently running.
*
* @return a String containing the version of the SC instance we are
* currently running.
*/
private String getVersion()
{
String version = versionProperties.getProperty(PNAME_VERSION);
return version == null
? "build.by.SVN"
: version;
}
/**
* Returns the name of the application. That should be Jitsi
* most of the time but who knows ..
*
* @return the name of the application (i.e. SIP Communicator until we
* change our name some day.)
*/
private String getApplicationName()
{
String name = versionProperties.getProperty(PNAME_APPLICATION_NAME);
return name == null
? "Jitsi"
: name;
}
/**
* Returns the package name of the application. That should be jitsi
* most of the time but who knows ..
*
* @return the package name of the application.
*/
private String getPackageName()
{
String name = versionProperties.getProperty(PNAME_PACKAGE_NAME);
return name == null
? "jitsi"
: name;
}
/**
* Prints an error message and then prints the help message.
*
* @param arg the unknown argument we need to print
*/
public void handleUnknownArg(String arg)
{
System.out.println("Unknown argument: " + arg);
handleHelpArg();
}
/**
* Prints a help message containing usage instructions and descriptions of
* all options currently supported by Jitsi.
*/
public void handleHelpArg()
{
handleVersionArg();
System.out.println("Usage: " + getPackageName() + " [OPTIONS] [uri-to-call]");
System.out.println("");
System.out.println(" -c, --config=DIR use DIR for config files");
System.out.println(" -d, --debug print debugging messages to stdout");
System.out.println(" -h, --help display this help message and exit");
System.out.println(" -m, --multiple do not ensure single instance");
System.out.println(" -6, --ipv6 prefer IPv6 addresses where possible only");
System.out.println(" -4, --ipv4 forces use of IPv4 only");
System.out.println(" -v, --version display the current version and exit");
}
/**
* Returns an error code that could help identify an error when
* <tt>handleArgs</tt> returns ACTION_ERROR or 0 if everything went fine.
*
* @return an error code that could help identify an error when
* <tt>handleArgs</tt> returns ACTION_ERROR or 0 if everything went fine.
*/
public int getErrorCode()
{
return errorCode;
}
/**
* Sets the <tt>delegationPeer</tt> that would be handling all URIs passed
* as command line arguments to SIP Communicator.
*
* @param delegationPeer the <tt>delegationPeer</tt> that should handle URIs
* or <tt>null</tt> if we'd like to unset a previously set peer.
*/
public void setDelegationPeer(ArgDelegationPeer delegationPeer)
{
this.argDelegator.setDelegationPeer(delegationPeer);
}
/**
* Called when the user has tried to launch a second instance of
* SIP Communicator while a first one was already running. This method
* only handles arguments that need to be handled by a running instance
* of SIP Communicator assuming that simple ones such as "--version" or
* "--help" have been handled by the calling instance.
*
* @param args the args that we need to handle.
*/
public void handleConcurrentInvocationRequestArgs(String[] args)
{
//if we have 1 or more args then we only care about the last one since
//the only interinstance arg we currently know how to handle are URIs.
//Change this if one day we implement fun stuff like inter instance
//command execution.
if(args.length >=1
&& !args[args.length -1].startsWith("-"))
{
this.argDelegator.handleUri(args[args.length -1]);
}
//otherwise, we simply notify SC of the request so that it could do
//stuff like showing the contact list for example.
else
{
this.argDelegator.handleConcurrentInvocationRequest();
}
}
}
|