01: /*****************************************************************************
02: * Java Plug-in Framework (JPF)
03: * Copyright (C) 2004-2006 Dmitry Olshansky
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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: *****************************************************************************/package org.java.plugin.tools;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.File;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.net.URL;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28: import org.java.plugin.util.IoUtil;
29:
30: /**
31: * @version $Id$
32: */
33: final class Util {
34: private static Log log = LogFactory.getLog(Util.class);
35:
36: private static File tempFolder;
37: private static boolean tempFolderInitialized = false;
38:
39: static File getTempFolder() throws IOException {
40: if (tempFolder != null) {
41: return tempFolderInitialized ? tempFolder : null;
42: }
43: synchronized (Util.class) {
44: tempFolder = new File(System.getProperty("java.io.tmpdir"), //$NON-NLS-1$
45: System.currentTimeMillis() + ".jpf-tool-cache"); //$NON-NLS-1$
46: log.debug("libraries cache folder is " + tempFolder); //$NON-NLS-1$
47: File lockFile = new File(tempFolder, "lock"); //$NON-NLS-1$
48: if (lockFile.exists()) {
49: throw new IOException(
50: "can't initialize temporary folder " //$NON-NLS-1$
51: + tempFolder
52: + " as lock file indicates that it is " //$NON-NLS-1$
53: + "owned by another JPF instance"); //$NON-NLS-1$
54: }
55: if (tempFolder.exists()) {
56: // clean up folder
57: IoUtil.emptyFolder(tempFolder);
58: } else {
59: tempFolder.mkdirs();
60: }
61: if (!lockFile.createNewFile()) {
62: throw new IOException("can\'t create lock file in JPF " //$NON-NLS-1$
63: + "tool temporary folder " + tempFolder); //$NON-NLS-1$
64: }
65: lockFile.deleteOnExit();
66: tempFolder.deleteOnExit();
67: tempFolderInitialized = true;
68: }
69: return tempFolder;
70: }
71:
72: static byte[] readUrlContent(final URL url) throws IOException {
73: ByteArrayOutputStream result = new ByteArrayOutputStream();
74: InputStream urlStrm = url.openStream();
75: try {
76: IoUtil.copyStream(urlStrm, result, 256);
77: } finally {
78: urlStrm.close();
79: }
80: return result.toByteArray();
81: }
82:
83: private Util() {
84: // no-op
85: }
86: }
|