gvsig-desktop-customize / trunk / org.gvsig.customize.app / org.gvsig.customize.app.mainplugin / src / main / java / org / gvsig / customize / DistributionBuilder.java @ 1
History | View | Annotate | Download (6.76 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 |
|
42 |
private String distributionId; |
43 |
private File onlineInstaller; |
44 |
private File workingFolder; |
45 |
private File customizePackagePath; |
46 |
private MessageBar messagebar;
|
47 |
private boolean includeCustomizePlugin; |
48 |
private File packagesetFolder; |
49 |
|
50 |
public DistributionBuilder(MessageBar messagebar) {
|
51 |
this.messagebar = messagebar;
|
52 |
} |
53 |
|
54 |
private void message(String msg) { |
55 |
this.messagebar.message(msg);
|
56 |
} |
57 |
|
58 |
public void setDistributionId(String distributionId) { |
59 |
this.distributionId = distributionId;
|
60 |
} |
61 |
|
62 |
public void setOnlineInstaller(File onlineInstaller) { |
63 |
this.onlineInstaller = onlineInstaller;
|
64 |
} |
65 |
|
66 |
public void setWorkingFolder(File workingFolder) { |
67 |
this.workingFolder = workingFolder;
|
68 |
} |
69 |
|
70 |
void setIncludeCustomizePlugin(boolean includeCustomizePlugin) { |
71 |
this.includeCustomizePlugin = includeCustomizePlugin;
|
72 |
} |
73 |
|
74 |
public boolean build() { |
75 |
try {
|
76 |
Installkit installkit = new Installkit();
|
77 |
|
78 |
String target_name = this.onlineInstaller.getName().replace("online", this.distributionId); |
79 |
File target = new File(this.workingFolder, target_name); |
80 |
|
81 |
message("Creating package set...");
|
82 |
File packageSet = new File(this.workingFolder, "packages.gvspks"); |
83 |
createPackageset(packageSet); |
84 |
|
85 |
if (this.includeCustomizePlugin) { |
86 |
message("Building customize plugin...");
|
87 |
this.buildCustomizePlugin();
|
88 |
|
89 |
message("Adding customized plugin to package set...");
|
90 |
this.addToZip(packageSet, customizePackagePath);
|
91 |
} |
92 |
|
93 |
message("Coping online installer...");
|
94 |
FileUtils.copyFile(this.onlineInstaller, target);
|
95 |
|
96 |
message("Adding package set to the installer...");
|
97 |
installkit.addpks(target.getAbsolutePath(), packageSet.getAbsolutePath()); |
98 |
|
99 |
message("Installer created.");
|
100 |
} catch (Exception ex) { |
101 |
message("Can't build the distribution.\n" + ex.getMessage());
|
102 |
logger.warn("Can't build the distribution.");
|
103 |
logger.warn("working folder:" + this.workingFolder.getAbsolutePath()); |
104 |
logger.warn("online installer:" + this.onlineInstaller.getAbsolutePath()); |
105 |
return false; |
106 |
} |
107 |
return true; |
108 |
} |
109 |
|
110 |
private void createPackageset(File zipfile) { |
111 |
Map<String, String> env = new HashMap<>(); |
112 |
env.put("create", "true"); |
113 |
|
114 |
FileSystem zipfs = null;
|
115 |
try {
|
116 |
Path zippath = Paths.get(zipfile.getAbsolutePath()); |
117 |
final URI uri = URI.create("jar:file:" + zippath.toUri().getPath()); |
118 |
zipfs = FileSystems.newFileSystem(uri, env); |
119 |
File[] packages = packagesetFolder.listFiles(); |
120 |
for (File newEntry : packages) { |
121 |
Path externalFile = Paths.get(newEntry.getAbsoluteFile().toURI()); |
122 |
Path pathInZipfile = zipfs.getPath("/" + newEntry.getName());
|
123 |
Files.copy(externalFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING); |
124 |
} |
125 |
zipfs.close(); |
126 |
} catch (Exception ex) { |
127 |
logger.warn("Can't create packageset ("+zipfile.getAbsolutePath()+")",ex); |
128 |
} finally {
|
129 |
if (zipfs != null) { |
130 |
try {
|
131 |
zipfs.close(); |
132 |
} catch (IOException ex) { |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
} |
138 |
|
139 |
private void buildCustomizePlugin() throws FileNotFoundException, MakePackageServiceException, IOException { |
140 |
FileOutputStream fos = null; |
141 |
try {
|
142 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
143 |
PluginServices plugin = pluginsManager.getPlugin(CustomizeExtension.class); |
144 |
InstallerManager installManager = InstallerLocator.getInstallerManager(); |
145 |
|
146 |
FileUtils.copyFileToDirectory( |
147 |
new File(packagesetFolder,"defaultPackages"), |
148 |
plugin.getPluginDirectory() |
149 |
); |
150 |
|
151 |
PackageInfo pkginfo = pluginsManager.getPackageInfo(CustomizeExtension.class); |
152 |
String pkgfilename = installManager.getPackageFileName(pkginfo);
|
153 |
customizePackagePath = new File(this.workingFolder, pkgfilename); |
154 |
|
155 |
MakePackageService maker = installManager.createMakePackage(plugin.getPluginDirectory(), pkginfo); |
156 |
fos = new FileOutputStream(customizePackagePath); |
157 |
maker.createPackage(fos); |
158 |
} finally {
|
159 |
if (fos != null) { |
160 |
fos.close(); |
161 |
} |
162 |
} |
163 |
|
164 |
} |
165 |
|
166 |
private void addToZip(File zipfile, File newEntry) { |
167 |
Map<String, String> env = new HashMap<>(); |
168 |
env.put("create", "true"); |
169 |
|
170 |
FileSystem zipfs = null;
|
171 |
try {
|
172 |
Path zippath = Paths.get(zipfile.getAbsolutePath()); |
173 |
final URI uri = URI.create("jar:file:" + zippath.toUri().getPath()); |
174 |
zipfs = FileSystems.newFileSystem(uri, env); |
175 |
Path externalFile = Paths.get(newEntry.getAbsoluteFile().toURI()); |
176 |
Path pathInZipfile = zipfs.getPath("/" + newEntry.getName());
|
177 |
Files.copy(externalFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING); |
178 |
zipfs.close(); |
179 |
} catch (IOException ex) { |
180 |
|
181 |
} finally {
|
182 |
if (zipfs != null) { |
183 |
try {
|
184 |
zipfs.close(); |
185 |
} catch (IOException ex) { |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
void setPackageSet(File packageSet) { |
193 |
this.packagesetFolder = packageSet;
|
194 |
} |
195 |
|
196 |
} |