1 | package dmg.cells.nucleus ; |
2 | import dmg.util.* ; |
3 | import dmg.cells.network.PingMessage ; |
4 | import java.util.*; |
5 | import java.io.* ; |
6 | import java.net.* ; |
7 | import java.lang.reflect. * ; |
8 | |
9 | import org.slf4j.Logger; |
10 | import org.slf4j.LoggerFactory; |
11 | |
12 | /** |
13 | * |
14 | * |
15 | * @author Patrick Fuhrmann |
16 | * @version 0.1, 15 Feb 1998 |
17 | */ |
18 | public class CellShell |
19 | extends CommandInterpreter |
20 | implements Replaceable, ClassDataProvider |
21 | { |
22 | private final static Logger _log = |
23 | LoggerFactory.getLogger(CellShell.class); |
24 | private final static Logger _logNucleus = |
25 | LoggerFactory.getLogger(CellNucleus.class); |
26 | |
27 | private final CellNucleus _nucleus ; |
28 | private StringBuilder _contextString = null ; |
29 | private String _contextName = null ; |
30 | private String _contextDelimiter = null ; |
31 | private StringBuilder _envString = null ; |
32 | private String _envName = null ; |
33 | private String _envDelimiter = null ; |
34 | private int _helpMode = 1 ; |
35 | private int _errorCode = 0 ; |
36 | private String _errorMsg = null ; |
37 | private String _doOnExit = null ; |
38 | private final Map<String, Object> _environment = |
39 | CollectionFactory.newConcurrentHashMap(); |
40 | private final ClassLoaderFactory _classLoaderFactory = new ClassLoaderFactory() ; |
41 | private CommandInterpreter _externalInterpreter = null ; |
42 | private String _classProvider = null ; |
43 | private List<String> _argumentVector = new Vector<String>() ; |
44 | |
45 | public CellShell( CellNucleus nucleus ){ |
46 | _nucleus = nucleus ; |
47 | try { |
48 | objectCommand( "exec context shellProfile" ) ; |
49 | } catch (CommandExitException e) { |
50 | } |
51 | } |
52 | |
53 | public CellShell(CellAdapter cell) |
54 | { |
55 | this(cell.getNucleus()); |
56 | _externalInterpreter = cell; |
57 | } |
58 | |
59 | /** |
60 | * Returns the environment of the shell. |
61 | * |
62 | * The map is backed by the shell's environment. Any modification |
63 | * to the map changes the shell environment and any change in the |
64 | * shell environment is reflected in the map. |
65 | */ |
66 | public Map<String,Object> environment() |
67 | { |
68 | return _environment; |
69 | } |
70 | |
71 | public String getReplacement( String name ){ |
72 | Object o = getDictionaryEntry( name ) ; |
73 | return o == null ? null : o.toString() ; |
74 | |
75 | } |
76 | private static long __sequenceNumber = 1000000L ; |
77 | private static synchronized long nextSequenceNumber(){ |
78 | return __sequenceNumber ++ ; |
79 | } |
80 | public Object getDictionaryEntry( String name ){ |
81 | if( name.equals( "rc" ) ){ |
82 | return ""+_errorCode ; |
83 | }else if( name.equals( "rmsg" ) ){ |
84 | return (_errorMsg==null?"(0)":_errorMsg) ; |
85 | }else if( name.equals( "thisDomain" ) ){ |
86 | return _nucleus.getCellDomainName() ; |
87 | }else if( name.equals( "thisCell" ) ){ |
88 | return _nucleus.getCellName() ; |
89 | }else if( name.equals( "nextSequenceNumber" ) ){ |
90 | return ""+nextSequenceNumber() ; |
91 | }else if( name.equals( "thisHostname" ) ){ |
92 | try{ |
93 | String xname = InetAddress.getLocalHost().getHostName() ; |
94 | return new StringTokenizer( xname , "." ).nextToken() ; |
95 | } catch (UnknownHostException e) { |
96 | return "UnknownHostname"; |
97 | } |
98 | }else if( name.equals( "thisFqHostname" ) ){ |
99 | try{ |
100 | return InetAddress.getLocalHost().getCanonicalHostName() ; |
101 | } catch (UnknownHostException e) { |
102 | return "UnknownHostname"; |
103 | } |
104 | }else { |
105 | try { |
106 | int position = Integer.parseInt(name); |
107 | if (position >= 0 && position < _argumentVector.size()) { |
108 | Object o = _argumentVector.get(position); |
109 | if (o == null) |
110 | throw new IllegalArgumentException(""); |
111 | return o; |
112 | } |
113 | } catch (NumberFormatException e) { |
114 | } |
115 | Object o = _environment.get(name); |
116 | if (o == null) |
117 | o = _nucleus.getDomainContext().get(name); |
118 | return o; |
119 | } |
120 | } |
121 | private String prepareCommand( String string ){ |
122 | // |
123 | // replace the variables ${...} |
124 | // |
125 | String str = Formats.replaceKeywords( string , this ) ; |
126 | |
127 | if( _contextString != null ){ |
128 | // |
129 | // are we in the define context ... |
130 | // |
131 | if( ( str.length() > 0 ) && |
132 | ( str.equals(_contextDelimiter) ) ){ |
133 | |
134 | _nucleus.getDomainContext(). |
135 | put( _contextName , _contextString.toString() ) ; |
136 | _contextString = null ; |
137 | return null ; |
138 | |
139 | } |
140 | _contextString.append( str ).append("\n"); |
141 | return null ; |
142 | }else if( _envString != null ){ |
143 | // |
144 | // are we in the define environment |
145 | // |
146 | if( ( str.length() > 0 ) && |
147 | ( str.equals(_envDelimiter) ) ){ |
148 | |
149 | _environment.put( _envName , _envString.toString() ) ; |
150 | _envString = null ; |
151 | return null ; |
152 | |
153 | } |
154 | _envString.append( str ).append("\n"); |
155 | return null ; |
156 | } |
157 | return str ; |
158 | } |
159 | |
160 | public Object objectCommand2(String strin) { |
161 | String str = null ; |
162 | if( ( str = prepareCommand( strin ) ) == null )return "" ; |
163 | try{ |
164 | |
165 | Object o = null ; |
166 | if( _externalInterpreter != null ){ |
167 | o = _externalInterpreter.command( new Args( str ) ) ; |
168 | }else{ |
169 | o = command( new Args( str ) ) ; |
170 | } |
171 | _errorCode = 0 ; |
172 | _errorMsg = null ; |
173 | if( o == null )return "" ; |
174 | return o ; |
175 | }catch( CommandException ce ){ |
176 | _errorCode = ce.getErrorCode() ; |
177 | _errorMsg = ce.getErrorMessage() ; |
178 | return ce ; |
179 | } |
180 | } |
181 | public Object objectCommand( String strin ) throws CommandExitException { |
182 | String str = null ; |
183 | if( ( str = prepareCommand( strin ) ) == null )return "" ; |
184 | try{ |
185 | |
186 | Object o = null ; |
187 | if( _externalInterpreter != null ){ |
188 | o = _externalInterpreter.command( new Args( str ) ) ; |
189 | }else{ |
190 | o = command( new Args( str ) ) ; |
191 | } |
192 | _errorCode = 0 ; |
193 | _errorMsg = null ; |
194 | if( o == null )return "" ; |
195 | return o ; |
196 | }catch( CommandException ce ){ |
197 | _errorCode = ce.getErrorCode() ; |
198 | _errorMsg = ce.getErrorMessage() ; |
199 | |
200 | if( _doOnExit != null ){ |
201 | if( _doOnExit.equals( "shutdown" ) ) |
202 | throw new CommandExitException( ce.toString() , 666 ) ; |
203 | |
204 | else |
205 | throw new CommandExitException( ce.getErrorMessage() , |
206 | ce.getErrorCode() ) ; |
207 | |
208 | } |
209 | if( ce instanceof CommandSyntaxException ){ |
210 | CommandSyntaxException cse = (CommandSyntaxException)ce ; |
211 | |
212 | StringBuilder sb = new StringBuilder() ; |
213 | sb.append( "Syntax Error : " ). |
214 | append( cse.getMessage() ) ; |
215 | if( _helpMode == 1 ){ |
216 | sb.append( "\nUse 'help' for more information\n" ) ; |
217 | }else if( _helpMode == 2 ){ |
218 | String help = cse.getHelpText() ; |
219 | if( help != null ) |
220 | sb.append( "\n").append( help ).append("\n") ; |
221 | } |
222 | return sb.toString() ; |
223 | }else if( ce instanceof CommandExitException ){ |
224 | if( _externalInterpreter != null ){ |
225 | _externalInterpreter = null ; |
226 | return "external shell exited ... " ; |
227 | }else{ |
228 | throw (CommandExitException) ce ; |
229 | } |
230 | }else if( ce instanceof CommandThrowableException ){ |
231 | CommandThrowableException cte = (CommandThrowableException)ce ; |
232 | StringBuilder sb = new StringBuilder() ; |
233 | sb.append( cte.getMessage()).append(" -> " ) ; |
234 | Throwable t = cte.getTargetException() ; |
235 | sb.append( t.getClass().getName()).append(" : ").append(t.getMessage()).append("\n" ) ; |
236 | return sb.toString() ; |
237 | }else if( ce instanceof CommandPanicException ){ |
238 | CommandPanicException cpe = (CommandPanicException)ce ; |
239 | StringBuilder sb = new StringBuilder() ; |
240 | sb.append( "Panic : ").append(cpe.getMessage()).append("\n" ) ; |
241 | Throwable t = cpe.getTargetException() ; |
242 | sb.append( t.getClass().getName()).append(" : ").append(t.getMessage()).append("\n" ) ; |
243 | return sb.toString() ; |
244 | }else{ |
245 | return "CommandException :"+ce.getMessage() ; |
246 | } |
247 | } |
248 | } |
249 | |
250 | @Override |
251 | public String command( String c ) throws CommandExitException { |
252 | StringTokenizer st = new StringTokenizer( c , "\n" ) ; |
253 | StringBuilder sb = new StringBuilder(); |
254 | for( ; st.hasMoreTokens() ; ){ |
255 | sb.append( commandLine( st.nextToken() ) ) ; |
256 | } |
257 | return sb.toString() ; |
258 | } |
259 | private String commandLine( String c ) throws CommandExitException { |
260 | if( _contextString != null ){ |
261 | _contextString.append( c ).append("\n"); |
262 | return "" ; |
263 | }else |
264 | return super.command( c ) ; |
265 | } |
266 | public Object binCommand( String c ){ |
267 | Args args = new Args( c ) ; |
268 | if( args.argc() == 0 )return "" ; |
269 | String cs = args.argv(0) ; |
270 | if( cs.equals( ".getroutes" ) ){ |
271 | return _nucleus.getRoutingList() ; |
272 | }else if( cs.equals( ".getcelltunnelinfos" ) ){ |
273 | return _nucleus.getCellTunnelInfos() ; |
274 | }else if( cs.equals( ".getcellinfos" ) ){ |
275 | String [] list = _nucleus.getCellNames() ; |
276 | CellInfo [] info = new CellInfo[list.length] ; |
277 | for( int i = 0 ; i < list.length ; i ++ ){ |
278 | info[i] = _nucleus.getCellInfo( list[i] ) ; |
279 | } |
280 | return info ; |
281 | }else{ |
282 | return null ; |
283 | } |
284 | |
285 | } |
286 | //////////////////////////////////////////////////////////// |
287 | // |
288 | // version |
289 | // |
290 | public String hh_version = "[<package>] ; package info of dmg/cells/nucleus" ; |
291 | public Object ac_version_$_0_1( Args args ){ |
292 | Package p = Package.getPackage( args.argc() == 0 ? "dmg.cells.nucleus" : args.argv(0) ); |
293 | StringBuilder sb = new StringBuilder(); |
294 | if( p != null ){ |
295 | String tmp = p.getSpecificationTitle() ; |
296 | sb.append("SpecificationTitle: ").append(tmp==null?"(Unknown)":tmp).append("\n"); |
297 | tmp = p.getSpecificationVendor() ; |
298 | sb.append("SpecificationVendor: ").append(tmp==null?"(Unknown)":tmp).append("\n"); |
299 | tmp = p.getSpecificationVersion() ; |
300 | sb.append("SpecificationVersion: ").append(tmp==null?"(Unknown)":tmp).append("\n"); |
301 | }else{ |
302 | sb.append("No version version found"); |
303 | } |
304 | return sb.toString() ; |
305 | |
306 | } |
307 | //////////////////////////////////////////////////////////// |
308 | // |
309 | // getroutes, getcelltunnelinfos, getcellinfos |
310 | // |
311 | public Object ac_getroutes( Args args ){ |
312 | return _nucleus.getRoutingList() ; |
313 | } |
314 | public Object ac_getcelltunnelinfos( Args args ){ |
315 | return _nucleus.getCellTunnelInfos() ; |
316 | } |
317 | public Object ac_getcellinfo_$_1( Args args ) throws CommandException { |
318 | CellInfo info = _nucleus.getCellInfo( args.argv(0) ) ; |
319 | if( info == null ) |
320 | throw new CommandException( 68 , "not found : "+args.argv(0) ) ; |
321 | |
322 | return info ; |
323 | } |
324 | public Object ac_getcellinfos( Args args ){ |
325 | String [] list = _nucleus.getCellNames() ; |
326 | CellInfo [] info = new CellInfo[list.length] ; |
327 | for( int i = 0 ; i < list.length ; i ++ ){ |
328 | info[i] = _nucleus.getCellInfo( list[i] ) ; |
329 | } |
330 | return info ; |
331 | } |
332 | public Object ac_getcontext_$_0_1( Args args ) throws CommandException { |
333 | if( args.argc() == 0 ){ |
334 | return _nucleus.getDomainContext().keySet().toArray(); |
335 | }else{ |
336 | Object o = _nucleus.getDomainContext( args.argv(0) ) ; |
337 | if( o == null ) |
338 | throw new CommandException( "Context not found : "+args.argv(0) ) ; |
339 | return o ; |
340 | } |
341 | } |
342 | //////////////////////////////////////////////////////////// |
343 | // |
344 | // waitfor cell/domain/context |
345 | // |
346 | public String hh_waitfor= |
347 | "context|cell|domain <objectName> [<domain>] [-i=<checkInterval>] [-wait=<maxTime>]" ; |
348 | public String fh_waitfor = |
349 | "waitfor [options] context <contextName> [<domainName]\n" + |
350 | "waitfor [options] cell <cellPath>\n" + |
351 | "waitfor [options] domain <domainName>\n"+ |
352 | " Options : -i=<probeInterval -wait=<maxWaitSeconds>\n" ; |
353 | |
354 | public String ac_waitfor_$_2_3( Args args ) throws CommandException{ |
355 | int waitTime = 0 ; |
356 | int check = 1 ; |
357 | for( int i = 0 ; i < args.optc() ; i ++ ){ |
358 | if( args.optv(i).startsWith("-i=") ) |
359 | check = Integer.parseInt( args.optv(i).substring(3) ) ; |
360 | else if( args.optv(i).startsWith("-wait=") ) |
361 | waitTime = Integer.parseInt( args.optv(i).substring(6) ) ; |
362 | } |
363 | if( waitTime < 0 )waitTime = 0 ; |
364 | String what = args.argv(0) ; |
365 | String name = args.argv(1) ; |
366 | |
367 | if( what.equals("cell" ) ) |
368 | return _waitForCell( name , waitTime , check , null ) ; |
369 | else if( what.equals( "domain" ) ) |
370 | return _waitForCell( "System@"+name , waitTime ,check , null ) ; |
371 | else if( what.equals( "context" ) ){ |
372 | if( args.argc() > 2 ){ |
373 | return _waitForCell( "System@"+args.argv(2) , |
374 | waitTime ,check , "test context "+name ) ; |
375 | }else{ |
376 | return _waitForContext( name , waitTime ,check ) ; |
377 | } |
378 | } |
379 | |
380 | throw new CommandException( "Unknown Observable : "+what ) ; |
381 | } |
382 | private String _waitForContext( String contextName , int waitTime , int check ) |
383 | throws CommandException { |
384 | |
385 | |
386 | if( check <= 0 )check = 1 ; |
387 | long finish = System.currentTimeMillis() + ( waitTime * 1000 ) ; |
388 | while( true ){ |
389 | Object o = _nucleus.getDomainContext( contextName ) ; |
390 | if( o != null )break ; |
391 | if( ( waitTime == 0 ) || ( finish > System.currentTimeMillis() ) ){ |
392 | try{ Thread.sleep(((long)check)*1000) ; } |
393 | catch( InterruptedException ie ){ |
394 | throw new |
395 | CommandException( 2 , "Command Was interrupted" ) ; |
396 | } |
397 | continue ; |
398 | } |
399 | throw new |
400 | CommandException( 1 , "Command Timed Out" ) ; |
401 | } |
402 | return "" ; |
403 | } |
404 | private String _waitForCell( String cellName , |
405 | int waitTime , int check , |
406 | String command ) |
407 | throws CommandException { |
408 | |
409 | if( check <= 4 )check = 5 ; |
410 | CellPath destination = new CellPath( cellName ) ; |
411 | long finish = System.currentTimeMillis() + ( waitTime * 1000 ) ; |
412 | CellMessage answer = null ; |
413 | // |
414 | // creating the message now and send it forever does not |
415 | // allow time messurements. |
416 | // |
417 | CellMessage request = |
418 | new CellMessage( destination , |
419 | (command == null ? |
420 | (Object) new PingMessage() : (Object)command ) ) ; |
421 | |
422 | Object o = null ; |
423 | boolean noRoute = false ; |
424 | while( true ){ |
425 | try{ |
426 | noRoute = false ; |
427 | answer = null ; |
428 | _log.warn( "waitForCell : Sending request" ) ; |
429 | answer = _nucleus.sendAndWait( request , ((long)check)*1000 ) ; |
430 | _log.warn( "waitForCell : got "+answer ) ; |
431 | }catch( NoRouteToCellException nrtce ){ |
432 | noRoute = true ; |
433 | }catch( InterruptedException e ){ |
434 | throw new |
435 | CommandException( 66 , "sendAndWait problem : "+e.toString() ) ; |
436 | } |
437 | if( ( answer != null ) && |
438 | ( ( o = answer.getMessageObject() ) != null ) && |
439 | ( ( o instanceof PingMessage ) || (o instanceof String) ) |
440 | )break ; |
441 | |
442 | if( ( waitTime == 0 ) || |
443 | ( finish > System.currentTimeMillis() ) ){ |
444 | |
445 | // |
446 | // not to waste cpu time, we should distinquish between |
447 | // between timeout and NoRouteToCellException |
448 | // |
449 | if( ( ! noRoute ) && ( answer == null ) )continue ; |
450 | // |
451 | // this answer was to fast to try it again, so we wait |
452 | // |
453 | try{ Thread.sleep(((long)check)*1000) ; } |
454 | catch( InterruptedException ie ){ |
455 | throw new |
456 | CommandException( 2 , "Command Was interrupted" ) ; |
457 | } |
458 | continue ; |
459 | } |
460 | throw new |
461 | CommandException( 1 , "Command Timed Out" ) ; |
462 | } |
463 | return "" ; |
464 | } |
465 | //////////////////////////////////////////////////////////// |
466 | // |
467 | // set printout <cellname> <level> |
468 | // |
469 | public String hh_set_printout = "<cellname>|CellGlue <level>" ; |
470 | public String fh_set_printout = |
471 | "Syntax: set printout <cellname> <level(hex)>\n\n"+ |
472 | "Obsolete: Replaced by the log4j command set, see help in the\n" + |
473 | " System cell. The printout level now only controls the\n" + |
474 | " log level at which messages generated through the old\n" + |
475 | " logging system are logged to log4j.\n\n" + |
476 | " <cellname> \n"+ |
477 | " Name of the target cell or 'CellGlue' for the kernel or\n"+ |
478 | " 'default' for the printout level of new cells.\n"+ |
479 | " <level> Bitmask of the following fields:\n"+ |
480 | " 1 -> log cell messages at WARN when set\n"+ |
481 | " 2 -> log cell errors at ERROR when set\n"+ |
482 | " 4 -> log nucleus messages at WARN when set\n"+ |
483 | " 8 -> log nucleus error at ERROR when set\n"+ |
484 | " If a field is not set, then the corresponding messages\n"+ |
485 | " are logged at INFO level.\n"; |
486 | public String ac_set_printout_$_2( Args args ){ |
487 | String cellName = args.argv(0) ; |
488 | String ls = args.argv(1) ; |
489 | int level = 0 ; |
490 | if( ls.equals( "errors" ) ){ |
491 | level = CellNucleus.PRINT_ERROR_CELL | CellNucleus.PRINT_ERROR_NUCLEUS ; |
492 | }else if( ls.equals( "all" ) ){ |
493 | level = CellNucleus.PRINT_EVERYTHING ; |
494 | }else if( ls.equals( "none" ) ){ |
495 | level = 0 ; |
496 | }else{ |
497 | level = Integer.parseInt( args.argv(1) , 16 ) ; |
498 | } |
499 | _nucleus.setPrintoutLevel( cellName , level ) ; |
500 | return "Obsolete, see help for details\n" ; |
501 | } |
502 | //////////////////////////////////////////////////////////// |
503 | // |
504 | // route |
505 | // |
506 | public String fh_route = |
507 | " Syntax : route # show all routes\n"+ |
508 | " route add|delete [options] <source> <destination>\n" ; |
509 | |
510 | public String ac_route_$_0( Args args ){ |
511 | return _nucleus.getRoutingTable().toString() ; |
512 | } |
513 | public String hh_route_add = "-options <source> <destination>" ; |
514 | public String fh_route_add = fh_route ; |
515 | public String ac_route_add_$_1_2(Args args) |
516 | throws IllegalArgumentException |
517 | { |
518 | _nucleus.routeAdd( new CellRoute( args ) ); |
519 | return "Done\n" ; |
520 | } |
521 | public String hh_route_delete = "-options <source> <destination>" ; |
522 | public String fh_route_delete = fh_route ; |
523 | public String ac_route_delete_$_1_2(Args args) |
524 | throws IllegalArgumentException |
525 | { |
526 | _nucleus.routeDelete( new CellRoute( args ) ); |
527 | return "Done\n" ; |
528 | } |
529 | public String hh_route_find = "<address>" ; |
530 | public String ac_route_find_$_1( Args args ) |
531 | throws IllegalArgumentException |
532 | { |
533 | CellAddressCore addr = new CellAddressCore( args.argv(0) ) ; |
534 | CellRoute route = _nucleus.routeFind( addr ); |
535 | if( route != null )return route.toString()+"\n" ; |
536 | else return "No Route To cell : "+addr.toString()+"\n" ; |
537 | } |
538 | //////////////////////////////////////////////////////////// |
539 | // |
540 | // ps -af <cellname> |
541 | // |
542 | public String hh_ps = "[-f] [<cellName> ...]" ; |
543 | public String fh_ps = |
544 | " Syntax : ps [-f] [<cellName> ...]\n" + |
545 | " ps displays various attibutes of active cells\n"+ |
546 | " or the full attributes of a particular cell\n" + |
547 | " Options : -f displays a one line comment if\n"+ |
548 | " <cellName> is specified, otherwise\n"+ |
549 | " all available informations (theads,...\n"+ |
550 | " will be shown\n" ; |
551 | public String ac_ps_$_0_99( Args args ){ |
552 | StringBuilder sb = new StringBuilder() ; |
553 | if( args.argc() == 0 ){ |
554 | sb.append( " Cell List\n------------------\n" ) ; |
555 | String [] list = _nucleus.getCellNames() ; |
556 | if( args.optc() > 0 ){ |
557 | for( int i = 0 ; i < list.length ; i ++ ){ |
558 | CellInfo info = _nucleus.getCellInfo( list[i] ) ; |
559 | if( info == null ){ |
560 | sb.append( list[i] ).append(" (defunc)\n" ) ; |
561 | }else{ |
562 | sb.append( info.toString() ).append( "\n" ) ; |
563 | } |
564 | } |
565 | }else{ |
566 | for( int i = 0 ; i < list.length ; i ++ ) |
567 | sb.append( list[i] + "\n" ) ; |
568 | } |
569 | |
570 | }else{ |
571 | boolean full = ( args.optc() > 0 ) && |
572 | ( args.optv(0).indexOf('f') > -1 ) ; |
573 | |
574 | for( int i = 0 ; i < args.argc() ; i++ ){ |
575 | String cellName = args.argv(i) ; |
576 | CellInfo info = _nucleus.getCellInfo( cellName ) ; |
577 | if( info == null ){ |
578 | sb.append( cellName + " Not found\n" ) ; |
579 | continue ; |
580 | } |
581 | if( full ){ |
582 | sb.append( " -- Short Info about Cell "+cellName+" --\n" ) ; |
583 | sb.append( info.toString() + "\n" ) ; |
584 | CellVersion version = info.getCellVersion() ; |
585 | if( version != null ) |
586 | sb.append( " -- Version : ").append(version.toString()).append("\n") ; |
587 | sb.append( " -- Threads --\n" ) ; |
588 | Thread [] threads = _nucleus.getThreads(cellName) ; |
589 | for( int j = 0 ; |
590 | ( j < threads.length ) && ( threads[j] != null ) ; j++ ){ |
591 | boolean isAlive = threads[j].isAlive() ; |
592 | sb.append( CellInfo.f( threads[j].getName() , 20 ) + |
593 | CellInfo.f( ""+threads[j].getPriority() , 2 ) + |
594 | ( isAlive ? " Alive" : " Dead" ) + |
595 | "\n" ) ; |
596 | } |
597 | sb.append( " -- Private Infos --\n" ) ; |
598 | } |
599 | sb.append( info.getPrivatInfo() + "\n" ) ; |
600 | } |
601 | } |
602 | return sb.toString() ; |
603 | |
604 | } |
605 | //////////////////////////////////////////////////////////// |
606 | // |
607 | // kill |
608 | // |
609 | public String hh_kill= "<cellName>" ; |
610 | public String fh_kill = |
611 | " Syntax : kill <cellName>\n"+ |
612 | " Starts the killl mechanism on the specified cell\n"+ |
613 | " and removes it from the cell list\n" ; |
614 | public String ac_kill_$_1( Args args ) |
615 | throws IllegalArgumentException |
616 | { |
617 | _nucleus.kill( args.argv(0) ); |
618 | return "\n" ; |
619 | } |
620 | //////////////////////////////////////////////////////////// |
621 | // |
622 | // send [-w] <cellAddress> <message> |
623 | // |
624 | public String hh_send = "[-w] <cellAddress> <message>" ; |
625 | public String fh_send = |
626 | " Syntax : send [options] <cellAddress> <message>\n"+ |
627 | " Sends the message <message> to the specified\n"+ |
628 | " <cellAddress>.\n"+ |
629 | " -w : wait 10 second for the answer to arrive\n"+ |
630 | " -nolocal : don't deliver locally\n"+ |
631 | " -noremote : don't deliver remotely\n" ; |
632 | public String ac_send_$_2( Args args ) |
633 | throws IllegalArgumentException, |
634 | InterruptedException, |
635 | NoRouteToCellException |
636 | { |
637 | CellMessage msg = new CellMessage( |
638 | new CellPath( args.argv(0) ) , |
639 | args.argv(1) ) ; |
640 | boolean wait = false ; |
641 | boolean locally = true ; |
642 | boolean remotely = true ; |
643 | for( int i = 0 ; i < args.optc() ; i++ ){ |
644 | if( args.optv(i).equals("-w" ) )wait = true ; |
645 | else if( args .optv(i).equals("-nolocal" ) )locally = false ; |
646 | else if( args .optv(i).equals("-noremote" ) )remotely = false ; |
647 | } |
648 | if( wait ){ |
649 | msg = _nucleus.sendAndWait( msg , locally , remotely , 10000 ) ; |
650 | if( msg == null )return "Timeout ... \n"; |
651 | Object obj = msg.getMessageObject() ; |
652 | if( obj == null )return msg.toString()+"\n" ; |
653 | String output = obj.toString() ; |
654 | if( output.charAt(output.length()-1) == '\n' ) |
655 | return output ; |
656 | else |
657 | return output+"\n" ; |
658 | }else{ |
659 | _nucleus.sendMessage( msg , locally , remotely ) ; |
660 | return "Msg UOID ="+msg.getUOID().toString()+"\n" ; |
661 | } |
662 | |
663 | } |
664 | //////////////////////////////////////////////////////////// |
665 | // |
666 | // sleep |
667 | // |
668 | public String hh_sleep = "<secondsToSleep>" ; |
669 | public String ac_sleep_$_1( Args args ) throws InterruptedException { |
670 | int s = Integer.valueOf( args.argv(0) ).intValue() ; |
671 | Thread.sleep( s*1000) ; |
672 | return "Ready\n" ; |
673 | |
674 | } |
675 | //////////////////////////////////////////////////////////// |
676 | // |
677 | // ping |
678 | // |
679 | public String hh_ping = "<destinationCell> [<packetSize>] [-count=numOfPackets]" ; |
680 | public String ac_ping_$_1_2( Args args ) |
681 | throws NoRouteToCellException |
682 | { |
683 | String countString = args.getOpt("count") ; |
684 | int count = 1 ; |
685 | int size = 0 ; |
686 | if( countString != null ){ |
687 | try{ |
688 | count = Integer.parseInt(countString) ; |
689 | }catch(NumberFormatException ee){ /* ignore bad values */ } |
690 | } |
691 | if( args.argc() > 1 ){ |
692 | try{ |
693 | size = Integer.parseInt(args.argv(1)) ; |
694 | }catch(NumberFormatException ee){ /* ignore bad values */ } |
695 | } |
696 | CellPath path = new CellPath( args.argv(0) ) ; |
697 | for( int i = 0 ; i < count ; i ++ ) |
698 | _nucleus.sendMessage(new CellMessage(path,new PingMessage(size))) ; |
699 | // return "Msg UOID ="+msg.getUOID().toString()+"\n" ; |
700 | return "Done\n" ; |
701 | } |
702 | //////////////////////////////////////////////////////////// |
703 | // |
704 | // create |
705 | // |
706 | public String hh_create = "<cellClass> <cellName> [<Arguments>]"; |
707 | public String ac_create_$_2_3(Args args) |
708 | throws Throwable |
709 | { |
710 | try { |
711 | if( ( args.optc() > 0 ) && ( args.optv(0).equals("-c") ) ){ |
712 | String [] argClasses = new String[1] ; |
713 | Object [] argObjects = new Object[1] ; |
714 | |
715 | argClasses[0] = "java.lang.String" ; |
716 | argObjects[0] = args.argc()>2?args.argv(2):"" ; |
717 | |
718 | Cell cell = (Cell)_nucleus.createNewCell(args.argv(0), |
719 | args.argv(1), |
720 | argClasses, |
721 | argObjects); |
722 | return "created : "+cell.toString() ; |
723 | }else{ |
724 | Cell cell = _nucleus.createNewCell(args.argv(0), |
725 | args.argv(1), |
726 | args.argc()>2?args.argv(2):"", |
727 | true); |
728 | return "created : "+cell.toString() ; |
729 | } |
730 | } catch (InvocationTargetException e) { |
731 | throw e.getTargetException(); |
732 | } |
733 | } |
734 | //////////////////////////////////////////////////////////// |
735 | // |
736 | // domain class loader routines |
737 | // |
738 | public String fh_set_classloader = |
739 | " set classloader <packageSelection> <provider>\n"+ |
740 | " <packageSelection> : e.g. java.lang.*\n"+ |
741 | " <provider> : \n"+ |
742 | " cell:class@domain \n"+ |
743 | " dir:/../.../../ \n"+ |
744 | " system, none\n" ; |
745 | |
746 | public String hh_set_classloader = "<packageSelection> <provider>" ; |
747 | public String ac_set_classloader_$_2( Args args ){ |
748 | _nucleus.setClassProvider( args.argv(0) , args.argv(1) ) ; |
749 | return "" ; |
750 | } |
751 | public String ac_show_classloader( Args args ){ |
752 | String [] [] out = _nucleus.getClassProviders() ; |
753 | StringBuilder sb = new StringBuilder() ; |
754 | for( int j = 0 ; j < out.length ; j++ ) |
755 | sb.append( Formats.field( out[j][0] , 20 , Formats.LEFT ) ). |
756 | append( out[j][1] ). |
757 | append( "\n" ) ; |
758 | return sb.toString() ; |
759 | } |
760 | //////////////////////////////////////////////////////////// |
761 | // |
762 | // private class loader routines |
763 | // |
764 | public String hh_load_cellprinter = "<cellprinterClassName> # Obsolete" ; |
765 | public String ac_load_cellprinter_$_1( Args args ) |
766 | { |
767 | return "Obsolete; use log4j instead." ; |
768 | } |
769 | public String hh_load_interpreter = "<interpreterClassName>" ; |
770 | public String ac_load_interpreter_$_1( Args args ) throws CommandException { |
771 | Object o = getDictionaryEntry( "classProvider" ) ; |
772 | |
773 | if( ( o == null ) || ( ! ( o instanceof String ) ) ) |
774 | throw new CommandException( 34 , "<classProvider> not set, or not a String" ) ; |
775 | |
776 | Class c = null ; |
777 | String className = args.argv(0) ; |
778 | String classProvider = (String) o ; |
779 | String providerType = null ; |
780 | |
781 | int pos = classProvider.indexOf( ':' ) ; |
782 | if( pos < 0 ){ |
783 | providerType = "file" ; |
784 | }else{ |
785 | providerType = classProvider.substring( 0 , pos ) ; |
786 | classProvider = classProvider.substring( pos+1 ) ; |
787 | } |
788 | |
789 | if( providerType.equals( "file" ) ){ |
790 | File directory = new File( classProvider ) ; |
791 | if( ! directory.isDirectory() ) |
792 | throw new CommandException( 34 , "<classDirectory> not a directory" ) ; |
793 | |
794 | if( (c = _classLoaderFactory.loadClass( className , directory )) == null ) |
795 | throw new |
796 | CommandException( 35 , "class not found in <"+ |
797 | _classLoaderFactory+"> : "+className ) ; |
798 | }else if( providerType.equals( "cell" ) ){ |
799 | _classProvider = classProvider ; |
800 | if( (c = _classLoaderFactory.loadClass( className , this )) == null ) |
801 | throw new |
802 | CommandException( 35 , "class not found in <"+ |
803 | _classLoaderFactory+"> : "+className ) ; |
804 | }else{ |
805 | throw new CommandException( 37, "Unknown class provider type : "+providerType ) ; |
806 | } |
807 | // |
808 | // try to find an constructor who knows what a _nucleus is. |
809 | // |
810 | Class [] paraList1 = { dmg.cells.nucleus.CellNucleus.class } ; |
811 | Class [] paraList2 = { dmg.cells.nucleus.CellNucleus.class , |
812 | dmg.cells.nucleus.CellShell.class } ; |
813 | Object [] paras ; |
814 | Constructor con ; |
815 | Object interObject ; |
816 | StringBuilder answer = new StringBuilder(); |
817 | try{ |
818 | con = c.getConstructor( paraList2 ) ; |
819 | paras = new Object[2] ; |
820 | paras[0] = _nucleus ; |
821 | paras[1] = this ; |
822 | interObject = con.newInstance( paras ) ; |
823 | }catch(Exception e0 ){ |
824 | answer.append( e0.toString() ).append( '\n' ) ; |
825 | try{ |
826 | con = c.getConstructor( paraList1 ) ; |
827 | paras = new Object[1] ; |
828 | paras[0] = _nucleus ; |
829 | interObject = con.newInstance( paras ) ; |
830 | }catch(Exception e1 ){ |
831 | answer.append( e1.toString() ).append( '\n' ) ; |
832 | try{ |
833 | interObject = c.newInstance() ; |
834 | if( interObject == null ) |
835 | throw new CommandException( 36 , answer.toString() ) ; |
836 | }catch(Throwable e2 ){ |
837 | answer.append( e2.toString() ).append( '\n' ) ; |
838 | throw new CommandException( 36 , answer.toString() ) ; |
839 | } |
840 | } |
841 | } |
842 | _externalInterpreter = new CommandInterpreter( interObject ) ; |
843 | return " !!! Your are now in a new Shell !!! " ; |
844 | } |
845 | public byte [] getClassData( String className ) throws IOException { |
846 | _log.info( "getClassData("+className+") send to classProvider" ) ; |
847 | CellMessage answer = null ; |
848 | try{ |
849 | answer = _nucleus.sendAndWait( |
850 | new CellMessage( |
851 | new CellPath( _classProvider ) , |
852 | "getclass "+className |
853 | ) , |
854 | 4000 |
855 | ) ; |
856 | }catch( InterruptedException e ){ |
857 | _log.info( "getClassData Exception : "+e ) ; |
858 | return null ; |
859 | }catch( NoRouteToCellException e ){ |
860 | _log.info( "getClassData Exception : "+e ) ; |
861 | return null ; |
862 | } |
863 | if( answer == null ){ |
864 | _log.info( "getClassData sendAndWait timed out" ) ; |
865 | return null ; |
866 | } |
867 | Object answerObject = answer.getMessageObject() ; |
868 | if( answerObject == null )return null ; |
869 | |
870 | if( ! ( answerObject instanceof byte [] ) ){ |
871 | _log.info( "getClassData sendAndWait got : "+answerObject.toString() ) ; |
872 | return null ; |
873 | } |
874 | |
875 | return (byte [] )answerObject ; |
876 | |
877 | } |
878 | //////////////////////////////////////////////////////////// |
879 | // |
880 | // this and that |
881 | // |
882 | public String ac_dumpHelp( Args args ){ |
883 | dumpCommands() ; |
884 | return "" ; |
885 | } |
886 | public String hh_onerror = "shutdown|exit|continue" ; |
887 | public String ac_onerror_$_1( Args args ){ |
888 | if( args.argv(0).equals( "continue" ) )_doOnExit = null ; |
889 | else _doOnExit = args.argv(0) ; |
890 | return "" ; |
891 | } |
892 | public String ac_show_onexit( Args args ){ |
893 | return _doOnExit != null ? _doOnExit : "" ; |
894 | } |
895 | public String hh_say = "<things to echo ...> [-level=<level>]" ; |
896 | public String fh_say = |
897 | "<things to echo ...> [-level=<level>]\n"+ |
898 | " Levels :\n" + |
899 | " say,esay,fsay\n"+ |
900 | " PRINT_CELL = 1\n" + |
901 | " PRINT_ERROR_CELL = 2\n" + |
902 | " PRINT_NUCLEUS = 4\n" + |
903 | " PRINT_ERROR_NUCLEUS = 8\n" + |
904 | " PRINT_FATAL = 0x10" ; |
905 | |
906 | public String ac_say_$_1_99( Args args ) |
907 | { |
908 | StringBuilder sb = new StringBuilder() ; |
909 | |
910 | for( int i = 0 ; i < args.argc() ; i++ ) |
911 | sb.append( args.argv(i) ).append(' ') ; |
912 | |
913 | String msg = sb.toString() ; |
914 | |
915 | String levelString = args.getOpt("level") ; |
916 | |
917 | if( ( levelString != null ) && ( levelString.length() > 0 ) ){ |
918 | if( levelString.equals("say") ){ |
919 | _log.info(msg) ; |
920 | } else if( levelString.equals("esay") ){ |
921 | _log.warn(msg) ; |
922 | } else if( levelString.equals("fsay") ){ |
923 | _log.error(msg) ; |
924 | }else{ |
925 | try { |
926 | int level = Integer.parseInt(levelString); |
927 | if ((level & CellNucleus.PRINT_CELL) != 0 ) { |
928 | _log.info(msg); |
929 | } |
930 | if ((level & CellNucleus.PRINT_ERROR_CELL) != 0 ) { |
931 | _log.warn(msg); |
932 | } |
933 | if ((level & CellNucleus.PRINT_FATAL) != 0 ) { |
934 | _log.error(msg); |
935 | } |
936 | if ((level & CellNucleus.PRINT_NUCLEUS) != 0 ) { |
937 | _logNucleus.info(msg); |
938 | } |
939 | if ((level & CellNucleus.PRINT_ERROR_NUCLEUS) != 0 ) { |
940 | _logNucleus.warn(msg); |
941 | } |
942 | } catch (NumberFormatException e) { |
943 | throw new IllegalArgumentException("Illegal Level string: " + levelString); |
944 | } |
945 | } |
946 | } |
947 | return msg ; |
948 | } |
949 | public String hh_echo = "<things to echo ...>" ; |
950 | public String ac_echo_$_1_99( Args args ){ |
951 | StringBuilder sb = new StringBuilder() ; |
952 | for( int i = 0 ; i < args.argc() ; i++ ) |
953 | sb.append( args.argv(i) ).append(' ') ; |
954 | return sb.toString() ; |
955 | } |
956 | public String hh_show_error = " # shows last errorCode and Message "; |
957 | public String ac_show_error( Args args ){ |
958 | if( _errorCode == 0 )return "No Error found" ; |
959 | return "errorCode="+_errorCode+"; Msg = "+ |
960 | (_errorMsg==null?"None":_errorMsg) ; |
961 | } |
962 | public String hh_set_helpmode = "none|full" ; |
963 | public String ac_set_helpmode_$_1( Args args ) throws CommandException { |
964 | String mode = args.argv(0) ; |
965 | if( mode.equals( "none" ) )_helpMode = 0 ; |
966 | else if( mode.equals( "full" ) )_helpMode = 2 ; |
967 | else |
968 | throw new CommandException( 22 ,"Illegal Help Mode : "+mode ) ; |
969 | return "" ; |
970 | } |
971 | public String ac_id( Args args ){ |
972 | return _nucleus.getCellDomainName()+"\n" ; |
973 | } |
974 | //////////////////////////////////////////////////////////// |
975 | // |
976 | // setting the context/environment |
977 | // |
978 | public Object ac_set_context_$_2( CommandRequestable request ) |
979 | throws CommandException { |
980 | |
981 | Map<String,Object> dict = _nucleus.getDomainContext() ; |
982 | Object contextName = request.getArgv(0) ; |
983 | if( ! ( contextName instanceof String ) ) |
984 | throw new CommandException( 67 , "ContextName not a string" ) ; |
985 | |
986 | dict.put((String) contextName , request.getArgv(1) ) ; |
987 | |
988 | Object o = dict.get( contextName ) ; |
989 | if( o == null ) |
990 | throw new CommandException( 68 , "Setting of "+contextName+" failed"); |
991 | |
992 | Object [] answer = new Object[2] ; |
993 | answer[0] = contextName ; |
994 | answer[1] = o ; |
995 | |
996 | return answer ; |
997 | |
998 | } |
999 | public String fh_check = |
1000 | " check [-strong] <var1> [<var2> [] ... ]\n"+ |
1001 | " checks if all of the specified variables are set.\n"+ |
1002 | " Returns an error it not.\n"+ |
1003 | " The -strong option requires that all variables must not be\n"+ |
1004 | " the zero string and must not only contain blanks\n" ; |
1005 | |
1006 | public String hh_check = "[-strong] <var1> [<var2> [] ... ]" ; |
1007 | public String ac_check_$_1_99( Args args )throws CommandException { |
1008 | |
1009 | boolean strong = args.getOpt("strong") != null ; |
1010 | |
1011 | String varName = null ; |
1012 | Object value = null ; |
1013 | for( int i= 0 ;i < args.argc() ; i++ ){ |
1014 | varName = args.argv(i) ; |
1015 | if( ( value = _environment.get( varName ) ) == null ) |
1016 | value = _nucleus.getDomainContext().get( varName) ; |
1017 | if( value == null ) |
1018 | throw new |
1019 | CommandException( 1 , "variable not define : "+varName ) ; |
1020 | |
1021 | if( strong ){ |
1022 | String strValue = value.toString() ; |
1023 | if( strValue.trim().equals("") ) |
1024 | throw new |
1025 | CommandException( 2 , "variable defined but empty : "+varName ) ; |
1026 | } |
1027 | } |
1028 | return "" ; |
1029 | |
1030 | } |
1031 | public String fh_import_context = |
1032 | " import context|env [options] <variableName>\n" + |
1033 | " options :\n"+ |
1034 | " -c : don't overwrite\n"+ |
1035 | " -source=env|context : only check the specifed\n"+ |
1036 | " source for the variableName\n"+ |
1037 | " -nr : don't run the variable resolver\n"+ |
1038 | "\n"+ |
1039 | " The source is interpreted as a set of lines separated by\n"+ |
1040 | " newlines. Each line is assumed to contain a key value pair\n"+ |
1041 | " separated by the '=' sign.\n"+ |
1042 | " The context/environment variables are set according to\n"+ |
1043 | " the assignment.\n" ; |
1044 | public String fh_import_env = fh_import_context ; |
1045 | |
1046 | public String hh_import_context = "[-source=context|env] [-nr]"+ |
1047 | "<contextVariableName>" ; |
1048 | public String hh_import_env = "[-source=context|env] [-nr]"+ |
1049 | "<environmentVariableName>" ; |
1050 | |
1051 | public String ac_import_context_$_1( Args args )throws CommandException { |
1052 | return imprt_dict( args , _nucleus.getDomainContext() ) ; |
1053 | } |
1054 | public String ac_import_env_$_1( Args args )throws CommandException { |
1055 | return imprt_dict( args , _environment ) ; |
1056 | } |
1057 | |
1058 | private String imprt_dict(Args args, Map<String,Object> dict) |
1059 | throws CommandException |
1060 | { |
1061 | String varName = args.argv(0); |
1062 | boolean opt_overwrite = (args.getOpt("c") == null); |
1063 | boolean resolve = (args.getOpt( "nr" ) == null); |
1064 | |
1065 | String src = args.getOpt("source"); |
1066 | Object input; |
1067 | if (src == null) { |
1068 | input = _environment.get(varName); |
1069 | if (input == null) { |
1070 | input = _nucleus.getDomainContext().get(varName); |
1071 | } |
1072 | } else if (src.equals("env")) { |
1073 | input = _environment.get(varName); |
1074 | } else if (src.equals("context")) { |
1075 | input = _nucleus.getDomainContext().get(varName); |
1076 | } else { |
1077 | throw new CommandException("Invalid value for -source=" + src); |
1078 | } |
1079 | |
1080 | if (input == null) { |
1081 | throw new CommandException("Variable not defined: " + varName); |
1082 | } |
1083 | |
1084 | try { |
1085 | Properties properties = new Properties(); |
1086 | properties.load(new StringReader(input.toString())); |
1087 | |
1088 | ReplaceableProperties replaceable = |
1089 | new ReplaceableProperties(properties); |
1090 | |
1091 | Enumeration e = properties.propertyNames(); |
1092 | while (e.hasMoreElements()) { |
1093 | String key = (String) e.nextElement(); |
1094 | if (opt_overwrite || (dict.get(key) == null)) { |
1095 | String value = properties.getProperty(key); |
1096 | |
1097 | int length = value.length(); |
1098 | if (length > 1 && |
1099 | value.charAt(0) == '"' && |
1100 | value.charAt(length - 1) == '"') { |
1101 | value = value.substring(1, length - 1); |
1102 | } |
1103 | |
1104 | if (resolve) { |
1105 | value = Formats.replaceKeywords(value, replaceable); |
1106 | } |
1107 | |
1108 | dict.put(key, value); |
1109 | } |
1110 | } |
1111 | } catch (IllegalArgumentException e) { |
1112 | throw new CommandException(3, "Failed to read " + varName + ": " + e); |
1113 | } catch (IOException e) { |
1114 | throw new CommandException(3, "Failed to read " + varName + ": " + e); |
1115 | } |
1116 | |
1117 | return ""; |
1118 | } |
1119 | |
1120 | /** |
1121 | * Properties wrapper which implements Replacable. Values are |
1122 | * looked up recursively in a Properties object. Upon failure to |
1123 | * lookup a name, getReplacement of the CellShell is called. |
1124 | */ |
1125 | private class ReplaceableProperties implements Replaceable |
1126 | { |
1127 | private Properties _properties; |
1128 | private Stack<String> _stack = new Stack<String>(); |
1129 | |
1130 | public ReplaceableProperties(Properties properties) |
1131 | { |
1132 | _properties = properties; |
1133 | } |
1134 | |
1135 | public String getReplacement(String name) |
1136 | { |
1137 | String value = _properties.getProperty(name); |
1138 | if (value != null) { |
1139 | if (_stack.search(name) == -1) { |
1140 | _stack.push(name); |
1141 | try { |
1142 | value = Formats.replaceKeywords(value, this); |
1143 | } finally { |
1144 | _stack.pop(); |
1145 | } |
1146 | } |
1147 | } else { |
1148 | value = CellShell.this.getReplacement(name); |
1149 | } |
1150 | return value; |
1151 | } |
1152 | } |
1153 | |
1154 | public String fh_set_context = |
1155 | "set context|env [options] <variableName> <value>\n"+ |
1156 | " options :\n"+ |
1157 | " -c : do not overwrite the variable if it's already set\n"+ |
1158 | " -s : run the value through the interpreter and\n"+ |
1159 | " convert '\\n' to a real newline" ; |
1160 | public String fh_set_env = fh_set_context ; |
1161 | public String hh_set_context = "[-c][-s] <contextName> <value>" ; |
1162 | public String hh_set_env = "[-c][-s] <environmentName> <value>" ; |
1163 | public String ac_set_context_$_2( Args args )throws CommandException{ |
1164 | return set_dict( args , _nucleus.getDomainContext() ) ; |
1165 | } |
1166 | public String ac_set_env_$_2( Args args )throws CommandException{ |
1167 | return set_dict( args , _environment ) ; |
1168 | } |
1169 | private String set_dict(Args args, Map<String,Object> dict) |
1170 | throws CommandException |
1171 | { |
1172 | String name = args.argv(0) ; |
1173 | String value = args.argv(1) ; |
1174 | boolean opt_overwrite = args.getOpt("c") == null ; |
1175 | boolean opt_interpreter = args.getOpt("s") != null ; |
1176 | |
1177 | if( ( ! opt_overwrite ) && ( dict.get( name ) != null ) ) |
1178 | throw new |
1179 | CommandEvaluationException ( 1 , "Variable "+name+" is already set and can't be overwritten due to '-c'" ) ; |
1180 | |
1181 | |
1182 | if( opt_interpreter ){ |
1183 | final int I_IDLE = 0 ; |
1184 | final int I_BS = 1 ; |
1185 | int state = I_IDLE ; |
1186 | StringBuilder sb = new StringBuilder(); |
1187 | for( int i = 0 ; i < value.length() ; i++ ){ |
1188 | char c = value.charAt(i) ; |
1189 | switch( state ){ |
1190 | case I_IDLE : |
1191 | if( c == '\\' ){ |
1192 | state = I_BS ; |
1193 | }else{ |
1194 | sb.append( c ) ; |
1195 | } |
1196 | break ; |
1197 | case I_BS : |
1198 | if( c == 'n' ){ |
1199 | state = I_IDLE ; |
1200 | sb.append( '\n' ) ; |
1201 | }else{ |
1202 | sb.append( '\\' ) ; |
1203 | sb.append( c ) ; |
1204 | } |
1205 | break ; |
1206 | |
1207 | } |
1208 | } |
1209 | value = sb.toString() ; |
1210 | |
1211 | } |
1212 | dict.put( name , value ) ; |
1213 | return "" ; |
1214 | |
1215 | } |
1216 | //////////////////////////////////////////////////////////// |
1217 | // |
1218 | // unsetting the context/environment |
1219 | // |
1220 | public String hh_unset_context="<contextName>" ; |
1221 | public String hh_unset_env ="<environmentName>" ; |
1222 | public String ac_unset_context_$_1( Args args )throws CommandException { |
1223 | return unset_dict( args , _nucleus.getDomainContext() ) ; |
1224 | } |
1225 | public String ac_unset_env_$_1( Args args )throws CommandException { |
1226 | return unset_dict( args , _environment ) ; |
1227 | } |
1228 | |
1229 | private String unset_dict(Args args, Map<String,Object> dict) |
1230 | throws CommandException |
1231 | { |
1232 | String name = args.argv(0) ; |
1233 | Object o = dict.remove( name ) ; |
1234 | if( o == null ){ |
1235 | throw new |
1236 | CommandException ( "Not found : "+name ) ; |
1237 | }else{ |
1238 | return name+"<"+o.getClass().getName()+"> removed\n" ; |
1239 | } |
1240 | } |
1241 | //////////////////////////////////////////////////////////// |
1242 | // |
1243 | // displaying the context/environment variables |
1244 | // |
1245 | public String hh_ls = "[-l] [-ll] [-e] [-list]" ; |
1246 | public String fh_ls = |
1247 | " ls [options]\n"+ |
1248 | " Prints context/environment\n"+ |
1249 | " Options\n"+ |
1250 | " -l adds class name\n"+ |
1251 | " -ll adds first 40 chars of content\n"+ |
1252 | " -e list environment instead of context\n"+ |
1253 | " -list prints simple list instead of formatted one\n"+ |
1254 | "\n"; |
1255 | public String hh_show_context = "[<contextName>]" ; |
1256 | public String hh_show_env = "[<environmentName>]" ; |
1257 | public String hh_test_context = "[<contextName>]" ; |
1258 | public String hh_test_env = "[<environmentName>]" ; |
1259 | public String ac_ls_$_0_1( Args args ) throws CommandException { |
1260 | return ls_dict( args , args.getOpt("e") != null ? |
1261 | _environment : |
1262 | _nucleus.getDomainContext() ) ; |
1263 | } |
1264 | public String ac_show_context_$_0_1( Args args ) throws CommandException { |
1265 | return show_dict( args , _nucleus.getDomainContext() ) ; |
1266 | } |
1267 | public String ac_show_env_$_0_1( Args args ) throws CommandException { |
1268 | return show_dict( args , _environment ) ; |
1269 | } |
1270 | public String ac_test_context_$_0_1( Args args ) throws CommandException { |
1271 | return test_dict( args , _nucleus.getDomainContext() ) ; |
1272 | } |
1273 | public String ac_test_env_$_0_1( Args args ) throws CommandException { |
1274 | return test_dict( args , _environment ) ; |
1275 | } |
1276 | private String test_dict(Args args, Map<String,Object> dict) |
1277 | throws CommandException |
1278 | { |
1279 | String name = args.argv(0) ; |
1280 | if( dict.get( name ) == null ){ |
1281 | throw new |
1282 | CommandException( 66 , "not found : "+name ); |
1283 | }return "" ; |
1284 | } |
1285 | private String show_dict(Args args, Map<String,Object> dict) |
1286 | throws CommandException |
1287 | { |
1288 | StringBuilder sb = new StringBuilder(); |
1289 | if( args.argc() == 0 ){ |
1290 | for (Map.Entry<String,Object> e: dict.entrySet()) { |
1291 | String name = e.getKey(); |
1292 | Object o = e.getValue(); |
1293 | if( o instanceof String ){ |
1294 | sb.append(name).append("=") ; |
1295 | String line = (String)o ; |
1296 | int len = line.length() ; |
1297 | len = len > 40 ? 40 : len ; |
1298 | for( int i = 0 ; i < len ; i++ ) |
1299 | sb.append( line.charAt(i) == '\n' ? '$' : line.charAt(i) ) ; |
1300 | if( len == 40 )sb.append("...\n") ; |
1301 | else sb.append("\n") ; |
1302 | }else |
1303 | sb.append( name).append("=<").append(o.getClass().getName()).append(">\n" ) ; |
1304 | } |
1305 | }else{ |
1306 | String name = args.argv(0) ; |
1307 | Object o = dict.get( name ) ; |
1308 | if( o == null ) |
1309 | throw new |
1310 | CommandException( 23 , "Context name "+name+" not found" ) ; |
1311 | sb.append( o.toString() ) ; |
1312 | } |
1313 | return sb.toString() ; |
1314 | } |
1315 | private String ls_dict(Args args, Map<String,Object> dict) |
1316 | throws CommandException |
1317 | { |
1318 | StringBuilder sb = new StringBuilder(); |
1319 | if( args.argc() == 0 ){ |
1320 | int maxLength = 0 ; |
1321 | SortedSet<String> set = CollectionFactory.newTreeSet(); |
1322 | |
1323 | for (String name: dict.keySet()) { |
1324 | maxLength = Math.max( maxLength , name.length() ) ; |
1325 | set.add(name); |
1326 | } |
1327 | boolean detail = args.getOpt("l") != null ; |
1328 | boolean moreDetail = args.getOpt("ll") != null ; |
1329 | if( moreDetail )detail =true ; |
1330 | boolean list = args.getOpt("list") != null ; |
1331 | for( Iterator e = set.iterator() ; e.hasNext() ; ){ |
1332 | String name = (String)e.next() ; |
1333 | sb.append(name) ; |
1334 | if( detail ){ |
1335 | sb.append(" ") ; |
1336 | if( ! list ){ |
1337 | int diff = maxLength - name.length() ; |
1338 | for( int i = 0 ; i < diff ; i++ )sb.append("."); |
1339 | } |
1340 | Object o = dict.get(name) ; |
1341 | sb.append(" ").append(o.getClass().getName()) ; |
1342 | if( moreDetail ){ |
1343 | sb.append("\n "); |
1344 | String line = o.toString() ; |
1345 | int len = line.length() ; |
1346 | len = len > 40 ? 40 : len ; |
1347 | for( int i = 0 ; i < len ; i++ ) |
1348 | sb.append( line.charAt(i) == '\n' ? '$' : line.charAt(i) ) ; |
1349 | if( len == 40 )sb.append("...") ; |
1350 | } |
1351 | } |
1352 | sb.append("\n"); |
1353 | } |
1354 | }else{ |
1355 | throw new |
1356 | CommandSyntaxException("Not yet supported"); |
1357 | } |
1358 | return sb.toString() ; |
1359 | } |
1360 | |
1361 | public final static String fh_exec = |
1362 | "exec [<options>] <url> [<args>]\n" + |
1363 | "exec context [<options>] <contextName> [<args>]\n" + |
1364 | "exec env [<options>] <envName> [<args>]\n"+ |
1365 | "\n"+ |
1366 | " Executes the content of an env or context variable or the\n" + |
1367 | " resource identified by the URL.\n"+ |
1368 | " -shell : opens a new shell for the execution\n"+ |
1369 | " -nooutput : discard the output of the executed commands\n"+ |
1370 | " -loop=<variableContextName> : \n"+ |
1371 | " Executes the block for each line in <varContextName> as arg\n"+ |
1372 | " -ifok[=<varName>] : run the context/env ONLY if the \n"+ |
1373 | " specified value of <varName> is '0'\n"+ |
1374 | " The default <varName> is 'rc'\n"+ |
1375 | " -ifnotok[=<varName>] : negation of -ifok\n\n"; |
1376 | public final static String hh_exec = |
1377 | "[-shell] [-nooutput] [-loop=<variable>] [-ifok[=<variable>]|-ifnotok[=<variable>}] <url> [<args>]"; |
1378 | public String ac_exec_$_1_99(Args args) |
1379 | throws CommandException |
1380 | { |
1381 | try { |
1382 | URI uri = new URI(args.argv(0)); |
1383 | args.shift(); |
1384 | return run_reader(uri, args); |
1385 | } catch (URISyntaxException e) { |
1386 | throw new CommandException(43 , e.getMessage()); |
1387 | } |
1388 | } |
1389 | |
1390 | public final static String fh_exec_env = fh_exec; |
1391 | public final static String hh_exec_env = |
1392 | "[-shell] [-nooutput] [-loop=<variable>] [-ifok[=<variable>]|-ifnotok[=<variable>}] <envName> [<args>]"; |
1393 | public String ac_exec_env_$_1_99(Args args) throws CommandException |
1394 | { |
1395 | try { |
1396 | URI uri = new URI("env", args.argv(0), null); |
1397 | args.shift(); |
1398 | return run_reader(uri, args); |
1399 | } catch (URISyntaxException e) { |
1400 | throw new CommandException(43, e.getMessage()); |
1401 | } |
1402 | } |
1403 | |
1404 | public final static String fh_exec_context = fh_exec; |
1405 | public final static String hh_exec_context = |
1406 | "[-shell] [-nooutput] [-loop=<variable>] [-ifok[=<variable>]|-ifnotok[=<variable>}] <contextName> [<args>]"; |
1407 | public String ac_exec_context_$_1_99(Args args) throws CommandException |
1408 | { |
1409 | try { |
1410 | URI uri = new URI("context", args.argv(0), null); |
1411 | args.shift(); |
1412 | return run_reader(uri, args); |
1413 | } catch (URISyntaxException e) { |
1414 | throw new CommandException(43 , e.getMessage()); |
1415 | } |
1416 | } |
1417 | |
1418 | private void println(Writer out, String s) |
1419 | throws IOException |
1420 | { |
1421 | if (!s.isEmpty()) { |
1422 | out.append(s); |
1423 | if ((s.length() > 0) && (s.charAt(s.length() - 1) != '\n')) { |
1424 | out.append('\n'); |
1425 | } |
1426 | } |
1427 | } |
1428 | |
1429 | public void execute(String source, Reader in, Args args) |
1430 | throws CommandExitException, IOException |
1431 | { |
1432 | Writer out = _nucleus.createInfoLogWriter(); |
1433 | try { |
1434 | Writer err = _nucleus.createErrorLogWriter(); |
1435 | try { |
1436 | execute(source, in, out, err, args); |
1437 | } finally { |
1438 | err.close(); |
1439 | } |
1440 | } finally { |
1441 | out.close(); |
1442 | } |
1443 | } |
1444 | |
1445 | public void execute(String source, Reader in, Writer out, Writer err, Args args) |
1446 | throws CommandExitException, IOException |
1447 | { |
1448 | List<String> store = _argumentVector; |
1449 | int no = 1; |
1450 | try { |
1451 | _argumentVector = new Vector<String>(); |
1452 | for (int i = 0; i < args.argc(); i++) { |
1453 | _argumentVector.add(args.argv(i)); |
1454 | } |
1455 | |
1456 | String line; |
1457 | StringBuilder sb = null; |
1458 | BufferedReader input = new BufferedReader(in); |
1459 | for (; (line = input.readLine()) != null; no = no + 1) { |
1460 | /* Skip empty and comment lines. |
1461 | */ |
1462 | String s = line.trim(); |
1463 | if (s.length() == 0 || s.charAt(0) == '#') |
1464 | continue; |
1465 | |
1466 | /* Handle line continuation. |
1467 | */ |
1468 | int len = line.length(); |
1469 | if (line.charAt(len - 1) == '\\') { |
1470 | if (sb == null) { |
1471 | sb = new StringBuilder(); |
1472 | } |
1473 | sb.append(line.substring(0, len - 1)).append(' '); |
1474 | continue; |
1475 | } else if (sb != null) { |
1476 | sb.append(line); |
1477 | line = sb.toString(); |
1478 | sb = null; |
1479 | } |
1480 | |
1481 | /* Execute command. |
1482 | */ |
1483 | Object answer = objectCommand2(line); |
1484 | |
1485 | /* Process result. |
1486 | */ |
1487 | if (!(answer instanceof Throwable)) { |
1488 | println(out, answer.toString()); |
1489 | } else { |
1490 | Throwable error = (Throwable) answer; |
1491 | if (_doOnExit != null) { |
1492 | String msg = |
1493 | String.format("%s: line %d: %s", source, no, |
1494 | error.getMessage()); |
1495 | if (_doOnExit.equals("shutdown")) { |
1496 | throw new CommandExitException(msg, 666, error); |
1497 | } else if (error instanceof CommandException) { |
1498 | int rc = ((CommandException) error).getErrorCode(); |
1499 | throw new CommandExitException(msg, rc, error); |
1500 | } else { |
1501 | throw new CommandExitException(msg, 1, error); |
1502 | } |
1503 | } |
1504 | |
1505 | /* CommandEvaluationException does not generate |
1506 | * output since it is not really an error. Runtime |
1507 | * exceptions other than IllegalArgumentException |
1508 | * are logged. Other exceptions are printed to the |
1509 | * error output. |
1510 | */ |
1511 | if (error instanceof IllegalArgumentException) { |
1512 | String msg = |
1513 | String.format("%s: line %d: Illegal argument (%s)", |
1514 | source, no, error.getMessage()); |
1515 | println(err, msg); |
1516 | } else if (error instanceof RuntimeException) { |
1517 | _log.warn(error.toString(), error); |
1518 | } else if (!(error instanceof CommandEvaluationException)) { |
1519 | String msg = |
1520 | Exceptions.getMessageWithCauses(error); |
1521 | println(err, String.format("%s: line %d: Command failed (%s)", |
1522 | source, no, msg)); |
1523 | } |
1524 | } |
1525 | } |
1526 | } catch (IOException e) { |
1527 | throw new IOException(String.format("%s: line %d: %s", source, |
1528 | no, e.getMessage()), e); |
1529 | } catch (RuntimeException e) { |
1530 | throw new RuntimeException(String.format("%s: line %d: %s", source, |
1531 | no, e.getMessage()), e); |
1532 | } finally { |
1533 | _argumentVector = store; |
1534 | } |
1535 | } |
1536 | |
1537 | private String run_reader(URI uri, Args args) |
1538 | throws CommandException |
1539 | { |
1540 | String loopName = args.getOpt("loop"); |
1541 | String var; |
1542 | if ((var = args.getOpt("ifok")) != null) { |
1543 | if (var.equals("")) { |
1544 | if (_errorCode != 0) { |
1545 | return ""; |
1546 | } |
1547 | } else { |
1548 | Object x = getDictionaryEntry(var) ; |
1549 | if ((x == null) || (!x.toString().equals("0"))) { |
1550 | return ""; |
1551 | } |
1552 | } |
1553 | } |
1554 | if ((var = args.getOpt("ifnotok")) != null) { |
1555 | if (var.equals("")) { |
1556 | if (_errorCode == 0) { |
1557 | return ""; |
1558 | } |
1559 | } else { |
1560 | Object x = getDictionaryEntry(var) ; |
1561 | if ((x != null) && (x.toString().equals("0"))) { |
1562 | return ""; |
1563 | } |
1564 | } |
1565 | } |
1566 | |
1567 | try { |
1568 | StringWriter out = new StringWriter(); |
1569 | |
1570 | if (loopName == null) { |
1571 | CellShell shell = |
1572 | (args.getOpt("shell") != null) |
1573 | ? new CellShell(_nucleus) |
1574 | : this; |
1575 | |
1576 | Reader in = open(uri); |
1577 | try { |
1578 | shell.execute(uri.toString(), in, out, out, args); |
1579 | } finally { |
1580 | in.close(); |
1581 | } |
1582 | } else { |
1583 | Reader loopReader = _nucleus.getDomainContextReader(loopName); |
1584 | try { |
1585 | BufferedReader reader = new BufferedReader(loopReader); |
1586 | String line; |
1587 | while ((line = reader.readLine()) != null) { |
1588 | CellShell shell = |
1589 | (args.getOpt("shell") != null) |
1590 | ? new CellShell(_nucleus) |
1591 | : this; |
1592 | |
1593 | Reader in = open(uri); |
1594 | try { |
1595 | shell.execute(uri.toString(), in, out, out, |
1596 | new Args(line)); |
1597 | } finally { |
1598 | in.close(); |
1599 | } |
1600 | } |
1601 | } finally { |
1602 | loopReader.close(); |
1603 | } |
1604 | } |
1605 | |
1606 | return (args.getOpt("nooutput") != null) ? "" : out.toString(); |
1607 | } catch (FileNotFoundException e) { |
1608 | throw new CommandException(66, e.getMessage(), e); |
1609 | } catch (IOException e) { |
1610 | throw new CommandExitException("I/O error: " + e.getMessage(), 11); |
1611 | } |
1612 | } |
1613 | |
1614 | public String hh_eval = "upn expression" ; |
1615 | public String ac_eval_$_1_99( Args args )throws CommandException{ |
1616 | Stack<String> v = new Stack<String>() ; |
1617 | for( int i = 0 ; i < args.argc() ; i++ ){ |
1618 | |
1619 | if( args.argv(i).equals("==") ){ |
1620 | // ------------- |
1621 | Object right = v.pop() ; |
1622 | Object left = v.pop() ; |
1623 | v.push(right.equals(left) ?"0" :"1") ; |
1624 | |
1625 | }else if( args.argv(i).equals("!=") ){ |
1626 | // ------------------- |
1627 | Object right = v.pop() ; |
1628 | Object left = v.pop() ; |
1629 | v.push(right.equals(left)?"1":"0") ; |
1630 | |
1631 | }else if( args.argv(i).equals("&&") ){ |
1632 | // ------------------- |
1633 | Object right = v.pop() ; |
1634 | Object left = v.pop() ; |
1635 | v.push( |
1636 | right.equals("0")&&left.equals("0")? |
1637 | "0":"1") ; |
1638 | |
1639 | }else if( args.argv(i).equals("||") ){ |
1640 | // ------------------- |
1641 | Object right = v.pop() ; |
1642 | Object left = v.pop() ; |
1643 | v.push( |
1644 | right.equals("0")||left.equals("0")? |
1645 | "0":"1") ; |
1646 | |
1647 | }else if( args.argv(i).equals("!") ){ |
1648 | // ------------------- |
1649 | Object right = v.pop() ; |
1650 | v.push(right.equals("0")?"1":"0") ; |
1651 | |
1652 | }else{ |
1653 | v.push( args.argv(i).trim() ) ; |
1654 | } |
1655 | |
1656 | } |
1657 | if( v.size() != 1 ) { |
1658 | throw new |
1659 | CommandException( 2 , "Stack position violation ("+v.size()+")" ) ; |
1660 | } |
1661 | |
1662 | String result = v.firstElement() ; |
1663 | if( result.equals("0") )return "" ; |
1664 | |
1665 | int rc = 0 ; |
1666 | try{ |
1667 | rc = Integer.parseInt(result) ; |
1668 | }catch(NumberFormatException nfe){ |
1669 | rc = 3 ; |
1670 | } |
1671 | |
1672 | throw new |
1673 | CommandEvaluationException( rc , "Eval Result : "+result ) ; |
1674 | |
1675 | } |
1676 | public String hh_define_context = "<contextName> [<delimiter>]" ; |
1677 | public String ac_define_context_$_1_2( Args args ){ |
1678 | _contextName = args.argv(0) ; |
1679 | _contextDelimiter = args.argc() > 1 ? args.argv(1) : "." ; |
1680 | _contextString = new StringBuilder() ; |
1681 | return "" ; |
1682 | } |
1683 | public String hh_define_env = "<environmentName>" ; |
1684 | public String ac_define_env_$_1_2( Args args ){ |
1685 | _envName = args.argv(0) ; |
1686 | _envDelimiter = args.argc() > 1 ? args.argv(1) : "." ; |
1687 | _envString = new StringBuilder(); |
1688 | return "" ; |
1689 | } |
1690 | public String hh_load_context = "[-b] <contextName> <fileName>" ; |
1691 | public String ac_load_context_$_2( Args args ) throws CommandException { |
1692 | String name = args.argv(0) ; |
1693 | File file = new File( args.argv(1) ) ; |
1694 | |
1695 | if( ! file.canRead() ) |
1696 | throw new CommandException( "File not found : "+args.argv(1) ) ; |
1697 | |
1698 | if( ( args.optc() != 0 ) && ( args.optv(0).equals("-b") ) ){ |
1699 | FileInputStream in = null ; |
1700 | try{ |
1701 | long fileLength = file.length() ; |
1702 | byte [] buffer = new byte[(int)fileLength] ; |
1703 | in = new FileInputStream( file ) ; |
1704 | in.read( buffer ) ; |
1705 | in.close() ; |
1706 | _nucleus.getDomainContext().put( name , buffer ) ; |
1707 | }catch( IOException ioe ){ |
1708 | |
1709 | throw new CommandException( 11 , |
1710 | "Problem with file : "+file+" : "+ioe ) ; |
1711 | }finally{ |
1712 | if(in != null) try{in.close();}catch(IOException eeee){} |
1713 | } |
1714 | }else{ |
1715 | StringBuilder sb = new StringBuilder(); |
1716 | BufferedReader reader = null ; |
1717 | String line = null ; |
1718 | try{ |
1719 | reader = new BufferedReader( new FileReader( file ) ) ; |
1720 | while( ( line = reader.readLine() ) != null ) |
1721 | sb.append( line ).append( "\n" ) ; |
1722 | }catch( IOException ioe ){ |
1723 | |
1724 | throw new CommandException( 11 , |
1725 | "Problem with file : "+file+" : "+ioe ) ; |
1726 | }finally{ |
1727 | if(reader != null) try{reader.close();}catch(IOException eeee){} |
1728 | } |
1729 | _nucleus.getDomainContext().put( name , sb.toString() ) ; |
1730 | } |
1731 | return "Loaded ... " ; |
1732 | } |
1733 | //////////////////////////////////////////////////////////// |
1734 | // |
1735 | // the incredible copy command |
1736 | // |
1737 | public String fh_copy = |
1738 | " copy <fromCellURL> <toCellURL>\n"+ |
1739 | " <fromCellURL> : <extendedCellURL>\n"+ |
1740 | " Protocols : env/context/cell/http/file/ftp\n"+ |
1741 | " <toCellURL> : <env/context CellURL>\n"+ |
1742 | " Protocols : env/context\n\n" + |
1743 | " Protocols :\n"+ |
1744 | " env:<environmentVariable>\n"+ |
1745 | " context:<contextVariable>\n"+ |
1746 | " context://<cellPath>/<contextVariable>\n"+ |
1747 | " cell://<cellPath>/<requestString>\n" ; |
1748 | |
1749 | public String hh_copy = "<fromCellURL> <toCellURL>" ; |
1750 | public String ac_copy_$_2( Args args ) throws CommandException { |
1751 | URI from; |
1752 | URI to; |
1753 | try { |
1754 | from = new URI(args.argv(0)); |
1755 | to = new URI(args.argv(1)); |
1756 | } catch (URISyntaxException e) { |
1757 | throw new CommandException(43, "Invalid URL: "+ e.toString()); |
1758 | } |
1759 | if (from.equals(to)) { |
1760 | throw new CommandException(43, "Source and destination URL must not be the same"); |
1761 | } |
1762 | |
1763 | String source; |
1764 | try { |
1765 | BufferedReader in = new BufferedReader(open(from)); |
1766 | try { |
1767 | String line; |
1768 | StringBuilder sb = new StringBuilder(); |
1769 | while ((line = in.readLine()) != null) { |
1770 | sb.append(line).append("\n"); |
1771 | } |
1772 | source = sb.toString(); |
1773 | } finally { |
1774 | in.close(); |
1775 | } |
1776 | } catch (IOException e) { |
1777 | throw new CommandException(43, e.toString()); |
1778 | } |
1779 | |
1780 | String scheme = to.getScheme(); |
1781 | if (scheme == null) { |
1782 | scheme = "env"; |
1783 | } |
1784 | |
1785 | String destination = to.getSchemeSpecificPart(); |
1786 | if (destination == null) { |
1787 | throw new CommandException( 43 , "Destination missing"); |
1788 | } |
1789 | |
1790 | if (scheme.equals("env")) { |
1791 | _environment.put(destination, source); |
1792 | } else if (scheme.equals("context")) { |
1793 | _nucleus.getDomainContext().put(destination, source); |
1794 | } else { |
1795 | throw new CommandException(43, "Unsupported scheme for destination:" + scheme); |
1796 | } |
1797 | return "" ; |
1798 | } |
1799 | |
1800 | //////////////////////////////////////////////////////////// |
1801 | // |
1802 | // ---------------------------------------------- |
1803 | // |
1804 | public String hh_exit = "[<exitCode> [<exitMessage>]]" ; |
1805 | public String ac_exit_$_0_2( Args args ) throws CommandExitException { |
1806 | String msg = "" ; |
1807 | int code = 0 ; |
1808 | if( args.argc() > 0 ){ |
1809 | try{ |
1810 | code = new Integer(args.argv(0)).intValue() ; |
1811 | }catch( Exception e ){ |
1812 | code = 0 ; |
1813 | } |
1814 | if( args.argc() > 1 ){ |
1815 | msg = args.argv(1) ; |
1816 | } |
1817 | } |
1818 | throw new CommandExitException( msg , code ) ; |
1819 | } |
1820 | |
1821 | private Reader open(URI uri) |
1822 | throws IOException |
1823 | { |
1824 | String scheme = uri.getScheme(); |
1825 | String ssp = uri.getSchemeSpecificPart(); |
1826 | |
1827 | if (scheme == null) { |
1828 | return new InputStreamReader(uri.toURL().openStream()); |
1829 | } else if (scheme.equals("context")) { |
1830 | String host = uri.getHost(); |
1831 | String path = uri.getPath(); |
1832 | if (host == null) { |
1833 | return _nucleus.getDomainContextReader(ssp); |
1834 | } else { |
1835 | if (path == null || path.length() < 2) { |
1836 | throw new MalformedURLException("Cell URI must be on the form: context://domainname/variable"); |
1837 | } |
1838 | |
1839 | Object o = getRemoteData("System@" + host, |
1840 | "show context " + path.substring(1), |
1841 | 4000); |
1842 | |
1843 | if (o instanceof Exception) { |
1844 | throw new IOException(o.toString()); |
1845 | } |
1846 | |
1847 | return new StringReader(o.toString()); |
1848 | } |
1849 | } else if (scheme.equals("env")) { |
1850 | Object o = _environment.get(ssp); |
1851 | if (o == null) { |
1852 | throw new IOException("Variable not defined: " + ssp); |
1853 | } |
1854 | return new StringReader(o.toString()); |
1855 | } else if (scheme.equals("cell")) { |
1856 | String host = uri.getHost(); |
1857 | String path = uri.getPath(); |
1858 | if (host == null || path == null || path.length() < 2) { |
1859 | throw new MalformedURLException("Cell URI must be on the form: cell://cellname/command"); |
1860 | } |
1861 | Object o = getRemoteData(host, path.substring(1), 4000); |
1862 | |
1863 | if (o instanceof Exception) { |
1864 | throw new IOException(o.toString()); |
1865 | } |
1866 | |
1867 | return new StringReader(o.toString()); |
1868 | } else { |
1869 | return new InputStreamReader(uri.toURL().openStream()); |
1870 | } |
1871 | } |
1872 | |
1873 | private Object getRemoteData(String path, String command, long timeout) |
1874 | throws IOException |
1875 | { |
1876 | try { |
1877 | CellMessage answer = |
1878 | _nucleus.sendAndWait(new CellMessage(new CellPath(path), |
1879 | command), |
1880 | timeout); |
1881 | if (answer == null) |
1882 | throw new IOException("Request timed out"); |
1883 | return answer.getMessageObject(); |
1884 | } catch (InterruptedException e) { |
1885 | throw new InterruptedIOException(e.toString()); |
1886 | } catch (NoRouteToCellException e){ |
1887 | throw new IOException("sendAndWait : " + e); |
1888 | } |
1889 | } |
1890 | } |