KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > Util


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
20 package org.netbeans.modules.derby;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27 import java.util.zip.ZipInputStream JavaDoc;
28 import org.openide.DialogDisplayer;
29 import org.openide.NotifyDescriptor;
30 import org.openide.filesystems.FileLock;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.Mutex;
34
35 /**
36  *
37  * @author Andrei Badea
38  */

39 public class Util {
40
41     private Util() {
42     }
43
44     public static boolean hasInstallLocation() {
45         return getCheckedLocation() != null;
46     }
47
48     private static File JavaDoc getCheckedLocation() {
49         File JavaDoc location = new File JavaDoc(DerbyOptions.getDefault().getLocation());
50         if (location.isAbsolute() && location.isDirectory() && location.exists()) {
51             return location;
52         }
53         return null;
54     }
55
56     public static File JavaDoc getDerbyFile(String JavaDoc relPath) {
57         File JavaDoc location = getCheckedLocation();
58         if (location != null) {
59             return new File JavaDoc(location, relPath);
60         }
61         return null;
62     }
63
64     public static void showInformation(final String JavaDoc msg){
65         Mutex.EVENT.readAccess(new Runnable JavaDoc() {
66             public void run() {
67                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
68                 DialogDisplayer.getDefault().notify(d);
69             }
70         });
71     }
72
73     public static void extractZip(File JavaDoc source, FileObject target) throws IOException JavaDoc {
74         FileInputStream JavaDoc is = new FileInputStream JavaDoc(source);
75         try {
76             ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(is);
77             ZipEntry JavaDoc ze;
78
79             while ((ze = zis.getNextEntry()) != null) {
80                 String JavaDoc name = ze.getName();
81
82                 // if directory, create
83
if (ze.isDirectory()) {
84                     FileUtil.createFolder(target, name);
85                     continue;
86                 }
87
88                 // if file, copy
89
FileObject fd = FileUtil.createData(target, name);
90                 FileLock lock = fd.lock();
91                 try {
92                     OutputStream JavaDoc os = fd.getOutputStream(lock);
93                     try {
94                         FileUtil.copy(zis, os);
95                     } finally {
96                         os.close();
97                     }
98                 } finally {
99                     lock.releaseLock();
100                 }
101             }
102         } finally {
103             is.close();
104         }
105     }
106 }
107
Popular Tags