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