gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / packageutils / impl / DefaultPackageManager.java @ 1431
History | View | Annotate | Download (8.23 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 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 |
* 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.tools.packageutils.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FileInputStream; |
28 |
import java.io.IOException; |
29 |
import java.io.InputStream; |
30 |
import java.io.OutputStream; |
31 |
import java.util.Properties; |
32 |
import org.apache.commons.io.IOUtils; |
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
|
35 |
import org.gvsig.tools.exception.BaseException; |
36 |
import org.gvsig.tools.packageutils.Dependencies; |
37 |
import org.gvsig.tools.packageutils.Dependency; |
38 |
import org.gvsig.tools.packageutils.PackageInfo; |
39 |
import org.gvsig.tools.packageutils.PackageManager; |
40 |
import org.gvsig.tools.packageutils.Version; |
41 |
|
42 |
public class DefaultPackageManager implements PackageManager { |
43 |
private String operatingSystemName; |
44 |
private String operatingSystemFamily; |
45 |
private String operatingSystemVersion; |
46 |
|
47 |
@Override
|
48 |
public Version createVersion() {
|
49 |
return new DefaultVersion(); |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public Version createVersion(String version) { |
54 |
Version v = new DefaultVersion();
|
55 |
v.parse(version); |
56 |
return v;
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
public PackageInfo createPackageInfo() {
|
61 |
return new DefaultPackageInfo(); |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public PackageInfo createPackageInfo(InputStream packegeinfo) |
66 |
throws BaseException {
|
67 |
PackageInfo pkg = createPackageInfo(); |
68 |
try {
|
69 |
this.readPacakgeInfo(pkg, packegeinfo);
|
70 |
} catch (IOException e) { |
71 |
throw new BaseIOException(e); |
72 |
} |
73 |
return pkg;
|
74 |
} |
75 |
|
76 |
@Override
|
77 |
public PackageInfo createPackageInfo(File packegeinfo) throws BaseException { |
78 |
PackageInfo pkg = createPackageInfo(); |
79 |
try {
|
80 |
this.readPacakgeInfo(pkg, packegeinfo);
|
81 |
} catch (IOException e) { |
82 |
throw new BaseIOException(packegeinfo, e); |
83 |
} |
84 |
return pkg;
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public Dependency createDependency() {
|
89 |
return new DefaultDependency(); |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public Dependency createDependency(PackageInfo packageInfo) {
|
94 |
DefaultDependency dependency = (DefaultDependency) this.createDependency();
|
95 |
dependency.fromPackageInfo(packageInfo); |
96 |
return dependency;
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
public Dependencies createDependencies() {
|
101 |
return new DefaultDependencies(); |
102 |
} |
103 |
|
104 |
private void loadOperatingSystemInfo() { |
105 |
String osname = System.getProperty("os.name"); |
106 |
String osversion = System.getProperty("os.version"); |
107 |
if (osname.toLowerCase().startsWith("linux")) { |
108 |
this.operatingSystemFamily = OS.LINUX;
|
109 |
FileInputStream fis = null; |
110 |
try {
|
111 |
Properties p = new Properties(); |
112 |
fis = new FileInputStream("/etc/os-release"); |
113 |
p.load(fis); |
114 |
this.operatingSystemName = p.getProperty("ID", null); |
115 |
this.operatingSystemVersion = p.getProperty("VERSION_ID", null); |
116 |
|
117 |
this.operatingSystemName = StringUtils.removeEnd(this.operatingSystemName,"\""); |
118 |
this.operatingSystemName = StringUtils.removeStart(this.operatingSystemName,"\""); |
119 |
this.operatingSystemName = StringUtils.trimToNull(this.operatingSystemName); |
120 |
this.operatingSystemVersion = StringUtils.removeEnd(this.operatingSystemVersion,"\""); |
121 |
this.operatingSystemVersion = StringUtils.removeStart(this.operatingSystemVersion,"\""); |
122 |
this.operatingSystemVersion = StringUtils.trimToNull(this.operatingSystemVersion); |
123 |
} catch(Exception ex) { |
124 |
|
125 |
} finally {
|
126 |
IOUtils.closeQuietly(fis); |
127 |
} |
128 |
|
129 |
} else if (osname.toLowerCase().startsWith("window")) { |
130 |
this.operatingSystemFamily = OS.WINDOWS;
|
131 |
String s = osname.replace(" ", ""); |
132 |
s = s.replace("_", ""); |
133 |
s = s.replace("-", ""); |
134 |
this.operatingSystemName = s;
|
135 |
this.operatingSystemVersion = osversion;
|
136 |
|
137 |
} else {
|
138 |
String s = osname.replace(" ", ""); |
139 |
s = s.replace("_", ""); |
140 |
s = s.replace("-", ""); |
141 |
this.operatingSystemFamily = s;
|
142 |
this.operatingSystemName = null; |
143 |
this.operatingSystemVersion = osversion;
|
144 |
} |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public String getOperatingSystem() { |
149 |
if( this.operatingSystemFamily == null ) { |
150 |
loadOperatingSystemInfo(); |
151 |
} |
152 |
StringBuilder builder = new StringBuilder(); |
153 |
builder.append(this.operatingSystemFamily);
|
154 |
if( this.operatingSystemName!=null ) { |
155 |
builder.append("_");
|
156 |
builder.append(this.operatingSystemName);
|
157 |
if( this.operatingSystemVersion!=null ) { |
158 |
builder.append("_");
|
159 |
builder.append(this.operatingSystemVersion);
|
160 |
} |
161 |
} |
162 |
return builder.toString();
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public String getOperatingSystemFamily() { |
167 |
if( this.operatingSystemFamily == null ) { |
168 |
loadOperatingSystemInfo(); |
169 |
} |
170 |
return this.operatingSystemFamily; |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public String getOperatingSystemName() { |
175 |
if( this.operatingSystemFamily == null ) { |
176 |
loadOperatingSystemInfo(); |
177 |
} |
178 |
return this.operatingSystemName; |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public String getOperatingSystemVersion() { |
183 |
if( this.operatingSystemFamily == null ) { |
184 |
loadOperatingSystemInfo(); |
185 |
} |
186 |
return this.operatingSystemVersion; |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public String getArchitecture() { |
191 |
String osarch = System.getProperty("os.arch"); |
192 |
if (osarch.toLowerCase().startsWith("i386")) { |
193 |
return ARCH.X86;
|
194 |
} |
195 |
if (osarch.toLowerCase().startsWith("i686")) { |
196 |
return ARCH.X86;
|
197 |
} |
198 |
if (osarch.toLowerCase().startsWith("x86_64")) { |
199 |
return ARCH.X86_64;
|
200 |
} |
201 |
if (osarch.toLowerCase().startsWith("x86")) { |
202 |
return ARCH.X86;
|
203 |
} |
204 |
if (osarch.toLowerCase().startsWith("amd64")) { |
205 |
return ARCH.X86_64;
|
206 |
} |
207 |
return osarch;
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void writePacakgeInfo(PackageInfo packageInfo, File file) |
212 |
throws IOException { |
213 |
PackageInfoFileWriter writer = new PackageInfoFileWriter();
|
214 |
writer.write(packageInfo, file); |
215 |
} |
216 |
|
217 |
@Override
|
218 |
public void writePacakgeInfo(PackageInfo pkg, OutputStream os) |
219 |
throws IOException { |
220 |
PackageInfoFileWriter writer = new PackageInfoFileWriter();
|
221 |
writer.write(pkg, os); |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public void readPacakgeInfo(PackageInfo packageInfo, File file) |
226 |
throws IOException { |
227 |
PackageInfoFileReader reader = new PackageInfoFileReader();
|
228 |
reader.read(packageInfo, file); |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void readPacakgeInfo(PackageInfo pkg, InputStream is) |
233 |
throws IOException { |
234 |
PackageInfoFileReader reader = new PackageInfoFileReader();
|
235 |
reader.read(pkg, is); |
236 |
} |
237 |
|
238 |
public class BaseIOException extends BaseException { |
239 |
|
240 |
/**
|
241 |
*
|
242 |
*/
|
243 |
private static final long serialVersionUID = 3154855738763990310L; |
244 |
|
245 |
public BaseIOException(Throwable cause) { |
246 |
super("Can't load package info","_Cant_load_package_info", serialVersionUID); |
247 |
} |
248 |
|
249 |
public BaseIOException(File file, Throwable cause) { |
250 |
super("Can't load package info %(file)","_Cant_load_package_info_XfileX", serialVersionUID); |
251 |
setValue("file",file);
|
252 |
} |
253 |
} |
254 |
|
255 |
} |