Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / info / InstallerInfoFileReader.java @ 34425

History | View | Annotate | Download (6.48 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27

    
28
package org.gvsig.installer.lib.impl.info;
29

    
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileNotFoundException;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.net.MalformedURLException;
36
import java.net.URL;
37
import java.util.Properties;
38

    
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
import org.gvsig.installer.lib.api.PackageInfo;
43
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
44

    
45
/**
46
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
47
 */
48
public class InstallerInfoFileReader {
49

    
50
    private static final Logger LOG = LoggerFactory
51
        .getLogger(InstallerInfoFileReader.class);
52

    
53
        /**
54
         * Reads and parses the install.info file and creates one object that
55
         * contains the information.
56
         * 
57
         * @param fileName
58
         *            The file name that contains the install.info information.
59
         * @return An object with the information about the installation
60
         * @throws InstallerInfoFileException
61
         */
62
        public void read(PackageInfo installerInfoResource, String fileName)
63
                        throws InstallerInfoFileException {
64
                read(installerInfoResource, new File(fileName));
65
        }
66

    
67
        /**
68
         * Reads and parses the install.info file and creates one object that
69
         * contains the information.
70
         * 
71
         * @param file
72
         *            The file that contains the install.info information.
73
         * @return An object with the information about the installation
74
         * @throws InstallerInfoFileException
75
         */
76
        public void read(PackageInfo installerInfoResource, File file)
77
                        throws InstallerInfoFileException {
78
                if (!file.exists()) {
79
                        throw new InstallerInfoFileException("install_infofile_not_found");
80
                }
81
                try {
82
                        read(installerInfoResource, new FileInputStream(file));
83
                } catch (FileNotFoundException e) {
84
                        throw new InstallerInfoFileException("install_infofile_not_found",
85
                                        e);
86
                }
87
        }
88

    
89
        /**
90
         * Reads and parses the install.info file and creates one object that
91
         * contains the information.
92
         * 
93
         * @param is
94
         *            The input stream that contains the install.info information.
95
         * @return An object with the information about the installation
96
         * @throws InstallerInfoFileException
97
         */
98
        public void read(PackageInfo installerInfoResource, InputStream is)
99
                        throws InstallerInfoFileException {
100
                Properties properties = new Properties();
101
                try {
102
                        properties.load(is);
103
                        is.close();
104
                } catch (IOException e) {
105
                        throw new InstallerInfoFileException(
106
                                        "install_infofile_reading_error", e);
107
                }
108

    
109
                installerInfoResource.setCode(properties
110
                                .getProperty(InstallerInfoTags.CODE));
111
                installerInfoResource.setName(properties
112
                                .getProperty(InstallerInfoTags.NAME));
113
                installerInfoResource.setDescription(properties
114
                                .getProperty(InstallerInfoTags.DESCRIPTION));
115
                installerInfoResource.setVersion(properties
116
                                .getProperty(InstallerInfoTags.VERSION));
117
                installerInfoResource.setType(properties
118
                                .getProperty(InstallerInfoTags.TYPE));
119
                String build = properties.getProperty(InstallerInfoTags.BUILD);
120
                try {
121
                        installerInfoResource.setBuild(Integer.parseInt(build));
122
                } catch (Exception e) {
123
                        installerInfoResource.setBuild(0);
124
            LOG.warn("Error while converting to int the "
125
                + InstallerInfoTags.BUILD + " property value: " + build, e);
126
                }
127

    
128
        // Look for the old build tag just in case (the new one is buildNumber).
129
        if (installerInfoResource.getBuild() <= 0) {
130
            String oldBuild =
131
                properties.getProperty(InstallerInfoTags.BUILD_OLD);
132
            if (oldBuild != null) {
133
                try {
134
                    installerInfoResource.setBuild(Integer
135
                        .parseInt(oldBuild));
136
                } catch (Exception e) {
137
                    installerInfoResource.setBuild(0);
138
                    LOG.warn("Error while converting to int the "
139
                        + InstallerInfoTags.BUILD_OLD + " property value: "
140
                        + oldBuild, e);
141
                }
142
            }
143
        }
144
                installerInfoResource.setState(properties
145
                                .getProperty(InstallerInfoTags.STATE));
146
        try {
147
            installerInfoResource.setOfficial(Boolean.parseBoolean(properties
148
                .getProperty(InstallerInfoTags.OFFICIAL)));
149
        } catch (Exception e) {
150
            installerInfoResource.setOfficial(false);
151
            LOG.warn(
152
                "Error while converting to boolean the "
153
                    + InstallerInfoTags.OFFICIAL + " property value: "
154
                    + properties.getProperty(InstallerInfoTags.OFFICIAL), e);
155
        }
156

    
157
                String os = properties.getProperty(InstallerInfoTags.OS);
158
                if (os != null) {
159
                        installerInfoResource.setOperatingSystem(os);
160
                }
161

    
162
                String arch = properties.getProperty(InstallerInfoTags.ARCHITECTURE);
163
                if (arch != null) {
164
                        installerInfoResource.setArchitecture(arch);
165
                }
166

    
167
                String jvm = properties.getProperty(InstallerInfoTags.JVM);
168
                if (jvm != null) {
169
                        installerInfoResource.setJavaVM(jvm);
170
                }
171

    
172
                installerInfoResource.setGvSIGVersion(properties
173
                                .getProperty(InstallerInfoTags.GVSIG_VERSION));
174

    
175
                String urlStr = properties.getProperty(InstallerInfoTags.DOWNLOAD_URL);
176
                if (urlStr != null) {
177
                        URL downloadURL;
178
                        try {
179
                                downloadURL = new URL(urlStr);
180
                        } catch (MalformedURLException e) {
181
                                throw new InstallerInfoFileException(
182
                                                "Error getting the value of the download url property as URL: "
183
                                                                + urlStr, e);
184
                        }
185
                        installerInfoResource.setDownloadURL(downloadURL);
186
                }
187

    
188
                String modelVersion = properties
189
                                .getProperty(InstallerInfoTags.MODEL_VERSION);
190
                if (modelVersion != null) {
191
                        installerInfoResource.setModelVersion(modelVersion);
192
                }
193
        }
194

    
195
}