KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > examples > Stack


1 /*
2
3 Copyright 2004, Martian Software, Inc.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun.examples;
20
21 import com.martiansoftware.nailgun.NGContext;
22 import com.martiansoftware.nailgun.NGServer;
23
24 /**
25  * Provides some nice command-line stack operations. This nail must
26  * have the aliases "push" and "pop" associated with it in order to
27  * work properly.
28  *
29  * If the "push" command is used, each argument on the command line
30  * is pushed onto the stack (in order) and the program returns
31  * immediately.
32  *
33  * If the "pop" command is used, the top item on the stack is displayed
34  * to the client's stdout. If the stack is empty, the client will
35  * block until another process calls push. If the nailgun server is
36  * shutdown while pop is blocking, pop will cause the client to exit
37  * with exit code 1. This is thread-safe: you can have multiple
38  * clients waiting on "pop" and only one of them (determined by the VM
39  * and the magic of synchronization) will receive any one pushed item.
40  *
41  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
42  */

43 public class Stack {
44
45     private static java.util.Stack JavaDoc sharedStack = new java.util.Stack JavaDoc();
46     private static boolean done = false;
47     
48     public static void nailShutdown(NGServer server) {
49         done = true;
50         synchronized(sharedStack) {
51             sharedStack.notifyAll();
52         }
53     }
54     
55     public static void nailMain(NGContext context) throws InterruptedException JavaDoc {
56         if (context.getCommand().equals("push")) {
57             synchronized(sharedStack) {
58                 String JavaDoc[] args = context.getArgs();
59                 for (int i = 0; i < args.length; ++i) {
60                     sharedStack.push(args[i]);
61                 }
62                 sharedStack.notifyAll();
63                 context.exit(0);
64                 return;
65             }
66         } else if (context.getCommand().equals("pop")) {
67             int exitCode = 1;
68             synchronized(sharedStack) {
69                 while (!done && (sharedStack.size() == 0)) {
70                     sharedStack.wait();
71                 }
72                 if (sharedStack.size() > 0) {
73                     context.out.println(sharedStack.pop());
74                     exitCode = 0;
75                 }
76             }
77             context.exit(exitCode);
78             return;
79         }
80     }
81 }
82
Popular Tags