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.filehandler;
042:
043: import com.sun.collablet.CollabException;
044:
045: import org.openide.cookies.EditorCookie;
046: import org.openide.filesystems.FileObject;
047: import org.openide.loaders.DataFolder;
048: import org.openide.loaders.DataObject;
049:
050: import java.beans.PropertyChangeEvent;
051:
052: import java.util.*;
053:
054: import javax.swing.SwingUtilities;
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.context.SendFileContext;
059: import org.netbeans.modules.collab.channel.filesharing.event.SendDataObject;
060: import org.netbeans.modules.collab.channel.filesharing.eventlistener.FilesharingTimerTask;
061: import org.netbeans.modules.collab.channel.filesharing.eventlistener.SendDataObjectTimerTask;
062: import org.netbeans.modules.collab.channel.filesharing.eventlistener.SwingThreadTask;
063: import org.netbeans.modules.collab.channel.filesharing.filesystem.CollabFilesystem;
064: import org.netbeans.modules.collab.channel.filesharing.util.FileshareUtil;
065: import org.netbeans.modules.collab.core.Debug;
066:
067: /**
068: * SharedFileGroup
069: *
070: * @author ayub.khan@sun.com
071: * @version 1.0
072: */
073: public class SharedFileGroupManager extends Object implements
074: FilesharingConstants {
075: ////////////////////////////////////////////////////////////////////////////
076: // Instance variables
077: ////////////////////////////////////////////////////////////////////////////
078: private boolean isValid = true;
079:
080: /* context */
081: private FilesharingContext context = null;
082:
083: /*CollabFilesystem*/
084: private CollabFilesystem fs = null;
085:
086: /* all shared filehandlers */
087: private HashMap allSharedFileHandlers = new HashMap();
088:
089: /* all shared fileObjects */
090: private HashMap allSharedFiles = new HashMap();
091:
092: /* owner shared fileGroup map */
093: private HashMap ownerSharedFileGroupMap = new HashMap();
094:
095: /* all shared fileGroup map */
096: private HashMap allSharedFileGroup = new HashMap();
097:
098: /**
099: *
100: * @param fileGroupName
101: * @param user
102: * @param fileHandlers
103: */
104: public SharedFileGroupManager(FilesharingContext context) {
105: super ();
106: this .context = context;
107: }
108:
109: ////////////////////////////////////////////////////////////////////////////
110: // methods
111: ////////////////////////////////////////////////////////////////////////////
112:
113: /**
114: * getContext
115: *
116: * @return context
117: */
118: public FilesharingContext getContext() {
119: return this .context;
120: }
121:
122: /**
123: * getCollabFilesystem
124: *
125: * @return fs
126: */
127: public CollabFilesystem getCollabFilesystem() {
128: if (fs == null) {
129: fs = (CollabFilesystem) context.getCollabFilesystem();
130: }
131:
132: return this .fs;
133: }
134:
135: /**
136: *
137: * @param fileName
138: */
139: public void addFileGroup(SharedFileGroup sharedFileGroup) {
140: synchronized (allSharedFileGroup) {
141: if (!allSharedFileGroup.containsKey(sharedFileGroup
142: .getName())) {
143: this .allSharedFileGroup.put(sharedFileGroup.getName(),
144: sharedFileGroup);
145: }
146: }
147: }
148:
149: /**
150: * removeFileGroup
151: *
152: * @param id
153: * @param collabFileHandler
154: */
155: public void removeFileGroup(String fileName) throws CollabException {
156: CollabFileHandler collabFileHandler = getFileHandler(fileName);
157:
158: synchronized (allSharedFiles) {
159: allSharedFiles.remove(collabFileHandler.getFileObject());
160: }
161:
162: synchronized (allSharedFileHandlers) {
163: allSharedFileHandlers.remove(fileName);
164: }
165: }
166:
167: /**
168: *
169: * @param fileName
170: */
171: public void addFileHandler(CollabFileHandler fileHandler)
172: throws CollabException {
173: synchronized (allSharedFileHandlers) {
174: if (!allSharedFileHandlers.containsKey(fileHandler
175: .getName())) {
176: this .allSharedFileHandlers.put(fileHandler.getName(),
177: fileHandler);
178: updateFileCount(allSharedFileGroup.size());
179: }
180: }
181:
182: synchronized (allSharedFiles) {
183: if (allSharedFiles.containsKey(fileHandler.getFileObject())) {
184: allSharedFiles.put(fileHandler.getFileObject(),
185: fileHandler);
186: }
187: }
188: }
189:
190: /**
191: * setValid
192: *
193: * @param status
194: * @throws CollabException
195: */
196: public void setValid(boolean valid) throws CollabException {
197: this .isValid = valid;
198: }
199:
200: /**
201: * getValid
202: *
203: * @return status if false handler is invalid
204: * @throws CollabException
205: */
206: public boolean isValid() throws CollabException {
207: return this .isValid;
208: }
209:
210: /**
211: *
212: *
213: */
214: public CollabFileHandler getFileHandler(String fileName) {
215: synchronized (allSharedFileHandlers) {
216: return (CollabFileHandler) allSharedFileHandlers
217: .get(fileName);
218: }
219: }
220:
221: public CollabFileHandler getFileHandler(FileObject fileObject)
222: throws CollabException {
223: CollabFileHandler[] fileHandlers = getAllFileHandlers();
224:
225: for (int i = 0; i < fileHandlers.length; i++) {
226: Debug.out.println("compare: file: " + fileObject.getPath()
227: + "to fh.file: "
228: + fileHandlers[i].getFileObject().getPath());
229:
230: if (fileObject.getPath().equals(
231: fileHandlers[i].getFileObject().getPath())) {
232: return fileHandlers[i];
233: }
234: }
235:
236: return null;
237: }
238:
239: /**
240: *
241: * @param fileName
242: * @throws CollabException
243: * @return
244: */
245: public CollabFileHandler getFileHandler(
246: javax.swing.text.Document document) {
247: return getFileHandler(((String) document
248: .getProperty("COLLAB_FILEHANDLER_FILE_NAME")));
249: }
250:
251: /**
252: * removeCollabFileHandler
253: *
254: * @param id
255: * @param collabFileHandler
256: */
257: public void removeFileHandler(String fileName)
258: throws CollabException {
259: CollabFileHandler collabFileHandler = getFileHandler(fileName);
260:
261: synchronized (allSharedFiles) {
262: allSharedFiles.remove(collabFileHandler.getFileObject());
263: }
264:
265: synchronized (allSharedFileHandlers) {
266: allSharedFileHandlers.remove(fileName);
267: updateFileCount(allSharedFileGroup.size());
268: }
269: }
270:
271: /**
272: *
273: * @return
274: */
275: public CollabFileHandler[] getAllFileHandlers() {
276: synchronized (allSharedFileHandlers) {
277: return (CollabFileHandler[]) allSharedFileHandlers.values()
278: .toArray(new CollabFileHandler[0]);
279: }
280: }
281:
282: /**
283: *
284: * @return
285: */
286: public SharedFileGroup[] getAllSharedFileGroup() {
287: synchronized (allSharedFileGroup) {
288: return (SharedFileGroup[]) allSharedFileGroup.values()
289: .toArray(new SharedFileGroup[0]);
290: }
291: }
292:
293: /**
294: *
295: * @param fileGroupName
296: * @return
297: */
298: public SharedFileGroup getSharedFileGroup(String fileGroupName) {
299: synchronized (allSharedFileGroup) {
300: return (SharedFileGroup) allSharedFileGroup
301: .get(fileGroupName);
302: }
303: }
304:
305: /**
306: *
307: * @param fileGroupName
308: * @return
309: */
310: public SharedFileGroup getSharedFileGroup(FileObject fileObject)
311: throws CollabException {
312: CollabFileHandler[] fileHandlers = getAllFileHandlers();
313:
314: for (int i = 0; i < fileHandlers.length; i++) {
315: if (fileObject == fileHandlers[i].getFileObject()) {
316: return getSharedFileGroup(fileHandlers[i]
317: .getFileGroupName());
318: }
319: }
320:
321: return null;
322: }
323:
324: /**
325: *
326: * @param id
327: * @param sharedFileGroup
328: * @throws CollabException
329: */
330: public void addToAllSharedFileGroup(String id,
331: SharedFileGroup sharedFileGroup) {
332: synchronized (allSharedFileGroup) {
333: if (!allSharedFileGroup.containsKey(id)) {
334: allSharedFileGroup.put(id, sharedFileGroup);
335: updateFileCount(allSharedFileGroup.size());
336: }
337: }
338: }
339:
340: private void updateFileCount(int fileCount) {
341: Debug.out.println("SFGM updateFileCount File count: "
342: + fileCount);
343:
344: PropertyChangeEvent event = new PropertyChangeEvent(this ,
345: FILE_COUNT_CHANGED, new Integer(fileCount),
346: new Integer(fileCount));
347:
348: if (getContext().getFilesystemExplorer() != null) {
349: getContext().getFilesystemExplorer().propertyChange(event);
350: }
351: }
352:
353: /**
354: * removeFromAllSharedFileGroup
355: *
356: * @param sharedFileGroup
357: * @throws CollabException
358: */
359: public void removeFromAllSharedFileGroup(
360: SharedFileGroup sharedFileGroup) {
361: String fileGroupName = sharedFileGroup.getName();
362:
363: synchronized (allSharedFileGroup) {
364: if (allSharedFileGroup.containsKey(fileGroupName)) {
365: allSharedFileGroup.remove(fileGroupName);
366: updateFileCount(allSharedFileGroup.size());
367: }
368: }
369: }
370:
371: /**
372: *
373: * @param fileGroupName
374: * @return
375: */
376: public boolean isSharedFileGroupExist(String fileGroupName) {
377: synchronized (allSharedFileGroup) {
378: return allSharedFileGroup.containsKey(fileGroupName);
379: }
380: }
381:
382: /**
383: *
384: * @return
385: */
386: public SharedFileGroup[] getOwnerSharedFileGroup() {
387: synchronized (ownerSharedFileGroupMap) {
388: return (SharedFileGroup[]) ownerSharedFileGroupMap.values()
389: .toArray(new SharedFileGroup[0]);
390: }
391: }
392:
393: /**
394: *
395: * @param fileGroupName
396: * @return
397: */
398: public SharedFileGroup getOwnerSharedFileGroup(String fileGroupName) {
399: synchronized (ownerSharedFileGroupMap) {
400: return (SharedFileGroup) ownerSharedFileGroupMap
401: .get(fileGroupName);
402: }
403: }
404:
405: /**
406: *
407: * @param name
408: * @param object
409: */
410: public void addToOwnerSharedFile(String fileGroupName,
411: SharedFileGroup object) {
412: synchronized (ownerSharedFileGroupMap) {
413: ownerSharedFileGroupMap.put(fileGroupName, object);
414: }
415:
416: context.addToUserSharedFileGroupNames(context.getLoginUser(),
417: fileGroupName);
418: }
419:
420: /**
421: *
422: * @param fileGroupName
423: * @param file
424: * @throws CollabException
425: */
426: public void addToOwnerSharedFile(String fileGroupName,
427: String projectName, CollabFileHandler fileHandler)
428: throws CollabException {
429: if (!isSharedFileGroupExist(fileGroupName)) {
430: SharedFileGroup sharedFileGroup = SharedFileGroupFactory
431: .getDefault().createSharedFileGroup(fileGroupName,
432: context.getLoginUser(), projectName, this );
433: addFileGroup(sharedFileGroup);
434: sharedFileGroup.addFileHandler(fileHandler);
435: addToOwnerSharedFile(fileGroupName, sharedFileGroup);
436: addToAllSharedFileGroup(fileGroupName, sharedFileGroup);
437: } else {
438: SharedFileGroup sharedFileGroup = getOwnerSharedFileGroup(fileGroupName);
439:
440: if (sharedFileGroup != null) {
441: if (!sharedFileGroup.contains(fileHandler.getName())) {
442: sharedFileGroup.addFileHandler(fileHandler);
443: }
444: }
445: }
446: }
447:
448: /**
449: * removeFromOwnerSharedFileGroup
450: *
451: * @param sharedFileGroup
452: * @throws CollabException
453: */
454: public void removeFromOwnerSharedFileGroup(
455: SharedFileGroup sharedFileGroup) throws CollabException {
456: String fileGroupName = sharedFileGroup.getName();
457:
458: synchronized (ownerSharedFileGroupMap) {
459: if (ownerSharedFileGroupMap.containsKey(fileGroupName)) {
460: ownerSharedFileGroupMap.remove(fileGroupName);
461: }
462: }
463: }
464:
465: public boolean isShared(FileObject fileObject)
466: throws CollabException {
467: CollabFileHandler[] fileHandlers = getAllFileHandlers();
468:
469: for (int i = 0; i < fileHandlers.length; i++) {
470: if (fileObject == fileHandlers[i].getFileObject()) {
471: return true;
472: }
473: }
474:
475: return false;
476: }
477:
478: public void sendDataObject(String loginUser, String projectName,
479: DataFolder leafFldr, final DataObject dataObject) {
480: FileObject[] fileObjects = (FileObject[]) dataObject.files()
481: .toArray(new FileObject[0]);
482: CollabFileHandler[] fileHandlers = new CollabFileHandler[fileObjects.length];
483: String[] fileNames = new String[fileObjects.length];
484: String fileGroupName = getCollabFilesystem().getPath(leafFldr,
485: dataObject)
486: + "_" + dataObject.getPrimaryFile().getExt();
487: fileGroupName = FileshareUtil.getNormalizedPath(fileGroupName);
488:
489: for (int j = 0; j < fileObjects.length; j++) {
490: fileNames[j] = FileshareUtil
491: .getNormalizedPath(getCollabFilesystem().getPath(
492: leafFldr, fileObjects[j]));
493: Debug.log("ProjectsRootNode", "SFGM, files : "
494: + fileNames[j]);
495: }
496:
497: final LazySharedFileGroup lazySharedFileGroup = SharedFileGroupFactory
498: .createLazySharedFileGroup(fileGroupName, loginUser,
499: projectName, getContext()
500: .getSharedFileGroupManager(),
501: fileNames, fileObjects);
502: SwingUtilities.invokeLater(new Runnable() {
503: public void run() {
504: try {
505: EditorCookie cookie = FileshareUtil
506: .getEditorCookie(dataObject);
507: lazySharedFileGroup.setEditorCookie(cookie);
508: } catch (Throwable th) {
509: Debug
510: .log("CollabFileHandlerSupport",
511: "CollabFileHandlerSupport, " + //NoI18n
512: "wait to getCollabFileHandler failed"); //NoI18n
513: Debug
514: .logDebugException(
515: "CollabFileHandlerSupport, " + //NoI18n
516: "wait to getCollabFileHandler failed", //NoI18n
517: th, true);
518: }
519: }
520: });
521:
522: TimerTask sendDataObjectTimerTask = new SwingThreadTask(
523: new SendDataObjectTimerTask(getContext()
524: .getChannelEventNotifier(), new SendDataObject(
525: new SendFileContext(
526: SendDataObject.getEventID(),
527: lazySharedFileGroup)), getContext()));
528: getContext().addTimerTask(SEND_SENDFILE_TIMER_TASK,
529: fileGroupName, sendDataObjectTimerTask);
530: getContext().schedule(sendDataObjectTimerTask,
531: FilesharingTimerTask.PERIOD * 2);
532: }
533:
534: /**
535: * clear
536: *
537: */
538: public void clear() {
539: synchronized (ownerSharedFileGroupMap) {
540: ownerSharedFileGroupMap.clear();
541: }
542:
543: synchronized (allSharedFileGroup) {
544: allSharedFileGroup.clear();
545: }
546:
547: synchronized (allSharedFileHandlers) {
548: allSharedFileHandlers.clear();
549: }
550: }
551:
552: /**
553: * print
554: *
555: */
556: public void print() {
557: if (!Debug.isAllowed("FilesharingContext")) {
558: return;
559: }
560:
561: synchronized (ownerSharedFileGroupMap) {
562: Debug.log("FilesharingContext", "ownerSharedFileGroupMap: "
563: + //NoI18n
564: ownerSharedFileGroupMap.keySet().toString()); //NoI18n
565: }
566:
567: synchronized (allSharedFileGroup) {
568: Debug.log("FilesharingContext", "allSharedFileGroupMap: " + //NoI18n
569: allSharedFileGroup.keySet().toString()); //NoI18n
570: }
571:
572: synchronized (allSharedFileHandlers) {
573: Debug.log("FilesharingContext", "collabFileHandlerMap: " + //NoI18n
574: allSharedFileHandlers.keySet().toString()); //NoI18n
575: }
576: }
577: }
|