Java tutorial
/* * Copyright 2013 Rgis Caspar <regis.caspar@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.rcaspar.fitbitbat; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.support.GenericXmlApplicationContext; import java.util.Arrays; import static com.rcaspar.fitbitbat.services.OAuthService.OAuthException; /** * Entry point. * * @author regis */ @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class App { /** * CLI args. * * @author regis */ private static final class Args { @Parameter(names = { "-p", "--pin" }, required = false, description = "PIN from fitbit") @Getter private String pin; } /** * Program entry point. * * @param args CLI args */ public static void main(final String[] args) { log.debug("main() - args={}", Arrays.asList(args)); final Args arguments = new Args(); try { new JCommander(arguments, args); final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext( "classpath:application-context.xml"); applicationContext.registerShutdownHook(); applicationContext.start(); final Package appPackage = App.class.getPackage(); log.info("=== {} v.{} started ===", appPackage.getImplementationTitle(), appPackage.getImplementationVersion()); final FitBitBat fitBitBat = applicationContext.getBean(FitBitBat.class); if (fitBitBat.checkCredentials(arguments.getPin())) { fitBitBat.startAsync(); } else { log.error("Bad pin: {}", arguments.getPin()); } } catch (final OAuthException ex) { System.err.println(ex.getMessage()); System.exit(1); } catch (final ParameterException ex) { new JCommander(arguments).usage(); System.exit(-1); } catch (final Exception ex) { log.error("Cannot startup", ex); System.exit(-2); } } }