KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > tools > Util


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.tools;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.java.plugin.util.IoUtil;
30
31 /**
32  * @version $Id: Util.java,v 1.1 2006/08/26 15:14:09 ddimon Exp $
33  */

34 final class Util {
35     private static Log log = LogFactory.getLog(Util.class);
36
37     private static File JavaDoc tempFolder;
38     private static boolean tempFolderInitialized = false;
39     
40     static File JavaDoc getTempFolder() throws IOException JavaDoc {
41         if (tempFolder != null) {
42             return tempFolderInitialized ? tempFolder : null;
43         }
44         synchronized (Util.class) {
45             tempFolder = new File JavaDoc(System.getProperty("java.io.tmpdir"), //$NON-NLS-1$
46
System.currentTimeMillis() + ".jpf-tool-cache"); //$NON-NLS-1$
47
log.debug("libraries cache folder is " + tempFolder); //$NON-NLS-1$
48
File JavaDoc lockFile = new File JavaDoc(tempFolder, "lock"); //$NON-NLS-1$
49
if (lockFile.exists()) {
50                 throw new IOException JavaDoc("can't initialize temporary folder " //$NON-NLS-1$
51
+ tempFolder + " as lock file indicates that it is " //$NON-NLS-1$
52
+ "owned by another JPF instance"); //$NON-NLS-1$
53
}
54             if (tempFolder.exists()) {
55                 // clean up folder
56
IoUtil.emptyFolder(tempFolder);
57             } else {
58                 tempFolder.mkdirs();
59             }
60             if (!lockFile.createNewFile()) {
61                 throw new IOException JavaDoc("can\'t create lock file in JPF " //$NON-NLS-1$
62
+ "tool temporary folder " + tempFolder); //$NON-NLS-1$
63
}
64             lockFile.deleteOnExit();
65             tempFolder.deleteOnExit();
66             tempFolderInitialized = true;
67         }
68         return tempFolder;
69     }
70
71     static byte[] readUrlContent(final URL JavaDoc url) throws IOException JavaDoc {
72         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
73         InputStream JavaDoc urlStrm = url.openStream();
74         try {
75             IoUtil.copyStream(urlStrm, result, 256);
76         } finally {
77             urlStrm.close();
78         }
79         return result.toByteArray();
80     }
81     
82     private Util() {
83         // no-op
84
}
85 }
86
Popular Tags