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;
48 public AbsServiceStarter(final AbsService<ENV,TRANS> service) {
49 this.service = service;
51 OrganizationFactory.init(service.env);
52 } catch (OrganizationException e) {
53 service.access.log(e, "Missing defined Organization Plugins");
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);
63 hostname = Inet4Address.getLocalHost().getHostName();
64 } catch (UnknownHostException e) {
65 hostname= "cannotBeDetermined";
70 public abstract void _start(RServlet<TRANS> rserv) throws Exception;
71 public abstract void _propertyAdjustment();
77 public Access access() {
78 return service.access;
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() {
91 absSS.access().printf(Level.INIT, "Shutting down %s:%s\n",absSS.service.app_name, absSS.service.app_version);
96 if(System.getProperty("ECLIPSE", null)!=null) {
98 System.out.println("Service Started in Eclipse: ");
99 System.out.print(" Hit <enter> to end:");
103 } catch (IOException e) {
111 public final synchronized void register(final Registrant<ENV> ... registrants) {
113 if (registrar==null) {
114 registrar = new Registrar<ENV>(env(),false);
116 for (Registrant<ENV> r : registrants) {
117 registrar.register(r);
126 } catch (Exception e) {
132 public void shutdown() {
133 if (registrar!=null) {
134 registrar.close(env());
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.");
144 fname = service.app_name + '-' + hostname;
146 fname = service.app_name.substring(lastdot).replace('.', '-')
149 status = new File(status, fname);
150 if(status.exists()) {
151 deleted=status.delete();
155 service.access.log(Level.INIT, "Deleted Status",status.getAbsolutePath());
157 service.access.log(Level.INIT, "Status not deleted: ",status.getAbsolutePath());