/
AppClientContainerBuilder.java
373 lines (317 loc) · 13.9 KB
/
AppClientContainerBuilder.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.appclient.client.acc;
import com.sun.enterprise.container.common.spi.util.InjectionException;
import com.sun.enterprise.module.bootstrap.BootException;
import com.sun.enterprise.util.LocalStringManager;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.callback.CallbackHandler;
import org.glassfish.appclient.client.acc.config.AuthRealm;
import org.glassfish.appclient.client.acc.config.ClientCredential;
import org.glassfish.appclient.client.acc.config.MessageSecurityConfig;
import org.glassfish.appclient.client.acc.config.Property;
import org.glassfish.appclient.client.acc.config.TargetServer;
import org.glassfish.appclient.client.acc.config.util.XML;
import org.glassfish.enterprise.iiop.api.GlassFishORBHelper;
import org.glassfish.internal.api.ORBLocator;
import org.xml.sax.SAXException;
/**
* Implements a builder for accumulating configuration information for the
* app client container and then starting the ACC.
* <p>
* The interface for the ACC builder is defined as AppClientContainer.Builder so the
* relevant JavaDoc is concentrated in that one class.
*<p>
* The AppClientContainerBuilder class records the
* information the container itself needs in order to operate.
*
* @author tjquinn
*/
public class AppClientContainerBuilder implements AppClientContainer.Builder {
private final static String ENDPOINTS_PROPERTY_NAME = "com.sun.appserv.iiop.endpoints";
private static final LocalStringManager localStrings = new LocalStringManagerImpl(AppClientContainerBuilder.class);
/** caller-specified target servers */
private TargetServer[] targetServers;
/** caller-optional logger - initialized to logger name from the class; caller can override with logger method */
private Logger logger = Logger.getLogger(getClass().getName());
private AuthRealm authRealm = null;
private final URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
/**
* The caller can pre-set the client credentials using the
* <code>clientCredentials</code> method. The ACC will use the
* username and realm values in intializing a callback handler if one is
* needed.
*/
private ClientCredential clientCredential;
private boolean sendPassword = true;
private GlassFishORBHelper orbHelper;
/** caller-provided message security configurations */
private final List<MessageSecurityConfig> messageSecurityConfigs = new ArrayList<>();
/**
* optional caller-specified properties governing the ACC's behavior.
* Correspond to the property elements available in the client-container
* element from sun-application-client-containerxxx.dtd.
*/
private Properties containerProperties;
AppClientContainerBuilder() {
}
/**
* Creates a new builder with the specified target servers and client URI.
*
* @param targetServers the <code>TargetServer</code>s to use
* @param clientURI the URI of the client archive to launch
*/
AppClientContainerBuilder(final TargetServer[] targetServers) {
this.targetServers = targetServers;
}
public AppClientContainer newContainer(final Class mainClass,
final CallbackHandler callerSpecifiedCallbackHandler) throws Exception {
prepareHabitat();
Launchable client = Launchable.LaunchableUtil.newLaunchable(
ACCModulesManager.getHabitat(), mainClass);
AppClientContainer container = createContainer(client,
callerSpecifiedCallbackHandler, false /* istextAuth */);
return container;
}
@Override
public AppClientContainer newContainer(final Class mainClass) throws Exception {
return newContainer(mainClass, null);
}
@Override
public AppClientContainer newContainer(final URI clientURI,
final CallbackHandler callerSpecifiedCallbackHandler,
final String callerSpecifiedMainClassName,
final String callerSpecifiedAppClientName) throws Exception, UserError {
return newContainer(clientURI, callerSpecifiedCallbackHandler,
callerSpecifiedMainClassName,
callerSpecifiedAppClientName,
false /* isTextAuth */);
}
@Override
public AppClientContainer newContainer(final URI clientURI,
final CallbackHandler callerSpecifiedCallbackHandler,
final String callerSpecifiedMainClassName,
final String callerSpecifiedAppClientName,
final boolean isTextAuth) throws Exception, UserError {
prepareHabitat();
prepareIIOP(targetServers, containerProperties);
Launchable client = Launchable.LaunchableUtil.newLaunchable(
clientURI,
callerSpecifiedMainClassName,
callerSpecifiedAppClientName,
ACCModulesManager.getHabitat());
AppClientContainer container = createContainer(client,
callerSpecifiedCallbackHandler, isTextAuth);
return container;
}
@Override
public AppClientContainer newContainer(final URI clientURI) throws Exception, UserError {
return newContainer(clientURI, null, null, null);
}
private AppClientContainer createContainer(final Launchable client,
final CallbackHandler callerSuppliedCallbackHandler,
final boolean isTextAuth) throws BootException, BootException, URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InjectionException, IOException, SAXException {
AppClientContainer container = ACCModulesManager.getService(AppClientContainer.class);
//process the packaged permissions.xml
container.processPermissions();
container.setClient(client);
container.setBuilder(this);
CallbackHandler callbackHandler =
(callerSuppliedCallbackHandler != null ?
callerSuppliedCallbackHandler : getCallbackHandlerFromDescriptor(client.getDescriptor(classLoader).getCallbackHandler()));
container.prepareSecurity(targetServers, messageSecurityConfigs, containerProperties,
clientCredential, callbackHandler, classLoader, isTextAuth);
return container;
}
private CallbackHandler getCallbackHandlerFromDescriptor(final String callbackHandlerName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
if (callbackHandlerName != null && ! callbackHandlerName.equals("")) {
Class<CallbackHandler> callbackHandlerClass =
(Class<CallbackHandler>) Class.forName(callbackHandlerName, true, classLoader);
return callbackHandlerClass.newInstance();
}
return null;
}
private void prepareHabitat() throws URISyntaxException {
ACCModulesManager.initialize(Thread.currentThread().getContextClassLoader());
orbHelper = ACCModulesManager.getService(GlassFishORBHelper.class);
}
/**
* Prepares the client ORB to bootstrap into the server ORB(s) specified
* by the TargetServer objects.
* @param targetServers the TargetServer endpoints to which the client ORB can try to connect
* @param containerProperties Properties, if specified, which might indicate that SSL is to be used
* @return ORB-related properties to define host and port for bootstrapping
*/
private void prepareIIOP(final TargetServer[] targetServers, Properties containerProperties) {
if (targetServers.length == 0) {
throw new IllegalArgumentException();
}
final StringBuilder sb = new StringBuilder();
for (TargetServer ts : targetServers) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(ts.getAddress()).append(":").append(ts.getPort());
}
/*
* If the user has explicitly defined the ORB-related properties, do
* not override those settings.
*/
if (targetServers.length == 1) {
defineIfNotDefined(ORBLocator.OMG_ORB_INIT_HOST_PROPERTY,
targetServers[0].getAddress());
defineIfNotDefined(ORBLocator.OMG_ORB_INIT_PORT_PROPERTY,
Integer.toString(targetServers[0].getPort()));
} else {
/*
* Currently, set a system property to specify multiple endpoints.
*/
defineIfNotDefined(ENDPOINTS_PROPERTY_NAME, sb.toString());
}
if (isSSLRequired(targetServers, containerProperties)) {
orbHelper.setCSIv2Prop(ORBLocator.ORB_SSL_CLIENT_REQUIRED, "true");
}
logger.log(Level.CONFIG, "Using endpoint address(es): {0}", sb.toString());
}
/**
* Define the specified system property using the new value unless the
* property is already set.
* @param propName name of the property to check and, possibly, set
* @param newPropValue value to set if the property is not already set
*/
private void defineIfNotDefined(final String propName, final String newPropValue) {
if (System.getProperty(propName) == null) {
if (newPropValue == null) {
throw new RuntimeException(localStrings.getLocalString(
AppClientContainerBuilder.class,
"appclient.missingValue",
"Value for {0} expected but was not configured or assigned",
new Object[] {propName}
));
}
System.setProperty(propName, newPropValue);
}
}
/**
* Reports whether the ORB should be requested to use SSL.
* <p>
* If any TargetServer specifies SSL or the container-level properties
* specify SSL then report "true."
* @param targetServers configured TargetServer(s)
* @param containerProperties configured container-level properties
* @return whether the target servers or the properties implies the use of SSL
*/
private boolean isSSLRequired(final TargetServer[] targetServers, final Properties containerProperties) {
if (containerProperties != null) {
String sslPropertyValue = containerProperties.getProperty("ssl");
if ("required".equals(sslPropertyValue)) {
return true;
}
}
for (TargetServer ts : targetServers) {
/*
* If this target server has the optional security sub-item then
* the security sub-item must have an ssl sub-item. So we can just
* look for the security sub-item.
*/
if (ts.getSecurity() != null) {
return true;
}
}
return false;
}
@Override
public AppClientContainerBuilder addMessageSecurityConfig(final MessageSecurityConfig msConfig) {
messageSecurityConfigs.add(msConfig);
return this;
}
@Override
public List<MessageSecurityConfig> getMessageSecurityConfig() {
return this.messageSecurityConfigs;
}
@Override
public AppClientContainerBuilder logger(final Logger logger) {
this.logger = logger;
return this;
}
@Override
public Logger getLogger() {
return logger;
}
@Override
public AppClientContainerBuilder authRealm(final String className) {
authRealm = new AuthRealm(className);
return this;
}
@Override
public AuthRealm getAuthRealm() {
return authRealm;
}
@Override
public AppClientContainerBuilder clientCredentials(final String user, final char[] password) {
return clientCredentials(user, password, null);
}
@Override
public AppClientContainerBuilder clientCredentials(final String user, final char[] password, final String realm) {
ClientCredential cc = new ClientCredential(user, new XML.Password(password), realm);
return clientCredentials(cc);
}
public AppClientContainerBuilder clientCredentials(final ClientCredential cc) {
clientCredential = cc;
return this;
}
@Override
public ClientCredential getClientCredential() {
return clientCredential;
}
@Override
public AppClientContainerBuilder containerProperties(final Properties props) {
this.containerProperties = props;
return this;
}
@Override
public AppClientContainerBuilder containerProperties(final List<Property> props) {
containerProperties = XML.toProperties(props);
return this;
}
@Override
public Properties getContainerProperties() {
return containerProperties;
}
@Override
public AppClientContainerBuilder sendPassword(final boolean sendPassword){
this.sendPassword = sendPassword;
return this;
}
@Override
public boolean getSendPassword() {
return sendPassword;
}
@Override
public TargetServer[] getTargetServers() {
return targetServers;
}
}