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