adjust to use password-service
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / web / env / KarafIniWebEnvironment.java
1 /*
2  * Copyright (c) 2015 - 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.shiro.web.env;
9
10 import java.util.concurrent.ExecutionException;
11 import java.util.function.Supplier;
12 import org.apache.shiro.config.Ini;
13 import org.apache.shiro.web.env.IniWebEnvironment;
14 import org.opendaylight.aaa.AAAShiroProvider;
15 import org.opendaylight.yangtools.util.ClassLoaderUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Identical to <code>IniWebEnvironment</code> except the Ini is loaded from
21  * <code>${KARAF_HOME}/etc/shiro.ini</code>, and setting the TCCL (x2) so that
22  * loading of classes by name (from aaa-app-config.xml) works even with
23  * ShiroWebContextSecurer.
24  *
25  * @deprecated in favor of {@link AAAIniWebEnvironment}. This class is kept for compatibility for other projects that
26  *             reference it in a web.xml file where it's instantiated via reflection and the dependencies must be
27  *             accessed statically.
28  */
29 @Deprecated
30 public class KarafIniWebEnvironment extends IniWebEnvironment {
31
32     private static final Logger LOG = LoggerFactory.getLogger(KarafIniWebEnvironment.class);
33
34     public KarafIniWebEnvironment() {
35         LOG.info("Initializing the Web Environment using {}", KarafIniWebEnvironment.class.getName());
36     }
37
38     @Override
39     public void init() {
40         try {
41             AAAShiroProvider provider = AAAShiroProvider.INSTANCE_FUTURE.get();
42
43             ThreadLocals.DATABROKER_TL.set(provider.getDataBroker());
44             ThreadLocals.CERT_MANAGER_TL.set(provider.getCertificateManager());
45             ThreadLocals.AUTH_SETVICE_TL.set(provider.getAuthenticationService());
46             ThreadLocals.TOKEN_AUTHENICATORS_TL.set(provider.getTokenAuthenticators());
47             ThreadLocals.TOKEN_STORE_TL.set(provider.getTokenStore());
48             ThreadLocals.PASSWORD_HASH_SERVICE_TL.set(provider.getPasswordHashService());
49
50             // Initialize the Shiro environment from clustered-app-config
51             final Ini ini = AAAIniWebEnvironment.createIniFromClusteredAppConfig(provider.getShiroConfiguration());
52             setIni(ini);
53             ClassLoaderUtils.withClassLoader(KarafIniWebEnvironment.class.getClassLoader(), (Supplier<Void>) () -> {
54                 super.init();
55                 return null;
56             });
57         } catch (InterruptedException | ExecutionException e) {
58             throw new RuntimeException("Error obtaining AAAShiroProvider", e);
59         } finally {
60             ThreadLocals.DATABROKER_TL.remove();
61             ThreadLocals.CERT_MANAGER_TL.remove();
62             ThreadLocals.AUTH_SETVICE_TL.remove();
63             ThreadLocals.TOKEN_AUTHENICATORS_TL.remove();
64             ThreadLocals.TOKEN_STORE_TL.remove();
65             ThreadLocals.PASSWORD_HASH_SERVICE_TL.remove();
66         }
67     }
68 }