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 @ 36678

History | View | Annotate | Download (8.02 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
        String name = properties.getProperty(InstallerInfoTags.NAME);
112
        installerInfoResource.setName(name);
113
                installerInfoResource.setDescription(properties
114
                                .getProperty(InstallerInfoTags.DESCRIPTION));
115
                String version  = properties
116
            .getProperty(InstallerInfoTags.VERSION);
117
                if (version == null) {
118
            LOG.warn("Got a null version string for the {} plugin "
119
                + "Ignoring it and leaving the default version value", name);
120
                }
121
                else {
122
                    installerInfoResource.setVersion(version);
123
                }
124
                installerInfoResource.setType(properties
125
                                .getProperty(InstallerInfoTags.TYPE));
126
                String build = properties.getProperty(InstallerInfoTags.BUILD);
127
        if (build != null) {
128
            try {
129
                installerInfoResource.setBuild(Integer.parseInt(build));
130
            } catch (Exception e) {
131
                installerInfoResource.setBuild(0);
132
                LOG.warn("Error while converting to int the "
133
                    + InstallerInfoTags.BUILD + " property value: " + build, e);
134
            }
135
                }
136

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

    
166
                String os = properties.getProperty(InstallerInfoTags.OS);
167
                if (os != null) {
168
                        installerInfoResource.setOperatingSystem(os);
169
                }
170

    
171
                String arch = properties.getProperty(InstallerInfoTags.ARCHITECTURE);
172
                if (arch != null) {
173
                        installerInfoResource.setArchitecture(arch);
174
                }
175

    
176
                String jvm = properties.getProperty(InstallerInfoTags.JVM);
177
                if (jvm != null) {
178
                        installerInfoResource.setJavaVM(jvm);
179
                }
180

    
181
                installerInfoResource.setGvSIGVersion(properties
182
                                .getProperty(InstallerInfoTags.GVSIG_VERSION));
183

    
184
                String urlStr = properties.getProperty(InstallerInfoTags.DOWNLOAD_URL);
185
                if (urlStr != null) {
186
                        URL downloadURL;
187
                        try {
188
                                downloadURL = new URL(urlStr);
189
                        } catch (MalformedURLException e) {
190
                                throw new InstallerInfoFileException(
191
                                                "Error getting the value of the download url property as URL: "
192
                                                                + urlStr, e);
193
                        }
194
                        installerInfoResource.setDownloadURL(downloadURL);
195
                }
196
                
197
                String modelVersion = properties
198
                                .getProperty(InstallerInfoTags.MODEL_VERSION);
199
                if (modelVersion != null) {
200
                        installerInfoResource.setModelVersion(modelVersion);
201
                }
202
                
203
        String owner = properties.getProperty(InstallerInfoTags.OWNER);
204
        if (owner != null) {
205
            installerInfoResource.setOwner(owner);
206
        }
207
       
208
        String dependencies = properties.getProperty(InstallerInfoTags.DEPENDENCIES);
209
        if (dependencies != null) {
210
            installerInfoResource.setDependencies(dependencies);
211
        }
212
        
213
        String sourcesUrlStr = properties.getProperty(InstallerInfoTags.SOURCES_URL);
214
        if (sourcesUrlStr != null && !sourcesUrlStr.equals("")){
215
            URL sourcesURL;
216
            try {
217
                sourcesURL = new URL(sourcesUrlStr);
218
            } catch (MalformedURLException e) {
219
                System.err.println("code: " + properties
220
                    .getProperty(InstallerInfoTags.CODE));
221
                System.err.println("id: " + installerInfoResource.getID());
222
                throw new InstallerInfoFileException(
223
                        "Error getting the value of the sources url property as URL: "
224
                                + sourcesUrlStr, e);
225
            }
226
            installerInfoResource.setSourcesURL(sourcesURL);
227
        } else {
228
            installerInfoResource.setSourcesURL(null);
229
        }
230
        
231
        }
232

    
233
}