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.eventhandler;
042:
043: import com.sun.collablet.CollabException;
044: import com.sun.collablet.CollabPrincipal;
045:
046: import org.openide.*;
047: import org.openide.filesystems.*;
048: import org.openide.loaders.*;
049: import org.openide.util.*;
050:
051: import java.io.*;
052:
053: import org.netbeans.modules.collab.channel.filesharing.FilesharingCollablet;
054: import org.netbeans.modules.collab.channel.filesharing.FilesharingConstants;
055: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
056: import org.netbeans.modules.collab.channel.filesharing.context.MessageContext;
057: import org.netbeans.modules.collab.channel.filesharing.event.JoinFilesharingEnd;
058: import org.netbeans.modules.collab.channel.filesharing.eventlistener.JoinEndTimerTask;
059: import org.netbeans.modules.collab.channel.filesharing.filehandler.CollabFileHandler;
060: import org.netbeans.modules.collab.channel.filesharing.filehandler.SharedFileGroup;
061: import org.netbeans.modules.collab.channel.filesharing.filesystem.CollabFilesystem;
062: import org.netbeans.modules.collab.channel.filesharing.mdc.CollabContext;
063: import org.netbeans.modules.collab.channel.filesharing.mdc.EventContext;
064: import org.netbeans.modules.collab.channel.filesharing.mdc.EventHandler;
065: import org.netbeans.modules.collab.channel.filesharing.msgbean.CCollab;
066: import org.netbeans.modules.collab.channel.filesharing.util.FileshareUtil;
067: import org.netbeans.modules.collab.core.Debug;
068:
069: /**
070: * FilesharingEventHandler
071: *
072: * @author Ayub Khan, ayub.khan@sun.com
073: * @version 1.0
074: */
075: public abstract class FilesharingEventHandler extends Object implements
076: EventHandler, FilesharingConstants {
077: ////////////////////////////////////////////////////////////////////////////
078: // Instance variables
079: ////////////////////////////////////////////////////////////////////////////
080:
081: /* valid */
082: private boolean valid = false;
083:
084: /* channel */
085: private FilesharingCollablet channel;
086:
087: /* context */
088: private FilesharingContext context = null;
089:
090: /* CollabFilesystem */
091: private CollabFilesystem fs = null;
092:
093: /**
094: * constructor
095: *
096: */
097: public FilesharingEventHandler(CollabContext context) {
098: this .context = (FilesharingContext) context;
099: this .channel = (FilesharingCollablet) this .context.getChannel();
100: }
101:
102: public FilesharingCollablet getChannel() {
103: return this .channel;
104: }
105:
106: public FilesharingContext getContext() {
107: return this .context;
108: }
109:
110: public CollabFilesystem getCollabFilesystem() {
111: if (fs == null) {
112: fs = (CollabFilesystem) this .context.getCollabFilesystem();
113: }
114:
115: return fs;
116: }
117:
118: public String getLoginUser() {
119: return getContext().getLoginUser();
120: }
121:
122: /**
123: * constructMsg
124: *
125: * @param evContext Event Context
126: */
127: public abstract CCollab constructMsg(EventContext evContext)
128: throws CollabException;
129:
130: /**
131: * handleMsg
132: *
133: * @param collabBean
134: * @param messageOriginator
135: * @param isUserSame
136: */
137: public abstract void handleMsg(CCollab collabBean,
138: String messageOriginator, boolean isUserSame)
139: throws CollabException;
140:
141: ////////////////////////////////////////////////////////////////////////////
142: // Event Handler methods
143: ////////////////////////////////////////////////////////////////////////////
144:
145: /**
146: * exec
147: *
148: * @param eventID
149: * @param evContext
150: */
151: public void exec(String eventID, EventContext evContext)
152: throws CollabException {
153: String user = getLoginUser();
154: boolean isUserSame = true;
155:
156: if ((eventID != null) && eventID.startsWith("receivedMessage")) //NoI18n
157: {
158: CCollab collabBean = ((MessageContext) evContext)
159: .getCollab();
160: String messageOriginator = ((MessageContext) evContext)
161: .getMessageOriginator();
162: user = messageOriginator;
163: isUserSame = ((MessageContext) evContext).isUserSame();
164: getContext().printAllData(
165: "\nIn FEV::before handleMsg event: \n" + eventID); //NoI18n
166: handleMsg(collabBean, messageOriginator, isUserSame);
167: getContext().printAllData(
168: "\nIn FEV::after handleMsg event: \n" + eventID); //NoI18n
169: } else {
170: boolean skipSend = skipSendMessage(eventID);
171:
172: if (skipSend) {
173: return;
174: }
175:
176: CCollab collab = constructMsg(evContext);
177:
178: if (collab != null) {
179: getContext().printAllData(
180: "\nIn FEV::after constructMsg event: \n"
181: + eventID); //NoI18n
182: sendMessage(collab);
183: }
184: }
185:
186: /*if(!isUserSame)
187: {
188: showDialog(eventID, user);
189: }*/
190: }
191:
192: /**
193: *
194: * @param key
195: * @param user
196: */
197: public void showDialog(String key, Object[] args) {
198: String message = NbBundle.getMessage(
199: FilesharingEventHandler.class, key, args);
200:
201: if ((message == null) || message.trim().equals("")) {
202: return;
203: }
204:
205: NotifyDescriptor descriptor = new NotifyDescriptor.Message(
206: message);
207: DialogDisplayer.getDefault().notify(descriptor);
208: }
209:
210: /**
211: *
212: * @param key
213: * @param user
214: */
215: public boolean showConfirmDialog(String key, Object[] args) {
216: String message = NbBundle.getMessage(
217: FilesharingEventHandler.class, key, args);
218:
219: if ((message == null) || message.trim().equals("")) {
220: return false;
221: }
222:
223: NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(
224: message, NotifyDescriptor.OK_CANCEL_OPTION);
225:
226: if (DialogDisplayer.getDefault().notify(descriptor) == NotifyDescriptor.OK_OPTION) {
227: return true;
228: }
229:
230: return false;
231: }
232:
233: // /**
234: // * showWaitDialog
235: // *
236: // */
237: // public boolean showWaitDialog(String titleKey, String messageKey, Object[] args)
238: // {
239: // Frame parent = WindowManager.getDefault().getMainWindow();
240: // if(parent == null)
241: // {
242: // return false;
243: // }
244: //
245: // JDialog waitDialog =
246: // new JDialog(parent, NbBundle.getMessage(FilesharingEventHandler.class,
247: // titleKey));
248: // getContext().setWaitDialog(waitDialog);
249: // JLabel loadText =
250: // new JLabel(
251: // NbBundle.getMessage(FilesharingEventHandler.class,
252: // messageKey, args),
253: // JLabel.CENTER);
254: // waitDialog.getContentPane().add(loadText,BorderLayout.CENTER);
255: // waitDialog.pack();
256: // Point parentCenter = parent.getLocation();
257: // double pXLoc = parentCenter.getX();
258: // double pYLoc = parentCenter.getY();
259: // int pX = parent.getWidth();
260: // int pY = parent.getHeight();
261: // int cX = waitDialog.getWidth();
262: // int cY = waitDialog.getHeight();
263: // int cXLoc = (int)pXLoc + pX/2 - cX/2;
264: // int cYLoc = (int)pYLoc + pY/2 - cY/2;
265: // Point childCenter = new Point(cXLoc, cYLoc);
266: // waitDialog.setLocation(childCenter);
267: // waitDialog.setVisible(true);
268: // parent.requestFocus();
269: // return true;
270: // }
271:
272: /**
273: *
274: * @param collabBean
275: * @throws CollabException
276: */
277: protected void sendMessage(CCollab collabBean)
278: throws CollabException {
279: getContext().sendMessage(collabBean);
280: }
281:
282: /**
283: * scheduleJoinEnd
284: *
285: * @param delay
286: */
287: public void scheduleJoinEnd(long delay) throws CollabException {
288: JoinEndTimerTask sendJoinMessageTimerTask = new JoinEndTimerTask(
289: getContext().getChannelEventNotifier(),
290: new JoinFilesharingEnd(new EventContext(
291: JoinFilesharingEnd.getEventID(), null)),
292: getContext());
293: getContext().addTimerTask(SEND_JOINEND_TIMER_TASK,
294: sendJoinMessageTimerTask);
295: sendJoinMessageTimerTask.schedule(FileshareUtil
296: .getRandomCount(delay));
297: getContext().setJoinFlag(false);
298: getContext().clearExpectedFileMap();
299: }
300:
301: /**
302: * delete shared files
303: *
304: * @param file
305: * @param user
306: * @param skipDelete
307: */
308: public boolean deleteSharedFiles(String fileGroupName, String user,
309: boolean skipDelete) throws CollabException {
310: boolean status = false;
311: SharedFileGroup sharedFileGroup = getContext()
312: .getSharedFileGroupManager().getSharedFileGroup(
313: fileGroupName);
314:
315: if ((sharedFileGroup != null) && sharedFileGroup.isValid()) //shared file exist
316: {
317: sharedFileGroup.setValid(false);
318:
319: CollabFileHandler[] fileHandlers = sharedFileGroup
320: .getFileHandlers();
321:
322: if (fileHandlers != null) {
323: for (int i = 0; i < fileHandlers.length; i++) {
324: CollabFileHandler collabFileHandler = fileHandlers[i];
325:
326: if (collabFileHandler != null) {
327: collabFileHandler.setValid(false);
328: }
329:
330: String fileName = collabFileHandler.getName();
331: FileObject fileObject = collabFileHandler
332: .getFileObject();
333: Debug.log("FilesharingEventHandler",
334: "CommandHandler, handleDeleteFile: "
335: + fileName); //NoI18n
336: getContext().setSkipSendDeleteFile(fileName, true);
337:
338: //FileObject fileObject=
339: // getCollabFilesystem().findResource(fileName);
340: if (fileObject != null) {
341: status = deleteSharedFiles(fileObject, user,
342: skipDelete); //remove handler + files
343: }
344:
345: getContext().setSkipSendDeleteFile(fileName, false);
346: }
347: }
348:
349: getContext().removeAllFileHandlerRef(sharedFileGroup, user);
350: }
351:
352: return status;
353: }
354:
355: /**
356: * delete shared files
357: *
358: * @param file
359: * @param user
360: * @param skipDelete
361: */
362: public boolean deleteSharedFiles(final FileObject file,
363: String user, boolean skipDelete) throws CollabException {
364: boolean deleteStatus = true;
365:
366: if (!skipDelete && (file == null)) {
367: return deleteStatus;
368: }
369:
370: Debug.out.println("deleteSharedFiles: file: " + file.getPath());
371:
372: if (file.isData()) {
373: String fileName = getCollabFilesystem().getPath(file);
374:
375: if (fileName == null) {
376: return deleteStatus;
377: }
378:
379: if (fileName.startsWith(FILE_SEPERATOR)) {
380: fileName = fileName.substring(1);
381: }
382:
383: try {
384: if (!skipDelete) {
385: DataObject dd = null;
386:
387: try {
388: dd = FileshareUtil.getDataObject(file);
389: } catch (Throwable th) {
390: th.printStackTrace(Debug.out);
391: }
392:
393: final DataObject dataObject = dd;
394:
395: //close file before delete
396: getContext().closeFile(file.getNameExt());
397:
398: FileSystem fs = getCollabFilesystem();
399: fs.runAtomicAction(new FileSystem.AtomicAction() {
400: public void run() throws IOException {
401: try {
402: if (dataObject != null) {
403: dataObject.delete();
404: } else {
405: file.delete();
406: }
407: } catch (FileAlreadyLockedException ale) {
408: //ignore
409: }
410: }
411: });
412: }
413: } catch (FileAlreadyLockedException ale) {
414: Debug.log(this , "FilesharingContext, delete "
415: + "failed for file: " + fileName); //NoI18n
416: Debug.logDebugException("FilesharingContext, delete "
417: + "failed for file: " + fileName, //NoI18n
418: ale, true);
419: deleteStatus = promptUserSave(user, file);
420:
421: if (deleteStatus && !skipDelete) {
422: try {
423: file.delete();
424: } catch (IOException iox1) {
425: Debug.log(this , "FilesharingContext, delete "
426: + "failed for file: "
427: + getCollabFilesystem().getPath(file)); //NoI18n
428: Debug.logDebugException(
429: "FilesharingContext, delete "
430: + "failed for file: "
431: + getCollabFilesystem()
432: .getPath(file), //NoI18n
433: iox1, true);
434:
435: //show delete failed dialog
436: String[] args = new String[] {
437: getContext().getPrincipal(user)
438: .getDisplayName(),
439: getCollabFilesystem().getPath(file) };
440: showDialog(
441: "MSG_FilesharingEventHandler_DeleteFileFailed",
442: args); //NoI18n
443:
444: return false;
445: }
446: }
447: } catch (IOException iox) {
448: Debug.log(this , "FilesharingContext, delete "
449: + "failed for file: " + fileName); //NoI18n
450: Debug.logDebugException("FilesharingContext, delete "
451: + "failed for file: " + fileName, //NoI18n
452: iox, true);
453: deleteStatus = promptUserSave(user, file);
454:
455: if (deleteStatus && !skipDelete) {
456: try {
457: file.delete();
458: } catch (IOException iox1) {
459: Debug.log(this , "FilesharingContext, delete "
460: + "failed for file: "
461: + getCollabFilesystem().getPath(file)); //NoI18n
462: Debug.logDebugException(
463: "FilesharingContext, delete "
464: + "failed for file: "
465: + getCollabFilesystem()
466: .getPath(file), //NoI18n
467: iox, true);
468:
469: //show delete failed dialog
470: String[] args = new String[] {
471: getContext().getPrincipal(user)
472: .getDisplayName(),
473: getCollabFilesystem().getPath(file) };
474: showDialog(
475: "MSG_FilesharingEventHandler_DeleteFileFailed",
476: args); //NoI18n
477:
478: return false;
479: }
480: }
481: } finally {
482: CollabFileHandler collabFileHandler = getContext()
483: .getSharedFileGroupManager().getFileHandler(
484: fileName);
485:
486: if (collabFileHandler != null) {
487: String fileGroupName = collabFileHandler
488: .getFileGroupName();
489: SharedFileGroup sharedFileGroup = getContext()
490: .getSharedFileGroupManager()
491: .getSharedFileGroup(fileGroupName);
492: getContext().removeAllFileHandlerRef(
493: sharedFileGroup, user);
494: } else {
495: Debug.log(this , "FEV, fileHandler null for file: "
496: + fileName); //NoI18n
497: }
498: }
499: } else {
500: FileObject[] children = file.getChildren();
501:
502: for (int i = 0; i < children.length; i++) {
503: return deleteSharedFiles(children[i], user, skipDelete);
504: }
505:
506: //Delete self
507: try {
508: if (!skipDelete) {
509: file.delete();
510: }
511: } catch (IOException iox) {
512: Debug.log(this , "FilesharingContext, delete "
513: + "failed for file: "
514: + getCollabFilesystem().getPath(file)); //NoI18n
515: Debug.logDebugException("FilesharingContext, delete "
516: + "failed for file: "
517: + getCollabFilesystem().getPath(file), //NoI18n
518: iox, true);
519: deleteStatus = promptUserSave(user, file);
520:
521: if (deleteStatus && !skipDelete) {
522: try {
523: file.delete();
524: } catch (IOException iox1) {
525: Debug.log(this , "FilesharingContext, delete "
526: + "failed for file: "
527: + getCollabFilesystem().getPath(file)); //NoI18n
528: Debug.logDebugException(
529: "FilesharingContext, delete "
530: + "failed for file: "
531: + getCollabFilesystem()
532: .getPath(file), //NoI18n
533: iox, true);
534:
535: //show delete failed dialog
536: String[] args = new String[] {
537: getContext().getPrincipal(user)
538: .getDisplayName(),
539: getCollabFilesystem().getPath(file) };
540: showDialog(
541: "MSG_FilesharingEventHandler_DeleteFileFailed",
542: args); //NoI18n
543:
544: return false;
545: }
546: }
547: }
548: }
549:
550: return deleteStatus;
551: }
552:
553: /**
554: * promptUserDeleteAction
555: *
556: * @param file
557: */
558: public boolean promptUserSave(String user, FileObject file)
559: throws CollabException {
560: String fileName = getCollabFilesystem().getPath(file);
561:
562: //show delete option dialog
563: String[] args = new String[] {
564: getContext().getPrincipal(user).getDisplayName(),
565: fileName };
566: boolean ok = showConfirmDialog(
567: "MSG_FilesharingEventHandler_DeleteFileConfirm", args); //NoI18n
568: boolean saveOk = false;
569:
570: if (ok) {
571: saveOk = saveFile(fileName);
572: }
573:
574: return saveOk;
575: }
576:
577: /**
578: * saveFile
579: *
580: */
581: protected boolean saveFile(String fileName) throws CollabException {
582: Debug.log("CollabFileHandlerSupport", //NoI18n
583: "saving File: " //NoI18n
584: + fileName + " before delete"); //NoI18n
585:
586: boolean status = false;
587:
588: try {
589: CollabFileHandler collabFileHandler = getContext()
590: .getSharedFileGroupManager().getFileHandler(
591: fileName);
592:
593: if (collabFileHandler != null) {
594: status = collabFileHandler.saveDocument();
595: }
596: } catch (CollabException ce) {
597: Debug.log("CollabFileHandlerSupport", //NoI18n
598: "Exception occured while saving the file: " //NoI18n
599: + fileName + " on update"); //NoI18n
600: Debug.logDebugException(
601: "Exception occured while saving the file: " //NoI18n
602: + fileName + " on update", //NoI18n
603: ce, true);
604:
605: return false;
606: }
607:
608: return status;
609: }
610:
611: /**
612: * setValid
613: *
614: * @param valid
615: * @throws CollabException
616: */
617: public void setValid(boolean valid) {
618: this .valid = valid;
619: }
620:
621: /**
622: * test if the filehandler is valid
623: *
624: * @return true/false true if valid
625: */
626: public boolean isValid() {
627: return valid;
628: }
629:
630: ////////////////////////////////////////////////////////////////////////////
631: // Support methods
632: ////////////////////////////////////////////////////////////////////////////
633: public String getVersion() {
634: return getContext().getVersion();
635: }
636:
637: /**
638: * skipSendMessage
639: *
640: * @return flag
641: */
642: public boolean skipSendMessage(String messageID) {
643: //skip sendMessage if readOnly privilige for this user
644: if (getContext().isReadOnlyConversation()) {
645: Debug.log("SendFileHandler",
646: "FilesharingEH, skipSendMessage: "
647: + "readOnlyConveration for user: "
648: + getContext().getLoginUser()); //NoI18n
649:
650: return true;
651: }
652:
653: //do not send any message if 0 or 1 user
654: //String[] filesharingUsers = getContext().getUser();
655: CollabPrincipal[] convUsers = getContext().getConversation()
656: .getParticipants();
657:
658: if (((convUsers != null) && (convUsers.length > 1)) //conv users are enough
659: //|| (filesharingUsers!=null && filesharingUsers.length>1)
660: ) {
661: return false;
662: } else {
663: Debug.log("SendFileHandler",
664: "FilesharingEH, skipSendMessage: "
665: + "#of participants<=1 for user: "
666: + getContext().getLoginUser()); //NoI18n
667: }
668:
669: Debug.log("SendFileHandler", "FilesharingEH, skipSendMessage: "
670: + //NoI18n
671: messageID);
672:
673: return true;
674: }
675: }
|