This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
RMIConfig.java
246 lines (212 loc) · 8.02 KB
/
RMIConfig.java
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
/**
* Copyright 1999-2010 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.globus.workspace.remoting.admin.client;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.varia.NullAppender;
import org.globus.workspace.remoting.RemotingClient;
import org.nimbustools.api.brain.NimbusHomePathResolver;
import java.io.*;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Properties;
/**
* This class handles the basic configuration for an RMI connection
*/
public class RMIConfig {
protected static final Log logger =
LogFactory.getLog(RMIConfig.class.getName());
public static final int EXIT_OK = 0;
public static final int EXIT_PARAMETER_PROBLEM = 1;
public static final int EXIT_EXECUTION_PROBLEM = 2;
public static final int EXIT_UNKNOWN_PROBLEM = 3;
private static final String PROP_SOCKET_DIR = "socket.dir";
protected final Gson gson = new Gson();
protected Reporter reporter;
protected OutputStream outStream;
protected Properties properties;
protected String configPath;
private File socketDirectory;
private String bindingName;
protected void setupDebug(String args[]) {
boolean isDebug = false;
final String debugFlag = "--" + Opts.DEBUG_LONG;
for (String arg : args) {
if (debugFlag.equals(arg)) {
isDebug = true;
break;
}
}
if (isDebug) {
final PatternLayout layout = new PatternLayout("%C{1}:%L - %m%n");
final ConsoleAppender consoleAppender = new ConsoleAppender(layout, "System.err");
BasicConfigurator.configure(consoleAppender);
logger.info("Debug mode enabled");
}
else {
BasicConfigurator.configure(new NullAppender());
}
}
protected void loadConfig(String bindingDir) throws ParameterProblem, ExecutionProblem {
if(configPath == null)
throw new ParameterProblem("Config path is invalid.");
final File configFile = new File(configPath);
logger.debug("Loading config file: " + configFile.getAbsolutePath());
if (!configFile.canRead()) {
throw new ParameterProblem(
"Specified config file path does not exist or is not readable: " +
configFile.getAbsolutePath());
}
final Properties props = new Properties();
this.properties = props;
try {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(configFile);
props.load(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
catch (IOException e) {
logger.debug("Caught error reading config file " + configFile.getAbsolutePath(), e);
throw new ParameterProblem("Failed to load config file: " +
configFile.getAbsolutePath() + ": " + e.getMessage(), e);
}
final String sockDir = props.getProperty(PROP_SOCKET_DIR);
if (sockDir == null) {
throw new ExecutionProblem("Configuration file is missing "+
PROP_SOCKET_DIR + " entry: " + configFile.getAbsolutePath());
}
final NimbusHomePathResolver resolver = new NimbusHomePathResolver();
String path = resolver.resolvePath(sockDir);
if (path == null) {
path = sockDir;
}
this.socketDirectory = new File(path);
final String binding = props.getProperty(bindingDir);
if (binding == null) {
throw new ExecutionProblem("Configuration file is missing " +
bindingDir + " entry: "+
configFile.getAbsolutePath());
}
this.bindingName = binding;
}
protected Remote setupRemoting() throws ExecutionProblem {
final RemotingClient client = new RemotingClient();
client.setSocketDirectory(this.socketDirectory);
try {
client.initialize();
}
catch (RemoteException e) {
handleRemoteException(e);
}
try {
final Remote remote = client.lookup(this.bindingName);
logger.debug("Found remote object " + remote.toString());
return remote;
}
catch (RemoteException e) {
handleRemoteException(e);
return null;
}
catch (NotBoundException e) {
throw new ExecutionProblem("Failed to bind to object '" +
this.bindingName +
"'. There may be a configuration problem between the "+
"client and service. Error: "+ e.getMessage(), e);
}
}
protected static String[] parseFields(String fieldsString, AdminEnum action)
throws ParameterProblem {
final String[] fieldsArray = fieldsString.trim().split("\\s*,\\s*");
if (fieldsArray.length == 0) {
throw new ParameterProblem("Report fields list is empty");
}
for (String field : fieldsArray) {
boolean found = false;
for (String actionField : action.fields()) {
if (field.equals(actionField)) {
found = true;
break;
}
}
if (!found) {
throw new ParameterProblem("Report field '"+ field +
"' is not allowed for this action. Allowed fields are: " +
csvString(action.fields()));
}
}
return fieldsArray;
}
protected String getHelpText(InputStream input) {
InputStream is = null;
BufferedInputStream bis = null;
try {
is = input;
if (is == null) {
return "";
}
bis = new BufferedInputStream(is);
StringBuilder sb = new StringBuilder();
byte[] chars = new byte[1024];
int bytesRead;
while( (bytesRead = bis.read(chars)) > -1){
sb.append(new String(chars, 0, bytesRead));
}
return sb.toString();
} catch (IOException e) {
logger.error("Error reading help text", e);
return "";
} finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
logger.error("Error reading help text", e);
}
}
}
private static String csvString(String[] fields) {
final StringBuilder sb = new StringBuilder();
for (String f : fields) {
if (sb.length() != 0) {
sb.append(", ");
}
sb.append(f);
}
return sb.toString();
}
protected void handleRemoteException(RemoteException e) throws ExecutionProblem {
throw new ExecutionProblem(
"Failed to connect to Nimbus service over domain sockets. "+
"Is the service running?\n\nSocket directory: " +
this.socketDirectory.getAbsolutePath() + "\n\nError: " +
e.getMessage(), e);
}
}