001/* 002 * Copyright (c) 2015 Maxim Yunusov 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. 014 */ 015 016package org.maxur.perfmodel.backend; 017 018import org.glassfish.hk2.api.Factory; 019import org.glassfish.hk2.api.ServiceLocator; 020import org.jvnet.hk2.annotations.Service; 021import org.maxur.perfmodel.backend.service.impl.CliApplication; 022import org.maxur.perfmodel.backend.service.impl.TrayIconApplication; 023import org.maxur.perfmodel.backend.service.Application; 024import org.slf4j.Logger; 025 026import javax.inject.Inject; 027 028import static org.slf4j.LoggerFactory.getLogger; 029 030/** 031 * @author myunusov 032 * @version 1.0 033 * @since <pre>01.09.2015</pre> 034 */ 035@Service 036public class ApplicationProvider implements Factory<Application> { 037 038 private static final Logger LOGGER = getLogger(ApplicationProvider.class); 039 040 private Application application; 041 042 @Inject 043 private ServiceLocator locator; 044 045 @Override 046 public Application provide() { 047 if (application == null) { 048 application = make(); 049 locator.inject(application); 050 } 051 return application; 052 } 053 054 private Application make() { 055 final Application result = new TrayIconApplication(); 056 if (result.isApplicable()) { 057 return result; 058 } else { 059 LOGGER.info("SystemTray is not supported"); 060 } 061 return new CliApplication(); 062 } 063 064 @Override 065 public void dispose(Application instance) { 066 instance.stop(); 067 } 068}