aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/elytrium/limboauth/dependencies/BaseLibrary.java
blob: e8c34b522a8b9da3908e39124922a9bfd4a3ec33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2021 - 2022 Elytrium
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.elytrium.limboauth.dependencies;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public enum BaseLibrary {
  H2_V1(
      "com.h2database",
      "h2",
      "1.4.200"
  ),
  H2_V2(
      "com.h2database",
      "h2",
      "2.1.214"
  ),
  MYSQL(
      "com.mysql",
      "mysql-connector-j",
      "8.0.31"
  ),
  POSTGRESQL(
      "org.postgresql",
      "postgresql",
      "42.5.1"
  ),
  SQLITE(
      "org.xerial",
      "sqlite-jdbc",
      "3.40.0.0"
  );

  private final Path filenamePath;
  private final URL mavenRepoURL;

  BaseLibrary(String groupId, String artifactId, String version) {
    String mavenPath = String.format("%s/%s/%s/%s-%s.jar",
        groupId.replace(".", "/"),
        artifactId,
        version,
        artifactId,
        version
    );

    this.filenamePath = Path.of("libraries/" + mavenPath);

    URL mavenRepoURL = null;
    try {
      mavenRepoURL = new URL("https://repo1.maven.org/maven2/" + mavenPath);
    } catch (Exception e) {
      e.printStackTrace();
    }

    this.mavenRepoURL = mavenRepoURL;
  }

  @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
  public URL getClassLoaderURL() throws MalformedURLException {
    if (!Files.exists(this.filenamePath)) {
      try {
        try (InputStream in = this.mavenRepoURL.openStream()) {
          Files.createDirectories(this.filenamePath.getParent());
          Files.copy(in, Files.createFile(this.filenamePath), StandardCopyOption.REPLACE_EXISTING);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return this.filenamePath.toUri().toURL();
  }
}