Statistics
| Revision:

gvsig-desktop-customize / trunk / org.gvsig.customize.app / org.gvsig.customize.app.mainplugin / src / main / java / org / gvsig / customize / DistributionBuilder.java @ 91

History | View | Annotate | Download (7.75 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.customize;
7

    
8
import java.io.File;
9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11
import java.io.IOException;
12
import java.net.URI;
13
import java.nio.file.FileSystem;
14
import java.nio.file.FileSystems;
15
import java.nio.file.Files;
16
import java.nio.file.Path;
17
import java.nio.file.Paths;
18
import java.nio.file.StandardCopyOption;
19
import java.util.HashMap;
20
import java.util.Map;
21

    
22
import org.apache.commons.io.FileUtils;
23
import org.gvsig.andami.PluginServices;
24
import org.gvsig.andami.PluginsLocator;
25
import org.gvsig.andami.PluginsManager;
26
import org.gvsig.installer.lib.api.InstallerLocator;
27
import org.gvsig.installer.lib.api.InstallerManager;
28
import org.gvsig.installer.lib.api.PackageInfo;
29
import org.gvsig.installer.lib.api.creation.MakePackageService;
30
import org.gvsig.installer.lib.api.creation.MakePackageServiceException;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
public class DistributionBuilder {
39

    
40
    private static final Logger logger = LoggerFactory.getLogger(DistributionBuilder.class);
41
    private static final String PCKGNAME= "package.gvspks";
42

    
43
    private String distributionId;
44
    private File onlineInstaller;
45
    private File workingFolder;
46
    private File customizePackagePath;
47
    private MessageBar messagebar;
48
    private boolean includeCustomizePlugin;
49
    private File packagesetFolder;
50

    
51
    public DistributionBuilder(MessageBar messagebar) {
52
        this.messagebar = messagebar;
53
    }
54

    
55
    private void message(String msg) {
56
        this.messagebar.message(msg);
57
    }
58

    
59
    public void setDistributionId(String distributionId) {
60
        this.distributionId = distributionId;
61
    }
62

    
63
    public void setOnlineInstaller(File onlineInstaller) {
64
        this.onlineInstaller = onlineInstaller;
65
    }
66

    
67
    public void setWorkingFolder(File workingFolder) {
68
        this.workingFolder = workingFolder;
69
    }
70

    
71
    void setIncludeCustomizePlugin(boolean includeCustomizePlugin) {
72
        this.includeCustomizePlugin = includeCustomizePlugin;
73
    }
74

    
75
    public boolean build() {
76
        try {
77
            String target_name = this.onlineInstaller.getName().replace("online", this.distributionId);
78
            File target = new File(this.workingFolder, target_name);
79

    
80
            message("Creating package set...");
81
            File packageSet = new File(this.workingFolder, "packages.gvspks");
82
            createPackageset(packageSet);
83

    
84
            if (this.includeCustomizePlugin) {
85
                message("Building customize plugin...");
86
                this.buildCustomizePlugin();
87

    
88
                message("Adding customized plugin to package set...");
89
                this.addToZip(packageSet, customizePackagePath);
90
            }
91

    
92
            message("Coping online installer...");
93
            FileUtils.copyFile(this.onlineInstaller, target);
94

    
95
            message("Adding package set to the installer...");
96
            addPackagetoZip(target, packageSet);
97

    
98
            message("Installer created.");
99
        } catch (Exception ex) {
100
            message("Can't build the distribution.\n" + ex.getMessage());
101
            logger.warn("Can't build the distribution.");
102
            logger.warn("working folder:" + this.workingFolder.getAbsolutePath());
103
            logger.warn("online installer:" + this.onlineInstaller.getAbsolutePath());
104
            return false;
105
        }
106
        return true;
107
    }
108

    
109
    private void addPackagetoZip(File zipFile, File packageFile) throws IOException{
110
        FileSystem zipfs = null;
111
        try {
112
        Map<String, Object> env = new HashMap<>();
113
        env.put("create", "true");
114
        env.put("useTempFile", Boolean.TRUE);
115
        Path zipPath = Paths.get(zipFile.getAbsolutePath());
116
        URI uriZip = URI.create("jar:file:" + zipPath.toUri().getPath());
117
        zipfs = FileSystems.newFileSystem(uriZip,env);
118
        Path pathInZipfile = zipfs.getPath("/"+PCKGNAME);
119
        // copy a file into the zip file
120
        Files.copy( packageFile.toPath(),pathInZipfile,
121
                StandardCopyOption.REPLACE_EXISTING );
122
        message("Package set added to the installer.");
123
        } catch (Exception e) {
124
            logger.warn("Impossible to add ("+packageFile.getAbsolutePath()+") to ("+zipFile.getAbsolutePath()+")",e);
125
        } finally{
126
            if (zipfs != null) {
127
                try {
128
                    zipfs.close();
129
                } catch (IOException ex) {
130
                }
131
            }
132
        }
133
    }
134

    
135
    private void createPackageset(File zipfile) {
136
        Map<String, String> env = new HashMap<>();
137
        env.put("create", "true");
138

    
139
        FileSystem zipfs = null;
140
        try {
141
            Path zippath = Paths.get(zipfile.getAbsolutePath());
142
            final URI uri = URI.create("jar:file:" + zippath.toUri().getPath());
143
            zipfs = FileSystems.newFileSystem(uri, env);
144
            File[] packages = packagesetFolder.listFiles();
145
            for (File newEntry : packages) {
146
                Path externalFile = Paths.get(newEntry.getAbsoluteFile().toURI());
147
                Path pathInZipfile = zipfs.getPath("/" + newEntry.getName());
148
                Files.copy(externalFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING);
149
            }
150
            zipfs.close();
151
        } catch (Exception ex) {
152
            logger.warn("Can't create packageset ("+zipfile.getAbsolutePath()+")",ex);
153
        } finally {
154
            if (zipfs != null) {
155
                try {
156
                    zipfs.close();
157
                } catch (IOException ex) {
158
                }
159
            }
160
        }
161

    
162
    }
163

    
164
    private void buildCustomizePlugin() throws FileNotFoundException, MakePackageServiceException, IOException {
165
        FileOutputStream fos = null;
166
        try {
167
            PluginsManager pluginsManager = PluginsLocator.getManager();
168
            PluginServices plugin = pluginsManager.getPlugin(CustomizeExtension.class);
169
            InstallerManager installManager = InstallerLocator.getInstallerManager();
170

    
171
            FileUtils.copyFileToDirectory(
172
                    new File(packagesetFolder,"defaultPackages"),
173
                    plugin.getPluginDirectory()
174
            );
175

    
176
            PackageInfo pkginfo = pluginsManager.getPackageInfo(CustomizeExtension.class);
177
            String pkgfilename = installManager.getPackageFileName(pkginfo);
178
            customizePackagePath = new File(this.workingFolder, pkgfilename);
179

    
180
            MakePackageService maker = installManager.createMakePackage(plugin.getPluginDirectory(), pkginfo);
181
            fos = new FileOutputStream(customizePackagePath);
182
            maker.createPackage(fos);
183
        } finally {
184
            if (fos != null) {
185
                fos.close();
186
            }
187
        }
188

    
189
    }
190

    
191
    private void addToZip(File zipfile, File newEntry) {
192
        Map<String, String> env = new HashMap<>();
193
        env.put("create", "true");
194

    
195
        FileSystem zipfs = null;
196
        try {
197
            Path zippath = Paths.get(zipfile.getAbsolutePath());
198
            final URI uri = URI.create("jar:file:" + zippath.toUri().getPath());
199
            zipfs = FileSystems.newFileSystem(uri, env);
200
            Path externalFile = Paths.get(newEntry.getAbsoluteFile().toURI());
201
            Path pathInZipfile = zipfs.getPath("/" + newEntry.getName());
202
            Files.copy(externalFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING);
203
            zipfs.close();
204
        } catch (IOException ex) {
205

    
206
        } finally {
207
            if (zipfs != null) {
208
                try {
209
                    zipfs.close();
210
                } catch (IOException ex) {
211
                }
212
            }
213
        }
214

    
215
    }
216

    
217
    void setPackageSet(File packageSet) {
218
        this.packagesetFolder = packageSet;
219
    }
220

    
221
}