aboutsummaryrefslogtreecommitdiffstats
path: root/installer/src/main/java/com/github/dcevm/installer/Installer.java
blob: fb5a40a3387a80bf872abf1d6d970baacd637708 (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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */
package com.github.dcevm.installer;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kerstin Breiteneder
 * @author Christoph Wimberger
 * @author Ivan Dubrov
 * @author Jiri Bubnik
 */
public class Installer {

    private ConfigurationInfo config;

    public Installer(ConfigurationInfo config) {
        this.config = config;
    }

    public void install(Path dir, boolean bit64, boolean altjvm) throws IOException {
        if (config.isJDK(dir)) {
            dir = dir.resolve(config.getJREDirectory());
        }

        if (!altjvm) {
            Path serverPath = dir.resolve(config.getServerPath(bit64));
            if (Files.exists(serverPath)) {
                installClientServer(serverPath, bit64);
            }

            Path clientPath = dir.resolve(config.getClientPath());
            if (Files.exists(clientPath) && !bit64) {
                installClientServer(clientPath, false);
            }
        } else {
            Path altjvmPath = dir.resolve(bit64 ? config.getDcevm64Path() : config.getDcevm32Path());
            if (!Files.exists(altjvmPath)) {
                Files.createDirectory(altjvmPath);
            }
            installClientServer(altjvmPath, bit64);
        }
    }

    /**
     * Try to uninstall DCEVM from all locations (skip if not exists).
     */
    public void uninstall(Path dir, boolean bit64) throws IOException {
        if (config.isJDK(dir)) {
            dir = dir.resolve(config.getJREDirectory());
        }

        Path serverPath = dir.resolve(config.getServerPath(bit64));
        if (Files.exists(serverPath)) {
            uninstallClientServer(serverPath);
        }

        Path clientPath = dir.resolve(config.getClientPath());
        if (Files.exists(clientPath) && !bit64) {
            uninstallClientServer(clientPath);
        }

        Path dcevm32Path = dir.resolve(config.getDcevm32Path());
        if (Files.exists(dcevm32Path)) {
            Files.deleteIfExists(dcevm32Path.resolve(config.getLibraryName()));
            Files.deleteIfExists(dcevm32Path.resolve(config.getBackupLibraryName()));
            Files.delete(dcevm32Path);
        }

        Path dcevm64Path = dir.resolve(config.getDcevm64Path());
        if (Files.exists(dcevm64Path)) {
            Files.deleteIfExists(dcevm64Path.resolve(config.getLibraryName()));
            Files.deleteIfExists(dcevm64Path.resolve(config.getBackupLibraryName()));
            Files.delete(dcevm64Path);
        }

    }

    public List<Installation> listInstallations() {
        return scanPaths(config.paths());
    }

    public ConfigurationInfo getConfiguration() {
        return config;
    }

    private void installClientServer(Path path, boolean bit64) throws IOException {
        String resource = config.getResourcePath(bit64) + "/product/" + config.getLibraryName();

        Path library = path.resolve(config.getLibraryName());
        Path backup = path.resolve(config.getBackupLibraryName());

        // backup any existing library (assume original JVM file)
        if (Files.exists(library)) {
            Files.move(library, backup);
        }

        try {
            // install actual DCEVM file
            try (InputStream in = getClass().getClassLoader().getResourceAsStream(resource)) {
                if (in == null) {
                    throw new IOException("DCEVM not available for java at '" + path + "'. Missing resource " + resource);
                }

                Files.copy(in, library);
            }
        } catch (NullPointerException | IOException e) {
            // try to restore original file
            if (Files.exists(backup)) {
                Files.move(backup, library, StandardCopyOption.REPLACE_EXISTING);
            }
            throw e;
        }
    }

    private void uninstallClientServer(Path path) throws IOException {
        Path library = path.resolve(config.getLibraryName());
        Path backup = path.resolve(config.getBackupLibraryName());

        if (Files.exists(backup)) {
            Files.delete(library);
            Files.move(backup, library);
        }
    }

    private List<Installation> scanPaths(String... dirPaths) {
        List<Installation> installations = new ArrayList<>();
        for (String dirPath : dirPaths) {
            Path dir = Paths.get(dirPath);
            if (Files.isDirectory(dir)) {
                try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
                    scanDirectory(stream, installations);
                } catch (Exception ex) {
                    // Ignore, try different directory
                    ex.printStackTrace();
                }
            }
        }
        return installations;
    }

    private void scanDirectory(DirectoryStream<Path> stream, List<Installation> installations) {
        for (Path path : stream) {
            if (Files.isDirectory(path) && (config.isJDK(path) || config.isJRE(path))) {
                try {
                    Installation inst = new Installation(config, path);
                    if (!installations.contains(inst)) {
                        installations.add(inst);
                    }
                } catch (Exception ex) {
                    // FIXME: just ignore the installation for now..
                    ex.printStackTrace();
                }
            }
        }
    }

}