PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/nakedobjects-4.0.0/core/runtime/src/main/java/org/nakedobjects/runtime/system/NakedObjectsSystemAbstract.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 403 lines | 223 code | 80 blank | 100 comment | 29 complexity | ea2782219ca3f0651875975a93fe0b48 MD5 | raw file
  1. package org.nakedobjects.runtime.system;
  2. import java.io.File;
  3. import java.util.List;
  4. import org.apache.log4j.Logger;
  5. import org.nakedobjects.applib.fixtures.LogonFixture;
  6. import org.nakedobjects.metamodel.commons.about.AboutNakedObjects;
  7. import org.nakedobjects.metamodel.commons.component.Installer;
  8. import org.nakedobjects.metamodel.commons.component.NoopUtils;
  9. import org.nakedobjects.metamodel.commons.debug.DebugInfo;
  10. import org.nakedobjects.metamodel.commons.debug.DebugString;
  11. import org.nakedobjects.metamodel.config.NakedObjectConfiguration;
  12. import org.nakedobjects.metamodel.specloader.NakedObjectReflector;
  13. import org.nakedobjects.runtime.authentication.AuthenticationManager;
  14. import org.nakedobjects.runtime.authentication.standard.exploration.ExplorationSession;
  15. import org.nakedobjects.runtime.context.NakedObjectsContext;
  16. import org.nakedobjects.runtime.fixturesinstaller.FixturesInstaller;
  17. import org.nakedobjects.runtime.imageloader.TemplateImageLoader;
  18. import org.nakedobjects.runtime.imageloader.awt.TemplateImageLoaderAwt;
  19. import org.nakedobjects.runtime.installers.InstallerLookup;
  20. import org.nakedobjects.runtime.persistence.PersistenceSessionFactory;
  21. import org.nakedobjects.runtime.session.NakedObjectSession;
  22. import org.nakedobjects.runtime.session.NakedObjectSessionFactory;
  23. import org.nakedobjects.runtime.system.internal.InitialisationSession;
  24. import org.nakedobjects.runtime.system.internal.NakedObjectsLocaleInitializer;
  25. import org.nakedobjects.runtime.system.internal.NakedObjectsTimeZoneInitializer;
  26. import org.nakedobjects.runtime.system.internal.SplashWindow;
  27. import org.nakedobjects.runtime.userprofile.UserProfileStore;
  28. /**
  29. *
  30. */
  31. public abstract class NakedObjectsSystemAbstract implements NakedObjectsSystem {
  32. public static final Logger LOG = Logger.getLogger(NakedObjectsSystemAbstract.class);
  33. private static final int SPLASH_DELAY_DEFAULT = 6;
  34. private final NakedObjectsLocaleInitializer localeInitializer;
  35. private final NakedObjectsTimeZoneInitializer timeZoneInitializer;
  36. private final DeploymentType deploymentType;
  37. private SplashWindow splashWindow;
  38. private FixturesInstaller fixtureInstaller;
  39. private boolean initialized = false;
  40. private NakedObjectSessionFactory sessionFactory;
  41. private LogonFixture logonFixture;
  42. // ///////////////////////////////////////////
  43. // Constructors
  44. // ///////////////////////////////////////////
  45. public NakedObjectsSystemAbstract(final DeploymentType deploymentType) {
  46. this(deploymentType, new NakedObjectsLocaleInitializer(), new NakedObjectsTimeZoneInitializer());
  47. }
  48. public NakedObjectsSystemAbstract(
  49. final DeploymentType deploymentType,
  50. final NakedObjectsLocaleInitializer localeInitializer,
  51. final NakedObjectsTimeZoneInitializer timeZoneInitializer) {
  52. this.deploymentType = deploymentType;
  53. this.localeInitializer = localeInitializer;
  54. this.timeZoneInitializer = timeZoneInitializer;
  55. }
  56. // ///////////////////////////////////////////
  57. // DeploymentType
  58. // ///////////////////////////////////////////
  59. public DeploymentType getDeploymentType() {
  60. return deploymentType;
  61. }
  62. // ///////////////////////////////////////////
  63. // init, shutdown
  64. // ///////////////////////////////////////////
  65. public void init() {
  66. if (initialized) {
  67. throw new IllegalStateException("Already initialized");
  68. } else {
  69. initialized = true;
  70. }
  71. LOG.info("initialising naked objects system");
  72. LOG.info("working directory: " + new File(".").getAbsolutePath());
  73. LOG.info("resource stream source: " + getConfiguration().getResourceStreamSource());
  74. localeInitializer.initLocale(getConfiguration());
  75. timeZoneInitializer.initTimeZone(getConfiguration());
  76. int splashDelay = SPLASH_DELAY_DEFAULT;
  77. try {
  78. TemplateImageLoader splashLoader = obtainTemplateImageLoader();
  79. splashLoader.init();
  80. showSplash(splashLoader);
  81. // REVIEW is this the best way to make the configuration available?
  82. NakedObjectsContext.setConfiguration(getConfiguration());
  83. sessionFactory = doCreateSessionFactory(deploymentType);
  84. NakedObjectsContext.setConfiguration(sessionFactory.getConfiguration());
  85. initContext(sessionFactory);
  86. sessionFactory.init();
  87. installFixturesIfRequired();
  88. } catch (NakedObjectSystemException ex) {
  89. LOG.error("failed to initialise", ex);
  90. splashDelay = 0;
  91. throw new RuntimeException(ex);
  92. } finally {
  93. removeSplash(splashDelay);
  94. }
  95. }
  96. private void installFixturesIfRequired() throws NakedObjectSystemException {
  97. // some deployment types (eg CLIENT) do not support installing fixtures
  98. // instead, any fixtures should be installed when server boots up.
  99. if (!deploymentType.canInstallFixtures()) {
  100. return;
  101. }
  102. fixtureInstaller = obtainFixturesInstaller();
  103. if (fixtureInstaller == null || NoopUtils.isNoop(fixtureInstaller)) {
  104. return;
  105. }
  106. NakedObjectsContext.openSession(new InitialisationSession());
  107. fixtureInstaller.installFixtures();
  108. try {
  109. // only allow logon fixtures if not in production mode.
  110. if (!deploymentType.isProduction()) {
  111. logonFixture = fixtureInstaller.getLogonFixture();
  112. }
  113. } finally {
  114. NakedObjectsContext.closeSession();
  115. }
  116. }
  117. private void initContext(NakedObjectSessionFactory sessionFactory) {
  118. getDeploymentType().initContext(sessionFactory);
  119. }
  120. public void shutdown() {
  121. LOG.info("shutting down system");
  122. NakedObjectsContext.closeAllSessions();
  123. }
  124. // ///////////////////////////////////////////
  125. // Hook:
  126. // ///////////////////////////////////////////
  127. /**
  128. * Hook method; the returned implementation is expected to use the same general approach as the subclass
  129. * itself.
  130. *
  131. * <p>
  132. * So, for example, <tt>NakedObjectsSystemUsingInstallers</tt> uses the {@link InstallerLookup} mechanism
  133. * to find its components. The corresponding <tt>ExecutionContextFactoryUsingInstallers</tt> object
  134. * returned by this method should use {@link InstallerLookup} likewise.
  135. */
  136. protected abstract NakedObjectSessionFactory doCreateSessionFactory(final DeploymentType deploymentType)
  137. throws NakedObjectSystemException;
  138. // ///////////////////////////////////////////
  139. // Configuration
  140. // ///////////////////////////////////////////
  141. /**
  142. * Populated after {@link #init()}.
  143. */
  144. public NakedObjectSessionFactory getSessionFactory() {
  145. return sessionFactory;
  146. }
  147. // ///////////////////////////////////////////
  148. // Configuration
  149. // ///////////////////////////////////////////
  150. public abstract NakedObjectConfiguration getConfiguration();
  151. // ///////////////////////////////////////////
  152. // TemplateImageLoader
  153. // ///////////////////////////////////////////
  154. /**
  155. * Just returns a {@link TemplateImageLoaderAwt}; subclasses may override if
  156. * required.
  157. */
  158. protected TemplateImageLoader obtainTemplateImageLoader() {
  159. return new TemplateImageLoaderAwt(getConfiguration());
  160. }
  161. // ///////////////////////////////////////////
  162. // Reflector
  163. // ///////////////////////////////////////////
  164. protected abstract NakedObjectReflector obtainReflector(DeploymentType deploymentType) throws NakedObjectSystemException;
  165. // ///////////////////////////////////////////
  166. // PersistenceSessionFactory
  167. // ///////////////////////////////////////////
  168. protected abstract PersistenceSessionFactory obtainPersistenceSessionFactory(DeploymentType deploymentType)
  169. throws NakedObjectSystemException;
  170. // ///////////////////////////////////////////
  171. // Fixtures
  172. // ///////////////////////////////////////////
  173. /**
  174. * This is the only {@link Installer} that is used by any (all) subclass implementations, because it
  175. * effectively <i>is</i> the component we need (as opposed to a builder/factory of the component we need).
  176. *
  177. * <p>
  178. * The fact that the component <i>is</i> an installer (and therefore can be {@link InstallerLookup} looked
  179. * up} is at this level really just an incidental implementation detail useful for the subclass that uses
  180. * {@link InstallerLookup} to create the other components.
  181. */
  182. protected abstract FixturesInstaller obtainFixturesInstaller() throws NakedObjectSystemException;
  183. // ///////////////////////////////////////////
  184. // Authentication Manager
  185. // ///////////////////////////////////////////
  186. protected abstract AuthenticationManager obtainAuthenticationManager(DeploymentType deploymentType)
  187. throws NakedObjectSystemException;
  188. // ///////////////////////////////////////////
  189. // UserProfileLoader
  190. // ///////////////////////////////////////////
  191. protected abstract UserProfileStore obtainUserProfileStore();
  192. // ///////////////////////////////////////////
  193. // Services
  194. // ///////////////////////////////////////////
  195. protected abstract List<Object> obtainServices();
  196. // ///////////////////////////////////////////
  197. // Fixtures Installer
  198. // ///////////////////////////////////////////
  199. public FixturesInstaller getFixturesInstaller() {
  200. return fixtureInstaller;
  201. }
  202. /**
  203. * The {@link LogonFixture}, if any, obtained by running fixtures.
  204. *
  205. * <p>
  206. * Intended to be used when for {@link DeploymentType#EXPLORATION exploration} (instead
  207. * of an {@link ExplorationSession}) or {@link DeploymentType#PROTOTYPE prototype} deployments
  208. * (saves logging in). Should be <i>ignored</i> in other {@link DeploymentType}s.
  209. */
  210. public LogonFixture getLogonFixture() {
  211. return logonFixture;
  212. }
  213. // ///////////////////////////////////////////
  214. // Splash
  215. // ///////////////////////////////////////////
  216. private void showSplash(TemplateImageLoader imageLoader) {
  217. boolean vetoSplashFromConfig = getConfiguration().getBoolean(SystemConstants.NOSPLASH_KEY, SystemConstants.NOSPLASH_DEFAULT);
  218. if (!vetoSplashFromConfig && getDeploymentType().shouldShowSplash()) {
  219. splashWindow = new SplashWindow(imageLoader);
  220. }
  221. }
  222. private void removeSplash(final int delay) {
  223. if (splashWindow != null) {
  224. if (delay == 0) {
  225. splashWindow.removeImmediately();
  226. } else {
  227. splashWindow.toFront();
  228. splashWindow.removeAfterDelay(delay);
  229. }
  230. }
  231. }
  232. // ///////////////////////////////////////////
  233. // debugging
  234. // ///////////////////////////////////////////
  235. private void debug(final DebugString debug, final Object object) {
  236. if (object instanceof DebugInfo) {
  237. final DebugInfo d = (DebugInfo) object;
  238. debug.appendTitle(d.debugTitle());
  239. d.debugData(debug);
  240. } else {
  241. debug.appendln(object.toString());
  242. debug.appendln("... no further debug information");
  243. }
  244. }
  245. public DebugInfo debugSection(String selectionName) {
  246. //DebugInfo deb;
  247. if (selectionName.equals("Configuration")) {
  248. return getConfiguration();
  249. } /*else if (selectionName.equals("Overview")) {
  250. debugOverview(debug);
  251. } else if (selectionName.equals("Authenticator")) {
  252. deb = NakedObjectsContext.getAuthenticationManager();
  253. } else if (selectionName.equals("Reflector")) {
  254. deb = NakedObjectsContext.getSpecificationLoader();
  255. } else if (selectionName.equals("Contexts")) {
  256. deb = debugListContexts(debug);
  257. } else {
  258. deb = debugDisplayContext(selectionName, debug);
  259. }*/
  260. return null;
  261. }
  262. private void debugDisplayContext(final String selector, final DebugString debug) {
  263. final NakedObjectSession d = NakedObjectsContext.getSession(selector);
  264. if (d != null) {
  265. d.debugAll(debug);
  266. } else {
  267. debug.appendln("No context: " + selector);
  268. }
  269. }
  270. private void debugListContexts(final DebugString debug) {
  271. final String[] contextIds = NakedObjectsContext.getInstance().allSessionIds();
  272. for (int i = 0; i < contextIds.length; i++) {
  273. debug.appendln(contextIds[i]);
  274. debug.appendln("-----");
  275. final NakedObjectSession d = NakedObjectsContext.getSession(contextIds[i]);
  276. d.debug(debug);
  277. debug.appendln();
  278. }
  279. }
  280. public String[] debugSectionNames() {
  281. final String[] general = new String[] { "Overview", "Authenticator", "Configuration", "Reflector", "Requests",
  282. "Contexts" };
  283. final String[] contextIds = NakedObjectsContext.getInstance().allSessionIds();
  284. final String[] combined = new String[general.length + contextIds.length];
  285. System.arraycopy(general, 0, combined, 0, general.length);
  286. System.arraycopy(contextIds, 0, combined, general.length, contextIds.length);
  287. return combined;
  288. }
  289. private void debugOverview(final DebugString debug) {
  290. try {
  291. debug.appendln(AboutNakedObjects.getFrameworkName());
  292. debug.appendln(AboutNakedObjects.getFrameworkVersion());
  293. if (AboutNakedObjects.getApplicationName() != null) {
  294. debug.appendln("application: " + AboutNakedObjects.getApplicationName());
  295. }
  296. if (AboutNakedObjects.getApplicationVersion() != null) {
  297. debug.appendln("version" + AboutNakedObjects.getApplicationVersion());
  298. }
  299. final String user = System.getProperty("user.name");
  300. final String system = System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ") "
  301. + System.getProperty("os.version");
  302. final String java = System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version");
  303. debug.appendln("user: " + user);
  304. debug.appendln("os: " + system);
  305. debug.appendln("java: " + java);
  306. debug.appendln("working directory: " + new File(".").getAbsolutePath());
  307. debug.appendTitle("System Installer");
  308. debug.appendln("Fixture Installer", fixtureInstaller == null ? "none" : fixtureInstaller.getClass().getName());
  309. debug.appendTitle("System Components");
  310. debug.appendln("Authentication manager", NakedObjectsContext.getAuthenticationManager().getClass().getName());
  311. debug.appendln("Configuration", getConfiguration().getClass().getName());
  312. final DebugInfo[] inf = NakedObjectsContext.debugSystem();
  313. for (int i = 0; i < inf.length; i++) {
  314. if (inf[i] != null) {
  315. inf[i].debugData(debug);
  316. }
  317. }
  318. } catch (RuntimeException e) {
  319. debug.appendException(e);
  320. }
  321. }
  322. }
  323. // Copyright (c) Naked Objects Group Ltd.