Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / execution / PackagesCache.java @ 43345

History | View | Annotate | Download (8.61 KB)

1

    
2
package org.gvsig.installer.lib.impl.execution;
3

    
4
import java.io.BufferedInputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
8
import java.io.InputStream;
9
import java.util.ArrayList;
10
import java.util.HashMap;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Map;
14
import org.gvsig.installer.lib.api.PackageInfo;
15
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
16
import org.gvsig.tools.task.SimpleTaskStatus;
17

    
18

    
19
public class PackagesCache implements Iterable<PackageInfo> {
20

    
21
    private interface CacheEntry {
22
        public PackageInfo getPackage();
23
        public InputStream getInputStream();
24
        public File getFile();
25
        public void download(SimpleTaskStatus taskStatus);
26
        public boolean isDownloaded();
27
    }
28
    
29
    private class PackageCacheEntry implements CacheEntry {
30
        private final PackageInfo pkg;
31
        private final File file;
32
        
33
        PackageCacheEntry(PackageInfo pkg, File file) {
34
            this.pkg = pkg;
35
            this.file = file;
36
        }
37

    
38
        @Override
39
        public PackageInfo getPackage() {
40
            return this.pkg;
41
        }
42
        
43
        @Override
44
        public InputStream getInputStream() {
45
            InputStream bis;
46
            try {
47
                FileInputStream fis = new FileInputStream(file);
48
                bis = new BufferedInputStream(fis);
49
            } catch (FileNotFoundException ex) {
50
                throw new RuntimeException("Can't open package of '"+pkg.getCode()+"'.", ex);
51
            }
52
            return bis;
53
        }
54

    
55
        @Override
56
        public File getFile() {
57
            return this.file;
58
        }
59

    
60
        @Override
61
        public void download(SimpleTaskStatus taskStatus) {
62
        }
63

    
64
        @Override
65
        public boolean isDownloaded() {
66
            return true;
67
        }
68
    }
69
    
70
    private class IndexCacheEntry implements CacheEntry {
71
        private final PackageInfo pkg;
72
        private File file;
73
        
74
        IndexCacheEntry(PackageInfo pkg) {
75
            this.pkg = pkg;
76
            this.file = null;
77
        }
78

    
79
        @Override
80
        public PackageInfo getPackage() {
81
            return this.pkg;
82
        }
83
        
84
        @Override
85
        public void download(SimpleTaskStatus taskStatus) {
86
            if( file == null ) {
87
                try {
88
                    file = pkg.downloadFile(taskStatus);
89
                } catch (Exception ex) {
90
                    throw new RuntimeException("Can't download package of '"+pkg.getCode()+"'.", ex);
91
                }
92
            }
93
        }
94

    
95
        @Override
96
        public File getFile() {
97
            this.download(null);
98
            return this.file;
99
        }
100
        
101
       @Override
102
        public InputStream getInputStream() {
103
            InputStream bis;
104
            try {
105
                if( file == null ) {
106
                    this.download(null);
107
                }
108
                FileInputStream fis = new FileInputStream(this.file);
109
                bis = new BufferedInputStream(fis);
110
            } catch (FileNotFoundException ex) {
111
                throw new RuntimeException("Can't open package of '"+pkg.getCode()+"'.", ex);
112
            }
113
            return bis;
114
        }
115

    
116
        @Override
117
        public boolean isDownloaded() {
118
            return this.file!=null;
119
        }        
120
    }
121

    
122
    private class BundledPackageCacheEntry implements CacheEntry {
123
        private final PackageInfo pkg;
124
        private final File bundleFile;
125
        private final String bundleEntryName;
126
        
127
        BundledPackageCacheEntry(PackageInfo pkg, File bundleFile, String bundleEntryName) {
128
            this.pkg = pkg;
129
            this.bundleFile = bundleFile;
130
            this.bundleEntryName = bundleEntryName;
131
        }
132

    
133
        @Override
134
        public PackageInfo getPackage() {
135
            return this.pkg;
136
        }
137
        
138
        @Override
139
        public File getFile() {
140
            return this.bundleFile;
141
        }
142

    
143
        @Override
144
        public void download(SimpleTaskStatus taskStatus) {
145
        }
146

    
147
        @Override
148
        public InputStream getInputStream() {
149
            InputStream is;
150
            try {
151
                FileInputStream fis = new FileInputStream(bundleFile);
152
                BufferedInputStream bis = new BufferedInputStream(fis);
153
                is = installerProviderServices.searchPackage(bis,bundleEntryName);
154
            } catch (Exception ex) {
155
                throw new RuntimeException("Can't open package of '"+pkg.getCode()+"'.", ex);
156
            }
157
            return is;
158
        }
159

    
160
        @Override
161
        public boolean isDownloaded() {
162
            return true;
163
        }        
164
    }
165

    
166
    private class BundledIndexCacheEntry extends IndexCacheEntry {
167

    
168
        private final File bundleFile;
169
        
170
        BundledIndexCacheEntry(PackageInfo pkg, File bundleFile, String bundleEntryName) {
171
            super(pkg);
172
            this.bundleFile = bundleFile;
173
        }
174

    
175
        @Override
176
        public File getFile() {
177
            return this.bundleFile;
178
        }
179
    }
180

    
181
    private List<CacheEntry> cacheEntries = null;
182
    private Map<PackageInfo, CacheEntry> package2cacheEntry = null;
183
    private InstallPackageProviderServices installerProviderServices;
184
    
185
    public PackagesCache(InstallPackageProviderServices installerProviderServices) {
186
        this.installerProviderServices = installerProviderServices;
187
        this.package2cacheEntry = new HashMap<>();
188
        this.cacheEntries = new ArrayList<>();
189
    }
190
    
191
    public void addPackage(PackageInfo pkg, File f) {
192
        CacheEntry cacheEntry = new PackageCacheEntry(pkg, f);
193
        this.cacheEntries.add(cacheEntry);
194
        this.package2cacheEntry.put(pkg, cacheEntry);
195
    }
196

    
197
    public void addPackageIndex(PackageInfo pkg) {
198
        CacheEntry cacheEntry = new IndexCacheEntry(pkg);
199
        this.cacheEntries.add(cacheEntry);
200
        this.package2cacheEntry.put(pkg, cacheEntry);
201
    }
202

    
203
    public void addBundledPackage(PackageInfo pkg, File bundleFile, String bundleEntryName) {
204
        CacheEntry cacheEntry = new BundledPackageCacheEntry(pkg, bundleFile, bundleEntryName);
205
        this.cacheEntries.add(cacheEntry);
206
        this.package2cacheEntry.put(pkg, cacheEntry );
207
    }
208

    
209
    public void addBundledPackageIndex(PackageInfo pkg, File bundleFile, String bundleEntryName) {
210
        CacheEntry cacheEntry = new BundledIndexCacheEntry(pkg, bundleFile, bundleEntryName);
211
        this.cacheEntries.add(cacheEntry);
212
        this.package2cacheEntry.put(pkg, cacheEntry );
213
    }
214

    
215
    /**
216
     * Return the file associated to this package.
217
     * If the package is a standalone package it return the file of package.
218
     * If the package is a standalone package index, return the file of the 
219
     * associated package, downloading it if need.
220
     * If is a package in a bundle, return the file of the bundle.
221
     * 
222
     * @param packageInfo
223
     * @return 
224
     */
225
    public File getFile(PackageInfo packageInfo) {
226
        CacheEntry entry = this.package2cacheEntry.get(packageInfo);
227
        return entry.getFile();
228
    }
229

    
230
    public boolean contains(PackageInfo packageInfo) {
231
        return this.package2cacheEntry.containsKey(packageInfo);
232
    }
233

    
234
    public boolean isDownloaded(PackageInfo packageInfo) {
235
        CacheEntry entry = this.package2cacheEntry.get(packageInfo);
236
        return entry.isDownloaded();
237
    }
238
    
239
    public void download(PackageInfo packageInfo, SimpleTaskStatus taskStatus) {
240
        CacheEntry entry = this.package2cacheEntry.get(packageInfo);
241
        entry.download(taskStatus);
242
    }
243

    
244
    /**
245
     * Return the InputStream of the package.
246
     * If is a package index, download if need the package and return 
247
     * a InputStream of the package associated to the index.
248
     * If is a package in a bundle, return a InputStream without extract 
249
     * the package of the bundle.
250
     * 
251
     * @param packageInfo
252
     * @return 
253
     */
254
    public InputStream getInputStream(PackageInfo packageInfo) {
255
        CacheEntry entry = this.package2cacheEntry.get(packageInfo);
256
        return entry.getInputStream();
257
    }
258

    
259
    @Override
260
    public Iterator<PackageInfo> iterator() {
261
        return new Iterator<PackageInfo>() {
262
            Iterator<CacheEntry> it = cacheEntries.iterator();
263
            @Override
264
            public boolean hasNext() {
265
                return it.hasNext();
266
            }
267

    
268
            @Override
269
            public PackageInfo next() {
270
                CacheEntry x = it.next();
271
                return x.getPackage();
272
            }
273
        };
274
    }
275

    
276
    public int size() {
277
        return this.package2cacheEntry.size();
278
    }
279
    
280
    public PackageInfo get(int n) {
281
        return this.cacheEntries.get(n).getPackage();
282
    }
283
}