View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.xml.registry.ebxml; 12 13 import com.sun.security.auth.callback.DialogCallbackHandler; 14 15 import com.sun.xml.registry.ebxml.infomodel.RegistryPackageImpl; 16 import com.sun.xml.registry.ebxml.util.I18nUtil; 17 import com.sun.xml.registry.ebxml.util.KeystoreUtil; 18 import com.sun.xml.registry.ebxml.jaas.LoginModuleManager; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.io.File; 24 import java.io.FileOutputStream; 25 import java.io.IOException; 26 import java.io.PrintWriter; 27 28 import java.util.ArrayList; 29 import java.util.HashSet; 30 import java.util.Iterator; 31 import java.util.Properties; 32 import java.util.Set; 33 34 import javax.security.auth.Subject; 35 import javax.security.auth.callback.CallbackHandler; 36 import javax.security.auth.login.LoginContext; 37 import javax.security.auth.login.LoginException; 38 import javax.security.auth.x500.X500PrivateCredential; 39 40 import javax.xml.registry.BulkResponse; 41 import javax.xml.registry.BusinessLifeCycleManager; 42 import javax.xml.registry.Connection; 43 import javax.xml.registry.JAXRException; 44 import javax.xml.registry.RegistryService; 45 import javax.xml.registry.infomodel.User; 46 47 48 /*** 49 * ConnectionImpl 50 */ 51 public class ConnectionImpl implements Connection { 52 53 /*** DOCUMENT ME! */ 54 private final Log log = LogFactory.getLog(this.getClass()); 55 private ConnectionFactoryImpl factory = null; 56 private RegistryServiceImpl service = null; 57 private String queryManagerURL; 58 private String lifeCycleManagerURL; 59 private X500PrivateCredential x500Cred; 60 private boolean closed = false; 61 private boolean synchronous = true; 62 private CallbackHandler handler = null; 63 private LoginModuleManager loginModuleMgr = null; 64 65 /*** 66 * Creates a new ConnectionImpl object. 67 * 68 * @param factory DOCUMENT ME! 69 * 70 * @throws JAXRException DOCUMENT ME! 71 */ 72 ConnectionImpl(ConnectionFactoryImpl factory) throws JAXRException { 73 this.factory = factory; 74 75 Properties props = factory.getProperties(); 76 queryManagerURL = 77 props.getProperty("javax.xml.registry.queryManagerURL"); 78 lifeCycleManagerURL = 79 props.getProperty("javax.xml.registry.lifeCycleManagerURL"); 80 loginModuleMgr = new LoginModuleManager(); 81 } 82 83 /*** 84 * Gets the RegistryService interface associated with the 85 * Connection. If a Connection property (e.g. credentials) is set 86 * after the client calls getRegistryService then the newly set 87 * Connection property is visible to the RegistryService 88 * previously returned by this call. 89 * 90 * <p> 91 * <DL> 92 * <dt> 93 * <B>Capability Level: 0 </B> 94 * </dt> 95 * </dl> 96 * </p> 97 * 98 * @see javax.xml.registry.RegistryService 99 */ 100 public RegistryService getRegistryService() throws JAXRException { 101 102 if (service == null) { 103 service = new RegistryServiceImpl(this); 104 } 105 106 return service; 107 } 108 109 /*** 110 * Since a provider typically allocates significant resources 111 * outside the JVM on behalf of a Connection, clients should 112 * close them when they are not needed. 113 * 114 * <p> 115 * 116 * <DL> 117 * <dt> 118 * <B>Capability Level: 0 </B> 119 * </dt> 120 * </dl> 121 * </p> 122 * 123 * @exception JAXRException if a JARR error occurs. 124 */ 125 public void close() throws JAXRException { 126 127 // ??eeg Do we need to do anything to reduce resources here? 128 closed = true; 129 } 130 131 /*** 132 * Return true if this Connection has been closed. 133 * 134 * <p> 135 * <DL> 136 * <dt> 137 * <B>Capability Level: 0 </B> 138 * </dt> 139 * </dl> 140 * </p> 141 * 142 * @return DOCUMENT ME! 143 * 144 * @throws JAXRException DOCUMENT ME! 145 */ 146 public boolean isClosed() throws JAXRException { 147 148 return closed; 149 } 150 151 /*** 152 * Return true if client uses synchronous communication with JAXR 153 * provider. Note that a JAXR provider must support both modes of 154 * communication, while the client can choose which mode it wants 155 * to use. Default is a return value of true (synchronous 156 * communication). 157 * 158 * <p> 159 * <DL> 160 * <dt> 161 * <B>Capability Level: 0</B> 162 * </dt> 163 * </dl> 164 * </p> 165 * 166 * @return DOCUMENT ME! 167 * 168 * @throws JAXRException DOCUMENT ME! 169 */ 170 public boolean isSynchronous() throws JAXRException { 171 172 return synchronous; 173 } 174 175 /*** 176 * Sets whether the client uses synchronous communication or not. 177 * A JAXR client may dynamically change its communication style 178 * preference. 179 * 180 * <p> 181 * <DL> 182 * <dt> 183 * <B>Capability Level: 0 </B> 184 * </dt> 185 * </dl> 186 * </p> 187 * 188 * @param sync DOCUMENT ME! 189 * 190 * @throws JAXRException DOCUMENT ME! 191 */ 192 public void setSynchronous(boolean sync) throws JAXRException { 193 194 //??eeg A value of false is not implemented yet! 195 synchronous = sync; 196 } 197 198 /*** 199 * Sets the Credentials associated with this client. The 200 * credentials is used to authenticate the client with the JAXR 201 * provider. A JAXR client may dynamically change its identity by 202 * changing the credentials associated with it. 203 * 204 * <p> 205 * <DL> 206 * <dt> 207 * <B>Capability Level: 0 </B> 208 * </dt> 209 * </dl> 210 * </p> 211 * 212 * @param credentials a Collection oj java.lang.Objects which 213 * provide identity related information for the caller. 214 * 215 * @throws JAXRException If the JAXR provider encounters an 216 * internal error 217 */ 218 public void setCredentials(Set credentials) throws JAXRException { 219 220 for (Iterator it = credentials.iterator(); it.hasNext();) { 221 222 Object obj = it.next(); 223 224 if (obj instanceof X500PrivateCredential) { 225 x500Cred = (X500PrivateCredential)obj; 226 return; 227 } 228 } 229 230 throw new JAXRException("No instance of X500PrivateCredential found"); 231 } 232 233 /*** 234 * Gets the credentials associated with this client. 235 * 236 * <p> 237 * <DL> 238 * <dt> 239 * <B>Capability Level: 0 </B> 240 * </dt> 241 * </dl> 242 * </p> 243 * 244 * @return Set of java.lang.Object instances. The Collection may be 245 * empty but not null. 246 * 247 * @throws JAXRException If the JAXR provider encounters an 248 * internal error 249 */ 250 public Set getCredentials() throws JAXRException { 251 252 HashSet ret = new HashSet(); 253 254 if (x500Cred != null) { 255 ret.add(x500Cred); 256 } 257 258 return ret; 259 } 260 261 /*** 262 * DOCUMENT ME! 263 * 264 * @return The X500PrivateCredential or null if not set 265 */ 266 X500PrivateCredential getX500PrivateCredential() { 267 268 if (x500Cred == null) { 269 270 try { 271 authenticate(); 272 } catch (JAXRException e) { 273 log.error(e); 274 } 275 } 276 277 return x500Cred; 278 } 279 280 /*** 281 * DOCUMENT ME! 282 * 283 * @return DOCUMENT ME! 284 */ 285 String getQueryManagerURL() { 286 287 return queryManagerURL; 288 } 289 290 /*** 291 * DOCUMENT ME! 292 * 293 * @return DOCUMENT ME! 294 */ 295 String getLifeCycleManagerURL() { 296 297 return lifeCycleManagerURL; 298 } 299 300 /*** 301 * DOCUMENT ME! 302 * 303 * @return DOCUMENT ME! 304 */ 305 public ConnectionFactoryImpl getConnectionFactory() { 306 307 return factory; 308 } 309 310 /*** 311 * This method is used to get the reference to the LoginModuleManager 312 * With this reference, references to a parent Frame and Log can be 313 * passed to the LoginModuleManager. 314 * 315 * @return 316 * A reference to the LoginModuleManager 317 */ 318 public LoginModuleManager getLoginModuleManager() 319 { 320 return loginModuleMgr; 321 } 322 323 /*** 324 * DOCUMENT ME! 325 * 326 * @return DOCUMENT ME! 327 */ 328 public CallbackHandler getCallbackHandler() throws JAXRException { 329 330 if (handler == null) { 331 handler = loginModuleMgr.getCallbackHandler(); 332 } 333 return handler; 334 } 335 336 /*** 337 * DOCUMENT ME! 338 * 339 * @param handler DOCUMENT ME! 340 */ 341 public void setCallbackHandler(CallbackHandler handler) { 342 loginModuleMgr.setDefaultCallbackHandler(handler); 343 } 344 345 /*** 346 * Determine whether the user has already authenticated and setCredentials 347 * on the Connection or not. 348 * Add to JAXR 2.0?? 349 * 350 * @param handler DOCUMENT ME! 351 */ 352 public boolean isAuthenticated() throws JAXRException { 353 boolean authenticated = false; 354 if (x500Cred != null) { 355 authenticated = true; 356 } 357 358 return authenticated; 359 } 360 361 /*** 362 * Forces authentication to occur. 363 ** Add to JAXR 2.0?? 364 * 365 * @throws JAXRException DOCUMENT ME! 366 */ 367 public void authenticate() throws JAXRException { 368 369 // Obtain a LoginContext, needed for authentication. Tell it 370 // to use the LoginModule implementation specified by the 371 // entry named "Sample" in the JAAS login configuration 372 // file and to also use the specified CallbackHandler. 373 LoginContext lc = null; 374 375 try { 376 377 loginModuleMgr.createLoginConfigFile(); 378 String applicationName = loginModuleMgr.getApplicationName(); 379 handler = loginModuleMgr.getCallbackHandler(); 380 381 lc = new LoginContext(applicationName, handler); 382 383 // attempt authentication 384 lc.login(); 385 386 //Get teh authenticated Subject. 387 Subject subject = lc.getSubject(); 388 Set privateCredentials = subject.getPrivateCredentials(); 389 390 //Set credentials on JAXR Connections 391 setCredentials(privateCredentials); 392 393 log.info("Set credentials on connection."); 394 } catch (LoginException le) { 395 396 String msg = le.getMessage(); 397 398 if ((msg != null) 399 && (!(msg.equalsIgnoreCase("Login cancelled")))) { 400 throw new JAXRException(le); 401 } 402 } catch (SecurityException se) { 403 throw new JAXRException(se); 404 } 405 } 406 407 /*** 408 * Logout current user if any. 409 * 410 */ 411 public void logoff() throws JAXRException { 412 boolean authenticated = isAuthenticated(); 413 if (authenticated) { 414 x500Cred = null; 415 } 416 } 417 418 }

This page was automatically generated by Maven