Sonar Fixes: Variable name changes
[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 java.io.File;
23 import java.io.IOException;
24 import java.net.Inet4Address;
25 import java.net.UnknownHostException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29
30 import org.onap.aaf.auth.org.OrganizationException;
31 import org.onap.aaf.auth.org.OrganizationFactory;
32 import org.onap.aaf.auth.rserv.RServlet;
33 import org.onap.aaf.cadi.Access;
34 import org.onap.aaf.cadi.Access.Level;
35 import org.onap.aaf.cadi.config.Config;
36 import org.onap.aaf.cadi.register.Registrant;
37 import org.onap.aaf.cadi.register.Registrar;
38 import org.onap.aaf.misc.env.Trans;
39 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
40
41 public abstract class AbsServiceStarter<ENV extends RosettaEnv, TRANS extends Trans> implements ServiceStarter {
42     private Registrar<ENV> registrar;
43     private boolean doRegister;
44     protected AbsService<ENV,TRANS> service;
45     protected String hostname;
46     protected final boolean secure;
47
48
49     public AbsServiceStarter(final AbsService<ENV,TRANS> service, boolean secure) {
50         this.secure = secure;
51         this.service = service;
52         try {
53             OrganizationFactory.init(service.env);
54         } catch (OrganizationException e) {
55             service.access.log(e, "Missing defined Organization Plugins");
56                System.exit(3);
57         }
58         // do_register - this is used for specialty Debug Situations.  Developer can create an Instance for a remote system
59         // for Debugging purposes without fear that real clients will start to call your debug instance
60         doRegister = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null));
61         hostname = access().getProperty(Config.HOSTNAME, null);
62         if (hostname==null) {
63             try {
64                 hostname = Inet4Address.getLocalHost().getHostName();
65             } catch (UnknownHostException e) {
66                 hostname= "cannotBeDetermined";
67             }
68         }
69         _propertyAdjustment();
70     }
71
72
73     protected abstract void _start(RServlet<TRANS> rserv) throws Exception;
74     protected abstract void _propertyAdjustment();
75
76     public ENV env() {
77         return service.env;
78     }
79
80     public Access access() {
81         return service.access;
82     }
83
84     @Override
85     public final void start() throws Exception {
86         ExecutorService es = Executors.newSingleThreadExecutor();
87         Future<?> app = es.submit(this);
88         final AbsServiceStarter<?,?> absSS = this;
89         // Docker/K8 may separately create startup Status in this dir for startup
90         // sequencing.  If so, delete ON EXIT
91         Runtime.getRuntime().addShutdownHook(new Thread() {
92           @Override
93           public void run() {
94               absSS.access().printf(Level.INIT, "Shutting down %s:%s\n",absSS.service.appName, absSS.service.appVersion);
95               absSS.shutdown();
96               app.cancel(true);
97           }
98         });
99         if(System.getProperty("ECLIPSE", null)!=null) {
100             Thread.sleep(2000);
101             if(!app.isCancelled()) {
102                 System.out.println("Service Started in Eclipse: ");
103                 System.out.print("  Hit <enter> to end:\n");
104                 try {
105                     System.in.read();
106                     System.exit(0);
107                 } catch (IOException e) {
108                 }
109             }
110         }
111     }
112
113     @SafeVarargs
114     public final synchronized void register(final Registrant<ENV> ... registrants) {
115         if (doRegister) {
116             if (registrar==null) {
117                 registrar = new Registrar<ENV>(env(),false);
118             }
119             for (Registrant<ENV> r : registrants) {
120                 registrar.register(r);
121             }
122         }
123     }
124
125     @Override
126     public void run() {
127         try {
128             _start(service);
129         } catch (Exception e) {
130             e.printStackTrace();
131             shutdown();
132         }
133     }
134
135     @Override
136     public void shutdown() {
137         if (registrar!=null) {
138             registrar.close(env());
139             registrar=null;
140         }
141         if (service!=null) {
142             File status = new File("/opt/app/aaf/status/");
143             boolean deleted = false;
144             if(status.exists()) {
145                 int lastdot = service.appName.lastIndexOf("aaf.");
146                 String fname;
147                 if(lastdot<0) {
148                     fname = service.appName + '-' + hostname;
149                 } else {
150                     fname = service.appName.substring(lastdot).replace('.', '-')
151                             + '-' + hostname;
152                 }
153                 status = new File(status, fname);
154                 if(status.exists()) {
155                     deleted=status.delete();
156                 }
157             }
158             if(deleted) {
159                 service.access.log(Level.INIT, "Deleted Status",status.getAbsolutePath());
160             } else {
161                 service.access.log(Level.INIT, "Status not deleted: ",status.getAbsolutePath());
162             }
163             service.destroy();
164         }
165     }
166 }