Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.installer / org.gvsig.desktop.installer.izpack / org.gvsig.desktop.installer.izpack.copyjre / src / main / java / org / gvsig / desktop / installer / izpack / CopyJRE.java @ 42794

History | View | Annotate | Download (6.95 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.desktop.installer.izpack;
25

    
26
import java.io.File;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import com.izforge.izpack.panels.process.AbstractUIProcessHandler;
30
import java.io.Closeable;
31
import java.io.FileInputStream;
32
import java.nio.channels.FileChannel;
33

    
34
/*
35
 * Based on portions of code from clases IOUtils and FileUtils 
36
 * of the project org.apache.commons.io of the Apache Software Foundation (ASF)
37
 *
38
 * http://svn.apache.org/viewvc/commons/proper/io/tags/commons-io-2.5/src/main/java/org/apache/commons/io/FileUtils.java?view=markup
39
 * http://svn.apache.org/viewvc/commons/proper/io/tags/commons-io-2.5/src/main/java/org/apache/commons/io/IOUtils.java?view=markup
40
 */
41
public class CopyJRE {
42

    
43
    public boolean run(AbstractUIProcessHandler handler, String[] args) {
44
        File java_home = new File(System.getProperties().getProperty("java.home"));
45
        File f = new File(java_home, "package.info");
46
        if (!f.exists()) {
47
            // Solo copiamos la JRE si es la de un plugin de gvSIG.
48
            return true;
49
        }
50

    
51
        handler.logOutput("Installing JRE plugin (This may take a while)...", false);
52
        try {
53
            File installation_folder = new File(args[0]);
54
            File target = getFile(installation_folder,"gvSIG", "extensiones","jre");
55
            doCopyDirectory(java_home,target,true);
56
            File java_bin = getFile(installation_folder,"gvSIG", "extensiones","jre","bin","java");
57
            java_bin.setExecutable(true, false);
58
        } catch (Exception ex) {
59
            handler.logOutput("Can't install JRE plugin. " + ex.toString(), false);
60
            return false;
61
        }
62

    
63
        handler.logOutput("JRE plugin installed.", false);
64
        return true;
65
    }
66

    
67
    public static final long ONE_KB = 1024;
68
    public static final long ONE_MB = ONE_KB * ONE_KB;
69
    private static final long FILE_COPY_BUFFER_SIZE = ONE_MB * 30;
70

    
71
    public static File getFile(final File directory, final String... names) {
72
        if (directory == null) {
73
            throw new NullPointerException("directory must not be null");
74
        }
75
        if (names == null) {
76
            throw new NullPointerException("names must not be null");
77
        }
78
        File file = directory;
79
        for (final String name : names) {
80
            file = new File(file, name);
81
        }
82
        return file;
83
    }
84

    
85
    private static void doCopyDirectory(final File srcDir, final File destDir,
86
            final boolean preserveFileDate)
87
            throws IOException {
88
        // recurse
89
        final File[] srcFiles = srcDir.listFiles();
90
        if (srcFiles == null) { // null if abstract pathname does not denote a directory, or if an I/O error occurs
91
            throw new IOException("Failed to list contents of " + srcDir);
92
        }
93
        if (destDir.exists()) {
94
            if (destDir.isDirectory() == false) {
95
                throw new IOException("Destination '" + destDir + "' exists but is not a directory");
96
            }
97
        } else {
98
            if (!destDir.mkdirs() && !destDir.isDirectory()) {
99
                throw new IOException("Destination '" + destDir + "' directory cannot be created");
100
            }
101
        }
102
        if (destDir.canWrite() == false) {
103
            throw new IOException("Destination '" + destDir + "' cannot be written to");
104
        }
105
        for (final File srcFile : srcFiles) {
106
            final File dstFile = new File(destDir, srcFile.getName());
107
            if (srcFile.isDirectory()) {
108
                doCopyDirectory(srcFile, dstFile, preserveFileDate);
109
            } else {
110
                doCopyFile(srcFile, dstFile, preserveFileDate);
111
            }
112
        }
113

    
114
        // Do this last, as the above has probably affected directory metadata
115
        if (preserveFileDate) {
116
            destDir.setLastModified(srcDir.lastModified());
117
        }
118
    }
119

    
120
    private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate)
121
            throws IOException {
122
        if (destFile.exists() && destFile.isDirectory()) {
123
            throw new IOException("Destination '" + destFile + "' exists but is a directory");
124
        }
125

    
126
        FileInputStream fis = null;
127
        FileOutputStream fos = null;
128
        FileChannel input = null;
129
        FileChannel output = null;
130
        try {
131
            fis = new FileInputStream(srcFile);
132
            fos = new FileOutputStream(destFile);
133
            input = fis.getChannel();
134
            output = fos.getChannel();
135
            final long size = input.size(); // TODO See IO-386
136
            long pos = 0;
137
            long count = 0;
138
            while (pos < size) {
139
                final long remain = size - pos;
140
                count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain;
141
                final long bytesCopied = output.transferFrom(input, pos, count);
142
                if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size
143
                    break; // ensure we don't loop forever
144
                }
145
                pos += bytesCopied;
146
            }
147
        } finally {
148
            closeQuietly(output, fos, input, fis);
149
        }
150

    
151
        final long srcLen = srcFile.length(); // TODO See IO-386
152
        final long dstLen = destFile.length(); // TODO See IO-386
153
        if (srcLen != dstLen) {
154
            throw new IOException("Failed to copy full contents from '"
155
                    + srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen);
156
        }
157
        if (preserveFileDate) {
158
            destFile.setLastModified(srcFile.lastModified());
159
        }
160
    }
161

    
162
    public static void closeQuietly(final Closeable closeable) {
163
        try {
164
            if (closeable != null) {
165
                closeable.close();
166
            }
167
        } catch (final IOException ioe) {
168
            // ignore
169
        }
170
    }
171

    
172
    public static void closeQuietly(final Closeable... closeables) {
173
        if (closeables == null) {
174
            return;
175
        }
176
        for (final Closeable closeable : closeables) {
177
            closeQuietly(closeable);
178
        }
179
    }
180

    
181
}