package com.ulisesbocchio.jasyptmavenplugin.mojo;

import com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties;
import com.ulisesbocchio.jasyptspringboot.util.AsymmetricCryptography;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.springframework.core.env.Environment;

@Mojo(name = "upgrade", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class UpgradeMojo extends AbstractReencryptMojo {
    @Parameter(property = "jasypt.plugin.old.major-version", defaultValue = "2")
    private int oldMajorVersion = 2;

    @Override
    protected void configure(JasyptEncryptorConfigurationProperties properties) {
        if (oldMajorVersion == 2) {
            upgradeFrom2(properties);
        } else {
            throw new RuntimeException("Unrecognised major version " + oldMajorVersion);
        }
    }

    private void upgradeFrom2(JasyptEncryptorConfigurationProperties properties) {
        Environment environment = getEnvironment();

        setIfNotNull(properties::setPassword, environment.getProperty("jasypt.encryptor.password"));
        setIfNotNull(properties::setPrivateKeyFormat, environment.getProperty("jasypt.encryptor.private-key-format", AsymmetricCryptography.KeyFormat.class));
        setIfNotNull(properties::setPrivateKeyString, environment.getProperty("jasypt.encryptor.private-key-string"));
        setIfNotNull(properties::setPrivateKeyLocation, environment.getProperty("jasypt.encryptor.private-key-location"));

        properties.setAlgorithm("PBEWithMD5AndDES");
        properties.setKeyObtentionIterations("1000");
        properties.setPoolSize("1");
        properties.setProviderName(null);
        properties.setProviderClassName(null);
        properties.setSaltGeneratorClassname("org.jasypt.salt.RandomSaltGenerator");
        properties.setIvGeneratorClassname("org.jasypt.iv.NoIvGenerator");
        properties.setStringOutputType("base64");
    }
}

@@ -772,288 +772,8 @@
 ) %7B%0A
-        if (oldMajorVersion == 2) %7B%0A            upgradeFrom2(properties);%0A        %7D else %7B%0A            throw new RuntimeException(%22Unrecognised major version %22 + oldMajorVersion);%0A        %7D%0A    %7D%0A%0A    private void upgradeFrom2(JasyptEncryptorConfigurationProperties properties) %7B%0A
     
@@ -1320,24 +1320,303 @@
 cation%22));%0A%0A
+        if (oldMajorVersion %3C 2) %7B%0A            upgradeFrom2(properties);%0A        %7D else %7B%0A            throw new RuntimeException(%22Unrecognised major version %22 + oldMajorVersion);%0A        %7D%0A    %7D%0A%0A    private void upgradeFrom2(JasyptEncryptorConfigurationProperties properties) %7B%0A
         prop

Make upgrading slightly more resilient.