cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cn.ksource.core.license;
 
import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.prefs.Preferences;
 
public class LicenseController {
 
    private static final String PROPERTIES_LICENSE_INSTALL_FILENAME = "LicenseInstallConfig.properties";
 
    private String appName;
    private String keystoreFilename;
    private String keystorePassword;
    private String alias;
    private String cipherParamPassword;
 
    public LicenseController() {
        loadLicenseInstallPropertiesFile();
    }
 
 
    public LicenseContent verifyLicense() throws Exception {
        
        LicenseParam licenseParam = getLicenseParam();
        try {
            LicenseManager lm = new LicenseManager(licenseParam);
 
            LicenseContent licenseContent = lm.verify();
 
            return licenseContent;
 
        } finally {
            try {
                licenseParam.getKeyStoreParam().getStream().close();
            } catch (IOException ex) {
 
            }
        }
 
    }
 
    /**
     * Install the license file.
     *
     * @return LicenseContent if the license installed properly, null otherwise.
     */
    public LicenseContent installLicense(String licenseFilename) throws Exception {
 
        LicenseParam licenseParam = getLicenseParam();
        try {
            LicenseManager lm = new LicenseManager(licenseParam);
 
            lm.uninstall();
 
            File licenseFile = new File(licenseFilename);
            LicenseContent licenseContent = lm.install(licenseFile);
 
            return licenseContent;
        } finally {
            try {
                licenseParam.getKeyStoreParam().getStream().close();
            } catch (IOException ex) {
 
            }
        }
    }
 
    private LicenseParam getLicenseParam() {
 
        //implementation of KeyStoreParam interface
        // required the keystore containing the private key
        final KeyStoreParam publicKeyStoreParam = new KeyStoreParam() {
            public InputStream getStream() throws IOException {
                final InputStream in = LicenseController.class
                        .getClassLoader().getResourceAsStream(keystoreFilename);
                if (in== null) {
                    System.err.println("Could not load file: " + keystoreFilename);
                    throw new FileNotFoundException(keystoreFilename);
                }
                return in;
            }
 
            public String getAlias() {
                return alias;
            }
 
            public String getStorePwd() {
                return keystorePassword;
            }
 
            public String getKeyPwd() {
                return null;
            }
        };
 
        final CipherParam cipherParam = new CipherParam() {
            public String getKeyPwd() {
                return cipherParamPassword;
            }
        };
 
        return new LicenseParam() {
            public String getSubject() {
                return appName;
            }
 
            public Preferences
                    getPreferences() {
 
                return Preferences.userNodeForPackage(LicenseController.class);
//                return null;
            }
 
            public KeyStoreParam getKeyStoreParam() {
                return publicKeyStoreParam;
            }
 
            public CipherParam getCipherParam() {
                return cipherParam;
            }
        };
    }
 
    private void loadLicenseInstallPropertiesFile() {
 
        Properties properties = new Properties();
 
        ClassLoader classLoader = LicenseController.class
                .getClassLoader();
        try {
            File file = new File(classLoader.getResource(PROPERTIES_LICENSE_INSTALL_FILENAME).getFile().replace("%20", " "));
            FileInputStream in = new FileInputStream(file);
            properties.load(in);
 
            appName = properties.getProperty("app_name");
            keystoreFilename = properties.getProperty("keystore_filename");
            keystorePassword = properties.getProperty("keystore_password");
            alias = properties.getProperty("alias");
            cipherParamPassword = properties.getProperty("cipher_param_password");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
}