KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > utils > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.localhistory.utils;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import org.netbeans.modules.localhistory.store.StoreEntry;
25 import org.openide.ErrorManager;
26 import org.openide.filesystems.FileAlreadyLockedException;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.nodes.Node;
30
31 /**
32  * Local History specific utilities.
33  *
34  * @author Tomas Stupka
35  */

36 public class Utils {
37     
38     public static void revert(final Node[] nodes) {
39         for(Node node : nodes) {
40             StoreEntry se = node.getLookup().lookup(StoreEntry.class);
41             if(se != null) {
42                 Utils.revert(se);
43             }
44         }
45     }
46     
47     public static void revert(StoreEntry se) {
48         InputStream JavaDoc is = null;
49         OutputStream JavaDoc os = null;
50         try {
51             FileObject fo = FileUtil.toFileObject(se.getFile());
52             if(se.getStoreFile() != null) { // XXX change this semantic to isDeleted() or something similar
53
if(fo == null) {
54                     fo = FileUtil.createData(se.getFile());
55                 }
56                 os = getOutputStream(fo);
57                 is = se.getStoreFileInputStream();
58                 FileUtil.copy(is, os);
59             } else {
60                 fo.delete();
61             }
62         } catch (Exception JavaDoc e) {
63             ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
64         } finally {
65             try {
66                 if(os != null) { os.close(); }
67                 if(is != null) { is.close(); }
68             } catch (IOException JavaDoc e) {}
69         }
70     }
71     
72     private static OutputStream JavaDoc getOutputStream(FileObject fo) throws FileAlreadyLockedException, IOException JavaDoc, InterruptedException JavaDoc {
73         int retry = 0;
74         while (true) {
75             try {
76                 return fo.getOutputStream();
77             } catch (IOException JavaDoc ioe) {
78                 retry++;
79                 if (retry > 7) {
80                     throw ioe;
81                 }
82                 Thread.sleep(retry * 30);
83             }
84         }
85     }
86     
87 }
88
Popular Tags