View Javadoc
1   package net.sumaris.core.service;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Sumaris Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import net.sumaris.core.service.schema.DatabaseSchemaService;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.BeanFactory;
31  import org.springframework.context.ApplicationContext;
32  /*import org.springframework.beans.factory.access.BeanFactoryLocator;
33  import org.springframework.beans.factory.access.BeanFactoryReference;
34  import org.springframework.context.ApplicationContext;
35  import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
36  import org.springframework.context.support.AbstractApplicationContext;*/
37  
38  import java.io.Closeable;
39  
40  /**
41   * Locates and provides all available application services.
42   */
43  public class ServiceLocator implements Closeable {
44  
45      //@Autowired
46      //private ApplicationContext appContext;
47  
48      /* Logger */
49      private static final Logger log = LoggerFactory.getLogger(ServiceLocator.class);
50  
51      /**
52       * The core instance of this ServiceLocator.
53       */
54      private static ServiceLocatorr.html#ServiceLocator">ServiceLocator instance = new ServiceLocator();
55  
56      /**
57       * Indicates if the spring context is open or not.
58       */
59      private boolean open = false;
60  
61      /**
62       * <p>Constructor for ServiceLocator.</p>
63       */
64      protected ServiceLocator() {
65      }
66  
67      /**
68       * replace the default core instance of this Class
69       *
70       * @param newInstance the new core service locator instance.
71       */
72      public static void setInstance(ServiceLocator newInstance) {
73          instance = newInstance;
74      }
75  
76      /**
77       * Gets the core instance of this Class
78       *
79       * @return the core service locator instance.
80       */
81      public static ServiceLocator instance() {
82          return instance;
83      }
84  
85      /**
86       * The bean factory reference instance.
87       */
88      private ApplicationContext applicationContext;
89  
90      /**
91       * <p>initDefault.</p>
92       */
93      public static void init(ApplicationContext applicationContext) {
94          instance.setApplicationContext(applicationContext);
95          ServiceLocator.setInstance(instance);
96      }
97  
98      /**
99       * Shuts down the ServiceLocator and releases any used resources.
100      */
101     public synchronized void shutdown() {
102         // Do not try to close if not already opened
103         if (!open) {
104             return;
105         }
106         if (log.isDebugEnabled()) {
107             log.debug("Close Spring application context");
108         }
109 
110         // applicationContext.close()
111 
112         open = false;
113     }
114 
115     /**
116      * Get a service.
117      *
118      * @param name        name of the service (i.e name of the spring bean)
119      * @param serviceType type of service
120      * @param <S>         type of the service
121      * @return the instantiated service
122      */
123     public <S> S getService(String name, Class<S> serviceType) {
124 
125         return applicationContext.getBean(name, serviceType);
126     }
127 
128     /**
129      * <p>isOpen.</p>
130      *
131      * @return {@code true} if spring context is open, {@code false} otherwise.
132      * @since 3.5.2
133      */
134     public boolean isOpen() {
135         return open;
136     }
137 
138     /**
139      * Gets the Spring ApplicationContext.
140      *
141      * @return a {@link ApplicationContext} object.
142      */
143     protected synchronized ApplicationContext getContext() {
144         return this.applicationContext;
145     }
146 
147 
148     protected synchronized void setApplicationContext(ApplicationContext applicationContext) {
149         this.applicationContext = applicationContext;
150     }
151 
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public void close() {
158         shutdown();
159     }
160 
161     /**
162      * <p>getDatabaseSchemaService.</p>
163      *
164      * @return a {@link DatabaseSchemaService} object.
165      */
166     public DatabaseSchemaService getDatabaseSchemaService() {
167         return getService("databaseSchemaService", DatabaseSchemaService.class);
168     }
169 }