01: /*
02:
03: Copyright 2004, Martian Software, Inc.
04:
05: Licensed under the Apache License, Version 2.0 (the "License");
06: you may not use this file except in compliance with the License.
07: You may obtain a copy of the License at
08:
09: 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.builtins;
20:
21: import java.util.Iterator;
22: import java.util.Set;
23:
24: import com.martiansoftware.nailgun.Alias;
25: import com.martiansoftware.nailgun.NGContext;
26: import com.martiansoftware.nailgun.NGServer;
27:
28: /**
29: * <p>Provides a means to view and add aliases. This is aliased by default
30: * to the command "<code>ng-alias</code>".</p>
31: *
32: * <p>No command line validation is performed. If you trigger an exception,
33: * your client will display it.</p>
34: *
35: * <p><b>To view the current alias list</b>, issue the command:
36: * <pre><code>ng-alias</code></pre>
37: * with no arguments.</p>
38: *
39: * <p><b>To add or replace an alias</b>, issue the command:
40: * <pre><code>ng-alias [alias name] [fully qualified aliased class name]</code></pre>
41: * </p>
42: *
43: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
44: */
45: public class NGAlias {
46:
47: private static String padl(String s, int len) {
48: StringBuffer buf = new StringBuffer(s);
49: while (buf.length() < len)
50: buf.append(" ");
51: return (buf.toString());
52: }
53:
54: public static void nailMain(NGContext context)
55: throws ClassNotFoundException {
56:
57: String[] args = context.getArgs();
58: NGServer server = context.getNGServer();
59:
60: if (args.length == 0) {
61: Set aliases = server.getAliasManager().getAliases();
62:
63: // let's pad this nicely. first, find the longest alias
64: // name. then pad the others to that width.
65: int maxAliasLength = 0;
66: int maxClassnameLength = 0;
67: for (Iterator i = aliases.iterator(); i.hasNext();) {
68: Alias alias = (Alias) i.next();
69: maxAliasLength = Math.max(maxAliasLength, alias
70: .getName().length());
71: maxClassnameLength = Math.max(maxClassnameLength, alias
72: .getAliasedClass().getName().length());
73: }
74: for (Iterator i = aliases.iterator(); i.hasNext();) {
75: Alias alias = (Alias) i.next();
76: context.out.println(padl(alias.getName(),
77: maxAliasLength)
78: + "\t"
79: + padl(alias.getAliasedClass().getName(),
80: maxClassnameLength));
81: context.out.println(padl("", maxAliasLength) + "\t"
82: + alias.getDescription());
83: context.out.println();
84: }
85: } else if (args.length == 2) {
86: server.getAliasManager().addAlias(
87: new Alias(args[0], "", Class.forName(args[1])));
88: }
89: }
90: }
|