]> git.argeo.org Git - lgpl/argeo-commons.git/blob - runtime/StaticCms.java
Prepare next development cycle
[lgpl/argeo-commons.git] / runtime / StaticCms.java
1 package org.argeo.cms.runtime;
2
3 import java.util.Dictionary;
4 import java.util.concurrent.CompletableFuture;
5
6 import org.argeo.api.cms.CmsContext;
7 import org.argeo.api.cms.CmsDeployment;
8 import org.argeo.api.cms.CmsState;
9 import org.argeo.cms.internal.osgi.DeployConfig;
10 import org.argeo.cms.internal.runtime.CmsContextImpl;
11 import org.argeo.cms.internal.runtime.CmsDeploymentImpl;
12 import org.argeo.cms.internal.runtime.CmsStateImpl;
13 import org.argeo.cms.internal.runtime.CmsUserAdmin;
14 import org.argeo.osgi.transaction.SimpleTransactionManager;
15 import org.argeo.osgi.transaction.WorkControl;
16 import org.argeo.osgi.transaction.WorkTransaction;
17 import org.argeo.util.register.Component;
18 import org.argeo.util.register.SimpleRegister;
19 import org.osgi.service.useradmin.UserAdmin;
20
21 /**
22 * A CMS assembly which is programatically defined, as an alternative to OSGi
23 * deployment. Useful for testing or AOT compilation.
24 */
25 public class StaticCms {
26 private static SimpleRegister register = new SimpleRegister();
27
28 private CompletableFuture<Void> stopped = new CompletableFuture<Void>();
29
30 public void start() {
31 // CMS State
32 CmsStateImpl cmsState = new CmsStateImpl();
33 Component<CmsStateImpl> cmsStateC = new Component.Builder<>(cmsState) //
34 .addType(CmsState.class) //
35 .addActivation(cmsState::start) //
36 .addDeactivation(cmsState::stop) //
37 .build(register);
38
39 // Deployment Configuration
40 DeployConfig deployConfig = new DeployConfig();
41 Component<DeployConfig> deployConfigC = new Component.Builder<>(deployConfig) //
42 .addType(DeployConfig.class) //
43 .addActivation(deployConfig::start) //
44 .addDeactivation(deployConfig::stop) //
45 .build(register);
46
47 // CMS Deployment
48 CmsDeploymentImpl cmsDeployment = new CmsDeploymentImpl();
49 Component<CmsDeploymentImpl> cmsDeploymentC = new Component.Builder<>(cmsDeployment) //
50 .addType(CmsDeployment.class) //
51 .addActivation(cmsDeployment::start) //
52 .addDeactivation(cmsDeployment::stop) //
53 .addDependency(cmsStateC.getType(CmsState.class), cmsDeployment::setCmsState, null) //
54 .addDependency(deployConfigC.getType(DeployConfig.class), cmsDeployment::setDeployConfig, null) //
55 .build(register);
56
57 // Transaction manager
58 SimpleTransactionManager transactionManager = new SimpleTransactionManager();
59 Component<SimpleTransactionManager> transactionManagerC = new Component.Builder<>(transactionManager) //
60 .addType(WorkControl.class) //
61 .addType(WorkTransaction.class) //
62 .build(register);
63
64 // User Admin
65 CmsUserAdmin userAdmin = new CmsUserAdmin();
66
67 Component<CmsUserAdmin> userAdminC = new Component.Builder<>(userAdmin) //
68 .addType(UserAdmin.class) //
69 .addDependency(transactionManagerC.getType(WorkControl.class), userAdmin::setTransactionManager, null) //
70 .addDependency(transactionManagerC.getType(WorkTransaction.class), userAdmin::setUserTransaction, null) //
71 .addDependency(deployConfigC.getType(DeployConfig.class), (d) -> {
72 for (Dictionary<String, Object> userDirectoryConfig : d.getUserDirectoryConfigs())
73 userAdmin.enableUserDirectory(userDirectoryConfig);
74 }, null) //
75 .build(register);
76
77 // CMS Context
78 CmsContextImpl cmsContext = new CmsContextImpl();
79 Component<CmsContextImpl> cmsContextC = new Component.Builder<>(cmsContext) //
80 .addType(CmsContext.class) //
81 .addActivation(cmsContext::start) //
82 .addDeactivation(cmsContext::stop) //
83 .addDependency(cmsStateC.getType(CmsState.class), cmsContext::setCmsState, null) //
84 .addDependency(cmsDeploymentC.getType(CmsDeployment.class), cmsContext::setCmsDeployment, null) //
85 .addDependency(userAdminC.getType(UserAdmin.class), cmsContext::setUserAdmin, null) //
86 .build(register);
87 assert cmsContextC.get() == cmsContext;
88
89 register.activate();
90 }
91
92 public void stop() {
93 if (register.isActive()) {
94 register.deactivate();
95 }
96 register.clear();
97 stopped.complete(null);
98 }
99
100 public void waitForStop() {
101 stopped.join();
102 }
103
104 public static void main(String[] args) {
105 StaticCms staticCms = new StaticCms();
106 Runtime.getRuntime().addShutdownHook(new Thread(() -> staticCms.stop(), "Static CMS Shutdown"));
107 staticCms.start();
108 staticCms.waitForStop();
109 }
110
111 }