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