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
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.repository.disk;
043:
044: import java.io.File;
045: import java.io.UnsupportedEncodingException;
046: import java.net.URLEncoder;
047: import java.util.HashMap;
048: import java.util.Map;
049: import java.util.concurrent.ConcurrentHashMap;
050: import org.netbeans.modules.cnd.repository.testbench.Stats;
051:
052: /**
053: *
054: * @author Sergey Grinev
055: */
056: public class StorageAllocator {
057: private final static StorageAllocator instance = new StorageAllocator();
058: private String diskRepositoryPath;
059:
060: private StorageAllocator() {
061: diskRepositoryPath = System
062: .getProperty("cnd.repository.cache.path");
063: if (diskRepositoryPath == null) {
064: long index = 0;
065: diskRepositoryPath = System.getProperty("java.io.tmpdir");
066:
067: diskRepositoryPath += File.separator + //NOI18N
068: System.getProperty("user.name") + "-cnd60-caches-"; //NOI18N
069:
070: File diskRepositoryFile = new File(diskRepositoryPath
071: + index);
072: // find name for directory which is not occupied by file
073:
074: while (diskRepositoryFile.exists()
075: && !diskRepositoryFile.isDirectory()) {
076: diskRepositoryFile = new File(diskRepositoryPath
077: + ++index);
078: }
079: // create directory if needed
080: if (!diskRepositoryFile.exists()) {
081: diskRepositoryFile.mkdirs();
082: }
083: diskRepositoryPath = diskRepositoryFile.getAbsolutePath();
084:
085: //System.out.println("Repository location is " + diskRepositoryPath);
086: }
087: };
088:
089: public static StorageAllocator getInstance() {
090: return instance;
091: }
092:
093: private Map<String, String> unit2path = new ConcurrentHashMap<String, String>();
094:
095: public String getCachePath() {
096: return diskRepositoryPath;
097: }
098:
099: public String reduceString(String name) {
100: if (name.length() > 128) {
101: int hashCode = name.hashCode();
102: name = name.substring(0, 64) + "--"
103: + name.substring(name.length() - 32); // NOI18N
104: name += hashCode;
105: }
106: return name;
107: }
108:
109: public String getUnitStorageName(String unit) {
110: String path = unit2path.get(unit);
111: if (path == null) {
112: String prefix = unit;
113: try {
114: prefix = URLEncoder.encode(unit, Stats.ENCODING);
115: } catch (UnsupportedEncodingException ex) {
116: ex.printStackTrace();
117: }
118:
119: prefix = reduceString(prefix);
120:
121: path = getCachePath() + File.separator + prefix
122: + File.separator; // NOI18N
123:
124: File pathFile = new File(path);
125:
126: if (!pathFile.exists()) {
127: pathFile.mkdirs();
128: }
129:
130: unit2path.put(unit, path);
131: }
132: return path;
133: }
134:
135: public void closeUnit(String unitName) {
136: unit2path.remove(unitName);
137: }
138:
139: public void deleteUnitFiles(String unitName,
140: boolean removeUnitFolder) {
141: if (Stats.TRACE_UNIT_DELETION)
142: System.err.printf("Deleting unit files for %s\n", unitName);
143: String path = getUnitStorageName(unitName);
144: File pathFile = new File(path);
145: deleteDirectory(pathFile, removeUnitFolder);
146: }
147:
148: private void deleteDirectory(File path, boolean deleteDir) {
149: if (path.exists()) {
150: File[] files = path.listFiles();
151: for (int i = 0; i < files.length; i++) {
152: if (files[i].isDirectory()) {
153: deleteDirectory(files[i], true);
154: } else {
155: files[i].delete();
156: }
157: }
158: }
159: if (deleteDir) {
160: path.delete();
161: }
162: }
163:
164: public void cleanRepositoryCaches() {
165: File repositoryPath = new File(diskRepositoryPath);
166: deleteDirectory(repositoryPath, false);
167: }
168: }
|