PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/net/oneandone/shared/artifactory/App.java

https://gitlab.com/mfriedenhagen/ono-artifactory-shared
Java | 99 lines | 68 code | 12 blank | 19 comment | 1 complexity | 788f441dd524c8ae1676ca8d3c42f309 MD5 | raw file
  1. /**
  2. * Copyright 1&1 Internet AG, https://github.com/1and1/
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package net.oneandone.shared.artifactory;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Injector;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URI;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.LogManager;
  26. import javax.inject.Inject;
  27. import javax.inject.Named;
  28. import net.oneandone.shared.artifactory.model.ArtifactoryStorage;
  29. import net.oneandone.shared.artifactory.model.Gav;
  30. import org.apache.commons.cli.BasicParser;
  31. import org.apache.commons.cli.CommandLine;
  32. import org.apache.commons.cli.Options;
  33. import org.apache.commons.cli.ParseException;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. /**
  37. * Hello world!
  38. *
  39. */
  40. public class App {
  41. private final static Logger LOG = LoggerFactory.getLogger(App.class);
  42. private final static URI DEFAULT_ARTIFACTORY_URI = URI.create("http://localhost:8081/artifactory/");
  43. private final PreemptiveRequestInterceptor preemptiveRequestInterceptor;
  44. private static void initLogging() {
  45. final InputStream resourceAsStream = App.class.getResourceAsStream("/logging.properties");
  46. try {
  47. try {
  48. LogManager.getLogManager().readConfiguration(resourceAsStream);
  49. } finally {
  50. resourceAsStream.close();
  51. }
  52. } catch (IOException ex) {
  53. throw new RuntimeException(ex);
  54. } catch (SecurityException ex) {
  55. throw new RuntimeException(ex);
  56. }
  57. }
  58. public static void main(String[] args) throws ParseException, IOException, NotFoundException {
  59. initLogging();
  60. LOG.info("CLI: {}", Arrays.toString(args));
  61. final Options options = new Options()
  62. .addOption("l", "uri", true, "Base-URI in the form of " + DEFAULT_ARTIFACTORY_URI)
  63. .addOption("u", "user", true, "Username")
  64. .addOption("p", "password", true, "Password")
  65. .addOption("d", "debug", false, "Turn on debugging");
  66. final CommandLine commandline = new BasicParser().parse(options, args);
  67. if (commandline.hasOption("d")) {
  68. LOG.info("Setting debug");
  69. java.util.logging.Logger.getLogger("net.oneandone.shared.artifactory").setLevel(Level.ALL);
  70. }
  71. final List<String> argList = commandline.getArgList();
  72. LOG.info("ARGS: {}", argList);
  73. Injector injector = Guice.createInjector(new ArtifactoryModule());
  74. App instance = injector.getInstance(App.class);
  75. instance.preemptiveRequestInterceptor.addCredentialsForHost("web.de", "foo", "bar");
  76. List<ArtifactoryStorage> search = instance.searchByGav.search("repo1-cache", Gav.valueOf(argList.get(0)));
  77. LOG.info("Got {} search results", search.size());
  78. }
  79. private final SearchByGav searchByGav;
  80. @Inject
  81. public App(
  82. @Named("preemptiveRequestInterceptor") PreemptiveRequestInterceptor preemptiveRequestInterceptor,
  83. @Named("searchByGav") SearchByGav searchByGav) {
  84. this.preemptiveRequestInterceptor = preemptiveRequestInterceptor;
  85. this.searchByGav = searchByGav;
  86. }
  87. }