Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.app.jcrs / org.gvsig.projection.app.jcrs.common / src / main / java / org / gvsig / crs / installer / EPSGDatabaseInstaller.java @ 249

History | View | Annotate | Download (5.77 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.crs.installer;
25

    
26
import java.io.File;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.net.MalformedURLException;
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.andami.LocaleManager;
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.PluginsLocator;
39
import org.gvsig.andami.PluginsManager;
40
import org.gvsig.crs.CrsFactory;
41
import org.gvsig.crs.JCrsExtension;
42
import org.gvsig.installer.lib.api.PackageInfo;
43
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
44
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
45
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.service.spi.AbstractProvider;
48
import org.gvsig.tools.service.spi.ProviderServices;
49
import org.gvsig.tools.task.SimpleTaskStatus;
50
import org.gvsig.tools.task.TaskStatusManager;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
public class EPSGDatabaseInstaller extends AbstractProvider implements InstallPackageProvider {
55

    
56
    private static Logger logger = LoggerFactory.getLogger(EPSGDatabaseInstaller.class);
57

    
58
    private int BUFFER = 2048;
59

    
60
    public EPSGDatabaseInstaller(ProviderServices providerServices) {
61
        super(providerServices);
62
    }
63

    
64
    public void install(File applicationFolder, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException {
65
        PluginsManager pluginsManager = PluginsLocator.getManager();
66
        File i18nFolder = pluginsManager.getApplicationI18nFolder();
67

    
68
        File epsgDatabaseFolder = new File(CrsFactory.getDataBaseFolder(),"EPSG");
69

    
70
        logger.info("Installing package '" + packageInfo.getCode() + "' in '" + epsgDatabaseFolder.getAbsolutePath() + "'.");
71
        try {
72
            FileUtils.forceMkdir(epsgDatabaseFolder);
73
            decompress(inputStream, epsgDatabaseFolder);
74
        } catch (Exception e) {
75
            try {
76
                logger.warn("Can install package '" + packageInfo.getCode() + "'.", e);
77
                // if there is an exception, installLater is called
78
                installLater(applicationFolder, inputStream, packageInfo);
79
            } catch (IOException e1) {
80
                logger.warn("Can install package '" + packageInfo.getCode() + "'.", e1);
81
                throw new InstallPackageServiceException(e1);
82
            }
83
        }
84

    
85
    }
86

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

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

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

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

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

    
133
        } catch (RuntimeException ex) {
134
            logger.warn("Problems uncompresing 'EPSG database' (last entry '" + entryName + "'.", ex);
135
            throw ex;
136

    
137
        } finally {
138
            taskStatus.remove();
139

    
140
        }
141
    }
142

    
143
    public void installLater(File applicationDirectory, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException, IOException {
144
        logger.warn("installLater is not implementes.");
145
    }
146
}