summaryrefslogtreecommitdiff
path: root/javainstaller2/src/JavaSetup/org/openoffice/setup/Util/SystemManager.java
blob: 08bb4842252b4c53e1ba662b7cd1f774cc2b2e8b (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: SystemManager.java,v $
 * $Revision: 1.7 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package org.openoffice.setup.Util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;

public class SystemManager {

    private SystemManager() {
    }

    /* the installation root is where the classes reside */
    static public File getJarFilePath() {

        File jarFile = null;

        try {
            Class c  = Class.forName("org.openoffice.setup.ResourceManager");
            URL url  = c.getResource("setupfiles.properties");

            String urlString = url.toString();

            if (urlString.startsWith("jar:")) {
                /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
                urlString = urlString.substring(4, urlString.lastIndexOf("!"));
                jarFile = new File(new URI(urlString));
            }

        } catch (Exception ex) {
            /* handle URISyntaxException and ClassNotFoundException */
            ex.printStackTrace();
            System.exit(1);
        }

        if ( jarFile != null ) {
            System.err.println("Jar file: " + jarFile.getPath());
        } else {
            System.err.println("No jar file used for installation!");
        }

        return jarFile;
    }

    /* the installation root is where the classes reside */
    static public File getResourceRoot() {

        File dir = null;

        try {
            Class c  = Class.forName("org.openoffice.setup.ResourceManager");
            URL url  = c.getResource("setupfiles.properties");

            String urlString = url.toString();

            if (urlString.startsWith("jar:")) {
                /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
                urlString = urlString.substring(4, urlString.lastIndexOf("!"));
            } else {
                /* ResourceManager.class resides in a directory tree. */
                urlString = urlString.substring(0, urlString.lastIndexOf("/org/openoffice/setup/setupfiles.properties"));
            }

            dir = new File(new URI(urlString));
            dir = dir.getParentFile();

        } catch (Exception ex) {
            /* handle URISyntaxException and ClassNotFoundException */
            ex.printStackTrace();
            System.exit(1);
        }
        // }

        if ( dir != null ) {
            // System.err.println("Resource Root: " + dir.getPath());
        } else {
            System.err.println("No resource root found!");
        }

        return dir;
    }

    static public String getPackagePath(String subdir) {

        String path = null;

        File dir = getResourceRoot();
        if (dir != null) {
            // System.err.println("Resource root: " + dir.getPath());
            dir = new File(dir, subdir);
            if (! dir.exists()) {
                System.err.println("Error: Directory \"" + subdir + "\" does not exist at resouce root");
            } else {
                path = dir.getPath();
            }
        }

        if ( path != null ) {
            if ( ! path.endsWith("/")) {
                path = path + "/";
            }
        }

        if ( path != null ) {
            System.err.println("Path to packages: " + path);
        } else {
            System.err.println("No path to packages found!");
        }

        return path;
    }

    static public boolean find_file(String fileName) {
        boolean found = false;
        File file = new File(fileName);
        found = file.exists();
        return found;
    }

    static public boolean exists_directory(String directory) {
        File dir = new File(directory);
        return dir.exists();
    }

    static public boolean create_directory(String directory) throws SecurityException {
        boolean created = false;
        File dir = new File(directory);
        try {
            created = dir.mkdirs();
        }
        catch (SecurityException ex) {
            throw ex;
        }

        return created;
    }

    static public String getParentDirectory(String dir) {
        File installFile = new File(dir);
        String parentDir = installFile.getParent();
        if ( parentDir == null ) {
            parentDir = "/";
        }
        return parentDir;
    }

    static public String getInstallationPrivileges() {

        String type = "";
        String user = java.lang.System.getProperty("user.name");
        // System.out.println("UserHome: " + java.lang.System.getProperty("user.home"));

        if ( user.equalsIgnoreCase("root")) {
            type = "root";
            System.err.println("Root privileges");
        } else {
            type = "user";
            System.err.println("User privileges");
        }

        return type;
    }

    static public boolean isUserInstallation() {

        boolean isUserInstallation = false;
        String user = java.lang.System.getProperty("user.name");

        if ( user.equalsIgnoreCase("root")) {
            isUserInstallation = false;
            System.err.println("Root privileges");
        } else {
            isUserInstallation = true;
            System.err.println("User privileges");
        }

        return isUserInstallation;
    }

    static public boolean isRootInstallation() {

        boolean isRootInstallation = false;
        String user = java.lang.System.getProperty("user.name");

        if ( user.equalsIgnoreCase("root")) {
            isRootInstallation = true;
        } else {
            isRootInstallation = false;
        }

        return isRootInstallation;
    }

    static public String getOSType() {
        String osVersion = java.lang.System.getProperty("os.name");
        System.err.println("OS: " + osVersion);
        return osVersion;
    }

    static public String getOSArchitecture() {
        String osArchitecture = java.lang.System.getProperty("os.arch");
        System.out.println("OSArchitecture: " + osArchitecture);
        return osArchitecture;
    }

    static public String getOSVersion() {
        String osVersion = java.lang.System.getProperty("os.version");
        System.out.println("OSVersion: " + osVersion);
        return osVersion;
    }

    static public HashMap getEnvironmentHashMap() {
        // readonly map from getenv()
        // System.getenv only supported in Java 1.5, properties have to be set in shell script
        // Map map = java.lang.System.getenv();
        // HashMap myMap = new HashMap(map);
        Properties props = System.getProperties();
        HashMap myMap = new HashMap(props);
        return myMap;
    }

    static public void dumpStringArray(String[] myStringArray) {
        for (int i = 0; i < myStringArray.length; i++) {
            System.out.println(myStringArray[i]);
        }
    }

    static public void dumpFile(String baseFileName, String dumpFileName) {
        Vector fileContent = readCharFileVector(baseFileName);
        saveCharFileVector(dumpFileName, fileContent);
    }

    static public Vector readCharFileVector(String fileName) {
        Vector fileContent = new Vector();

        File file = new File(fileName);
        if ( file.exists()) {
            try {
                FileInputStream fs = new FileInputStream(file);
                BufferedReader bs = new BufferedReader(new InputStreamReader(fs));
                String zeile;
                while((zeile = bs.readLine())!=null) {
                    fileContent.addElement(zeile);
                }
            }
            catch (IOException e) {
                System.out.println(e);
            }
        } else {
            System.out.println( "Error: File not found: " +  fileName);
        }

        return fileContent;
    }


    static public void saveCharFileVector(String fileName, Vector fileContent) {
        FileWriter fw = null;
        try
        {
            fw = new FileWriter(fileName);
            String fileContentStr = "";
            for (int i = 0; i < fileContent.size() ; i++) {
                fileContentStr = fileContentStr + fileContent.get(i) + "\n";
                // System.out.println(fileContent.get(i));
            }
            fw.write(fileContentStr);
        }
        catch ( IOException e ) {
            System.out.println( "Could not create file: " +  fileName);
        }
        finally {
            try {
                if ( fw != null ) fw.close();
            } catch (IOException e) {}
        }
    }

    static public void copyAllFiles(File source, File dest) {
        File[] file = source.listFiles();
        if (file != null) {
            for (int i = 0; i < file.length; i++) {
                copy(file[i].getPath(), dest.getPath());
            }
        }
    }

    static public void copyAllFiles(File source, File dest, String ext) {
        File[] file = source.listFiles(new FileExtensionFilter(ext));
        if (file != null) {
            for (int i = 0; i < file.length; i++) {
                copy(file[i].getPath(), dest.getPath());
            }
        }
    }

    // second parameter can be a complete file name or an existing directory
    static public boolean copy(String source, String dest) {

        // is the second parameter a file name or a directory?
        File dir = new File(dest);
        if ( dir.isDirectory() ) {
            File sourceFile = new File(source);
            String fileName = sourceFile.getName();
            File destFile = new File(dest, fileName);
            dest = destFile.getPath();
        }

        boolean file_copied = false;
        FileInputStream fis;
        BufferedInputStream bis;
        FileOutputStream fos;
        BufferedOutputStream bos;
        byte[] b;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(dest);
        } catch (FileNotFoundException ex) {
            throw new Error("File not found");
        }
        // put file into buffer
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);
        try { // read file, write and close
            b = new byte[bis.available()];
            bis.read(b);
            bos.write(b);
            bis.close();
            bos.close();
            file_copied = true;
        } catch (IOException e) {
            System.out.println("Dateien wurden nicht kopiert!");
        }

        return file_copied;
    }

    static public boolean deleteFile(File file) {
        boolean success = false;
        if ( file.exists() && file != null ) {
            success = file.delete();
        }
        return success;
    }

    static public boolean createDirectory(File dir) throws SecurityException {
        boolean created = false;
        try {
            created = dir.mkdirs();
        }
        catch (SecurityException ex) {
            throw ex;
        }

        return created;
    }

    static public void removeDirectory(File dir) {
        if ( dir.exists() && dir.isDirectory() ) {
            File[] file = dir.listFiles();
            if (file != null) {
                for (int i = 0; i < file.length; i++) {
                    deleteFile(file[i]);
                }
            }
            dir.delete();
        }
    }

    static public boolean logModuleStates() {
        boolean logStates = false;
        // System.getenv only supported in Java 1.5, property set in shell script
        // String logStatesEnv = System.getenv("LOG_MODULE_STATES");
        String logStatesEnv = System.getProperty("LOG_MODULE_STATES");

        if ( logStatesEnv != null ) {
            logStates = true;
        }

        return logStates;
    }

    static public void setUnixPrivileges(String fileName, String unixRights) {
        // String command = "chmod " + unixRights + " " + fileName;
        String[] commandArray = new String[3];
        commandArray[0] = "chmod";
        commandArray[1] = unixRights;
        commandArray[2] = fileName;
        int value = ExecuteProcess.executeProcessReturnValue(commandArray);
    }

    static public void setUnixPrivilegesDirectory(File directoryName, String ext, String unixRights) {
        File[] file = directoryName.listFiles(new FileExtensionFilter(ext));
        if (file != null) {
            for (int i = 0; i < file.length; i++) {
                setUnixPrivileges(file[i].getPath(), unixRights);
            }
        }
    }

    static public int calculateDiscSpace(String directory) {
        String command = "df -k " + directory;
        String[] commandArray = new String[3];
        commandArray[0] = "df";
        commandArray[1] = "-k";
        commandArray[2] = directory;

        int size = 0;
        Vector returnVector = new Vector();
        Vector returnErrorVector = new Vector();
        int returnValue = ExecuteProcess.executeProcessReturnVector(commandArray, returnVector, returnErrorVector);
        if ( returnValue == 0) {
            int max = returnVector.size();
            if ( max > 0 ) {
                String returnLine = (String) returnVector.get(max-1);

                // The fourth value is the available disc space (if the first value is a path)
                // Otherwise it can also be the third value, if the first is not a path.
                // If the first value is not a path, the string starts with white spaces.

                int position = 3;
                if ( returnLine.startsWith(" ")) {
                    position = 2;
                }

                returnLine = returnLine.trim();
                String[] returnArray = returnLine.split("\\s+");

                if ( returnArray.length > 3 ) {
                    String sizeString = returnArray[position];

                    // Special handling for very large hard discs that cannot be converted to int
                    if ( sizeString.length() >= Integer.toString(Integer.MAX_VALUE).length() ) {
                        sizeString = Integer.toString(Integer.MAX_VALUE);
                    }

                    // Converting from String to int
                    size = Integer.parseInt(sizeString);
                }
            }
        }

        return size;
    }

}