View Javadoc

1   /*
2    * Copyright (c) 2015 Maxim Yunusov
3    *    Licensed under the Apache License, Version 2.0 (the "License");
4    *    you may not use this file except in compliance with the License.
5    *    You may obtain a copy of the License at
6    *
7    *        http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *    Unless required by applicable law or agreed to in writing, software
10   *    distributed under the License is distributed on an "AS IS" BASIS,
11   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *    See the License for the specific language governing permissions and
13   *    limitations under the License.
14   */
15  
16  package org.maxur.perfmodel.backend;
17  
18  import org.glassfish.hk2.api.Factory;
19  import org.glassfish.hk2.api.ServiceLocator;
20  import org.jvnet.hk2.annotations.Service;
21  import org.maxur.perfmodel.backend.service.impl.CliApplication;
22  import org.maxur.perfmodel.backend.service.impl.TrayIconApplication;
23  import org.maxur.perfmodel.backend.service.Application;
24  import org.slf4j.Logger;
25  
26  import javax.inject.Inject;
27  
28  import static org.slf4j.LoggerFactory.getLogger;
29  
30  /**
31   * @author myunusov
32   * @version 1.0
33   * @since <pre>01.09.2015</pre>
34   */
35  @Service
36  public class ApplicationProvider implements Factory<Application> {
37  
38      private static final Logger LOGGER = getLogger(ApplicationProvider.class);
39  
40      private Application application;
41  
42      @Inject
43      private ServiceLocator locator;
44  
45      @Override
46      public Application provide() {
47          if (application == null) {
48              application = make();
49              locator.inject(application);
50          }
51          return application;
52      }
53  
54      private Application make() {
55          final Application result = new TrayIconApplication();
56          if (result.isApplicable()) {
57              return result;
58          } else {
59              LOGGER.info("SystemTray is not supported");
60          }
61          return new CliApplication();
62      }
63  
64      @Override
65      public void dispose(Application instance) {
66          instance.stop();
67      }
68  }