PageRenderTime 2991ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/scenarios/ScenarioSharedState.java

https://gitlab.com/libpgrid/github_mirror
Java | 137 lines | 96 code | 17 blank | 24 comment | 0 complexity | ae0850d48c0ed7aa5780c8d45e8c93c0 MD5 | raw file
  1. /*
  2. * This file is part of the pgrid project.
  3. *
  4. * Copyright (c) 2012. Vourlakis Nikolas. All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package scenarios;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. import com.sun.corba.se.spi.logging.CORBALogDomains;
  23. import org.omg.CORBA.ORB;
  24. import org.omg.CORBA.ORBPackage.InvalidName;
  25. import org.omg.PortableServer.POA;
  26. import org.omg.PortableServer.POAHelper;
  27. import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
  28. import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
  29. import org.omg.PortableServer.POAPackage.ServantNotActive;
  30. import org.omg.PortableServer.POAPackage.WrongPolicy;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import pgrid.entity.CorbaFactory;
  34. import pgrid.entity.EntityModule;
  35. import pgrid.service.LocalPeerContext;
  36. import pgrid.service.ServiceModule;
  37. import pgrid.service.corba.exchange.ExchangeHandleHelper;
  38. import pgrid.service.corba.exchange.ExchangeHandlePOA;
  39. import pgrid.service.corba.repair.RepairHandleHelper;
  40. import pgrid.service.corba.repair.RepairHandlePOA;
  41. import java.net.UnknownHostException;
  42. import java.util.logging.Level;
  43. /**
  44. * @author Vourlakis Nikolas <nvourlakis@gmail.com>
  45. */
  46. public class ScenarioSharedState {
  47. private static final Logger logger_ = LoggerFactory.getLogger(ScenarioSharedState.class);
  48. private static final Injector injector_;
  49. private static final String localIP_ = "127.0.0.1";
  50. private static final int localPort_ = 3000;
  51. static {
  52. injector_ = Guice.createInjector(new EntityModule(), new ServiceModule(localIP_, localPort_, Integer.MAX_VALUE));
  53. try {
  54. localPeerContextInit();
  55. serviceRegistration();
  56. } catch (UnknownHostException e) {
  57. logger_.error("Something went wrong with the initialization of CORBA.");
  58. }
  59. }
  60. public static Injector getInjector() {
  61. return injector_;
  62. }
  63. private static void localPeerContextInit() throws UnknownHostException {
  64. LocalPeerContext context = injector_.getInstance(LocalPeerContext.class);
  65. CorbaFactory corbaFactory = injector_.getInstance(CorbaFactory.class);
  66. ORB orb = corbaFactory.getInstance(localIP_, localPort_);
  67. context.setOrb(orb);
  68. }
  69. private static void serviceRegistration() {
  70. final ORB orb = injector_.getInstance(LocalPeerContext.class).getCorba();
  71. try {
  72. POA rootPOA = null;
  73. try {
  74. rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
  75. } catch (InvalidName invalidName) {
  76. invalidName.printStackTrace();
  77. }
  78. rootPOA.the_POAManager().activate();
  79. //********* Exchange Service Registration *********//
  80. ExchangeHandlePOA exchangeServant = injector_.getProvider(ExchangeHandlePOA.class).get();
  81. rootPOA.activate_object(exchangeServant);
  82. String[] ID = ExchangeHandleHelper.id().split(":");
  83. ((com.sun.corba.se.spi.orb.ORB) orb).register_initial_reference(
  84. ID[1],
  85. rootPOA.servant_to_reference(exchangeServant)
  86. );
  87. logger_.info("Exchange service registered");
  88. //********** Repair Service Registration **********//
  89. RepairHandlePOA repairServant = injector_.getProvider(RepairHandlePOA.class).get();
  90. rootPOA.activate_object(repairServant);
  91. ID = RepairHandleHelper.id().split(":");
  92. ((com.sun.corba.se.spi.orb.ORB) orb).register_initial_reference(
  93. ID[1],
  94. rootPOA.servant_to_reference(repairServant)
  95. );
  96. logger_.info("Repair service registered");
  97. } catch (ServantNotActive servantNotActive) {
  98. servantNotActive.printStackTrace();
  99. } catch (WrongPolicy wrongPolicy) {
  100. wrongPolicy.printStackTrace();
  101. } catch (InvalidName invalidName) {
  102. invalidName.printStackTrace();
  103. } catch (AdapterInactive adapterInactive) {
  104. adapterInactive.printStackTrace();
  105. } catch (ServantAlreadyActive servantAlreadyActive) {
  106. servantAlreadyActive.printStackTrace();
  107. }
  108. Thread orbThread_ = new Thread(new Runnable() {
  109. private final ORB orb_ = orb;
  110. @Override
  111. public void run() {
  112. orb_.run();
  113. }
  114. });
  115. orbThread_.start();
  116. logger_.info("Initialization finished!");
  117. // shutdown logging
  118. ((com.sun.corba.se.spi.orb.ORB) orb).getLogger(CORBALogDomains.RPC).setLevel(Level.OFF);
  119. }
  120. }