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-2006 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.upgrade.systemoptions;
043:
044: import java.io.FilterOutputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.Date;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Properties;
054: import org.openide.filesystems.FileLock;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.filesystems.Repository;
058:
059: /**
060: * @author Radek Matous
061: */
062: class PropertiesStorage {
063: private static final String USERROOT_PREFIX = "/Preferences";//NOI18N
064: private final static FileObject SFS_ROOT = Repository.getDefault()
065: .getDefaultFileSystem().getRoot();
066:
067: private final String folderPath;
068: private String filePath;
069:
070: static PropertiesStorage instance(final String absolutePath) {
071: return new PropertiesStorage(absolutePath);
072: }
073:
074: FileObject preferencesRoot() throws IOException {
075: return FileUtil.createFolder(SFS_ROOT, USERROOT_PREFIX);
076: }
077:
078: /** Creates a new instance */
079: private PropertiesStorage(final String absolutePath) {
080: StringBuffer sb = new StringBuffer();
081: sb.append(USERROOT_PREFIX).append(absolutePath);
082: folderPath = sb.toString();
083: }
084:
085: public Properties load() throws IOException {
086: try {
087: Properties retval = new Properties();
088: InputStream is = inputStream();
089: if (is != null) {
090: try {
091: retval.load(is);
092: } finally {
093: if (is != null)
094: is.close();
095: }
096: }
097: return retval;
098: } finally {
099: }
100: }
101:
102: public void save(final Properties properties) throws IOException {
103: if (!properties.isEmpty()) {
104: OutputStream os = null;
105: try {
106: os = outputStream();
107: properties.store(os, new Date().toString());//NOI18N
108: } finally {
109: if (os != null)
110: os.close();
111: }
112: } else {
113: FileObject file = toPropertiesFile();
114: if (file != null) {
115: file.delete();
116: }
117: FileObject folder = toFolder();
118: while (folder != null && folder != preferencesRoot()
119: && folder.getChildren().length == 0) {
120: folder.delete();
121: folder = folder.getParent();
122: }
123: }
124: }
125:
126: private InputStream inputStream() throws IOException {
127: FileObject file = toPropertiesFile(false);
128: return (file == null) ? null : file.getInputStream();
129: }
130:
131: private OutputStream outputStream() throws IOException {
132: FileObject fo = toPropertiesFile(true);
133: final FileLock lock = fo.lock();
134: final OutputStream os = fo.getOutputStream(lock);
135: return new FilterOutputStream(os) {
136: public void close() throws IOException {
137: super .close();
138: lock.releaseLock();
139: }
140: };
141: }
142:
143: private String folderPath() {
144: return folderPath;
145: }
146:
147: private String filePath() {
148: if (filePath == null) {
149: String[] all = folderPath().split("/");//NOI18N
150: StringBuilder sb = new StringBuilder();
151: for (int i = 0; i < all.length - 1; i++) {
152: sb.append(all[i]).append("/");//NOI18N
153: }
154: if (all.length > 0) {
155: sb.append(all[all.length - 1]).append(".properties");//NOI18N
156: } else {
157: sb.append("root.properties");//NOI18N
158: }
159: filePath = sb.toString();
160: }
161: return filePath;
162: }
163:
164: protected FileObject toFolder() {
165: return SFS_ROOT.getFileObject(folderPath);
166: }
167:
168: protected FileObject toPropertiesFile() {
169: return SFS_ROOT.getFileObject(filePath());
170: }
171:
172: protected FileObject toFolder(boolean create) throws IOException {
173: FileObject retval = toFolder();
174: if (retval == null && create) {
175: retval = FileUtil.createFolder(SFS_ROOT, folderPath);
176: }
177: assert (retval == null && !create)
178: || (retval != null && retval.isFolder());
179: return retval;
180: }
181:
182: protected FileObject toPropertiesFile(boolean create)
183: throws IOException {
184: FileObject retval = toPropertiesFile();
185: if (retval == null && create) {
186: retval = FileUtil.createData(SFS_ROOT, filePath());//NOI18N
187: }
188: assert (retval == null && !create)
189: || (retval != null && retval.isData());
190: return retval;
191: }
192: }
|