001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.downloader.impl;
038:
039: import org.netbeans.installer.downloader.Pumping;
040: import org.netbeans.installer.downloader.DownloadMode;
041: import java.io.File;
042: import java.io.IOException;
043: import java.net.URL;
044: import java.util.Date;
045: import java.util.LinkedList;
046: import java.util.List;
047: import org.netbeans.installer.downloader.Pumping.Section;
048: import org.netbeans.installer.downloader.queue.QueueBase;
049: import org.netbeans.installer.utils.ErrorManager;
050: import org.netbeans.installer.utils.StringUtils;
051: import org.netbeans.installer.utils.exceptions.ParseException;
052: import org.netbeans.installer.utils.xml.DomExternalizable;
053: import org.netbeans.installer.utils.xml.DomUtil;
054: import org.netbeans.installer.utils.xml.visitors.DomVisitor;
055: import org.netbeans.installer.utils.xml.visitors.RecursiveDomVisitor;
056: import org.w3c.dom.Document;
057: import org.w3c.dom.Element;
058:
059: /**
060: * @author Danila_Dugurov
061: */
062:
063: public class PumpingImpl implements Pumping, DomExternalizable {
064:
065: /////////////////////////////////////////////////////////////////////////////////
066: // Static
067: private static int nextId;
068:
069: /////////////////////////////////////////////////////////////////////////////////
070: // Instance
071: private final transient String id;
072: private final QueueBase queue;
073:
074: protected URL url;
075: protected URL realUrl;
076: protected File folder;
077: protected File file;
078: protected long length = -2;
079: protected boolean acceptBytes = false;
080: protected Date lastModif = new Date(0);
081: protected List<SectionImpl> sections = new LinkedList<SectionImpl>();
082: protected State state = State.NOT_PROCESSED;
083: protected DownloadMode mode = DownloadMode.SINGLE_THREAD;
084:
085: public PumpingImpl(URL url, File folder, QueueBase queue) {
086: this (queue);
087: this .url = url;
088: this .folder = folder;
089: }
090:
091: //before read from xml
092: public PumpingImpl(QueueBase queue) {
093: this .id = getClass().getName() + nextId++;
094: this .queue = queue;
095: }
096:
097: public String getId() {
098: return id;
099: }
100:
101: public URL declaredURL() {
102: return url;
103: }
104:
105: public URL realURL() {
106: return realUrl;
107: }
108:
109: public File outputFile() {
110: return file;
111: }
112:
113: public File folder() {
114: return folder;
115: }
116:
117: public long length() {
118: return length;
119: }
120:
121: public DownloadMode mode() {
122: return mode;
123: }
124:
125: public State state() {
126: return state;
127: }
128:
129: public void changeState(State newState) {
130: this .state = newState;
131: fireChanges("pumpingStateChange");
132: }
133:
134: public Section[] getSections() {
135: return sections.toArray(new Section[sections.size()]);
136: }
137:
138: public void fireChanges(String method) {
139: queue.fire(method, id);
140: }
141:
142: public SectionImpl getSection() {
143: if (mode == DownloadMode.SINGLE_THREAD || !acceptBytes) {
144: if (sections.isEmpty())
145: sections.add(new SectionImpl(this , 0, length));
146: return sections.get(0);
147: }
148: throw new UnsupportedOperationException(
149: "multi mode not supported yet!");
150: }
151:
152: public void readXML(Element element) {
153: final DomVisitor visitor = new RecursiveDomVisitor() {
154: public void visit(Element element) {
155: final String name = element.getNodeName();
156: if ("url".equals(name)) {
157: try {
158: url = StringUtils.parseUrl(element
159: .getTextContent());
160: } catch (ParseException e) {
161: ErrorManager.notifyDebug("Could not parse URL",
162: e);
163: }
164: } else if ("realUrl".equals(name)) {
165: try {
166: realUrl = StringUtils.parseUrl(element
167: .getTextContent());
168: } catch (ParseException e) {
169: ErrorManager.notifyDebug("Could not parse URL",
170: e);
171: }
172: } else if ("length".equals(name)) {
173: length = Long.valueOf(element.getTextContent());
174: } else if ("lastModif".equals(name)) {
175: lastModif = new Date(Long.valueOf(element
176: .getTextContent()));
177: } else if ("acceptBytes".equals(name)) {
178: acceptBytes = Boolean.valueOf(element
179: .getTextContent());
180: } else if ("state".equals(name)) {
181: state = State.valueOf(element.getTextContent());
182: } else if ("file".equals(name)) {
183: if (!"".equals(element.getTextContent().trim()))
184: file = new File(element.getTextContent());
185: } else if ("folder".equals(name)) {
186: folder = new File(element.getTextContent());
187: } else if ("section".equals(name)) {
188: SectionImpl section = new SectionImpl(
189: PumpingImpl.this );
190: section.readXML(element);
191: sections.add(section);
192: } else
193: super .visit(element);
194: }
195: };
196: visitor.visit(element);
197: }
198:
199: public Element writeXML(Document document) {
200: final Element root = document.createElement("pumping");
201: DomUtil.addElement(root, "url", url.toString());
202: DomUtil.addElement(root, "realUrl", realUrl != null ? realUrl
203: .toString() : null);
204: DomUtil.addElement(root, "length", String.valueOf(length));
205: DomUtil.addElement(root, "lastModif", String.valueOf(lastModif
206: .getTime()));
207: DomUtil.addElement(root, "acceptBytes", String
208: .valueOf(acceptBytes));
209: DomUtil.addElement(root, "state", state.toString());
210: DomUtil.addElement(root, "file", file != null ? file
211: .getAbsolutePath() : null);
212: DomUtil.addElement(root, "folder", folder.getAbsolutePath());
213: for (SectionImpl section : sections) {
214: DomUtil.addChild(root, section);
215: }
216: return root;
217: }
218:
219: public void init(URL realUrl, long length, Date lastModif,
220: boolean acceptBytes) throws IOException {
221: if (wasModified(realUrl, length, lastModif, acceptBytes)) {
222: reset();
223: this .realUrl = realUrl;
224: this .length = length;
225: this .acceptBytes = acceptBytes;
226: if (lastModif != null)
227: this .lastModif = lastModif;
228: this .file = PumpingUtil.getFileNameFromURL(folder,
229: this .realUrl.getPath());
230: if (file.getParentFile() != null)
231: file.getParentFile().mkdirs();
232: file.createNewFile();
233: }
234: }
235:
236: public void reset() {
237: realUrl = null;
238: length = -2;
239: acceptBytes = false;
240: lastModif = new Date(0);
241: sections = new LinkedList<SectionImpl>();
242: if (file != null)
243: file.delete();
244: }
245:
246: protected boolean wasModified(URL realUrl, long length,
247: Date lastModif, boolean acceptBytes) {
248: if (this .realUrl == null || !this .realUrl.equals(realUrl))
249: return true;
250: if (this .length == -2 || this .length != length)
251: return true;
252: if (lastModif != null && this .lastModif.before(lastModif))
253: return true;
254: return this.acceptBytes != acceptBytes;
255: }
256: }
|