X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fserver%2FAbsServiceStarter.java;h=90f4158fe1088247a8299aa8e0f6385d30dfca97;hb=3d1706fcbe7f95830ff6fd23cf679ee55c6d0595;hp=1a6c54d774b399e012cd192a82a371e6ba13bbb7;hpb=71037c39a37d3549dcfe31926832a657744fbe05;p=aaf%2Fauthz.git diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java index 1a6c54d7..90f4158f 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java @@ -19,77 +19,148 @@ * */ package org.onap.aaf.auth.server; +import java.io.File; +import java.io.IOException; +import java.net.Inet4Address; +import java.net.UnknownHostException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.auth.org.OrganizationFactory; import org.onap.aaf.auth.rserv.RServlet; import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.Access.Level; +import org.onap.aaf.cadi.config.Config; import org.onap.aaf.cadi.register.Registrant; import org.onap.aaf.cadi.register.Registrar; import org.onap.aaf.misc.env.Trans; import org.onap.aaf.misc.rosetta.env.RosettaEnv; public abstract class AbsServiceStarter implements ServiceStarter { - private Registrar registrar; - private boolean do_register; - protected AbsService service; + private Registrar registrar; + private boolean do_register; + protected AbsService service; + protected String hostname; + protected final boolean secure; - public AbsServiceStarter(final AbsService service) { - this.service = service; - try { - OrganizationFactory.init(service.env); - } catch (OrganizationException e) { - service.access.log(e, "Missing defined Organzation Plugins"); - System.exit(3); - } - // do_register - this is used for specialty Debug Situations. Developer can create an Instance for a remote system - // for Debugging purposes without fear that real clients will start to call your debug instance - do_register = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null)); - _propertyAdjustment(); - } - - public abstract void _start(RServlet rserv) throws Exception; - public abstract void _propertyAdjustment(); - - public ENV env() { - return service.env; - } - - public Access access() { - return service.access; - } + public AbsServiceStarter(final AbsService service, boolean secure) { + this.secure = secure; + this.service = service; + try { + OrganizationFactory.init(service.env); + } catch (OrganizationException e) { + service.access.log(e, "Missing defined Organization Plugins"); + System.exit(3); + } + // do_register - this is used for specialty Debug Situations. Developer can create an Instance for a remote system + // for Debugging purposes without fear that real clients will start to call your debug instance + do_register = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null)); + hostname = access().getProperty(Config.HOSTNAME, null); + if (hostname==null) { + try { + hostname = Inet4Address.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + hostname= "cannotBeDetermined"; + } + } + _propertyAdjustment(); + } + + + protected abstract void _start(RServlet rserv) throws Exception; + protected abstract void _propertyAdjustment(); + + public ENV env() { + return service.env; + } + + public Access access() { + return service.access; + } - @Override - public final void start() throws Exception { - _start(service); - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - shutdown(); - } - }); - } + @Override + public final void start() throws Exception { + ExecutorService es = Executors.newSingleThreadExecutor(); + Future app = es.submit(this); + final AbsServiceStarter absSS = this; + // Docker/K8 may separately create startup Status in this dir for startup + // sequencing. If so, delete ON EXIT + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + absSS.access().printf(Level.INIT, "Shutting down %s:%s\n",absSS.service.app_name, absSS.service.app_version); + absSS.shutdown(); + app.cancel(true); + } + }); + if(System.getProperty("ECLIPSE", null)!=null) { + Thread.sleep(2000); + if(!app.isCancelled()) { + System.out.println("Service Started in Eclipse: "); + System.out.print(" Hit to end:\n"); + try { + System.in.read(); + System.exit(0); + } catch (IOException e) { + } + } + } + } + + @SafeVarargs + public final synchronized void register(final Registrant ... registrants) { + if (do_register) { + if (registrar==null) { + registrar = new Registrar(env(),false); + } + for (Registrant r : registrants) { + registrar.register(r); + } + } + } - @SafeVarargs - public final synchronized void register(final Registrant ... registrants) { - if(do_register) { - if(registrar==null) { - registrar = new Registrar(env(),false); - } - for(Registrant r : registrants) { - registrar.register(r); - } - } - } + @Override + public void run() { + try { + _start(service); + } catch (Exception e) { + e.printStackTrace(); + shutdown(); + } + } - @Override + @Override public void shutdown() { - if(registrar!=null) { - registrar.close(env()); - registrar=null; - } - if(service!=null) { - service.destroy(); - } + if (registrar!=null) { + registrar.close(env()); + registrar=null; + } + if (service!=null) { + File status = new File("/opt/app/aaf/status/"); + boolean deleted = false; + if(status.exists()) { + int lastdot = service.app_name.lastIndexOf("aaf."); + String fname; + if(lastdot<0) { + fname = service.app_name + '-' + hostname; + } else { + fname = service.app_name.substring(lastdot).replace('.', '-') + + '-' + hostname; + } + status = new File(status, fname); + if(status.exists()) { + deleted=status.delete(); + } + } + if(deleted) { + service.access.log(Level.INIT, "Deleted Status",status.getAbsolutePath()); + } else { + service.access.log(Level.INIT, "Status not deleted: ",status.getAbsolutePath()); + } + service.destroy(); + } } }