]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
Rather use DataAdmin login context
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / Kernel.java
1 package org.argeo.cms.internal.kernel;
2
3 /**
4 * Argeo CMS Kernel. Responsible for :
5 * <ul>
6 * <li>security</li>
7 * <li>provisioning</li>
8 * <li>transaction</li>
9 * <li>logging</li>
10 * <li>local and remote file systems access</li>
11 * <li>OS access</li>
12 * </ul>
13 */
14 @Deprecated
15 final class Kernel{
16 //implements KernelHeader, KernelConstants {
17 // /*
18 // * SERVICE REFERENCES
19 // */
20 // // private ServiceReference<ConfigurationAdmin> configurationAdmin;
21 // private final ServiceTracker<ConfigurationAdmin, ConfigurationAdmin> configurationAdmin;
22 // private final ServiceTracker<LogReaderService, LogReaderService> logReaderService;
23 // private final ServiceTracker<HttpService, HttpService> httpService;
24 // private final ConditionalPermissionAdmin permissionAdmin;
25 // /*
26 // * REGISTERED SERVICES
27 // */
28 // private ServiceRegistration<ArgeoLogger> loggerReg;
29 // private ServiceRegistration<TransactionManager> tmReg;
30 // private ServiceRegistration<UserTransaction> utReg;
31 // private ServiceRegistration<TransactionSynchronizationRegistry> tsrReg;
32 // private ServiceRegistration<?> repositoryReg;
33 // private ServiceRegistration<RepositoryFactory> repositoryFactoryReg;
34 // private ServiceRegistration<UserAdmin> userAdminReg;
35 //
36 // /*
37 // * SERVICES IMPLEMENTATIONS
38 // */
39 // private NodeLogger logger;
40 // private BitronixTransactionManager transactionManager;
41 // private BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry;
42 // private NodeRepositoryFactory repositoryFactory;
43 // private Repository repository;
44 // private NodeUserAdmin userAdmin;
45 //
46 // // Members
47 // private final BundleContext bc;// = Activator.getBundleContext();
48 // private final NodeSecurity nodeSecurity;
49 //
50 // private final static Log log = LogFactory.getLog(Kernel.class);
51 // ThreadGroup threadGroup = new ThreadGroup(Kernel.class.getSimpleName());
52 // private DataHttp dataHttp;
53 // private NodeHttp nodeHttp;
54 // private KernelThread kernelThread;
55 //
56 // private Locale defaultLocale = null;
57 // private List<Locale> locales = null;
58 //
59 // public Kernel() {
60 // // KernelUtils.logFrameworkProperties(log);
61 // nodeSecurity = new NodeSecurity();
62 // bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
63 // configurationAdmin = new ServiceTracker<ConfigurationAdmin, ConfigurationAdmin>(bc, ConfigurationAdmin.class,
64 // new PrepareStc<ConfigurationAdmin>());
65 // configurationAdmin.open();
66 // logReaderService = new ServiceTracker<LogReaderService, LogReaderService>(bc, LogReaderService.class,
67 // new PrepareStc<LogReaderService>());
68 // logReaderService.open();
69 // httpService = new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, new PrepareHttpStc());
70 // httpService.open();
71 //
72 // permissionAdmin = bc.getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
73 // }
74 //
75 // /*
76 // * PACKAGE RESTRICTED INTERFACE
77 // */
78 // Subject getKernelSubject() {
79 // return nodeSecurity.getKernelSubject();
80 // }
81 //
82 // /*
83 // * INITIALISATION
84 // */
85 //
86 // final void init() {
87 // Subject.doAs(nodeSecurity.getKernelSubject(), new PrivilegedAction<Void>() {
88 // @Override
89 // public Void run() {
90 // doInit();
91 // return null;
92 // }
93 // });
94 // }
95 //
96 // private void doInit() {
97 // long begin = System.currentTimeMillis();
98 // // Use CMS bundle classloader
99 // ClassLoader currentContextCl = Thread.currentThread().getContextClassLoader();
100 // Thread.currentThread().setContextClassLoader(Kernel.class.getClassLoader());
101 // try {
102 // // Listen to service publication (also ours)
103 // // bc.addServiceListener(Kernel.this);
104 //
105 // if (nodeSecurity.isFirstInit())
106 // firstInit();
107 //
108 // defaultLocale = new Locale(getFrameworkProp(NodeConstants.I18N_DEFAULT_LOCALE, ENGLISH.getLanguage()));
109 // locales = asLocaleList(getFrameworkProp(NodeConstants.I18N_LOCALES));
110 //
111 // // ServiceTracker<LogReaderService, LogReaderService>
112 // // logReaderService = new ServiceTracker<LogReaderService,
113 // // LogReaderService>(
114 // // bc, LogReaderService.class, null);
115 // // logReaderService.open();
116 // logger = new NodeLogger(logReaderService.getService());
117 // // logReaderService.close();
118 //
119 // if (isMaintenance())
120 // maintenanceInit();
121 // else
122 // normalInit();
123 // } catch (Throwable e) {
124 // log.error("Cannot initialize Argeo CMS", e);
125 // throw new ArgeoException("Cannot initialize", e);
126 // } finally {
127 // Thread.currentThread().setContextClassLoader(currentContextCl);
128 // // FIXME better manage lifecycle.
129 // try {
130 // new LoginContext(LOGIN_CONTEXT_KERNEL, nodeSecurity.getKernelSubject()).logout();
131 // } catch (LoginException e) {
132 // e.printStackTrace();
133 // }
134 // }
135 //
136 // long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
137 // log.info("## ARGEO CMS UP in " + (jvmUptime / 1000) + "." + (jvmUptime % 1000) + "s ##");
138 // long initDuration = System.currentTimeMillis() - begin;
139 // if (log.isTraceEnabled())
140 // log.trace("Kernel initialization took " + initDuration + "ms");
141 // directorsCut(initDuration);
142 // }
143 //
144 // private void normalInit() {
145 // ConfigurationAdmin conf = findConfigurationAdmin();
146 //
147 // // HTTP
148 // initWebServer(conf);
149 // // ServiceReference<ExtendedHttpService> sr =
150 // // bc.getServiceReference(ExtendedHttpService.class);
151 // // if (sr != null)
152 // // addHttpService(sr);
153 // // else
154 // // log.warn("No http service found");
155 //
156 // // Initialise services
157 // initTransactionManager();
158 //
159 // RepositoryServiceFactory jrsf = new RepositoryServiceFactory();
160 // String[] clazzes = { ManagedServiceFactory.class.getName() };
161 // Hashtable<String, String> serviceProps = new Hashtable<String, String>();
162 // serviceProps.put(Constants.SERVICE_PID, ArgeoJcrConstants.JACKRABBIT_REPO_FACTORY_PID);
163 // bc.registerService(clazzes, jrsf, serviceProps);
164 //
165 // try {
166 // Configuration nodeConf = conf.createFactoryConfiguration(ArgeoJcrConstants.JACKRABBIT_REPO_FACTORY_PID);
167 // // Configuration nodeConf =
168 // // conf.getConfiguration(ArgeoJcrConstants.REPO_PID_NODE);
169 // if (nodeConf.getProperties() == null) {
170 // Dictionary<String, Object> props = getNodeConfigFromFrameworkProperties();
171 // if (props == null) {
172 // // TODO interactive configuration
173 // if (log.isDebugEnabled())
174 // log.debug("No argeo.node.repo.type=localfs|h2|postgresql|memory"
175 // + " property defined, entering interactive mode...");
176 // return;
177 // }
178 // // props.put(ConfigurationAdmin.SERVICE_FACTORYPID,
179 // // ArgeoJcrConstants.JACKRABBIT_REPO_FACTORY_PID);
180 // props.put(Constants.SERVICE_PID, ArgeoJcrConstants.REPO_PID_NODE);
181 // nodeConf.update(props);
182 // }
183 // } catch (IOException e) {
184 // throw new CmsException("Cannot get configuration", e);
185 // }
186 //
187 // // ManagedJackrabbitRepository nodeRepo = new
188 // // ManagedJackrabbitRepository();
189 // // String[] clazzes = { ManagedService.class.getName(),
190 // // Repository.class.getName(),
191 // // JackrabbitRepository.class.getName() };
192 // // Hashtable<String, String> serviceProps = new Hashtable<String,
193 // // String>();
194 // // serviceProps.put(Constants.SERVICE_PID,
195 // // ArgeoJcrConstants.REPO_PID_NODE);
196 // // serviceProps.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
197 // // ArgeoJcrConstants.ALIAS_NODE);
198 // // repositoryReg = bc.registerService(clazzes, nodeRepo, serviceProps);
199 // // nodeRepo.waitForInit();
200 //
201 // ServiceTracker<JackrabbitRepository, JackrabbitRepository> jackrabbitSt = new ServiceTracker<>(bc,
202 // JackrabbitRepository.class, new ServiceTrackerCustomizer<JackrabbitRepository, JackrabbitRepository>() {
203 //
204 // @Override
205 // public JackrabbitRepository addingService(ServiceReference<JackrabbitRepository> reference) {
206 // JackrabbitRepository nodeRepo = bc.getService(reference);
207 // // new
208 // // JackrabbitDataModel(bc).prepareDataModel(nodeRepo);
209 // prepareDataModel(KernelUtils.openAdminSession(nodeRepo));
210 //
211 // // repository = (JackrabbitRepository)
212 // // bc.getService(repositoryReg.getReference());
213 // repository = new HomeRepository(nodeRepo);
214 // Hashtable<String, String> regProps = new Hashtable<String, String>();
215 // regProps.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, ArgeoJcrConstants.ALIAS_NODE);
216 // repositoryReg = (ServiceRegistration<? extends Repository>) bc.registerService(Repository.class,
217 // repository, regProps);
218 //
219 // // if (repository == null)
220 // // repository = new NodeRepository();
221 // if (repositoryFactory == null) {
222 // repositoryFactory = new NodeRepositoryFactory();
223 // // repositoryFactory.setBundleContext(bc);
224 // repositoryFactoryReg = bc.registerService(RepositoryFactory.class, repositoryFactory, null);
225 // }
226 // userAdmin = new NodeUserAdmin(transactionManager, repository);
227 // userAdminReg = bc.registerService(UserAdmin.class, userAdmin, userAdmin.currentState());
228 // return nodeRepo;
229 // }
230 //
231 // @Override
232 // public void modifiedService(ServiceReference<JackrabbitRepository> reference,
233 // JackrabbitRepository service) {
234 // }
235 //
236 // @Override
237 // public void removedService(ServiceReference<JackrabbitRepository> reference,
238 // JackrabbitRepository service) {
239 // }
240 // });
241 // jackrabbitSt.open();
242 //
243 // // new JackrabbitDataModel(bc).prepareDataModel(nodeRepo);
244 // // prepareDataModel(nodeRepo);
245 // //
246 // // repository = (JackrabbitRepository)
247 // // bc.getService(repositoryReg.getReference());
248 // //
249 // //// if (repository == null)
250 // //// repository = new NodeRepository();
251 // // if (repositoryFactory == null) {
252 // // repositoryFactory = new OsgiJackrabbitRepositoryFactory();
253 // // repositoryFactory.setBundleContext(bc);
254 // // }
255 // // userAdmin = new NodeUserAdmin(transactionManager, repository);
256 //
257 // // ADMIN UIs
258 // UserUi userUi = new UserUi();
259 // Hashtable<String, String> props = new Hashtable<String, String>();
260 // props.put("contextName", "user");
261 // bc.registerService(ApplicationConfiguration.class, userUi, props);
262 //
263 // // Bundle rapWorkbenchBundle =
264 // // findBundle("org.eclipse.rap.ui.workbench");
265 // // if (rapWorkbenchBundle != null)
266 // // try {
267 // // Class<?> clss = rapWorkbenchBundle
268 // // .loadClass("org.eclipse.rap.ui.internal.servlet.WorkbenchApplicationConfiguration");
269 // //
270 // // Hashtable<String, String> rapWorkbenchProps = new Hashtable<String,
271 // // String>();
272 // // rapWorkbenchProps.put("contextName", "ui");
273 // // ApplicationConfiguration workbenchApplicationConfiguration =
274 // // (ApplicationConfiguration) clss
275 // // .newInstance();
276 // // bc.registerService(ApplicationConfiguration.class,
277 // // workbenchApplicationConfiguration,
278 // // rapWorkbenchProps);
279 // // } catch (Exception e) {
280 // // log.error("Cannot initalize RAP workbench", e);
281 // // }
282 //
283 // // Kernel thread
284 //// kernelThread = new KernelThread(this);
285 //// kernelThread.setContextClassLoader(Kernel.class.getClassLoader());
286 //// kernelThread.start();
287 //
288 // // Publish services to OSGi
289 // publish();
290 // }
291 //
292 // private Dictionary<String, Object> getNodeConfigFromFrameworkProperties() {
293 // String repoType = KernelUtils.getFrameworkProp(NodeConstants.NODE_REPO_PROP_PREFIX + RepoConf.type.name());
294 // if (repoType == null)
295 // return null;
296 //
297 // Hashtable<String, Object> props = new Hashtable<String, Object>();
298 // for (RepoConf repoConf : RepoConf.values()) {
299 // String value = KernelUtils.getFrameworkProp(NodeConstants.NODE_REPO_PROP_PREFIX + repoConf.name());
300 // if (value != null)
301 // props.put(repoConf.name(), value);
302 // }
303 // return props;
304 // }
305 //
306 // /** Session is logged out. */
307 // private void prepareDataModel(Session adminSession) {
308 // try {
309 // Set<String> processed = new HashSet<String>();
310 // bundles: for (Bundle bundle : bc.getBundles()) {
311 // BundleWiring wiring = bundle.adapt(BundleWiring.class);
312 // if (wiring == null) {
313 // if (log.isTraceEnabled())
314 // log.error("No wiring for " + bundle.getSymbolicName());
315 // continue bundles;
316 // }
317 // processWiring(adminSession, wiring, processed);
318 // }
319 // } finally {
320 // JcrUtils.logoutQuietly(adminSession);
321 // }
322 // }
323 //
324 // private void processWiring(Session adminSession, BundleWiring wiring, Set<String> processed) {
325 // // recursively process requirements first
326 // List<BundleWire> requiredWires = wiring.getRequiredWires(CMS_DATA_MODEL_NAMESPACE);
327 // for (BundleWire wire : requiredWires) {
328 // processWiring(adminSession, wire.getProviderWiring(), processed);
329 // // registerCnd(adminSession, wire.getCapability(), processed);
330 // }
331 // List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
332 // for (BundleCapability capability : capabilities) {
333 // registerCnd(adminSession, capability, processed);
334 // }
335 // }
336 //
337 // private void registerCnd(Session adminSession, BundleCapability capability, Set<String> processed) {
338 // Map<String, Object> attrs = capability.getAttributes();
339 // String name = attrs.get(DataModelNamespace.CAPABILITY_NAME_ATTRIBUTE).toString();
340 // if (processed.contains(name)) {
341 // if (log.isTraceEnabled())
342 // log.trace("Data model " + name + " has already been processed");
343 // return;
344 // }
345 // String path = attrs.get(DataModelNamespace.CAPABILITY_CND_ATTRIBUTE).toString();
346 // URL url = capability.getRevision().getBundle().getResource(path);
347 // try (Reader reader = new InputStreamReader(url.openStream())) {
348 // CndImporter.registerNodeTypes(reader, adminSession, true);
349 // processed.add(name);
350 // if (log.isDebugEnabled())
351 // log.debug("Registered CND " + url);
352 // } catch (Exception e) {
353 // throw new CmsException("Cannot read cnd " + url, e);
354 // }
355 //
356 // Hashtable<String, Object> properties = new Hashtable<>();
357 // properties.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, name);
358 // bc.registerService(Repository.class, adminSession.getRepository(), properties);
359 // if (log.isDebugEnabled())
360 // log.debug("Published data model " + name);
361 // }
362 //
363 // private boolean isMaintenance() {
364 // String startLevel = KernelUtils.getFrameworkProp("osgi.startLevel");
365 // if (startLevel == null)
366 // return false;
367 // int bundleStartLevel = bc.getBundle().adapt(BundleStartLevel.class).getStartLevel();
368 // // int frameworkStartLevel =
369 // // bc.getBundle(0).adapt(BundleStartLevel.class)
370 // // .getStartLevel();
371 // int frameworkStartLevel = Integer.parseInt(startLevel);
372 // // int frameworkStartLevel = bc.getBundle(0)
373 // // .adapt(FrameworkStartLevel.class).getStartLevel();
374 // return bundleStartLevel == frameworkStartLevel;
375 // }
376 //
377 // private void maintenanceInit() {
378 // log.info("## MAINTENANCE ##");
379 // // bc.addServiceListener(Kernel.this);
380 // initWebServer(null);
381 // MaintenanceUi maintenanceUi = new MaintenanceUi();
382 // Hashtable<String, String> props = new Hashtable<String, String>();
383 // props.put("contextName", "maintenance");
384 // bc.registerService(ApplicationConfiguration.class, maintenanceUi, props);
385 // }
386 //
387 // private void firstInit() {
388 // log.info("## FIRST INIT ##");
389 // String nodeInit = getFrameworkProp(NodeConstants.NODE_INIT);
390 // if (nodeInit == null)
391 // nodeInit = "../../init";
392 // if (nodeInit.startsWith("http")) {
393 // // remoteFirstInit(nodeInit);
394 // return;
395 // }
396 // File initDir;
397 // if (nodeInit.startsWith("."))
398 // initDir = KernelUtils.getExecutionDir(nodeInit);
399 // else
400 // initDir = new File(nodeInit);
401 // // TODO also uncompress archives
402 // if (initDir.exists())
403 // try {
404 // FileUtils.copyDirectory(initDir, getOsgiInstanceDir(), new FileFilter() {
405 //
406 // @Override
407 // public boolean accept(File pathname) {
408 // if (pathname.getName().equals(".svn") || pathname.getName().equals(".git"))
409 // return false;
410 // return true;
411 // }
412 // });
413 // log.info("CMS initialized from " + initDir.getCanonicalPath());
414 // } catch (IOException e) {
415 // throw new CmsException("Cannot initialize from " + initDir, e);
416 // }
417 // }
418 //
419 // // private void remoteFirstInit(String uri) {
420 // // try {
421 // // repository = new NodeRepository();
422 // // repositoryFactory = new OsgiJackrabbitRepositoryFactory();
423 // // Repository remoteRepository =
424 // // ArgeoJcrUtils.getRepositoryByUri(repositoryFactory, uri);
425 // // Session remoteSession = remoteRepository.login(new
426 // // SimpleCredentials("root", "demo".toCharArray()), "main");
427 // // Session localSession = this.repository.login();
428 // // // FIXME register node type
429 // // // if (false)
430 // // // CndImporter.registerNodeTypes(null, localSession);
431 // // ByteArrayOutputStream out = new ByteArrayOutputStream();
432 // // remoteSession.exportSystemView("/", out, true, false);
433 // // ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
434 // // localSession.importXML("/", in,
435 // // ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
436 // // // JcrUtils.copy(remoteSession.getRootNode(),
437 // // // localSession.getRootNode());
438 // // } catch (Exception e) {
439 // // throw new CmsException("Cannot first init from " + uri, e);
440 // // }
441 // // }
442 //
443 // /** Can be null */
444 // private ConfigurationAdmin findConfigurationAdmin() {
445 // // configurationAdmin =
446 // // bc.getServiceReference(ConfigurationAdmin.class);
447 // // if (configurationAdmin == null) {
448 // // return null;
449 // // }
450 // // return bc.getService(configurationAdmin);
451 // return configurationAdmin.getService();
452 // }
453 //
454 // /** Can be null */
455 // Bundle findBundle(String symbolicName) {
456 // for (Bundle b : bc.getBundles())
457 // if (b.getSymbolicName().equals(symbolicName))
458 // return b;
459 // return null;
460 // }
461 //
462 // private void initTransactionManager() {
463 // bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
464 // tmConf.setServerId(getFrameworkProp(FRAMEWORK_UUID));
465 //
466 // // File tmBaseDir = new File(getFrameworkProp(TRANSACTIONS_HOME,
467 // // getOsgiInstancePath(DIR_TRANSACTIONS)));
468 // Bundle bitronixBundle = FrameworkUtil.getBundle(bitronix.tm.Configuration.class);
469 // File tmBaseDir = bitronixBundle.getDataFile(DIR_TRANSACTIONS);
470 // // File tmBaseDir = bc.getDataFile(DIR_TRANSACTIONS);
471 // File tmDir1 = new File(tmBaseDir, "btm1");
472 // tmDir1.mkdirs();
473 // tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog").getAbsolutePath());
474 // File tmDir2 = new File(tmBaseDir, "btm2");
475 // tmDir2.mkdirs();
476 // tmConf.setLogPart2Filename(new File(tmDir2, tmDir2.getName() + ".tlog").getAbsolutePath());
477 // transactionManager = getTransactionManager();
478 // transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
479 // }
480 //
481 // private void initWebServer(final ConfigurationAdmin conf) {
482 // String httpPort = getFrameworkProp("org.osgi.service.http.port");
483 // String httpsPort = getFrameworkProp("org.osgi.service.http.port.secure");
484 // try {
485 // if (httpPort != null || httpsPort != null) {
486 // final Hashtable<String, Object> jettyProps = new Hashtable<String, Object>();
487 // if (httpPort != null) {
488 // jettyProps.put(JettyConstants.HTTP_PORT, httpPort);
489 // jettyProps.put(JettyConstants.HTTP_ENABLED, true);
490 // }
491 // if (httpsPort != null) {
492 // jettyProps.put(JettyConstants.HTTPS_PORT, httpsPort);
493 // jettyProps.put(JettyConstants.HTTPS_ENABLED, true);
494 // jettyProps.put(JettyConstants.SSL_KEYSTORETYPE, "PKCS12");
495 // jettyProps.put(JettyConstants.SSL_KEYSTORE,
496 // nodeSecurity.getHttpServerKeyStore().getCanonicalPath());
497 // jettyProps.put(JettyConstants.SSL_PASSWORD, "changeit");
498 // jettyProps.put(JettyConstants.SSL_WANTCLIENTAUTH, true);
499 // }
500 // if (conf != null) {
501 // // TODO make filter more generic
502 // String filter = "(" + JettyConstants.HTTP_PORT + "=" + httpPort + ")";
503 // if (conf.listConfigurations(filter) != null)
504 // return;
505 // Configuration jettyConf = conf.createFactoryConfiguration(JETTY_FACTORY_PID, null);
506 // jettyConf.update(jettyProps);
507 //
508 // } else {
509 // JettyConfigurator.startServer("default", jettyProps);
510 // }
511 // }
512 // } catch (Exception e) {
513 // throw new CmsException("Cannot initialize web server on " + httpPortsMsg(httpPort, httpsPort), e);
514 // }
515 // }
516 //
517 // private void publish() {
518 //
519 // // Logging
520 // loggerReg = bc.registerService(ArgeoLogger.class, logger, null);
521 // // Transaction
522 // tmReg = bc.registerService(TransactionManager.class, transactionManager, null);
523 // utReg = bc.registerService(UserTransaction.class, transactionManager, null);
524 // tsrReg = bc.registerService(TransactionSynchronizationRegistry.class, transactionSynchronizationRegistry, null);
525 // // User admin
526 // // userAdminReg = bc.registerService(UserAdmin.class, userAdmin,
527 // // userAdmin.currentState());
528 // // JCR
529 // // Hashtable<String, String> regProps = new Hashtable<String, String>();
530 // // regProps.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
531 // // ArgeoJcrConstants.ALIAS_NODE);
532 // // repositoryReg = (ServiceRegistration<? extends Repository>)
533 // // bc.registerService(Repository.class, repository,
534 // // regProps);
535 // // repositoryFactoryReg = bc.registerService(RepositoryFactory.class,
536 // // repositoryFactory, null);
537 // }
538 //
539 // void destroy() {
540 // long begin = System.currentTimeMillis();
541 // unpublish();
542 //
543 // kernelThread.destroyAndJoin();
544 //
545 // if (dataHttp != null)
546 // dataHttp.destroy();
547 // if (nodeHttp != null)
548 // nodeHttp.destroy();
549 // if (userAdmin != null)
550 // userAdmin.destroy();
551 // // if (repository != null)
552 // // repository.shutdown();
553 // if (transactionManager != null)
554 // transactionManager.shutdown();
555 //
556 // // bc.removeServiceListener(this);
557 //
558 // // Clean hanging threads from Jackrabbit
559 // TransientFileFactory.shutdown();
560 //
561 // // Clean hanging Gogo shell thread
562 // new GogoShellKiller().start();
563 //
564 // nodeSecurity.destroy();
565 // long duration = System.currentTimeMillis() - begin;
566 // log.info("## ARGEO CMS DOWN in " + (duration / 1000) + "." + (duration % 1000) + "s ##");
567 // }
568 //
569 // private void unpublish() {
570 // userAdminReg.unregister();
571 // repositoryFactoryReg.unregister();
572 // repositoryReg.unregister();
573 // tmReg.unregister();
574 // utReg.unregister();
575 // tsrReg.unregister();
576 // loggerReg.unregister();
577 // }
578 //
579 // // @Override
580 // // public void serviceChanged(ServiceEvent event) {
581 // // ServiceReference<?> sr = event.getServiceReference();
582 // // Object service = bc.getService(sr);
583 // // if (service instanceof Repository) {
584 // // Object jcrRepoAlias =
585 // // sr.getProperty(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS);
586 // // if (jcrRepoAlias != null) {// JCR repository
587 // // String alias = jcrRepoAlias.toString();
588 // // Repository repository = (Repository) bc.getService(sr);
589 // // Map<String, Object> props = new HashMap<String, Object>();
590 // // for (String key : sr.getPropertyKeys())
591 // // props.put(key, sr.getProperty(key));
592 // // if (ServiceEvent.REGISTERED == event.getType()) {
593 // // try {
594 // // // repositoryFactory.register(repository, props);
595 // // dataHttp.registerRepositoryServlets(alias, repository);
596 // // } catch (Exception e) {
597 // // throw new CmsException("Could not publish JCR repository " + alias, e);
598 // // }
599 // // } else if (ServiceEvent.UNREGISTERING == event.getType()) {
600 // // // repositoryFactory.unregister(repository, props);
601 // // dataHttp.unregisterRepositoryServlets(alias);
602 // // }
603 // // }
604 // // }
605 // // // else if (service instanceof ExtendedHttpService) {
606 // // // if (ServiceEvent.REGISTERED == event.getType()) {
607 // // // addHttpService(sr);
608 // // // } else if (ServiceEvent.UNREGISTERING == event.getType()) {
609 // // // dataHttp.destroy();
610 // // // dataHttp = null;
611 // // // }
612 // // // }
613 // // }
614 //
615 // private HttpService addHttpService(ServiceReference<HttpService> sr) {
616 // // for (String key : sr.getPropertyKeys())
617 // // log.debug(key + "=" + sr.getProperty(key));
618 // HttpService httpService = bc.getService(sr);
619 // // TODO find constants
620 // Object httpPort = sr.getProperty("http.port");
621 // Object httpsPort = sr.getProperty("https.port");
622 // dataHttp = new DataHttp(httpService);
623 // nodeHttp = new NodeHttp(httpService, bc);
624 // if (log.isDebugEnabled())
625 // log.debug(httpPortsMsg(httpPort, httpsPort));
626 // return httpService;
627 // }
628 //
629 // private String httpPortsMsg(Object httpPort, Object httpsPort) {
630 // return "HTTP " + httpPort + (httpsPort != null ? " - HTTPS " + httpsPort : "");
631 // }
632 //
633 // @Override
634 // public Locale getDefaultLocale() {
635 // return defaultLocale;
636 // }
637 //
638 // /** Can be null. */
639 // @Override
640 // public List<Locale> getLocales() {
641 // return locales;
642 // }
643 //
644 // final private static void directorsCut(long initDuration) {
645 // // final long ms = 128l + (long) (Math.random() * 128d);
646 // long ms = initDuration / 100;
647 // log.info("Spend " + ms + "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
648 // long beginNano = System.nanoTime();
649 // try {
650 // Thread.sleep(ms, 0);
651 // } catch (InterruptedException e) {
652 // // silent
653 // }
654 // long durationNano = System.nanoTime() - beginNano;
655 // final double M = 1000d * 1000d;
656 // double sleepAccuracy = ((double) durationNano) / (ms * M);
657 // if (log.isDebugEnabled())
658 // log.debug("Sleep accuracy: " + String.format("%.2f", 100 - (sleepAccuracy * 100 - 100)) + " %");
659 // }
660 //
661 // private class PrepareStc<T> implements ServiceTrackerCustomizer<T, T> {
662 //
663 // @Override
664 // public T addingService(ServiceReference<T> reference) {
665 // T service = bc.getService(reference);
666 // System.out.println("addingService " + service);
667 // return service;
668 // }
669 //
670 // @Override
671 // public void modifiedService(ServiceReference<T> reference, T service) {
672 // System.out.println("modifiedService " + service);
673 // }
674 //
675 // @Override
676 // public void removedService(ServiceReference<T> reference, T service) {
677 // System.out.println("removedService " + service);
678 // }
679 //
680 // }
681 //
682 // private class PrepareHttpStc implements ServiceTrackerCustomizer<HttpService, HttpService> {
683 //
684 // @Override
685 // public HttpService addingService(ServiceReference<HttpService> reference) {
686 // HttpService httpService = addHttpService(reference);
687 // return httpService;
688 // }
689 //
690 // @Override
691 // public void modifiedService(ServiceReference<HttpService> reference, HttpService service) {
692 // }
693 //
694 // @Override
695 // public void removedService(ServiceReference<HttpService> reference, HttpService service) {
696 // dataHttp.destroy();
697 // dataHttp = null;
698 // }
699 //
700 // }
701 //
702 // /** Workaround for blocking Gogo shell by system shutdown. */
703 // private class GogoShellKiller extends Thread {
704 //
705 // public GogoShellKiller() {
706 // super("Gogo shell killer");
707 // setDaemon(true);
708 // }
709 //
710 // @Override
711 // public void run() {
712 // ThreadGroup rootTg = getRootThreadGroup(null);
713 // Thread gogoShellThread = findGogoShellThread(rootTg);
714 // if (gogoShellThread == null)
715 // return;
716 // while (getNonDaemonCount(rootTg) > 2) {
717 // try {
718 // Thread.sleep(100);
719 // } catch (InterruptedException e) {
720 // // silent
721 // }
722 // }
723 // gogoShellThread = findGogoShellThread(rootTg);
724 // if (gogoShellThread == null)
725 // return;
726 // System.exit(0);
727 // }
728 // }
729 //
730 // private static ThreadGroup getRootThreadGroup(ThreadGroup tg) {
731 // if (tg == null)
732 // tg = Thread.currentThread().getThreadGroup();
733 // if (tg.getParent() == null)
734 // return tg;
735 // else
736 // return getRootThreadGroup(tg.getParent());
737 // }
738 //
739 // private static int getNonDaemonCount(ThreadGroup rootThreadGroup) {
740 // Thread[] threads = new Thread[rootThreadGroup.activeCount()];
741 // rootThreadGroup.enumerate(threads);
742 // int nonDameonCount = 0;
743 // for (Thread t : threads)
744 // if (t != null && !t.isDaemon())
745 // nonDameonCount++;
746 // return nonDameonCount;
747 // }
748 //
749 // private static Thread findGogoShellThread(ThreadGroup rootThreadGroup) {
750 // Thread[] threads = new Thread[rootThreadGroup.activeCount()];
751 // rootThreadGroup.enumerate(threads, true);
752 // for (Thread thread : threads) {
753 // if (thread.getName().equals("Gogo shell"))
754 // return thread;
755 // }
756 // return null;
757 // }
758
759 }