]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/runtime/StaticCms.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / runtime / StaticCms.java
1 package org.argeo.cms.runtime;
2
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.concurrent.CompletableFuture;
6
7 import org.argeo.api.acr.ContentRepository;
8 import org.argeo.api.acr.spi.ProvidedRepository;
9 import org.argeo.api.cms.CmsContext;
10 import org.argeo.api.cms.CmsDeployment;
11 import org.argeo.api.cms.CmsState;
12 import org.argeo.api.cms.directory.CmsUserManager;
13 import org.argeo.api.cms.transaction.SimpleTransactionManager;
14 import org.argeo.api.cms.transaction.WorkControl;
15 import org.argeo.api.cms.transaction.WorkTransaction;
16 import org.argeo.api.register.Component;
17 import org.argeo.api.register.ComponentRegister;
18 import org.argeo.api.register.SimpleRegister;
19 import org.argeo.api.uuid.ConcurrentUuidFactory;
20 import org.argeo.api.uuid.NodeIdSupplier;
21 import org.argeo.api.uuid.UuidFactory;
22 import org.argeo.cms.internal.runtime.CmsContextImpl;
23 import org.argeo.cms.internal.runtime.CmsDeploymentImpl;
24 import org.argeo.cms.internal.runtime.CmsStateImpl;
25 import org.argeo.cms.internal.runtime.CmsUserAdmin;
26 import org.argeo.cms.internal.runtime.CmsUserManagerImpl;
27 import org.argeo.cms.internal.runtime.DeployedContentRepository;
28 import org.osgi.service.useradmin.UserAdmin;
29
30 /**
31 * A CMS assembly which is programmatically defined, as an alternative to OSGi
32 * deployment. Useful for testing or AOT compilation.
33 */
34 public class StaticCms {
35 private SimpleRegister register = new SimpleRegister();
36
37 private CompletableFuture<Void> stopped = new CompletableFuture<Void>();
38
39 public void start() {
40 // CMS State
41 CmsStateImpl cmsState = new CmsStateImpl();
42 Component<CmsStateImpl> cmsStateC = new Component.Builder<>(cmsState) //
43 .addType(CmsState.class) //
44 .addType(NodeIdSupplier.class) //
45 .addActivation(cmsState::start) //
46 .addDeactivation(cmsState::stop) //
47 .build(register);
48
49 // UID factory
50 ConcurrentUuidFactory uuidFactory = new ConcurrentUuidFactory();
51 Component<ConcurrentUuidFactory> uuidFactoryC = new Component.Builder<>(uuidFactory) //
52 .addType(UuidFactory.class) //
53 .addDependency(cmsStateC.getType(NodeIdSupplier.class), uuidFactory::setNodeIdSupplier, null) //
54 .build(register);
55
56 // Transaction manager
57 SimpleTransactionManager transactionManager = new SimpleTransactionManager();
58 Component<SimpleTransactionManager> transactionManagerC = new Component.Builder<>(transactionManager) //
59 .addType(WorkControl.class) //
60 .addType(WorkTransaction.class) //
61 .build(register);
62
63 // User Admin
64 CmsUserAdmin userAdmin = new CmsUserAdmin();
65 Component<CmsUserAdmin> userAdminC = new Component.Builder<>(userAdmin) //
66 .addType(UserAdmin.class) //
67 .addActivation(userAdmin::start) //
68 .addDeactivation(userAdmin::stop) //
69 .addDependency(cmsStateC.getType(CmsState.class), userAdmin::setCmsState, null) //
70 .addDependency(transactionManagerC.getType(WorkControl.class), userAdmin::setTransactionManager, null) //
71 .addDependency(transactionManagerC.getType(WorkTransaction.class), userAdmin::setUserTransaction, null) //
72 .build(register);
73
74 // User manager
75 CmsUserManagerImpl userManager = new CmsUserManagerImpl();
76 // for (UserDirectory userDirectory : userAdmin.getUserDirectories()) {
77 // // FIXME deal with properties
78 // userManager.addUserDirectory(userDirectory, new HashMap<>());
79 // }
80 Component<CmsUserManagerImpl> userManagerC = new Component.Builder<>(userManager) //
81 .addType(CmsUserManager.class) //
82 .addActivation(userManager::start) //
83 .addDeactivation(userManager::stop) //
84 .addDependency(userAdminC.getType(UserAdmin.class), userManager::setUserAdmin, null) //
85 .addDependency(transactionManagerC.getType(WorkTransaction.class), userManager::setUserTransaction,
86 null) //
87 .build(register);
88
89 // Content Repository
90 DeployedContentRepository contentRepository = new DeployedContentRepository();
91 Component<DeployedContentRepository> contentRepositoryC = new Component.Builder<>(contentRepository) //
92 .addType(ProvidedRepository.class) //
93 .addType(ContentRepository.class) //
94 .addActivation(contentRepository::start) //
95 .addDeactivation(contentRepository::stop) //
96 .addDependency(cmsStateC.getType(CmsState.class), contentRepository::setCmsState, null) //
97 .addDependency(uuidFactoryC.getType(UuidFactory.class), contentRepository::setUuidFactory, null) //
98 .addDependency(userManagerC.getType(CmsUserManager.class), contentRepository::setUserManager, null) //
99 .build(register);
100
101 // CMS Deployment
102 CmsDeploymentImpl cmsDeployment = new CmsDeploymentImpl();
103 Component<CmsDeploymentImpl> cmsDeploymentC = new Component.Builder<>(cmsDeployment) //
104 .addType(CmsDeployment.class) //
105 .addActivation(cmsDeployment::start) //
106 .addDeactivation(cmsDeployment::stop) //
107 .addDependency(cmsStateC.getType(CmsState.class), cmsDeployment::setCmsState, null) //
108 // .addDependency(deployConfigC.getType(DeployConfig.class), cmsDeployment::setDeployConfig, null) //
109 .build(register);
110
111 // CMS Context
112 CmsContextImpl cmsContext = new CmsContextImpl();
113 Component<CmsContextImpl> cmsContextC = new Component.Builder<>(cmsContext) //
114 .addType(CmsContext.class) //
115 .addActivation(cmsContext::start) //
116 .addDeactivation(cmsContext::stop) //
117 .addDependency(cmsStateC.getType(CmsState.class), cmsContext::setCmsState, null) //
118 .addDependency(cmsDeploymentC.getType(CmsDeployment.class), cmsContext::setCmsDeployment, null) //
119 .addDependency(userAdminC.getType(UserAdmin.class), cmsContext::setUserAdmin, null) //
120 .addDependency(uuidFactoryC.getType(UuidFactory.class), cmsContext::setUuidFactory, null) //
121 // .addDependency(contentRepositoryC.getType(ProvidedRepository.class), cmsContext::setContentRepository,
122 // null) //
123 .build(register);
124 assert cmsContextC.get() == cmsContext;
125
126 addComponents(register);
127
128 register.activate();
129
130 postActivation(register);
131 }
132
133 protected void addComponents(ComponentRegister register) {
134
135 }
136
137 protected void postActivation(ComponentRegister register) {
138
139 }
140
141 public ComponentRegister getComponentRegister() {
142 return register;
143 }
144
145 public void stop() {
146 if (register.isActive()) {
147 register.deactivate();
148 }
149 register.clear();
150 stopped.complete(null);
151 }
152
153 public void waitForStop() {
154 stopped.join();
155 }
156
157 public static void main(String[] args) {
158 if (args.length == 0) {
159 System.err.println("Usage: <data path>");
160 System.exit(1);
161 }
162 Path instancePath = Paths.get(args[0]);
163 System.setProperty("osgi.instance.area", instancePath.toUri().toString());
164
165 StaticCms staticCms = new StaticCms();
166 Runtime.getRuntime().addShutdownHook(new Thread(() -> staticCms.stop(), "Static CMS Shutdown"));
167 staticCms.start();
168 staticCms.waitForStop();
169 }
170
171 }