001package co.codewizards.cloudstore.core.version;
002
003import static co.codewizards.cloudstore.core.io.StreamUtil.*;
004import static co.codewizards.cloudstore.core.objectfactory.ObjectFactoryUtil.*;
005import static co.codewizards.cloudstore.core.util.UrlUtil.*;
006
007import java.io.BufferedReader;
008import java.io.IOException;
009import java.io.InputStream;
010import java.io.InputStreamReader;
011import java.net.URL;
012import java.nio.charset.StandardCharsets;
013import java.util.regex.Matcher;
014import java.util.regex.Pattern;
015
016import co.codewizards.cloudstore.core.oio.File;
017
018public class LocalVersionInIdeHelper {
019
020        protected URL resource;
021
022        protected LocalVersionInIdeHelper() {
023        }
024
025        public static LocalVersionInIdeHelper getInstance() {
026                return createObject(LocalVersionInIdeHelper.class);
027        }
028
029        public Version getLocalVersionInIde() {
030                resource = this.getClass().getResource("");
031
032                if (resource.getProtocol().equalsIgnoreCase(PROTOCOL_JAR)) {
033                        return getLocalVersionInIde_jar();
034                } else if (resource.getProtocol().equalsIgnoreCase(PROTOCOL_FILE)) {
035                        return getLocalVersionInIde_file();
036                } else
037                        throw new IllegalStateException("LocalVersionInIdeHelper was not loaded from a local JAR or class file!");
038        }
039
040        protected Version getLocalVersionInIde_jar() {
041                final String pomXmlResourceName = "META-INF/maven/co.codewizards.cloudstore/co.codewizards.cloudstore.core/pom.xml";
042                resource = this.getClass().getClassLoader().getResource(pomXmlResourceName);
043                if (resource == null)
044                        throw new IllegalStateException("Resource not found in JAR: " + pomXmlResourceName);
045
046                try {
047                        try (InputStream pomXmlIn = resource.openStream()) {
048                                return readVersionFromPomXml(pomXmlIn);
049                        }
050                } catch (IOException e) {
051                        throw new RuntimeException(e);
052                }
053        }
054
055        protected Version getLocalVersionInIde_file() {
056                try {
057                        File dir = getFile(resource).getCanonicalFile();
058                        File pomXmlFile;
059                        do {
060                                pomXmlFile = null;
061                                dir = dir.getParentFile();
062                                if (dir == null)
063                                        break;
064
065                                pomXmlFile = dir.createFile("pom.xml");
066                        } while (! pomXmlFile.exists());
067
068                        if (pomXmlFile != null) {
069                                try (InputStream pomXmlIn = castStream(pomXmlFile.createInputStream())) {
070                                        return readVersionFromPomXml(pomXmlIn);
071                                }
072                        }
073                        throw new IllegalStateException("Could not determine local version!");
074                } catch (IOException e) {
075                        throw new RuntimeException(e);
076                }
077        }
078
079        protected Version readVersionFromPomXml(InputStream pomXmlIn) throws IOException {
080                // quick'n'dirty implementation
081                final Pattern pattern = Pattern.compile("<version>([^<]*)</version>");
082                BufferedReader reader = new BufferedReader(new InputStreamReader(pomXmlIn, StandardCharsets.UTF_8));
083                Matcher matcher = null;
084                String line;
085                while (null != (line = reader.readLine())) {
086                        if (matcher == null)
087                                matcher = pattern.matcher(line);
088                        else
089                                matcher.reset(line);
090
091                        if (matcher.find()) {
092                                String versionString = matcher.group(1);
093                                return new Version(versionString);
094                        }
095                }
096                throw new IllegalStateException("Could not find version inside pom.xml!");
097        }
098}