Add Batch in Config image
[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.IOException;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.TimeUnit;
27
28 import org.onap.aaf.auth.org.OrganizationException;
29 import org.onap.aaf.auth.org.OrganizationFactory;
30 import org.onap.aaf.auth.rserv.RServlet;
31 import org.onap.aaf.cadi.Access;
32 import org.onap.aaf.cadi.Access.Level;
33 import org.onap.aaf.cadi.register.Registrant;
34 import org.onap.aaf.cadi.register.Registrar;
35 import org.onap.aaf.misc.env.Trans;
36 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
37
38 public abstract class AbsServiceStarter<ENV extends RosettaEnv, TRANS extends Trans> implements ServiceStarter {
39     private Registrar<ENV> registrar;
40     private boolean do_register;
41     protected AbsService<ENV,TRANS> service;
42
43
44     public AbsServiceStarter(final AbsService<ENV,TRANS> service, boolean noexit) {
45         this.service = service;
46         try {
47             OrganizationFactory.init(service.env);
48         } catch (OrganizationException e) {
49             service.access.log(e, "Missing defined Organization Plugins");
50             if(!noexit) {
51                 System.exit(3);
52             }
53         }
54         // do_register - this is used for specialty Debug Situations.  Developer can create an Instance for a remote system
55         // for Debugging purposes without fear that real clients will start to call your debug instance
56         do_register = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null));
57         _propertyAdjustment();
58     }
59     
60     public abstract void _start(RServlet<TRANS> rserv) throws Exception;
61     public abstract void _propertyAdjustment();
62     
63     public ENV env() {
64         return service.env;
65     }
66     
67     public Access access() {
68         return service.access;
69     }
70
71     @Override
72     public final void start() throws Exception {
73         ExecutorService es = Executors.newSingleThreadExecutor();
74         Future<?> app = es.submit(this);
75         final AbsServiceStarter<?,?> absSS = this;
76         Runtime.getRuntime().addShutdownHook(new Thread() {
77               @Override
78           public void run() {
79                   absSS.access().printf(Level.INIT, "Shutting down %s:%s\n",absSS.service.app_name, absSS.service.app_version);
80                   absSS.shutdown();
81                   app.cancel(true);
82               }
83         });
84                 if(System.getProperty("ECLIPSE", null)!=null) {
85                         Thread.sleep(2000);
86                 System.out.println("Service Started in Eclipse: ");
87                 System.out.print("  Hit <enter> to end:");
88                 try {
89                                 System.in.read();
90                                 System.exit(0);
91                         } catch (IOException e) {
92                         }
93                 }
94
95     }
96     
97
98     @SafeVarargs
99     public final synchronized void register(final Registrant<ENV> ... registrants) {
100         if (do_register) {
101             if (registrar==null) {
102                 registrar = new Registrar<ENV>(env(),false);
103             }
104             for (Registrant<ENV> r : registrants) {
105                 registrar.register(r);
106             }
107         }
108     }
109
110     @Override
111         public void run() {
112         try {
113                         _start(service);
114                 } catch (Exception e) {
115                         e.printStackTrace();
116                 }
117         }
118
119         @Override
120     public void shutdown() {
121         if (registrar!=null) {
122             registrar.close(env());
123             registrar=null;
124         } 
125         if (service!=null) {
126             service.destroy();
127         }
128     }
129 }