29166b0b3b8dd6e91eba76931687b3a0d16759b4
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / server / AbsServiceStarter.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21 package org.onap.aaf.auth.server;
22 import org.onap.aaf.auth.org.OrganizationException;
23 import org.onap.aaf.auth.org.OrganizationFactory;
24 import org.onap.aaf.auth.rserv.RServlet;
25 import org.onap.aaf.cadi.Access;
26 import org.onap.aaf.cadi.register.Registrant;
27 import org.onap.aaf.cadi.register.Registrar;
28 import org.onap.aaf.misc.env.Trans;
29 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
30
31 public abstract class AbsServiceStarter<ENV extends RosettaEnv, TRANS extends Trans> implements ServiceStarter {
32     private Registrar<ENV> registrar;
33     private boolean do_register;
34     protected AbsService<ENV,TRANS> service;
35
36
37     public AbsServiceStarter(final AbsService<ENV,TRANS> service) {
38         this.service = service;
39         try {
40             OrganizationFactory.init(service.env);
41         } catch (OrganizationException e) {
42             service.access.log(e, "Missing defined Organzation Plugins");
43             System.exit(3);
44         }
45         // do_register - this is used for specialty Debug Situations.  Developer can create an Instance for a remote system
46         // for Debugging purposes without fear that real clients will start to call your debug instance
47         do_register = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null));
48         _propertyAdjustment();
49     }
50     
51     public abstract void _start(RServlet<TRANS> rserv) throws Exception;
52     public abstract void _propertyAdjustment();
53     
54     public ENV env() {
55         return service.env;
56     }
57     
58     public Access access() {
59         return service.access;
60     }
61
62     @Override
63     public final void start() throws Exception {
64         _start(service);
65         Runtime.getRuntime().addShutdownHook(new Thread() {
66             @Override
67             public void run() {
68                 shutdown();
69             }
70         });
71     }
72
73     @SafeVarargs
74     public final synchronized void register(final Registrant<ENV> ... registrants) {
75         if (do_register) {
76             if (registrar==null) {
77                 registrar = new Registrar<ENV>(env(),false);
78             }
79             for (Registrant<ENV> r : registrants) {
80                 registrar.register(r);
81             }
82         }
83     }
84
85     @Override
86     public void shutdown() {
87         if (registrar!=null) {
88             registrar.close(env());
89             registrar=null;
90         } 
91         if (service!=null) {
92             service.destroy();
93         }
94     }
95 }