1 4 package com.nightlabs.ipanema.organisation; 5 6 import java.rmi.RemoteException ; 7 import java.util.Collection ; 8 import java.util.List ; 9 import java.util.Properties ; 10 11 import javax.ejb.CreateException ; 12 import javax.ejb.EJBException ; 13 import javax.ejb.SessionBean ; 14 import javax.ejb.SessionContext ; 15 import javax.jdo.FetchPlan; 16 import javax.jdo.JDOObjectNotFoundException; 17 import javax.jdo.PersistenceManager; 18 import javax.naming.InitialContext ; 19 20 import org.apache.log4j.Logger; 21 22 import com.nightlabs.ModuleException; 23 import com.nightlabs.ipanema.base.BaseSessionBeanImpl; 24 import com.nightlabs.ipanema.security.User; 25 import com.nightlabs.ipanema.security.UserManager; 26 import com.nightlabs.ipanema.security.UserManagerHome; 27 import com.nightlabs.ipanema.security.UserManagerUtil; 28 import com.nightlabs.ipanema.server.id.ServerID; 29 import com.nightlabs.ipanema.servermanager.IpanemaServerManager; 30 import com.nightlabs.ipanema.servermanager.config.OrganisationCf; 31 32 36 37 38 45 public abstract class OrganisationManagerBean 46 extends BaseSessionBeanImpl 47 implements SessionBean  48 { 49 public static final Logger LOGGER = Logger.getLogger(OrganisationManagerBean.class); 50 51 54 public void setSessionContext(SessionContext sessionContext) 55 throws EJBException , RemoteException  56 { 57 System.out.println(this.getClass().getName() + ".setSessionContext("+sessionContext+")"); 58 super.setSessionContext(sessionContext); 59 } 60 67 public void ejbCreate() 68 throws CreateException  69 { 70 System.out.println(this.getClass().getName() + ".ejbCreate()"); 71 } 80 85 public void ejbRemove() throws EJBException , RemoteException { } 86 87 103 public void createOrganisation(String organisationID, String organisationDisplayName, String masterOrganisationID) 104 throws ModuleException 105 { 106 IpanemaServerManager ism = getIpanemaServerManager(); 107 try { 108 ism.createOrganisation(organisationID, organisationDisplayName, masterOrganisationID); 109 } finally { 110 ism.close(); 111 } 112 } 113 114 130 public void createOrganisation(String organisationID, String organisationDisplayName, String userID, String password, boolean isServerAdmin) 131 throws ModuleException 132 { 133 IpanemaServerManager ism = getIpanemaServerManager(); 134 try { 135 ism.createOrganisation(organisationID, organisationDisplayName, userID, password, isServerAdmin); 136 } finally { 137 ism.close(); 138 } 139 } 140 141 142 146 public List getOrganisationCfs(boolean sorted) 147 throws ModuleException 148 { 149 IpanemaServerManager ism = getIpanemaServerManager(); 150 try { 151 return ism.getOrganisationCfs(sorted); 152 } finally { 153 ism.close(); 154 } 155 } 156 157 161 public OrganisationCf getOrganisationConfig(String organisationID) 162 throws ModuleException 163 { 164 IpanemaServerManager ism = getIpanemaServerManager(); 165 try { 166 return ism.getOrganisationConfig(organisationID); 167 } finally { 168 ism.close(); 169 } 170 } 171 172 180 public boolean isOrganisationCfsEmpty() 181 throws ModuleException 182 { 183 IpanemaServerManager ism = getIpanemaServerManager(); 184 try { 185 return ism.isOrganisationCfsEmpty(); 186 } finally { 187 ism.close(); 188 } 189 } 190 191 195 public Collection getPendingRegistrations(String [] fetchGroups) 196 throws ModuleException 197 { 198 PersistenceManager pm = getPersistenceManager(); 199 try { 200 if (fetchGroups != null) { 201 FetchPlan fetchPlan = pm.getFetchPlan(); 202 for (int i = 0; i < fetchGroups.length; ++i) 203 fetchPlan.addGroup(fetchGroups[i]); 204 } 205 206 return pm.detachCopyAll( 207 LocalOrganisation.getLocalOrganisation(pm).getPendingRegistrations()); 208 } finally { 209 pm.close(); 210 } 211 } 212 213 226 public void notifyAcceptRegistration(String registrationID, Organisation grantOrganisation, String userPassword) 227 throws ModuleException 228 { 229 if (registrationID == null) 230 throw new NullPointerException ("registrationID"); 231 232 if (userPassword == null) 233 throw new NullPointerException ("userPassword"); 234 235 String userID = getPrincipal().getUserID(); 236 if (!userID.startsWith(User.USERID_PREFIX_TYPE_ORGANISATION)) 237 throw new IllegalStateException ("This method can only be executed by an organisation!"); 238 239 String grantOrganisationID = userID.substring(User.USERID_PREFIX_TYPE_ORGANISATION.length()); 240 241 PersistenceManager pm = getPersistenceManager(); 242 try { 243 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 244 RegistrationStatus registrationStatus = localOrganisation.getPendingRegistration(grantOrganisationID); 245 if (registrationStatus == null) 246 throw new IllegalStateException ("There is no pending registration for applicantOrganisationID=\""+getOrganisationID()+"\" and grantOrganisationID=\""+grantOrganisationID+"\"!"); 247 248 if (!registrationID.equals(registrationStatus.getRegistrationID())) 249 throw new IllegalArgumentException ("The given registrationID \""+registrationID+"\" does not match the pending registration for applicantOrganisationID=\""+getOrganisationID()+"\" and grantOrganisationID=\""+grantOrganisationID+"\"!"); 250 251 localOrganisation.setPassword(grantOrganisationID, userPassword); 252 registrationStatus.accept(User.getUser(pm, getPrincipal())); 253 254 try { 257 pm.getObjectById(ServerID.create(grantOrganisation.getServer().getServerID())); 258 } catch (JDOObjectNotFoundException x) { 259 pm.makePersistent(grantOrganisation.getServer()); 260 } 261 pm.makePersistent(grantOrganisation); 262 } finally { 263 pm.close(); 264 } 265 } 266 267 272 public void notifyRejectRegistration(String registrationID) 273 throws ModuleException 274 { 275 if (registrationID == null) 276 throw new NullPointerException ("registrationID"); 277 278 String userID = getPrincipal().getUserID(); 279 if (!userID.startsWith(User.USERID_PREFIX_TYPE_ORGANISATION)) 280 throw new IllegalStateException ("This method can only be executed by an organisation!"); 281 282 String grantOrganisationID = userID.substring(User.USERID_PREFIX_TYPE_ORGANISATION.length()); 283 284 PersistenceManager pm = getPersistenceManager(); 285 try { 286 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 287 RegistrationStatus registrationStatus = localOrganisation.getPendingRegistration(grantOrganisationID); 288 if (registrationStatus == null) 289 throw new IllegalStateException ("There is no pending registration for applicantOrganisationID=\""+getOrganisationID()+"\" and grantOrganisationID=\""+grantOrganisationID+"\"!"); 290 291 if (!registrationID.equals(registrationStatus.getRegistrationID())) 292 throw new IllegalArgumentException ("The given registrationID \""+registrationID+"\" does not match the pending registration for applicantOrganisationID=\""+getOrganisationID()+"\" and grantOrganisationID=\""+grantOrganisationID+"\"!"); 293 294 User user = User.getUser(pm, getOrganisationID(), userID); 295 pm.deletePersistent(user); 297 298 registrationStatus.reject(null); 299 } finally { 303 pm.close(); 304 } 305 } 306 307 316 public void acceptRegistration(String applicantOrganisationID) 317 throws ModuleException 318 { 319 PersistenceManager pm = getPersistenceManager(); 320 try { 321 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 322 323 RegistrationStatus registrationStatus = localOrganisation 324 .getPendingRegistration(applicantOrganisationID); 325 326 if (registrationStatus == null) 327 throw new IllegalArgumentException ("There is no pending registration for applicantOrganisation \""+applicantOrganisationID+"\" at grantOrganisation \""+getOrganisationID()+"\"!"); 328 329 String usrPassword = User.generatePassword(8, 16); 332 333 try { 334 String userID = User.USERID_PREFIX_TYPE_ORGANISATION + applicantOrganisationID; 336 try { 337 User user = User.getUser(pm, getOrganisationID(), userID); 338 user.setPasswordPlain(usrPassword); } catch (JDOObjectNotFoundException x) { 340 UserManager userManager = UserManagerUtil.getHome().create(); 342 User user = new User(userID); 343 user.setPassword(usrPassword); 344 userManager.saveUser(user); 345 userManager.remove(); 346 } 347 348 pm.getFetchPlan().addGroup(FetchPlan.ALL); 349 Organisation grantOrganisation = (Organisation) pm.detachCopy( 350 localOrganisation.getOrganisation()); 351 352 OrganisationManager organisationManager = OrganisationManagerUtil.getHome(getInitialContextProps(applicantOrganisationID)).create(); 355 organisationManager.notifyAcceptRegistration( 356 registrationStatus.getRegistrationID(), grantOrganisation, usrPassword); 357 organisationManager.remove(); 358 } catch (RuntimeException x) { 359 throw x; 360 } catch (ModuleException x) { 361 throw x; 362 } catch (Exception x) { 363 throw new ModuleException(x); 364 } 365 366 registrationStatus.accept(User.getUser(pm, getPrincipal())); 369 localOrganisation.removePendingRegistration(applicantOrganisationID); 370 371 } finally { 372 pm.close(); 373 } 374 } 375 376 381 public void rejectRegistration(String applicantOrganisationID) 382 throws ModuleException 383 { 384 PersistenceManager pm = getPersistenceManager(); 385 try { 386 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 387 388 RegistrationStatus registrationStatus = localOrganisation 389 .getPendingRegistration(applicantOrganisationID); 390 391 if (registrationStatus == null) 392 throw new IllegalArgumentException ("There is no pending registration for applicantOrganisation \""+applicantOrganisationID+"\" at grantOrganisation \""+getOrganisationID()+"\"!"); 393 394 try { 395 OrganisationManager organisationManager = OrganisationManagerUtil.getHome(getInitialContextProps(applicantOrganisationID)).create(); 398 organisationManager.notifyRejectRegistration(registrationStatus.getRegistrationID()); 399 } catch (RuntimeException x) { 403 throw x; 404 } catch (ModuleException x) { 405 throw x; 406 } catch (Exception x) { 407 throw new ModuleException(x); 408 } 409 410 registrationStatus.reject(User.getUser(pm, getPrincipal())); 413 localOrganisation.removePendingRegistration(applicantOrganisationID); 414 } finally { 415 pm.close(); 416 } 417 } 418 419 424 public void cancelRegistration(String grantOrganisationID) 425 throws ModuleException 426 { 427 PersistenceManager pm = getPersistenceManager(); 428 try { 429 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 430 RegistrationStatus registrationStatus = localOrganisation.getPendingRegistration(grantOrganisationID); 431 if (registrationStatus == null) 432 throw new IllegalArgumentException ("There is no pending registration for grantOrganisation \""+grantOrganisationID+"\" at applicantOrganisation \""+getOrganisationID()+"\"!"); 433 434 try { 435 Properties props = new Properties (); 437 props.put(InitialContext.INITIAL_CONTEXT_FACTORY, registrationStatus.getInitialContextFactory()); 438 props.put(InitialContext.PROVIDER_URL, registrationStatus.getInitialContextURL()); 439 440 OrganisationLinker organisationLinker = OrganisationLinkerUtil.getHome(props).create(); 442 organisationLinker.cancelRegistration( 443 registrationStatus.getRegistrationID(), 444 getOrganisationID(), grantOrganisationID); 445 organisationLinker.remove(); 446 } catch (ModuleException e) { 447 throw e; 448 } catch (Exception e) { 449 throw new ModuleException(e); 450 } 451 452 User user = User.getUser( 454 pm, getOrganisationID(), 455 User.USERID_PREFIX_TYPE_ORGANISATION + grantOrganisationID); 456 pm.deletePersistent(user); 457 458 registrationStatus.cancel(User.getUser(pm, getPrincipal())); 461 localOrganisation.removePendingRegistration(grantOrganisationID); 462 } finally { 463 pm.close(); 464 } 465 } 466 467 476 public void beginRegistration( 477 String initialContextFactory, String initialContextURL, String organisationID) 478 throws ModuleException 479 { 480 String registrationID = getOrganisationID() 481 + '-' + Long.toHexString(System.currentTimeMillis()) 482 + '-' + Integer.toHexString((int)Math.round(Integer.MAX_VALUE * Math.random())); 483 484 Organisation myOrganisation; 486 PersistenceManager pm = getPersistenceManager(); 487 try { 488 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 489 490 RegistrationStatus.ensureRegisterability( 494 pm, localOrganisation, organisationID); 495 496 pm.getFetchPlan().addGroup(FetchPlan.ALL); 497 myOrganisation = (Organisation) pm.detachCopy(localOrganisation.getOrganisation()); 498 499 RegistrationStatus registrationStatus = new RegistrationStatus( 500 registrationID, 501 organisationID, User.getUser(pm, getPrincipal()), 502 initialContextFactory, initialContextURL); 503 504 localOrganisation.addPendingRegistration(registrationStatus); 505 506 String usrPassword = User.generatePassword(8, 16); 509 510 try { 511 String userID = User.USERID_PREFIX_TYPE_ORGANISATION + organisationID; 513 try { 514 User user = User.getUser(pm, getOrganisationID(), userID); 515 user.setPasswordPlain(usrPassword); } catch (JDOObjectNotFoundException x) { 517 UserManager userManager = UserManagerUtil.getHome().create(); 519 User user = new User(userID); 520 user.setPassword(usrPassword); 521 userManager.saveUser(user); 522 userManager.remove(); 523 } 524 525 Properties props = new Properties (); 527 props.put(InitialContext.INITIAL_CONTEXT_FACTORY, initialContextFactory); 528 props.put(InitialContext.PROVIDER_URL, initialContextURL); 529 530 OrganisationLinker organisationLinker = OrganisationLinkerUtil.getHome(props).create(); 532 organisationLinker.requestRegistration(registrationID, myOrganisation, organisationID, usrPassword); 533 organisationLinker.remove(); 534 535 } catch (ModuleException e) { 536 throw e; 537 } catch (Exception e) { 538 throw new ModuleException(e); 539 } 540 } finally { 541 pm.close(); 542 } 543 } 544 545 556 public void ackRegistration( 557 String grantOrganisationID) 558 throws ModuleException 559 { 560 PersistenceManager pm = getPersistenceManager(); 561 try { 562 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 563 RegistrationStatus registrationStatus = localOrganisation.getPendingRegistration(grantOrganisationID); 564 if (registrationStatus == null) 565 throw new IllegalArgumentException ("There is no pending registration for grantOrganisation \""+grantOrganisationID+"\" at applicantOrganisation \""+getOrganisationID()+"\"!"); 566 567 if (registrationStatus.getCloseDT() == null) 568 throw new IllegalArgumentException ("The registration for grantOrganisation \""+grantOrganisationID+"\" at applicantOrganisation \""+getOrganisationID()+"\" is still open and has neither been rejected nor accepted!"); 569 570 localOrganisation.removePendingRegistration(grantOrganisationID); 571 } finally { 572 pm.close(); 573 } 574 } 575 576 581 public void testBackhand(String [] organisationIDs) 582 throws ModuleException 583 { 584 LOGGER.info("testBackhand ("+organisationIDs.length+"): begin: principal="+getPrincipalString()); 585 if (organisationIDs != null && organisationIDs.length > 0) { 586 String organisationID = organisationIDs[0]; 587 String [] bhOrgaIDs = new String [organisationIDs.length - 1]; 588 for (int i = 1; i < organisationIDs.length; ++i) { 589 bhOrgaIDs[i-1] = organisationIDs[i]; 590 } 591 592 608 LOGGER.info("testBackhand ("+organisationIDs.length+"): backhanding to organisation \""+organisationID+"\""); 609 try { 610 LOGGER.info("testBackhand ("+organisationIDs.length+"): OrganisationManagerUtil.getHome(...)"); 611 OrganisationManager organisationManager = OrganisationManagerUtil.getHome(getInitialContextProps(organisationID)).create(); 612 613 LOGGER.info("testBackhand ("+organisationIDs.length+"): UserManagerUtil.getHome()"); 614 UserManagerHome userManagerHome = UserManagerUtil.getHome(); 615 616 LOGGER.info("testBackhand ("+organisationIDs.length+"): userManagerHome.create()"); 617 UserManager userManager = userManagerHome.create(); 618 619 LOGGER.info("testBackhand ("+organisationIDs.length+"): organisationManager.testBackhand(...)"); 620 organisationManager.testBackhand(bhOrgaIDs); 621 622 LOGGER.info("testBackhand ("+organisationIDs.length+"): userManager.whoami()"); 623 userManager.whoami(); 624 625 organisationManager.remove(); 626 userManager.remove(); 627 } catch (RuntimeException e) { 628 throw e; 629 } catch (ModuleException e) { 630 throw e; 631 } catch (Exception e) { 632 throw new ModuleException(e); 633 } 634 } 635 LOGGER.info("testBackhand ("+organisationIDs.length+"): end: principal="+getPrincipalString()); 636 } 637 638 663 }
| Popular Tags
|