PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/nakedobjects-4.0.0/core/runtime/src/main/java/org/nakedobjects/runtime/context/NakedObjectsContext.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 504 lines | 208 code | 73 blank | 223 comment | 19 complexity | 911d44a6f6464df0c78bb9e49fe17dea MD5 | raw file
  1. package org.nakedobjects.runtime.context;
  2. import java.util.List;
  3. import org.apache.log4j.Logger;
  4. import org.nakedobjects.metamodel.authentication.AuthenticationSession;
  5. import org.nakedobjects.metamodel.commons.component.TransactionScopedComponent;
  6. import org.nakedobjects.metamodel.commons.debug.DebugInfo;
  7. import org.nakedobjects.metamodel.commons.debug.DebugList;
  8. import org.nakedobjects.metamodel.commons.debug.DebugString;
  9. import org.nakedobjects.metamodel.commons.exceptions.NakedObjectException;
  10. import org.nakedobjects.metamodel.config.NakedObjectConfiguration;
  11. import org.nakedobjects.metamodel.specloader.SpecificationLoader;
  12. import org.nakedobjects.runtime.authentication.AuthenticationManager;
  13. import org.nakedobjects.runtime.authorization.AuthorizationManager;
  14. import org.nakedobjects.runtime.imageloader.TemplateImageLoader;
  15. import org.nakedobjects.runtime.persistence.PersistenceSession;
  16. import org.nakedobjects.runtime.session.NakedObjectSession;
  17. import org.nakedobjects.runtime.session.NakedObjectSessionFactory;
  18. import org.nakedobjects.runtime.system.DeploymentType;
  19. import org.nakedobjects.runtime.transaction.NakedObjectTransaction;
  20. import org.nakedobjects.runtime.transaction.NakedObjectTransactionManager;
  21. import org.nakedobjects.runtime.transaction.messagebroker.MessageBroker;
  22. import org.nakedobjects.runtime.transaction.updatenotifier.UpdateNotifier;
  23. import org.nakedobjects.runtime.userprofile.UserProfile;
  24. import org.nakedobjects.runtime.userprofile.UserProfileLoader;
  25. /**
  26. * Provides singleton <i>access to</i> the current (session scoped) {@link NakedObjectSession}, along with
  27. * convenience methods to obtain application-scoped components and also any transaction-scoped components
  28. * {@link TransactionScopedComponent}s if a {@link NakedObjectTransaction}
  29. * {@link NakedObjectSession#getCurrentTransaction() is in progress}.
  30. *
  31. * <p>
  32. * Somewhat analogous to (the static methods in) <tt>HibernateUtil</tt>.
  33. */
  34. public abstract class NakedObjectsContext implements DebugInfo {
  35. private static final Logger LOG = Logger.getLogger(NakedObjectsContext.class);
  36. private static NakedObjectsContext singleton;
  37. private final NakedObjectSessionFactory sessionFactory;
  38. private final ContextReplacePolicy replacePolicy;
  39. private final SessionClosePolicy sessionClosePolicy;
  40. private static NakedObjectConfiguration configuration;
  41. // ///////////////////////////////////////////////////////////
  42. // Singleton & Constructor
  43. // ///////////////////////////////////////////////////////////
  44. /**
  45. * Returns the singleton providing access to the set of execution contexts.
  46. */
  47. public static NakedObjectsContext getInstance() {
  48. return singleton;
  49. }
  50. /**
  51. * Whether a singleton has been created using {@link #getInstance()}.
  52. */
  53. public static boolean exists() {
  54. return singleton != null;
  55. }
  56. /**
  57. * Resets the singleton, so another can created.
  58. *
  59. * @see #NakedObjects()
  60. */
  61. public static void testReset() {
  62. singleton = null;
  63. }
  64. protected static enum SessionClosePolicy {
  65. /**
  66. * Sessions must be explicitly closed.
  67. */
  68. EXPLICIT_CLOSE,
  69. /**
  70. * Sessions will be automatically closed.
  71. */
  72. AUTO_CLOSE;
  73. }
  74. /**
  75. * Whether the {@link NakedObjectsContext#getInstance() singleton} itself may be replaced.
  76. */
  77. protected static enum ContextReplacePolicy {
  78. NOT_REPLACEABLE, REPLACEABLE
  79. }
  80. /**
  81. * Creates a new instance of the {@link NakedObjectSession} holder.
  82. *
  83. * <p>
  84. * Will throw an exception if an instance has already been created and is not
  85. * {@link ContextReplacePolicy#REPLACEABLE}.
  86. */
  87. protected NakedObjectsContext(
  88. final ContextReplacePolicy replacePolicy,
  89. final SessionClosePolicy sessionClosePolicy,
  90. final NakedObjectSessionFactory sessionFactory) {
  91. if (singleton != null && !singleton.isContextReplaceable()) {
  92. throw new NakedObjectException("Naked Objects Context already set up and cannot be replaced");
  93. }
  94. singleton = this;
  95. this.sessionFactory = sessionFactory;
  96. this.sessionClosePolicy = sessionClosePolicy;
  97. this.replacePolicy = replacePolicy;
  98. }
  99. // ///////////////////////////////////////////////////////////
  100. // SessionFactory
  101. // ///////////////////////////////////////////////////////////
  102. /**
  103. * As injected in constructor.
  104. */
  105. public final NakedObjectSessionFactory getSessionFactoryInstance() {
  106. return sessionFactory;
  107. }
  108. // ///////////////////////////////////////////////////////////
  109. // Policies
  110. // ///////////////////////////////////////////////////////////
  111. /**
  112. * Whether a context singleton can simply be replaced or not.
  113. */
  114. public final boolean isContextReplaceable() {
  115. return replacePolicy == ContextReplacePolicy.REPLACEABLE;
  116. }
  117. /**
  118. * Whether any open session can be automatically {@link #closeSessionInstance() close}d on
  119. * {@link #openSessionInstance(AuthenticationSession) open}.
  120. */
  121. public final boolean isSessionAutocloseable() {
  122. return sessionClosePolicy == SessionClosePolicy.AUTO_CLOSE;
  123. }
  124. /**
  125. * Helper method for subclasses' implementation of {@link #openSessionInstance(AuthenticationSession)}.
  126. */
  127. protected void applySessionClosePolicy() {
  128. if (getSessionInstance() == null) {
  129. return;
  130. }
  131. if (!isSessionAutocloseable()) {
  132. throw new IllegalStateException("Session already open and context not configured for autoclose");
  133. }
  134. closeSessionInstance();
  135. }
  136. // ///////////////////////////////////////////////////////////
  137. // open / close / shutdown
  138. // ///////////////////////////////////////////////////////////
  139. /**
  140. * Creates a new {@link NakedObjectSession} and binds into the current context.
  141. *
  142. * @throws IllegalStateException
  143. * if already opened.
  144. */
  145. public abstract NakedObjectSession openSessionInstance(AuthenticationSession session);
  146. /**
  147. * Closes the {@link NakedObjectSession} for the current context.
  148. *
  149. * <p>
  150. * Ignored if already closed.
  151. */
  152. public final void closeSessionInstance() {
  153. if (getSessionInstance() != null) {
  154. getSessionInstance().close();
  155. doClose();
  156. }
  157. }
  158. /**
  159. * Overridable hook method called from {@link #closeSessionInstance()}, allowing subclasses to clean up
  160. * (for example datastructures).
  161. *
  162. * <p>
  163. * The {@link #getSessionInstance() current} {@link NakedObjectSession} will already have been
  164. * {@link NakedObjectSession#close() closed}.
  165. */
  166. protected void doClose() {}
  167. /**
  168. * Shutdown the application.
  169. */
  170. protected abstract void closeAllSessionsInstance();
  171. // ///////////////////////////////////////////////////////////
  172. // getSession()
  173. // ///////////////////////////////////////////////////////////
  174. /**
  175. * Locates the current {@link NakedObjectSession}.
  176. *
  177. * <p>
  178. * This might just be a singleton (eg {@link NakedObjectsContextStatic}), or could be retrieved from the
  179. * thread (eg {@link NakedObjectsContextThreadLocal}).
  180. */
  181. public abstract NakedObjectSession getSessionInstance();
  182. /**
  183. * The {@link NakedObjectSession} for specified {@link NakedObjectSession#getId()}.
  184. */
  185. protected abstract NakedObjectSession getSessionInstance(String sessionId);
  186. /**
  187. * All known session Ids.
  188. *
  189. * <p>
  190. * Provided primarily for debugging.
  191. */
  192. public abstract String[] allSessionIds();
  193. // ///////////////////////////////////////////////////////////
  194. // Static Convenience methods (session management)
  195. // ///////////////////////////////////////////////////////////
  196. /**
  197. * Convenience method to open a new {@link NakedObjectSession}.
  198. *
  199. * @see #openSessionInstance(AuthenticationSession)
  200. */
  201. public static NakedObjectSession openSession(final AuthenticationSession authenticationSession) {
  202. return getInstance().openSessionInstance(authenticationSession);
  203. }
  204. /**
  205. * Convenience method to close the current {@link NakedObjectSession}.
  206. *
  207. * @see #closeSessionInstance()
  208. */
  209. public static void closeSession() {
  210. getInstance().closeSessionInstance();
  211. }
  212. /**
  213. * Convenience method to return {@link NakedObjectSession} for specified
  214. * {@link NakedObjectSession#getId()}.
  215. *
  216. * <p>
  217. * Provided primarily for debugging.
  218. *
  219. * @see #getSessionInstance(String)
  220. */
  221. public static NakedObjectSession getSession(final String sessionId) {
  222. return getInstance().getSessionInstance(sessionId);
  223. }
  224. /**
  225. * Convenience method to close all sessions.
  226. */
  227. public static void closeAllSessions() {
  228. LOG.info("closing all instances");
  229. NakedObjectsContext instance = getInstance();
  230. if (instance != null) {
  231. instance.closeAllSessionsInstance();
  232. }
  233. }
  234. // ///////////////////////////////////////////////////////////
  235. // Static Convenience methods (application scoped)
  236. // ///////////////////////////////////////////////////////////
  237. /**
  238. * Convenience method returning the {@link NakedObjectSessionFactory} of the current {@link #getSession()
  239. * session}.
  240. */
  241. public static NakedObjectSessionFactory getSessionFactory() {
  242. return getInstance().getSessionFactoryInstance();
  243. }
  244. /**
  245. * Convenience method.
  246. *
  247. * @see NakedObjectSessionFactory#getConfiguration()
  248. */
  249. public static NakedObjectConfiguration getConfiguration() {
  250. // REVIEW
  251. return configuration;
  252. //return getSessionFactory().getConfiguration();
  253. }
  254. public static void setConfiguration(NakedObjectConfiguration configuration) {
  255. NakedObjectsContext.configuration = configuration;
  256. }
  257. /**
  258. * Convenience method.
  259. *
  260. * @see NakedObjectSessionFactory#getDeploymentType()
  261. */
  262. public static DeploymentType getDeploymentType() {
  263. return getSessionFactory().getDeploymentType();
  264. }
  265. /**
  266. * Convenience method.
  267. *
  268. * @see NakedObjectSessionFactory#getSpecificationLoader()
  269. */
  270. public static SpecificationLoader getSpecificationLoader() {
  271. return getSessionFactory().getSpecificationLoader();
  272. }
  273. /**
  274. * Convenience method.
  275. *
  276. * @see NakedObjectSessionFactory#getAuthenticationManager()
  277. */
  278. public static AuthenticationManager getAuthenticationManager() {
  279. return getSessionFactory().getAuthenticationManager();
  280. }
  281. /**
  282. * Convenience method.
  283. *
  284. * @see NakedObjectSessionFactory#getAuthorizationManager()
  285. */
  286. public static AuthorizationManager getAuthorizationManager() {
  287. return getSessionFactory().getAuthorizationManager();
  288. }
  289. /**
  290. * Convenience method.
  291. *
  292. * @see NakedObjectSessionFactory#getTemplateImageLoader()
  293. */
  294. public static TemplateImageLoader getTemplateImageLoader() {
  295. return getSessionFactory().getTemplateImageLoader();
  296. }
  297. public static UserProfileLoader getUserProfileLoader() {
  298. return getSessionFactory().getUserProfileLoader();
  299. }
  300. public static List<Object> getServices() {
  301. return getSessionFactory().getServices();
  302. }
  303. // ///////////////////////////////////////////////////////////
  304. // Static Convenience methods (session scoped)
  305. // ///////////////////////////////////////////////////////////
  306. public static boolean inSession() {
  307. NakedObjectSession session = getInstance().getSessionInstance();
  308. return session != null;
  309. }
  310. /**
  311. * Convenience method returning the current {@link NakedObjectSession}.
  312. */
  313. public static NakedObjectSession getSession() {
  314. NakedObjectSession session = getInstance().getSessionInstance();
  315. if (session == null) {
  316. throw new IllegalStateException("No Session opened for this thread");
  317. }
  318. return session;
  319. }
  320. /**
  321. * Convenience method to return the {@link #getSession() current} {@link NakedObjectSession}'s
  322. * {@link NakedObjectSession#getId() id}.
  323. *
  324. * @see NakedObjectSession#getId()
  325. */
  326. public static String getSessionId() {
  327. return getSession().getId();
  328. }
  329. /**
  330. * @see NakedObjectSession#getAuthenticationSession()
  331. */
  332. public static AuthenticationSession getAuthenticationSession() {
  333. return getSession().getAuthenticationSession();
  334. }
  335. /**
  336. * Convenience method.
  337. *
  338. * @see NakedObjectSession#getPersistenceSession()
  339. */
  340. public static PersistenceSession getPersistenceSession() {
  341. return getSession().getPersistenceSession();
  342. }
  343. /**
  344. * Convenience method.
  345. *
  346. * @see NakedObjectSession#getUserProfile()
  347. */
  348. public static UserProfile getUserProfile() {
  349. return getSession().getUserProfile();
  350. }
  351. /**
  352. * Convenience methods
  353. *
  354. * @see NakedObjectSession#getPersistenceSession()
  355. * @see PersistenceSession#getTransactionManager()
  356. */
  357. public static NakedObjectTransactionManager getTransactionManager() {
  358. return getPersistenceSession().getTransactionManager();
  359. }
  360. // ///////////////////////////////////////////////////////////
  361. // Static Convenience methods (transaction scoped)
  362. // ///////////////////////////////////////////////////////////
  363. public static boolean inTransaction() {
  364. return inSession() &&
  365. getCurrentTransaction() != null &&
  366. !getCurrentTransaction().getState().isComplete();
  367. }
  368. /**
  369. * Convenience method, returning the current {@link NakedObjectTransaction transaction} (if any).
  370. *
  371. * <p>
  372. * Transactions are managed using the {@link NakedObjectTransactionManager} obtainable from the
  373. * {@link NakedObjectSession's} {@link PersistenceSession}.
  374. *
  375. * @see NakedObjectSession#getCurrentTransaction()
  376. * @see PersistenceSession#getTransactionManager()
  377. */
  378. public static NakedObjectTransaction getCurrentTransaction() {
  379. return getSession().getCurrentTransaction();
  380. }
  381. /**
  382. * Convenience method, returning the {@link MessageBroker} of the {@link #getCurrentTransaction() current
  383. * transaction}.
  384. */
  385. public static MessageBroker getMessageBroker() {
  386. return getCurrentTransaction().getMessageBroker();
  387. }
  388. /**
  389. * Convenience method, returning the {@link UpdateNotifier} of the {@link #getCurrentTransaction() current
  390. * transaction}.
  391. */
  392. public static UpdateNotifier getUpdateNotifier() {
  393. return getCurrentTransaction().getUpdateNotifier();
  394. }
  395. // ///////////////////////////////////////////////////////////
  396. // Debug
  397. // ///////////////////////////////////////////////////////////
  398. public static DebugInfo[] debugSystem() {
  399. DebugList debugList = new DebugList("Naked Objects System");
  400. debugList.add("Context", getInstance());
  401. debugList.add("Naked Objects session factory", getSessionFactory());
  402. debugList.add(" Authentication manager", getSessionFactory().getAuthenticationManager());
  403. debugList.add(" Persistence session factory", getSessionFactory().getPersistenceSessionFactory());
  404. debugList.add("User profile loader", getUserProfileLoader());
  405. debugList.add("Reflector", getSpecificationLoader());
  406. debugList.add("Template image loader", getTemplateImageLoader());
  407. debugList.add("Deployment type", getDeploymentType().getDebug());
  408. debugList.add("Configuration", getConfiguration());
  409. debugList.add("Services", getServices());
  410. return debugList.debug();
  411. }
  412. public static DebugInfo[] debugSession() {
  413. DebugList debugList = new DebugList("Naked Objects Session");
  414. debugList.add("Naked Objects session", getSession());
  415. debugList.add("Authentication session", getAuthenticationSession());
  416. debugList.add("User profile", getUserProfile());
  417. debugList.add("Persistence Session", getPersistenceSession());
  418. debugList.add("Transaction Manager", getTransactionManager());
  419. debugList.add("Service injector", getPersistenceSession().getServicesInjector());
  420. debugList.add("Adapter factory", getPersistenceSession().getAdapterFactory());
  421. debugList.add("Object factory", getPersistenceSession().getObjectFactory());
  422. debugList.add("OID generator", getPersistenceSession().getOidGenerator());
  423. debugList.add("Adapter manager", getPersistenceSession().getAdapterManager());
  424. debugList.add("Services", getPersistenceSession().getServices());
  425. return debugList.debug();
  426. }
  427. public void debugData(final DebugString debug) {
  428. debug.appendln("context ", this);
  429. }
  430. }
  431. // Copyright (c) Naked Objects Group Ltd.