2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.auth.server;
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;
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;
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 protected final boolean secure;
49 public AbsServiceStarter(final AbsService<ENV,TRANS> service, boolean secure) {
51 this.service = service;
53 OrganizationFactory.init(service.env);
54 } catch (OrganizationException e) {
55 service.access.log(e, "Missing defined Organization Plugins");
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 do_register = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null));
61 hostname = access().getProperty(Config.HOSTNAME, null);
64 hostname = Inet4Address.getLocalHost().getHostName();
65 } catch (UnknownHostException e) {
66 hostname= "cannotBeDetermined";
69 _propertyAdjustment();
73 protected abstract void _start(RServlet<TRANS> rserv) throws Exception;
74 protected abstract void _propertyAdjustment();
80 public Access access() {
81 return service.access;
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() {
94 absSS.access().printf(Level.INIT, "Shutting down %s:%s\n",absSS.service.app_name, absSS.service.app_version);
99 if(System.getProperty("ECLIPSE", null)!=null) {
101 if(!app.isCancelled()) {
102 System.out.println("Service Started in Eclipse: ");
103 System.out.print(" Hit <enter> to end:\n");
107 } catch (IOException e) {
114 public final synchronized void register(final Registrant<ENV> ... registrants) {
116 if (registrar==null) {
117 registrar = new Registrar<ENV>(env(),false);
119 for (Registrant<ENV> r : registrants) {
120 registrar.register(r);
129 } catch (Exception e) {
136 public void shutdown() {
137 if (registrar!=null) {
138 registrar.close(env());
142 File status = new File("/opt/app/aaf/status/");
143 boolean deleted = false;
144 if(status.exists()) {
145 int lastdot = service.app_name.lastIndexOf("aaf.");
148 fname = service.app_name + '-' + hostname;
150 fname = service.app_name.substring(lastdot).replace('.', '-')
153 status = new File(status, fname);
154 if(status.exists()) {
155 deleted=status.delete();
159 service.access.log(Level.INIT, "Deleted Status",status.getAbsolutePath());
161 service.access.log(Level.INIT, "Status not deleted: ",status.getAbsolutePath());