1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33
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 }