PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 358 lines | 207 code | 72 blank | 79 comment | 26 complexity | 709d205819d88c8ca0bf570ba6e1eb39 MD5 | raw file
  1. package org.nakedobjects.runtime.system.installers;
  2. import static org.hamcrest.CoreMatchers.is;
  3. import static org.hamcrest.CoreMatchers.not;
  4. import static org.hamcrest.CoreMatchers.nullValue;
  5. import static org.nakedobjects.metamodel.commons.ensure.Ensure.ensureThatArg;
  6. import static org.nakedobjects.metamodel.commons.ensure.Ensure.ensureThatState;
  7. import java.util.List;
  8. import org.apache.log4j.Logger;
  9. import org.nakedobjects.metamodel.config.NakedObjectConfiguration;
  10. import org.nakedobjects.metamodel.specloader.FacetDecoratorInstaller;
  11. import org.nakedobjects.metamodel.specloader.NakedObjectReflector;
  12. import org.nakedobjects.metamodel.specloader.NakedObjectReflectorInstaller;
  13. import org.nakedobjects.runtime.authentication.AuthenticationManager;
  14. import org.nakedobjects.runtime.authentication.AuthenticationManagerInstaller;
  15. import org.nakedobjects.runtime.authorization.AuthorizationManager;
  16. import org.nakedobjects.runtime.authorization.AuthorizationManagerInstaller;
  17. import org.nakedobjects.runtime.fixturesinstaller.FixturesInstaller;
  18. import org.nakedobjects.runtime.imageloader.TemplateImageLoader;
  19. import org.nakedobjects.runtime.imageloader.TemplateImageLoaderInstaller;
  20. import org.nakedobjects.runtime.installers.InstallerLookup;
  21. import org.nakedobjects.runtime.persistence.PersistenceMechanismInstaller;
  22. import org.nakedobjects.runtime.persistence.PersistenceSessionFactory;
  23. import org.nakedobjects.runtime.persistence.internal.RuntimeContextFromSession;
  24. import org.nakedobjects.runtime.persistence.services.ServicesInstaller;
  25. import org.nakedobjects.runtime.remoting.ClientConnectionInstaller;
  26. import org.nakedobjects.runtime.session.NakedObjectSessionFactory;
  27. import org.nakedobjects.runtime.session.NakedObjectSessionFactoryDefault;
  28. import org.nakedobjects.runtime.system.DeploymentType;
  29. import org.nakedobjects.runtime.system.NakedObjectSystemException;
  30. import org.nakedobjects.runtime.system.NakedObjectsSystemAbstract;
  31. import org.nakedobjects.runtime.system.SystemConstants;
  32. import org.nakedobjects.runtime.transaction.facetdecorator.standard.TransactionFacetDecoratorInstaller;
  33. import org.nakedobjects.runtime.userprofile.UserProfileLoader;
  34. import org.nakedobjects.runtime.userprofile.UserProfileLoaderDefault;
  35. import org.nakedobjects.runtime.userprofile.UserProfileStore;
  36. import org.nakedobjects.runtime.userprofile.UserProfileStoreInstaller;
  37. public class NakedObjectsSystemUsingInstallers extends NakedObjectsSystemAbstract {
  38. public static final Logger LOG = Logger.getLogger(NakedObjectsSystemUsingInstallers.class);
  39. private final InstallerLookup installerLookup;
  40. private AuthenticationManagerInstaller authenticationInstaller;
  41. private AuthorizationManagerInstaller authorizationInstaller;
  42. private NakedObjectReflectorInstaller reflectorInstaller;
  43. private ServicesInstaller servicesInstaller;
  44. private UserProfileStoreInstaller userProfileStoreInstaller;
  45. private PersistenceMechanismInstaller persistenceMechanismInstaller;
  46. private FixturesInstaller fixtureInstaller;
  47. // ///////////////////////////////////////////
  48. // Constructors
  49. // ///////////////////////////////////////////
  50. public NakedObjectsSystemUsingInstallers(final DeploymentType deploymentType, final InstallerLookup installerLookup) {
  51. super(deploymentType);
  52. ensureThatArg(installerLookup, is(not(nullValue())));
  53. this.installerLookup = installerLookup;
  54. }
  55. // ///////////////////////////////////////////
  56. // InstallerLookup
  57. // ///////////////////////////////////////////
  58. /**
  59. * As per {@link #NakedObjectsSystemUsingInstallers(DeploymentType, InstallerLookup) constructor}.
  60. */
  61. public InstallerLookup getInstallerLookup() {
  62. return installerLookup;
  63. }
  64. // ///////////////////////////////////////////
  65. // Create context hooks
  66. // ///////////////////////////////////////////
  67. public NakedObjectSessionFactory doCreateSessionFactory(final DeploymentType deploymentType)
  68. throws NakedObjectSystemException {
  69. final PersistenceSessionFactory persistenceSessionFactory = obtainPersistenceSessionFactory(deploymentType);
  70. final UserProfileLoader userProfileLoader = new UserProfileLoaderDefault(obtainUserProfileStore());
  71. return createSessionFactory(deploymentType, userProfileLoader, persistenceSessionFactory);
  72. }
  73. /**
  74. * Overloaded version designed to be called by subclasses that need to explicitly specify different
  75. * persistence mechanisms.
  76. *
  77. * <p>
  78. * This is <i>not</i> a hook method, rather it is designed to be called <i>from</i> the
  79. * {@link #doCreateSessionFactory(DeploymentType) hook method}.
  80. */
  81. protected final NakedObjectSessionFactory createSessionFactory(
  82. final DeploymentType deploymentType,
  83. final UserProfileLoader userProfileLoader,
  84. final PersistenceSessionFactory persistenceSessionFactory) throws NakedObjectSystemException {
  85. final NakedObjectConfiguration configuration = getConfiguration();
  86. final AuthenticationManager authenticationManager = obtainAuthenticationManager(deploymentType);
  87. final AuthorizationManager authorizationManager = obtainAuthorizationManager(deploymentType);
  88. final TemplateImageLoader templateImageLoader = obtainTemplateImageLoader();
  89. final NakedObjectReflector reflector = obtainReflector(deploymentType);
  90. final List<Object> servicesList = obtainServices();
  91. // bind metamodel to the (runtime) framework
  92. // REVIEW: misplaced? seems like a side-effect...
  93. reflector.setRuntimeContext(new RuntimeContextFromSession());
  94. return new NakedObjectSessionFactoryDefault(deploymentType, configuration, templateImageLoader, reflector,
  95. authenticationManager, authorizationManager, userProfileLoader, persistenceSessionFactory, servicesList);
  96. }
  97. // ///////////////////////////////////////////
  98. // Configuration
  99. // ///////////////////////////////////////////
  100. /**
  101. * Returns a <i>snapshot</i> of the {@link NakedObjectConfiguration configuration} held by the
  102. * {@link #getInstallerLookup() installer lookup}.
  103. *
  104. * @see InstallerLookup#getConfiguration()
  105. */
  106. @Override
  107. public NakedObjectConfiguration getConfiguration() {
  108. return installerLookup.getConfiguration();
  109. }
  110. // ///////////////////////////////////////////
  111. // Authentication & Authorization
  112. // ///////////////////////////////////////////
  113. public void lookupAndSetAuthenticatorAndAuthorization(DeploymentType deploymentType) {
  114. NakedObjectConfiguration configuration = installerLookup.getConfiguration();
  115. String connection = configuration.getString(SystemConstants.CLIENT_CONNECTION_KEY);
  116. if (connection != null) {
  117. lookupAndSetAuthenticatorAndAuthorizationUsingClientConnectionInstaller(connection);
  118. } else {
  119. lookupAndSetAuthenticatorAndAuthorizationInstallers(deploymentType);
  120. }
  121. }
  122. private void lookupAndSetAuthenticatorAndAuthorizationUsingClientConnectionInstaller(
  123. String connection) {
  124. ClientConnectionInstaller clientConnectionInstaller = installerLookup.clientConnectionInstaller(connection);
  125. if (clientConnectionInstaller == null) {
  126. return;
  127. }
  128. setAuthenticationInstaller(clientConnectionInstaller);
  129. setAuthorizationInstaller(clientConnectionInstaller);
  130. }
  131. private void lookupAndSetAuthenticatorAndAuthorizationInstallers(DeploymentType deploymentType) {
  132. // use the one specified in configuration
  133. final AuthenticationManagerInstaller authenticationInstaller = installerLookup.authenticationManagerInstaller(null, deploymentType.isExploring());
  134. if (authenticationInstaller != null) {
  135. setAuthenticationInstaller(authenticationInstaller);
  136. }
  137. // use the one specified in configuration
  138. final AuthorizationManagerInstaller authorizationInstaller = installerLookup.authorizationManagerInstaller(null, !deploymentType.isProduction());
  139. if (authorizationInstaller != null) {
  140. setAuthorizationInstaller(authorizationInstaller);
  141. }
  142. }
  143. /**
  144. * Set the type of connection to used to access the server.
  145. *
  146. * <p>
  147. * Note that the {@link NakedObjectSessionFactoryUsingInstallers} also checks the
  148. * {@link ClientConnectionInstaller} twice over: to see if a <tt>PersistenceSessionProxy</tt> should be
  149. * used as a persistor, and for any {@link FacetDecoratorInstaller}s.
  150. */
  151. public void setAuthenticationInstaller(final AuthenticationManagerInstaller authenticationManagerInstaller) {
  152. this.authenticationInstaller = authenticationManagerInstaller;
  153. }
  154. /**
  155. * Set the type of connection to used to access the server.
  156. *
  157. * <p>
  158. * Note that the {@link NakedObjectSessionFactoryUsingInstallers} also checks the
  159. * {@link ClientConnectionInstaller} twice over: to see if a <tt>PersistenceSessionProxy</tt> should be
  160. * used as a persistor, and for any {@link FacetDecoratorInstaller}s.
  161. */
  162. public void setAuthorizationInstaller(final AuthorizationManagerInstaller authorizationManagerInstaller) {
  163. this.authorizationInstaller = authorizationManagerInstaller;
  164. }
  165. protected AuthenticationManager obtainAuthenticationManager(DeploymentType deploymentType) {
  166. return authenticationInstaller.createAuthenticationManager();
  167. }
  168. protected AuthorizationManager obtainAuthorizationManager(DeploymentType deploymentType) {
  169. return authorizationInstaller.createAuthorizationManager();
  170. }
  171. // ///////////////////////////////////////////
  172. // Fixtures
  173. // ///////////////////////////////////////////
  174. public void lookupAndSetFixturesInstaller() {
  175. NakedObjectConfiguration configuration = installerLookup.getConfiguration();
  176. String fixture = configuration.getString(SystemConstants.FIXTURES_INSTALLER_KEY);
  177. final FixturesInstaller fixturesInstaller = installerLookup.fixturesInstaller(fixture);
  178. if (fixturesInstaller != null) {
  179. this.fixtureInstaller = fixturesInstaller;
  180. }
  181. }
  182. public void setFixtureInstaller(FixturesInstaller fixtureInstaller) {
  183. this.fixtureInstaller = fixtureInstaller;
  184. }
  185. @Override
  186. protected FixturesInstaller obtainFixturesInstaller() throws NakedObjectSystemException {
  187. return fixtureInstaller;
  188. }
  189. // ///////////////////////////////////////////
  190. // Template Image Loader
  191. // ///////////////////////////////////////////
  192. /**
  193. * Uses the {@link TemplateImageLoader} configured in {@link InstallerLookup}, if available, else falls
  194. * back to that of the superclass.
  195. */
  196. @Override
  197. protected TemplateImageLoader obtainTemplateImageLoader() {
  198. TemplateImageLoaderInstaller templateImageLoaderInstaller = installerLookup.templateImageLoaderInstaller(null);
  199. if (templateImageLoaderInstaller != null) {
  200. return templateImageLoaderInstaller.createLoader();
  201. } else {
  202. return super.obtainTemplateImageLoader();
  203. }
  204. }
  205. // ///////////////////////////////////////////
  206. // Reflector
  207. // ///////////////////////////////////////////
  208. public void setReflectorInstaller(final NakedObjectReflectorInstaller reflectorInstaller) {
  209. this.reflectorInstaller = reflectorInstaller;
  210. }
  211. @Override
  212. protected NakedObjectReflector obtainReflector(DeploymentType deploymentType) throws NakedObjectSystemException {
  213. if (reflectorInstaller == null) {
  214. String fromCmdLine = getConfiguration().getString(SystemConstants.REFLECTOR_KEY);
  215. reflectorInstaller = installerLookup.reflectorInstaller(fromCmdLine);
  216. }
  217. ensureThatState(reflectorInstaller, is(not(nullValue())),
  218. "reflector installer has not been injected and could not be looked up");
  219. // add in transaction support (if already in set then will be ignored)
  220. reflectorInstaller.addFacetDecoratorInstaller(installerLookup
  221. .getInstaller(TransactionFacetDecoratorInstaller.class));
  222. // if there is a client connection installer, then add facet decorator installer also
  223. String connection = getConfiguration().getString(SystemConstants.CLIENT_CONNECTION_KEY);
  224. if (connection != null) {
  225. FacetDecoratorInstaller clientConnectionInstaller = installerLookup.clientConnectionInstaller(connection);
  226. reflectorInstaller.addFacetDecoratorInstaller(clientConnectionInstaller);
  227. }
  228. return reflectorInstaller.createReflector();
  229. }
  230. // ///////////////////////////////////////////
  231. // Services
  232. // ///////////////////////////////////////////
  233. public void setServicesInstaller(ServicesInstaller servicesInstaller) {
  234. this.servicesInstaller = servicesInstaller;
  235. }
  236. @Override
  237. protected List<Object> obtainServices() {
  238. if (servicesInstaller == null) {
  239. servicesInstaller = installerLookup.servicesInstaller(null);
  240. }
  241. ensureThatState(servicesInstaller, is(not(nullValue())),
  242. "services installer has not been injected and could not be looked up");
  243. return servicesInstaller.getServices(getDeploymentType());
  244. }
  245. // ///////////////////////////////////////////
  246. // User Profile Loader/Store
  247. // ///////////////////////////////////////////
  248. public void lookupAndSetUserProfileFactoryInstaller() {
  249. NakedObjectConfiguration configuration = installerLookup.getConfiguration();
  250. String persistor = configuration.getString(SystemConstants.PROFILE_PERSISTOR_INSTALLER_KEY);
  251. UserProfileStoreInstaller userProfilePersistenceMechanismInstaller = installerLookup.userProfilePersistenceMechanismInstaller(persistor, getDeploymentType());
  252. if (userProfilePersistenceMechanismInstaller != null) {
  253. setUserProfileStoreInstaller(userProfilePersistenceMechanismInstaller);
  254. }
  255. }
  256. public void setUserProfileStoreInstaller(UserProfileStoreInstaller userProfilestoreInstaller) {
  257. this.userProfileStoreInstaller = userProfilestoreInstaller;
  258. }
  259. protected UserProfileStore obtainUserProfileStore() {
  260. return userProfileStoreInstaller.createUserProfileStore(getConfiguration());
  261. }
  262. // ///////////////////////////////////////////
  263. // PersistenceSessionFactory
  264. // ///////////////////////////////////////////
  265. public void setPersistenceMechanismInstaller(final PersistenceMechanismInstaller persistenceMechanismInstaller) {
  266. this.persistenceMechanismInstaller = persistenceMechanismInstaller;
  267. }
  268. @Override
  269. protected PersistenceSessionFactory obtainPersistenceSessionFactory(DeploymentType deploymentType)
  270. throws NakedObjectSystemException {
  271. // attempt to look up connection (that is, a ProxyPersistor)
  272. String connection = getConfiguration().getString(SystemConstants.CLIENT_CONNECTION_KEY);
  273. if (connection != null) {
  274. persistenceMechanismInstaller = installerLookup.clientConnectionInstaller(connection);
  275. }
  276. // if nothing, look for a object store persistor
  277. if (persistenceMechanismInstaller == null) {
  278. String persistenceMechanism = getConfiguration().getString(SystemConstants.OBJECT_PERSISTOR_INSTALLER_KEY);
  279. persistenceMechanismInstaller = installerLookup.persistenceMechanismInstaller(persistenceMechanism, deploymentType);
  280. }
  281. ensureThatState(persistenceMechanismInstaller, is(not(nullValue())),
  282. "persistor installer has not been injected and could not be looked up");
  283. return persistenceMechanismInstaller.createPersistenceSessionFactory(deploymentType);
  284. }
  285. }
  286. // Copyright (c) Naked Objects Group Ltd.