Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / installer / icontheme / IconThemeInstaller.java @ 43067

History | View | Annotate | Download (5.58 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

    
25
package org.gvsig.andami.installer.icontheme;
26

    
27
import java.io.File;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.util.zip.ZipEntry;
32
import java.util.zip.ZipException;
33
import java.util.zip.ZipInputStream;
34
import org.apache.commons.io.FileUtils;
35
import org.apache.commons.io.FilenameUtils;
36
import org.gvsig.installer.lib.api.PackageInfo;
37
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
38
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
39
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.service.spi.AbstractProvider;
42
import org.gvsig.tools.service.spi.ProviderServices;
43
import org.gvsig.tools.swing.api.ToolsSwingLocator;
44
import org.gvsig.tools.swing.icontheme.IconThemeManager;
45
import org.gvsig.tools.task.SimpleTaskStatus;
46
import org.gvsig.tools.task.TaskStatusManager;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50

    
51
public class IconThemeInstaller extends AbstractProvider implements InstallPackageProvider {
52

    
53
    private static final Logger logger = LoggerFactory.getLogger(IconThemeInstaller.class);
54
    
55
    private final int BUFFER = 2048;
56
    
57
    public IconThemeInstaller(ProviderServices providerServices) {
58
            super(providerServices);
59
    }
60

    
61
    @Override
62
    public void install(File applicationFolder, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException {
63
        IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager();
64
        File folder = iconThemeManager.getRepository().asFile();        
65
        logger.info("Installing package '"+packageInfo.getCode()+"' in '"+folder.getAbsolutePath()+"'.");
66
        try {
67
            if (!folder.exists()) {
68
                String s = "Can install package '"+packageInfo.getCode()+"', install folder '"+folder+"' does not exists.";
69
                logger.warn(s);
70
                throw new RuntimeException(s);
71
            }
72
            decompress(inputStream, folder);
73
        } catch (Exception e) {
74
            try {
75
                logger.warn("Can install package '"+packageInfo.getCode()+"'.", e);
76
                // if there is an exception, installLater is called
77
                installLater(applicationFolder, inputStream, packageInfo);
78
            } catch (IOException e1) {
79
                logger.warn("Can install package '"+packageInfo.getCode()+"'.", e1);
80
                throw new InstallPackageServiceException(e1);
81
            }
82
        }
83

    
84
    }
85

    
86
    private void decompress(InputStream inputStream, File folder) 
87
        throws ZipException, IOException, InstallerInfoFileException {
88

    
89
        ZipInputStream zis;
90
        ZipEntry entry;
91
        byte data[] = new byte[BUFFER];
92
        int count;
93

    
94
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
95
        SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Uncompressing...");
96

    
97
        manager.add(taskStatus);
98
        String entryName = "(header)";
99
        try {
100
            long readed = 0;
101
            zis = new ZipInputStream(inputStream);
102
            while ((entry = zis.getNextEntry()) != null) {
103
                entryName = FilenameUtils.separatorsToSystem(entry.getName());
104
                taskStatus.message(entryName);
105

    
106
                File file = new File(folder, entryName);
107
                if (entry.isDirectory()) {
108
                        file.mkdirs();                    
109
                } else {
110
                    if (file.exists()) {
111
                         FileUtils.forceDelete(file);
112
                    }
113
                    if( !file.getParentFile().exists() ) {
114
                        FileUtils.forceMkdir(file.getParentFile());
115
                    }
116
                    logger.debug("extracting " + file.getAbsolutePath());
117
                    try (FileOutputStream fos = new FileOutputStream(file)) {
118
                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
119
                            fos.write(data, 0, count);
120
                            readed += count;
121
                            taskStatus.setCurValue(readed);
122
                        }
123
                        fos.flush();
124
                    }
125
                }
126
            }
127
            zis.close();
128
        } catch(Exception ex) {
129
                logger.warn("Problems uncompresing 'translations' (last entry '"+entryName+"'.",ex);
130
                throw ex;
131
        } finally {
132
            taskStatus.remove();
133

    
134
        }
135
    }
136

    
137
    
138
    @Override
139
    public void installLater(File applicationDirectory, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException, IOException {
140
        logger.warn("installLater is not implementes.");
141
    }
142

    
143
}