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: package org.netbeans.modules.collab.channel.filesharing.filesystem;
042:
043: import com.sun.collablet.CollabSession;
044:
045: import org.openide.*;
046: import org.openide.filesystems.*;
047: import org.openide.loaders.*;
048: import org.openide.util.*;
049:
050: import java.awt.Image;
051:
052: import java.io.*;
053:
054: import java.util.*;
055:
056: import org.netbeans.modules.collab.channel.filesharing.FilesharingConstants;
057: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
058: import org.netbeans.modules.collab.channel.filesharing.filehandler.CollabFileHandler;
059: import org.netbeans.modules.collab.channel.filesharing.ui.FilesharingCollabletFactorySettings;
060: import org.netbeans.modules.collab.channel.filesharing.ui.ProjectsRootNode;
061: import org.netbeans.modules.collab.channel.filesharing.util.FileshareUtil;
062: import org.netbeans.modules.collab.core.Debug;
063:
064: /**
065: *
066: * @author Todd Fast <todd.fast@sun.com>
067: */
068: public class CollabFilesystem extends LocalFileSystem implements
069: FileSystem.Status, FilesharingConstants {
070: ////////////////////////////////////////////////////////////////////////////
071: // Class variables
072: ////////////////////////////////////////////////////////////////////////////
073: private static final long serialVersionUID = 1L; // DO NOT CHANGE!
074: public static final String SYSTEM_NAME = "Collaboration"; // NOI18N
075: public static final String EDITOR_ANNOTATION_SOURCE = "editor.name.annotation.source";
076: public static final String EDITOR_ANNOTATION = "editor.name.annotation";
077: public static final String EDITOR_ANNOTATION_TEMPLATE = "editor.name.annotation.html.template";
078: public static final String ANNOTATION_SOURCE_FILESHARING = "com.sun.tools.ide.collab.channel.filesharing"; // NOI18N
079:
080: ////////////////////////////////////////////////////////////////////////////
081: // Instance variables
082: ////////////////////////////////////////////////////////////////////////////
083: private String filesystemID = null;
084: private FilesharingContext context = null;
085: private File collabFileRoot;
086: private String rPath = null;
087:
088: /**
089: *
090: * @param context
091: * @param filesystemID
092: */
093: public CollabFilesystem(FilesharingContext context,
094: String filesystemID) {
095: super ();
096: this .filesystemID = filesystemID;
097: this .context = context;
098:
099: String collabFileRootDir = getCollabRoot(this .filesystemID);
100: collabFileRoot = new File(collabFileRootDir);
101:
102: if (collabFileRoot.exists()) {
103: collabFileRootDir += ("_" + System.currentTimeMillis());
104: collabFileRoot = new File(collabFileRootDir);
105: }
106:
107: Debug.log("CollabFilesystem", "collabFileRootDir is:"
108: + collabFileRootDir);
109: createCollabFileRoot(collabFileRoot);
110: collabFileRoot.mkdirs();
111:
112: try {
113: super .setRootDirectory(collabFileRoot);
114: } catch (Exception e) {
115: ErrorManager.getDefault().notify(e);
116: }
117: }
118:
119: /**
120: *
121: *
122: */
123: public FilesharingContext getContext() {
124: return context;
125: }
126:
127: /**
128: *
129: * @return filesystemID
130: */
131: public String getID() {
132: return this .filesystemID; // NOI18N
133: }
134:
135: /**
136: *
137: * @return filesystem root
138: */
139: public String getCollabRoot(String filesystemID) {
140: return getSharedSystemFolder() + File.separator + filesystemID;
141: }
142:
143: /**
144: *
145: * @return filesystem root
146: */
147: public File getCollabRoot() {
148: return collabFileRoot;
149: }
150:
151: /**
152: *
153: * @return display name
154: */
155: public String getDisplayName() {
156: return getDisplayName(false);
157: }
158:
159: /**
160: *
161: * @param isChannelView
162: * @return displayName
163: */
164: public String getDisplayName(boolean isChannelView) {
165: String rootDisplayName = NbBundle.getMessage(
166: ProjectsRootNode.class,
167: "LBL_ProjectsRootNode_DisplayName"); //NoI18n
168:
169: if (isChannelView) {
170: return rootDisplayName;
171: } else {
172: return NbBundle.getMessage(CollabFilesystem.class,
173: "LBL_CollabFilesystem_FilesystemDisplayName", //NoI18n
174: rootDisplayName, this .filesystemID);
175: }
176: }
177:
178: /**
179: *
180: * @param name
181: * @param files
182: * @return file/folder annotation
183: */
184: public String annotateName(String name, Set files) {
185: String annotation = NbBundle.getMessage(CollabFilesystem.class,
186: "LBL_CollabFilesystem_SharedFileAnnotation", name);
187:
188: return annotation;
189: }
190:
191: /**
192: *
193: * @param image
194: * @param iconType
195: * @param files
196: * @return icon image
197: */
198: public Image annotateIcon(Image image, int iconType, Set files) {
199: return image;
200: }
201:
202: ////////////////////////////////////////////////////////////////////////////
203: // File management methods
204: ////////////////////////////////////////////////////////////////////////////
205:
206: /**
207: *
208: * @param name
209: * @throws IOException
210: */
211: public void createData(String name) throws IOException {
212: super .createData(name);
213:
214: if (name.startsWith(FILE_SEPERATOR)) {
215: name = name.substring(1);
216: }
217: }
218:
219: /**
220: *
221: * @param name
222: * @throws IOException
223: */
224: public void delete(String name) throws IOException {
225: super .delete(name);
226:
227: if (name.startsWith(FILE_SEPERATOR)) {
228: name = name.substring(1);
229: }
230: }
231:
232: /**
233: *
234: *
235: */
236: private String getSharedSystemFolder() {
237: String userHome = System.getProperty("netbeans.user"); // NOI18N
238: String sharedFiles = userHome + File.separator + "collab" + // NOI18N
239: File.separator + "shared_files"; // NOI18N
240:
241: return sharedFiles;
242: }
243:
244: /**
245: *
246: *
247: */
248: private void createCollabFileRoot(File collabFileRoot) {
249: int max = FilesharingCollabletFactorySettings.getDefault()
250: .getMaxSharedFileFolders().intValue();
251:
252: if (max < 1) {
253: max = Integer.MAX_VALUE;
254: }
255:
256: CollabSession[] sessions = getContext().getConversation()
257: .getCollabSession().getManager().getSessions();
258: int numOfSessions = sessions.length;
259: int convs = 0;
260:
261: for (int i = 0; i < numOfSessions; i++) {
262: convs += sessions[i].getConversations().length;
263: }
264:
265: File shared = new File(getSharedSystemFolder());
266:
267: if (shared.exists()) {
268: File[] dirs = shared.listFiles();
269:
270: while ((dirs != null) && (dirs.length >= max)
271: && (max > convs)) {
272: deleteOldestDirectory(dirs);
273: dirs = shared.listFiles();
274: }
275: }
276:
277: collabFileRoot.mkdirs();
278: }
279:
280: /**
281: *
282: *
283: */
284: private void deleteOldestDirectory(File[] dirs) {
285: long lastModified = 0;
286: long oldest = 0;
287: File oldestFolder = new File("temp");
288:
289: for (int i = 0; i < dirs.length; i++) {
290: lastModified = dirs[i].lastModified();
291:
292: if (lastModified == 0) {
293: continue;
294: }
295:
296: if (i == 0) {
297: oldest = lastModified;
298: oldestFolder = dirs[i];
299: } else {
300: if (lastModified < oldest) {
301: oldest = lastModified;
302: oldestFolder = dirs[i];
303: }
304: }
305: }
306:
307: try {
308: deleteFolder(oldestFolder);
309: } catch (Exception e) {
310: Debug.debugNotify(e);
311: e.printStackTrace();
312: }
313: }
314:
315: /**
316: * delete file/folder
317: *
318: * @param file/folder
319: */
320: public void deleteFolder(File file) {
321: if (file == null) {
322: return;
323: }
324:
325: if (file.isFile()) {
326: file.delete();
327: } else {
328: File[] files = file.listFiles();
329:
330: if (files != null) {
331: for (int i = 0; i < files.length; i++) {
332: deleteFolder(files[i]);
333: }
334: }
335:
336: //Delete self
337: file.delete();
338: }
339: }
340:
341: /**
342: *
343: *
344: */
345: public void cleanup() {
346: if ((collabFileRoot.list() == null)
347: || (collabFileRoot.list().length == 0)) {
348: collabFileRoot.delete();
349: }
350: }
351:
352: public String getPath(DataFolder folder) {
353: String path = folder.getPrimaryFile().getPath();
354:
355: if (path.startsWith("/") || path.startsWith("\\")) {
356: path = path.substring(1);
357: }
358:
359: String rootPath = getCollabRoot().getAbsolutePath();
360:
361: if (rootPath.startsWith("/") || rootPath.startsWith("\\")) {
362: rootPath = rootPath.substring(1);
363: }
364:
365: return path.substring(rootPath.length() + 1);
366: }
367:
368: public String getPath() {
369: if (rPath == null) {
370: rPath = FileshareUtil.getNormalizedPath(getCollabRoot()
371: .getPath());
372: }
373:
374: return rPath;
375: }
376:
377: public String getPath(FileObject fObj) {
378: if (fObj == null) {
379: return null;
380: }
381:
382: if (fObj.getPath().indexOf(getPath()) != -1) {
383: DataFolder df = null;
384:
385: try {
386: df = (DataFolder) DataFolder.find(fObj.getParent());
387: } catch (DataObjectNotFoundException ddnf) {
388: ddnf.printStackTrace(Debug.out);
389: }
390:
391: if (df != null) {
392: return FileshareUtil.getNormalizedPath(getPath(df)
393: + FILE_SEPERATOR + fObj.getNameExt()); //NoI18n
394: } else {
395: return fObj.getPath(); //NoI18n
396: }
397: } else {
398: try {
399: CollabFileHandler fh = getContext()
400: .getSharedFileGroupManager().getFileHandler(
401: fObj);
402:
403: if (fh != null) {
404: Debug.out.println("getPath: for file: "
405: + fObj.getPath() + " is: " + fh.getName());
406:
407: return fh.getName();
408: }
409: } catch (Throwable th) {
410: th.printStackTrace(Debug.out);
411: }
412: }
413:
414: return null;
415: }
416:
417: public String getPath(DataFolder folder, FileObject fObj) {
418: return getPath(folder) + FILE_SEPERATOR + fObj.getNameExt(); //NoI18n
419: }
420:
421: public String getPath(DataFolder folder, DataObject d) {
422: return getPath(folder) + FILE_SEPERATOR + d.getName(); //NoI18n
423: }
424:
425: public File getAbsoluteFile(String path) {
426: return new File(getCollabRoot(), path); //NoI18n
427: }
428: }
|