1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.app.client.regular.shell;
18
19 import java.io.IOException;
20 import java.rmi.RemoteException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.gwe.api.ClientOrderBasedOperation;
27 import org.gwe.api.ClientOrderBasedQuery;
28 import org.gwe.api.ServerAPIConnectionException;
29 import org.gwe.api.Session4ClientAPIEnhancer;
30 import org.gwe.api.exceptions.GWEDomainException;
31 import org.gwe.api.exceptions.PasswordMismatchException;
32 import org.gwe.app.client.GWEASCIILogo;
33 import org.gwe.app.client.ProgressTracker;
34 import org.gwe.app.client.config.ClientConfig;
35 import org.gwe.app.client.config.XMLClientConfigReader;
36 import org.gwe.p2elv2.P2ELSyntaxException;
37 import org.gwe.persistence.model.HeadResourceInfo;
38 import org.gwe.persistence.model.OrderInfo;
39 import org.gwe.utils.StringUtils;
40 import org.gwe.utils.cmd.ArgsList;
41 import org.gwe.utils.rex.REXException;
42 import org.gwe.utils.security.CredentialNotFoundException;
43 import org.gwe.utils.security.Realm;
44 import org.gwe.utils.security.RealmTestResult;
45
46
47
48
49
50 public class ClientShellApp {
51
52 protected static final String QUEUE_ORDER_COMMAND = "queue-order";
53
54 private static Log log = LogFactory.getLog(ClientShellApp.class);
55
56 protected ProgressTracker tracker;
57 protected ClientConfig appConfig;
58 protected HeadResourceInfo cluster;
59
60 public ClientShellApp(String clientName, ProgressTracker tracker, int index, ArgsList argsList) throws CredentialNotFoundException, IOException {
61 this(clientName, tracker, argsList.extractArgIfPrefixed(index, XMLClientConfigReader.CONF_ARG_PREFIX));
62 }
63
64 public ClientShellApp(String clientName, ProgressTracker tracker, String confArg) throws CredentialNotFoundException, IOException {
65 this.tracker = tracker;
66 XMLClientConfigReader configReader = new XMLClientConfigReader(confArg);
67 this.cluster = configReader.getHeadResource();
68 printHeader(clientName);
69 this.appConfig = new ClientConfig(configReader);
70 printKeyStoreTestProblems(tracker);
71 this.appConfig.setTracker(tracker);
72 }
73
74 private void printHeader(String clientName) {
75 tracker.trackProgress("==============================================================");
76 tracker.trackProgress(GWEASCIILogo.prefixLogo(GWEASCIILogo.GWE, "\t") + "\n");
77 tracker.trackProgress("\tWelcome to the GWE " + clientName + " Application");
78 tracker.trackProgress("\t\tGWE daemon descriptor selected: '" + cluster.getName() + "'");
79 tracker.trackProgress("\t\tHost: " + cluster.getHost() + "");
80 tracker.trackProgress("\t\tQueue Size: " + cluster.getQueueSize() + " nodes");
81 tracker.trackProgress("\t\tHijack Timeout: " + toPrecision(cluster.getMaxHijackMins()) + " minutes");
82 tracker.trackProgress("\t\tIdle Timeout: " + toPrecision(cluster.getMaxIdleMins()) + " minutes");
83 tracker.trackProgress("\n==============================================================\n");
84 }
85
86 private void printKeyStoreTestProblems(ProgressTracker tracker) {
87 for (Realm realm : appConfig.getKeys().getRealms()) {
88 RealmTestResult testResult = realm.getTestResult();
89 if (testResult != RealmTestResult.OK)
90 tracker.trackProgress("WARNING: Problems testing realm '" + realm + "' - [" + testResult.getMessage() + "]" + "");
91 }
92 }
93
94 private String toPrecision(float number) {
95 int num100 = (int)(number * 100);
96 return (int)num100/100 + "." + (int)num100%100;
97 }
98
99 public ClientConfig getAppConfig() {
100 return appConfig;
101 }
102
103 public Session4ClientAPIEnhancer getSession() {
104 try {
105 return appConfig.getSessionsRepository().getSession(cluster, true);
106 } catch (Exception e) {
107 exit("");
108 }
109 return null;
110 }
111
112 public String processCommand(String[] args) throws ServerAPIConnectionException, P2ELSyntaxException, PasswordMismatchException, RemoteException, GWEDomainException, REXException {
113 String cmdName = args[0];
114 String[] actualArgs = StringUtils.removeArgs(args, 0, 1);
115 String output = "Command '" + cmdName + "' not supported";
116
117 Session4ClientAPIEnhancer session = getSession();
118 if (QUEUE_ORDER_COMMAND.equals(cmdName)) {
119 String stmt = StringUtils.getArrayAsStr(actualArgs);
120 if (stmt.trim().equals("")) return "P2EL statement missing!";
121 OrderInfo order = new OrderInfo(appConfig.createOrderDescriptor(stmt));
122 OrderInfo queuedOrder = session.queueOrder(order);
123 output = "Order queued for processing with id " + queuedOrder.getId();
124 }
125
126 if (ClientOrderBasedQuery.getQuery(cmdName) != null)
127 output = session.doOrderBasedQuery(ClientOrderBasedQuery.getQuery(cmdName), getIdArg(0, actualArgs), getIdArg(1, actualArgs));
128
129 if (ClientOrderBasedOperation.getOperation(cmdName) != null)
130 output = session.doOrderBasedOperation(ClientOrderBasedOperation.getOperation(cmdName), getIdArg(0, actualArgs));
131
132 return output;
133 }
134
135 protected List<String> getCommandList() {
136 List<String> result = new ArrayList<String>();
137 result.add(QUEUE_ORDER_COMMAND);
138 result.addAll(ClientOrderBasedOperation.getOperations());
139 result.addAll(ClientOrderBasedQuery.getQueries());
140 return result;
141 }
142
143 private int getIdArg(int idx, String[] args) {
144 try {
145 return (idx > args.length - 1) ? -1 : Integer.parseInt(args[idx]);
146 } catch(NumberFormatException e) {
147 throw new RuntimeException("Argument '" + args[idx] + "' is not an integer");
148 }
149 }
150
151 protected void exit(String message) {
152 tracker.trackProgress((message + ".\nExiting."));
153 System.exit(0);
154 }
155 }
156