View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.pkg;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URL;
26  import java.util.Properties;
27  import java.util.StringTokenizer;
28  import java.util.zip.ZipInputStream;
29  
30  import org.apache.log4j.Logger;
31  import org.xnap.XNap;
32  import org.xnap.gui.component.ProgressMonitor;
33  import org.xnap.gui.component.ProgressMonitorAdapter;
34  import org.xnap.util.FileHelper;
35  import org.xnap.util.StringHelper;
36  
37  public class PackageUpdater implements Runnable {
38  
39      //--- Constant(s) ---
40  
41      //--- Data field(s) ---
42  
43      private static Logger logger = Logger.getLogger(PackageUpdater.class);
44  
45  	private File file;
46  	private PackageManager manager;
47  	private ProgressMonitor monitor;
48  
49      //--- Constructor(s) ---
50  
51      public PackageUpdater(PackageManager manager, File file,
52  						  ProgressMonitor monitor) 
53      {
54  		this.manager = manager;
55  		this.file = file;
56  		this.monitor = (monitor != null) 
57  			? monitor 
58  			: new ProgressMonitorAdapter();
59      }
60  
61      //--- Method(s) ---
62  
63  	public void run()
64  	{			
65  		try {
66  			if (!file.exists()) {
67  				monitor.setText
68  					(XNap.tr("No sources defined, creating default"));
69  				FileHelper.writeText
70  					(file, manager.getDefaultSources());
71  			}
72  
73  			monitor.setText(XNap.tr("Reading sources"));
74  			String locations[] = FileHelper.readConfig(file);
75  			
76  			monitor.setMinimum(0);
77  			monitor.setMaximum(locations.length + 1);
78  			
79  			manager.markAllUnavailable();
80  			
81  			Properties table = new Properties();
82  			table.put("Available", "True");
83  			table.put("New", "True");
84  
85  			for (int i = 0; i < locations.length; i++) {
86  				monitor.setValue(i + 1);
87  				updateFrom(locations[i], table);
88  			}
89  
90  			manager.removeUnavailable();
91  			monitor.setValue(locations.length + 1);
92  		}
93  		catch (IOException e) {
94  			logger.info("Could not read file: " + file, e);
95  			monitor.setText(XNap.tr("Error reading sources: {0}.", 
96  									e.getLocalizedMessage()));
97  		}
98  		finally {
99  			monitor.done();
100 		}
101 	}
102 
103 	/***
104 	 * Returns the path component of url, including a trailing '/'.
105 	 */
106 	private String chopFilename(String url) 
107 	{
108 		int pos = url.lastIndexOf("/");
109 		return (pos != -1) ? url.substring(0, pos + 1) : "";
110 	}
111 
112 	private String[] readMirrorList(String location) throws IOException
113 	{
114 		URL url = new URL(location);
115 		monitor.setText(XNap.tr("Reading {0}", url.toString()));
116 
117 		InputStream in = url.openStream();
118 		if (location.endsWith(".gz")) {
119 			in = new ZipInputStream(in);
120 		}
121 		return FileHelper.readConfig(in);
122 	}
123 
124 	private void updateFrom(String line, Properties table) 
125 	{
126 		StringTokenizer t = new StringTokenizer(line);
127 		String url = t.nextToken();
128 		if (t.hasMoreTokens()) {
129 			// list of mirrors
130 			String mirrorURL = t.nextToken();
131 			boolean mirrorsSet = false;
132 			if (mirrorURL.endsWith("mirrors.list") 
133 				|| mirrorURL.endsWith("mirrors.list.gz")) {
134 
135 				try {
136 					String[] mirrors = readMirrorList(mirrorURL);
137 					table.put("Download-URLs",
138 							  StringHelper.toString(mirrors, ", "));
139 					mirrorsSet = true;
140 				}
141 				catch (IOException e) {
142 					logger.info("Could not read mirror list", e);
143 				}
144 			}
145 
146 			if (!mirrorsSet) {
147 				table.put("Download-URLs", chopFilename(mirrorURL));
148 			}
149 		}
150 		else {
151 			table.put("Download-URLs", chopFilename(url));
152 		}	
153 
154 		monitor.setText(XNap.tr("Reading {0}", url.toString()));
155 		
156 		try {
157 			manager.read(url, table);
158 		}
159 		catch (IOException e) {
160 			logger.info("Could not read URL", e);
161 		}
162 	}
163 
164     // --- Inner Class(es) ---
165 
166 
167 }