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