KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > ipod > MainWindow


1 /**
2  * Copyright (C) 2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.syncclient.ipod;
19
20 import java.io.File JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27
28 import sync4j.syncclient.spdm.DMException;
29 import sync4j.syncclient.spdm.ManagementNode;
30 import sync4j.syncclient.spdm.SimpleDeviceManager;
31
32 import sync4j.syncclient.spds.SyncManager;
33 import sync4j.syncclient.spds.UpdateException;
34 import sync4j.syncclient.spds.AuthenticationException;
35 import sync4j.syncclient.spds.event.SyncListener;
36 import sync4j.syncclient.spds.event.SyncItemListener;
37 import sync4j.syncclient.spds.event.SyncSourceListener;
38 import sync4j.syncclient.spds.event.SyncStatusListener;
39 import sync4j.syncclient.spds.event.SyncTransportListener;
40 import sync4j.syncclient.spds.event.SyncEvent;
41 import sync4j.syncclient.spds.event.SyncItemEvent;
42 import sync4j.syncclient.spds.event.SyncSourceEvent;
43 import sync4j.syncclient.spds.event.SyncStatusEvent;
44 import sync4j.syncclient.spds.event.SyncTransportEvent;
45
46 import sync4j.syncclient.ipod.utils.*;
47
48 import sync4j.syncclient.common.logging.Logger;
49
50
51 /**
52  * The main window for the IPod Client GUI.
53  *
54  * @author Luigia Fassina @ Funambol
55  * @version $Id: MainWindow.java,v 1.8 2005/05/09 07:59:13 luigiafassina Exp $
56  */

57 public class MainWindow extends JFrame
58 implements ActionListener, Constants, SyncItemListener, SyncListener,
59            SyncSourceListener, SyncStatusListener, SyncTransportListener {
60
61     //---------------------------------------------------------- Constants
62
public static final String JavaDoc DM_VALUE_PATH = "spds/syncml" ;
63     public static final String JavaDoc DM_VALUE_CONTACT_PATH = "spds/sources/contact" ;
64     public static final String JavaDoc DM_VALUE_CALENDAR_PATH = "spds/sources/calendar";
65     public static final String JavaDoc DM_VALUE_NOTE_PATH = "spds/sources/note" ;
66
67     public static final String JavaDoc PROP_CHARSET = "spds.charset";
68     public static final String JavaDoc PROP_WD = "wd" ;
69
70     public static final String JavaDoc VALUE_UTF8 = "UTF8" ;
71     
72     public static final String JavaDoc ROOT_DIRECTORY = "config";
73
74     //---------------------------------------------------------- Private data
75

76     /**
77      * If true there was a SyncError event into synchronization process, false
78      * otherwise
79      */

80     private boolean syncError = false;
81
82     /**
83      * DM root node
84      */

85     private ManagementNode rootNode = null;
86
87     /**
88      * DM values from DM_VALUE_PATH
89      */

90     private Hashtable JavaDoc syncmlValues = null;
91
92     /**
93      * DM values from DM_VALUE_CONTACT_PATH
94      */

95     private Hashtable JavaDoc xmlContactValues = null;
96
97     /**
98      * DM values from DM_VALUE_CALENDAR_PATH
99      */

100     private Hashtable JavaDoc xmlCalendarValues = null;
101
102     /**
103      * DM values from DM_VALUE_NOTE_PATH
104      */

105     private Hashtable JavaDoc xmlNoteValues = null;
106
107     /**
108      * The root directory
109      */

110     private String JavaDoc rootDirectory = null;
111
112     /**
113      * The source directory
114      */

115     private String JavaDoc sourceDirectory = "";
116
117     /**
118      * Status label: contains the status of synchronization process
119      */

120     private JLabel jlStatus;
121     private JButton btSync;
122     //----------------------------------------------------------- Public methods
123
public static void main(String JavaDoc[] args) {
124         MainWindow mw = new MainWindow();
125         mw.show();
126     }
127
128     public MainWindow() {
129         super();
130         Language.init();
131         createAndShowMainGUI();
132
133         Properties JavaDoc props = System.getProperties();
134         rootDirectory = System.getProperty(PROP_WD);
135
136         if (System.getProperty(SimpleDeviceManager.PROP_DM_DIR_BASE) == null) {
137             props.put(SimpleDeviceManager.PROP_DM_DIR_BASE ,
138                       buildPath(rootDirectory, ROOT_DIRECTORY)
139             );
140             System.setProperties(props);
141         }
142         props = System.getProperties();
143         props.put(PROP_CHARSET, VALUE_UTF8);
144         System.setProperties(props);
145
146         sourceDirectory = buildPath(rootDirectory,sourceDirectory);
147
148         //
149
// --- Device Manager ---
150
//
151
rootNode = SimpleDeviceManager.getDeviceManager().getManagementTree();
152
153         try {
154             syncmlValues = rootNode.getNodeValues(DM_VALUE_PATH );
155             xmlContactValues = rootNode.getNodeValues(DM_VALUE_CONTACT_PATH );
156             xmlCalendarValues = rootNode.getNodeValues(DM_VALUE_CALENDAR_PATH);
157             xmlNoteValues = rootNode.getNodeValues(DM_VALUE_NOTE_PATH );
158
159             //
160
// Setting default logging properties
161
//
162
String JavaDoc logLevel = (String JavaDoc)syncmlValues.get(PARAM_LOGLEVEL);
163             if(logLevel.equals(LOG_INFO)) {
164                 Logger.setLevel(Logger.INFO);
165             } else if(logLevel.equals(LOG_DEBUG)) {
166                 Logger.setLevel(Logger.DEBUG);
167             }
168             Logger.setDefaultLogFile();
169
170         } catch (DMException e) {
171             if (Logger.isLoggable(Logger.ERROR)) {
172                 Logger.error(Language.getMessage(Language.ERROR_NOT_FOUNT_DM_VALUES));
173                 Logger.error(e.getMessage());
174             }
175         }
176     }
177
178     /**
179      * Create and show the main GUI.
180      */

181     public void createAndShowMainGUI() {
182         try {
183             UIManager.setLookAndFeel(lookAndFeel);
184         } catch(Exception JavaDoc e) {
185             e.printStackTrace();
186         }
187
188         //
189
// Set up the window.
190
//
191
getContentPane().setLayout(new BorderLayout(1,1));
192         setTitle(Language.getMessage(Language.LABEL_TITLE_MAINWINDOW));
193         setIconImage(Toolkit.getDefaultToolkit().getImage(FRAME_ICONNAME));
194         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
195         setSize(280,362);
196         setLocationRelativeTo(null);
197         setResizable(false);
198         getContentPane().setBackground(new Color(236,233,216));
199
200         JPanel mainPanel = new JPanel(null);
201         mainPanel.setBackground(Color.WHITE);
202         getContentPane().add(mainPanel, BorderLayout.CENTER);
203
204         JLabel lab = new JLabel(new ImageIcon(FRAME_LOGONAME));
205         lab.setBounds(47,22,185,70);
206         mainPanel.add(lab);
207
208         JLabel lab1 = new JLabel(new ImageIcon(FRAME_ARROWSNAME));
209         lab1.setBounds(96,110,88,77);
210         mainPanel.add(lab1);
211
212         btSync = new JButton(Language.getMessage(Language.BT_SYNC));
213         btSync.setFont(new Font("Microsoft Sans Serif", 0, 13));
214         btSync.setBounds(84,228,112,24);
215         btSync.setBackground(Color.WHITE);
216         btSync.addActionListener(this);
217
218         mainPanel.add(btSync);
219
220         mainPanel.setBorder(BorderFactory.createRaisedBevelBorder());
221
222         //
223
// Label for show the status of synchronization process
224
//
225
jlStatus = new JLabel();
226         jlStatus.setFont(font);
227         jlStatus.setPreferredSize(new Dimension(260, 25));
228         getContentPane().add(jlStatus, BorderLayout.SOUTH);
229
230         setJMenuBar(new MenuBar(this));
231     }
232
233     /**
234      * This method is call only when the button "Synchronization" is pushed.
235      */

236     public void actionPerformed(ActionEvent e) {
237         if (checkSyncSettings()) {
238           runSynchronization();
239         } else {
240             setStatusMessage(Language.getMessage(Language.STATUS_NOT_SYNC_REQUIRED));
241         }
242     }
243     
244     /**
245      * Check if there is least one source to synchronize
246      *
247      * @return true if there is least one source to sync, otherwise false
248      */

249     private boolean checkSyncSettings() {
250         String JavaDoc contactSM = (String JavaDoc)xmlContactValues.get(PARAM_SYNCMODE);
251         String JavaDoc calendarSM = (String JavaDoc)xmlCalendarValues.get(PARAM_SYNCMODE);
252                 
253         if (contactSM.equalsIgnoreCase(SYNC_NONE) &&
254             calendarSM.equalsIgnoreCase(SYNC_NONE) ) {
255             return false;
256         }
257         return true;
258     }
259
260     public void runSynchronization() {
261         File JavaDoc f = null;
262         if ((f = getFileLog()) != null) {
263             f.delete();
264         }
265         setCursor(new Cursor(Cursor.WAIT_CURSOR));
266         setStatusMessage(Language.getMessage(Language.STATUS_SYNC_BEGIN));
267
268         new Thread JavaDoc(new Runnable JavaDoc() {
269           public void run() {
270               synchronize();
271           }
272         }).start();
273     }
274
275     /**
276      * Start the synchronization process based on the configuration parameters
277      */

278     private void synchronize() {
279         try {
280
281             SyncManager syncManager = SyncManager.getSyncManager("");
282
283             syncManager.addSyncItemListener (this);
284             syncManager.addSyncListener (this);
285             syncManager.addSyncSourceListener (this);
286             syncManager.addSyncStatusListener (this);
287             syncManager.addSyncTransportListener(this);
288
289             syncManager.sync();
290
291         } catch (AuthenticationException e) {
292             setStatusMessage(Language.getMessage(Language.ERROR_AUTH));
293         } catch (UpdateException e) {
294             setStatusMessage(Language.getMessage(Language.ERROR_SERVER_GENERIC));
295         } catch (Exception JavaDoc e) {
296             setStatusMessage(Language.getMessage(Language.ERROR_CONNECT));
297         }
298
299         if (syncError) {
300             setStatusMessage(Language.getMessage(Language.ERROR_SYNC));
301         }
302
303         setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
304     }
305
306     /**
307      * Show the message for the status of synchronization process
308      *
309      * @param msg the message to show
310      */

311     private void setStatusMessage(String JavaDoc msg) {
312         jlStatus.setText(msg);
313         jlStatus.setAutoscrolls(true);
314         jlStatus.setHorizontalAlignment(SwingConstants.CENTER);
315     }
316
317     /**
318      * Saves the specified configuration parameters
319      * by passing them to the DeviceManager
320      *
321      * @param values a list of configuration values
322      */

323     public void writeSyncSettings(Hashtable JavaDoc values) {
324
325         try {
326             xmlContactValues.put(PARAM_SYNCSOURCEDRIVE, values.get(PARAM_SYNCSOURCEDRIVE));
327             xmlContactValues.put(PARAM_SYNCMODE, values.get(PARAM_SYNCCONTACT));
328
329             xmlCalendarValues.put(PARAM_SYNCSOURCEDRIVE, values.get(PARAM_SYNCSOURCEDRIVE));
330             xmlCalendarValues.put(PARAM_SYNCMODE, values.get(PARAM_SYNCCALENDAR));
331
332             xmlNoteValues.put(PARAM_SYNCSOURCEDRIVE, values.get(PARAM_SYNCSOURCEDRIVE));
333             xmlNoteValues.put(PARAM_SYNCMODE, values.get(PARAM_SYNCNOTE));
334
335             //
336
// Set Logging level
337
//
338
syncmlValues.put (PARAM_LOGLEVEL,values.get(PARAM_LOGLEVEL));
339             rootNode.setValue(DM_VALUE_PATH, PARAM_LOGLEVEL, values.get(PARAM_LOGLEVEL));
340
341             //
342
// Store source drive
343
//
344
rootNode.setValue(DM_VALUE_CONTACT_PATH,
345                               PARAM_SYNCSOURCEDRIVE,
346                               values.get(PARAM_SYNCSOURCEDRIVE)
347             );
348             rootNode.setValue(DM_VALUE_CALENDAR_PATH,
349                               PARAM_SYNCSOURCEDRIVE ,
350                               values.get(PARAM_SYNCSOURCEDRIVE)
351             );
352             rootNode.setValue(DM_VALUE_NOTE_PATH ,
353                               PARAM_SYNCSOURCEDRIVE,
354                               values.get(PARAM_SYNCSOURCEDRIVE)
355             );
356
357             //
358
// Store sync mode
359
//
360
rootNode.setValue(DM_VALUE_CONTACT_PATH,
361                               PARAM_SYNCMODE ,
362                               values.get(PARAM_SYNCCONTACT)
363             );
364             rootNode.setValue(DM_VALUE_CALENDAR_PATH,
365                               PARAM_SYNCMODE ,
366                               values.get(PARAM_SYNCCALENDAR)
367             );
368             rootNode.setValue(DM_VALUE_NOTE_PATH,
369                               PARAM_SYNCMODE ,
370                               values.get(PARAM_SYNCNOTE)
371             );
372
373         } catch (DMException e) {
374             if (Logger.isLoggable(Logger.ERROR)) {
375                 Logger.error(Language.getMessage(Language.ERROR_DEVICE_MANAGER));
376                 Logger.error(e.getMessage());
377             }
378         } catch (Exception JavaDoc e) {
379             if (Logger.isLoggable(Logger.ERROR)) {
380                 Logger.error(Language.getMessage(Language.ERROR_WRITE_SYNCSET));
381                 Logger.error(e.getMessage());
382             }
383         }
384     }
385
386     /**
387      * Saves the specified configuration parameters
388      * by passing them to the DeviceManager
389      *
390      * @param values a list of configuration values
391      */

392     public void writeRemoteSettings(Hashtable JavaDoc values) {
393
394         try {
395             xmlContactValues.put(PARAM_SYNCSOURCEURI, values.get(PARAM_SOURCEURICONTACT));
396             xmlCalendarValues.put(PARAM_SYNCSOURCEURI,values.get(PARAM_SOURCEURICALENDAR));
397             xmlNoteValues.put(PARAM_SYNCSOURCEURI,values.get(PARAM_SOURCEURINOTE));
398
399             rootNode.setValue(DM_VALUE_CONTACT_PATH ,
400                               PARAM_SYNCSOURCEURI ,
401                               values.get(PARAM_SOURCEURICONTACT)
402             );
403             rootNode.setValue(DM_VALUE_CALENDAR_PATH ,
404                               PARAM_SYNCSOURCEURI ,
405                               values.get(PARAM_SOURCEURICALENDAR)
406             );
407             rootNode.setValue(DM_VALUE_NOTE_PATH ,
408                               PARAM_SYNCSOURCEURI ,
409                               values.get(PARAM_SOURCEURINOTE)
410             );
411         } catch (DMException e) {
412             if (Logger.isLoggable(Logger.ERROR)) {
413                 Logger.error(Language.getMessage(Language.ERROR_DEVICE_MANAGER));
414                 Logger.error(e.getMessage());
415             }
416         } catch (Exception JavaDoc e) {
417             if (Logger.isLoggable(Logger.ERROR)) {
418                 Logger.error(Language.getMessage(Language.ERROR_WRITE_REMOTESET));
419                 Logger.error(e.getMessage());
420             }
421         }
422     }
423
424     /**
425      * Saves the specified configuration parameters
426      * by passing them to the DeviceManager
427      *
428      * @param values a list of configuration values
429      */

430     public void writeCommunicationSettings(Hashtable JavaDoc values) {
431         try {
432             syncmlValues.put(PARAM_SYNCMLURL, values.get(PARAM_SYNCMLURL));
433             syncmlValues.put(PARAM_USERNAME , values.get(PARAM_USERNAME) );
434             syncmlValues.put(PARAM_PASSWORD , values.get(PARAM_PASSWORD) );
435             syncmlValues.put(PARAM_DEVICEID , values.get(PARAM_DEVICEID) );
436
437             rootNode.setValue(DM_VALUE_PATH ,
438                               PARAM_TARGETLOCALURI ,
439                               getBaseURL((String JavaDoc) values.get(PARAM_SYNCMLURL))
440             );
441             rootNode.setValue(DM_VALUE_PATH ,
442                               PARAM_SYNCMLURL ,
443                               values.get(PARAM_SYNCMLURL)
444             ) ;
445             rootNode.setValue(DM_VALUE_PATH ,
446                               PARAM_USERNAME ,
447                               values.get(PARAM_USERNAME)
448             );
449             rootNode.setValue(DM_VALUE_PATH ,
450                               PARAM_PASSWORD ,
451                               values.get(PARAM_PASSWORD)
452             );
453             rootNode.setValue(DM_VALUE_PATH ,
454                               PARAM_DEVICEID ,
455                               values.get(PARAM_DEVICEID)
456             );
457
458         } catch (DMException e) {
459             if (Logger.isLoggable(Logger.ERROR)) {
460                 Logger.error(Language.getMessage(Language.ERROR_DEVICE_MANAGER));
461                 Logger.error(e.getMessage());
462             }
463         } catch (Exception JavaDoc e) {
464             if (Logger.isLoggable(Logger.ERROR)) {
465                 Logger.error(Language.getMessage(Language.ERROR_WRITE_COMMUNICATIONSET));
466                 Logger.error(e.getMessage());
467             }
468         }
469     }
470
471     public Hashtable JavaDoc getXmlContactValues() {
472         return this.xmlContactValues;
473     }
474
475     public Hashtable JavaDoc getXmlCalendarValues() {
476         return this.xmlCalendarValues;
477     }
478
479     public Hashtable JavaDoc getXmlNoteValues() {
480         return this.xmlNoteValues;
481     }
482
483     public Hashtable JavaDoc getSyncmlValues() {
484         return this.syncmlValues;
485     }
486
487     public File JavaDoc getFileLog() {
488         File JavaDoc f = new File JavaDoc(rootDirectory + File.separator + "sync.log");
489         if (f.exists()) {
490             return f;
491         }
492         return null;
493     }
494
495     /**
496      * Closes the window and stops the application
497      */

498     public void exit() {
499         setVisible(false);
500         dispose();
501
502         if (Logger.isLoggable(Logger.INFO)) {
503             Logger.info(Language.getMessage(Language.LOGGING_STOPPED));
504         }
505         System.exit(0);
506     }
507
508     /**
509      * Fired when the synchronization process start.
510      *
511      * @param syncEvent the synchronization event
512      */

513     public void syncBegin(SyncEvent syncEvent) {
514         syncError = false;
515         if (Logger.isLoggable(Logger.INFO)) {
516             Logger.info(syncEvent.getDate() + ", " +
517                         Language.getMessage(Language.LOG_SYNC_BEGIN)
518             );
519         }
520     }
521
522     /**
523      * Fired when the synchronization process end.
524      *
525      * @param syncEvent the synchronization event
526      */

527     public void syncEnd(SyncEvent syncEvent) {
528         if (Logger.isLoggable(Logger.INFO)) {
529             Logger.info(syncEvent.getDate() + ", " +
530                         Language.getMessage(Language.LOG_SYNC_END)
531             );
532         }
533         setStatusMessage(Language.getMessage(Language.STATUS_SYNC_END));
534     }
535
536     /**
537      * Fired to notify that the engine encountered a not blocking error
538      *
539      * @param syncEvent the synchronization event
540      */

541     public void syncError(SyncEvent syncEvent) {
542         syncError = true;
543         if (Logger.isLoggable(Logger.DEBUG)) {
544             Logger.debug(syncEvent.getDate() + ", "
545                          + Language.getMessage(Language.LOG_SYNC_ERROR)
546                          + " - " + Language.getMessage(Language.LOG_SYNCERROR_MSG)
547                          + " " + syncEvent.getMessage()
548                          + " - " + Language.getMessage(Language.LOG_SYNCCAUSE_MSG)
549                          + " " + syncEvent.getCause().getMessage()
550             );
551         }
552     }
553
554     /**
555      * Fired to notify that the initialization package was correctly set and
556      * precessed.
557      *
558      * @param syncEvent the synchronization event
559      */

560     public void sendInitialization(SyncEvent syncEvent) {
561         if (Logger.isLoggable(Logger.DEBUG)) {
562             Logger.debug(syncEvent.getDate() + ", " +
563                          Language.getMessage(Language.LOG_SYNC_SENDINIT)
564             );
565         }
566     }
567
568     /**
569      * Fired to notify that the modifications package was correctly set and
570      * processed
571      *
572      * @param syncEvent the synchronization event
573      */

574     public void sendModification(SyncEvent syncEvent) {
575        if (Logger.isLoggable(Logger.DEBUG)) {
576             Logger.debug(syncEvent.getDate() + ", " +
577                          Language.getMessage(Language.LOG_SYNC_SENDMOD)
578             );
579         }
580     }
581
582     /**
583      * Fired to notify that the final package was correctly set and processed.
584      *
585      * @param syncEvent the synchronization event
586      */

587     public void sendFinalization(SyncEvent syncEvent) {
588        if (Logger.isLoggable(Logger.DEBUG)) {
589             Logger.debug(syncEvent.getDate() + ", " +
590                          Language.getMessage(Language.LOG_SYNC_SENDFINAL)
591             );
592         }
593     }
594
595     /**
596      * Fired to notify that the engine started to send data to the server
597      *
598      * @param syncTransportEvent the SyncTransportEvent event
599      */

600     public void sendDataBegin(SyncTransportEvent syncTransportEvent) {
601        if (Logger.isLoggable(Logger.DEBUG)) {
602             Logger.debug(Language.getMessage(Language.LOG_SYNCTRANSPORT_SENDDATABEGIN)
603                          + " " + syncTransportEvent.getData()
604             );
605         }
606     }
607
608     /**
609      * Fired to notify that the engine has sent all data to the server
610      *
611      * @param syncTransportEvent the SyncTransportEvent event
612      */

613     public void sendDataEnd(SyncTransportEvent syncTransportEvent) {
614        if (Logger.isLoggable(Logger.DEBUG)) {
615             Logger.debug(Language.getMessage(Language.LOG_SYNCTRANSPORT_SENDDATAEND)
616                          + " " + syncTransportEvent.getData()
617             );
618         }
619     }
620
621     /**
622      * Fired to notify that the engine started to receive data from the server
623      *
624      * @param syncTransportEvent the SyncTransportEvent event
625      */

626     public void receiveDataBegin(SyncTransportEvent syncTransportEvent) {
627        if (Logger.isLoggable(Logger.DEBUG)) {
628             Logger.debug(Language.getMessage(Language.LOG_SYNCTRANSPORT_RECEIVEDATABEGIN)
629                          + " " + syncTransportEvent.getData()
630             );
631         }
632     }
633
634     /**
635      * Fired to notify that the engine is receiving data from the server
636      *
637      * @param syncTransportEvent the SyncTransportEvent event
638      */

639     public void dataReceived(SyncTransportEvent syncTransportEvent) {
640        if (Logger.isLoggable(Logger.DEBUG)) {
641             Logger.debug(Language.getMessage(Language.LOG_SYNCTRANSPORT_RECEIVEDATA)
642                          + " " + syncTransportEvent.getData()
643             );
644         }
645     }
646
647     /**
648      * Fired to notify that the engine has received all data from the server
649      *
650      * @param syncTransportEvent the SyncTransportEvent event
651      */

652     public void receiveDataEnd(SyncTransportEvent syncTransportEvent) {
653        if (Logger.isLoggable(Logger.DEBUG)) {
654             Logger.debug(Language.getMessage(Language.LOG_SYNCTRANSPORT_RECEIVEDATAEND)
655                          + " " + syncTransportEvent.getData()
656             );
657         }
658     }
659
660     /**
661      * Fired to notify the beginning of the synchronization of a SyncSource
662      *
663      * @param syncSourceEvent the SyncSource event
664      */

665     public void syncBegin(SyncSourceEvent syncSourceEvent) {
666         if (Logger.isLoggable(Logger.DEBUG)) {
667             Logger.debug(syncSourceEvent.getDate() + ", "
668                          + Language.getMessage(Language.LOG_SYNCSOURCE_BEGIN)
669                          + " - "
670                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
671                          + " " + syncSourceEvent.getSourceUri()
672                          + " - "
673                          + Language.getMessage(Language.LOG_SYNCSOURCE_SYNCMODE)
674                          + " " + syncSourceEvent.getSyncMode()
675             );
676         }
677     }
678
679     /**
680      * Fired to notify the end of the synchronization of a SyncSource
681      *
682      * @param syncSourceEvent the SyncSource event
683      */

684     public void syncEnd(SyncSourceEvent syncSourceEvent) {
685         if (Logger.isLoggable(Logger.DEBUG)) {
686             Logger.debug(syncSourceEvent.getDate() + ", "
687                          + Language.getMessage(Language.LOG_SYNCSOURCE_END)
688                          + " - "
689                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
690                          + " " + syncSourceEvent.getSourceUri()
691                          + " - "
692                          + Language.getMessage(Language.LOG_SYNCSOURCE_SYNCMODE)
693                          + " " + syncSourceEvent.getSyncMode()
694             );
695         }
696     }
697
698     /**
699      * Fired to notify that an item addition has been received
700      *
701      * @param syncItemEvent the SyncItemEvent event
702      */

703     public void itemAddedByServer(SyncItemEvent syncItemEvent) {
704         if (Logger.isLoggable(Logger.DEBUG)) {
705             Logger.debug(Language.getMessage(Language.LOG_SYNCITEM_ADDSERVER)
706                          + " - "
707                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
708                          + " " + syncItemEvent.getSourceUri()
709                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
710                          + " " + syncItemEvent.getItemKey().getKeyAsString()
711             );
712         }
713     }
714
715     /**
716      * Fired to notify that an item deletion has been received
717      *
718      * @param syncItemEvent the SyncItemEvent event
719      */

720     public void itemDeletedByServer(SyncItemEvent syncItemEvent) {
721         if (Logger.isLoggable(Logger.DEBUG)) {
722             Logger.debug(Language.getMessage(Language.LOG_SYNCITEM_DELETESERVER)
723                          + " - "
724                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
725                          + " " + syncItemEvent.getSourceUri()
726                          + " - "
727                          + Language.getMessage(Language.LOG_SYNCITEM_KEY)
728                          + " " + syncItemEvent.getItemKey().getKeyAsString()
729             );
730         }
731     }
732
733     /**
734      * Fired to notify that an item update has been received.
735      *
736      * @param syncItemEvent the SyncItemEvent event
737      */

738     public void itemUpdatedByServer(SyncItemEvent syncItemEvent) {
739         if (Logger.isLoggable(Logger.DEBUG)) {
740             Logger.debug(Language.getMessage(Language.LOG_SYNCITEM_UPDATESERVER)
741                          + " - "
742                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
743                          + " " + syncItemEvent.getSourceUri()
744                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
745                          + " " + syncItemEvent.getItemKey().getKeyAsString()
746             );
747         }
748     }
749
750     /**
751      * Fired to notify that an item addition has been sent.
752      *
753      * @param syncItemEvent the SyncItemEvent event
754      */

755     public void itemAddedByClient(SyncItemEvent syncItemEvent) {
756         if (Logger.isLoggable(Logger.DEBUG)) {
757             Logger.debug(Language.getMessage(Language.LOG_SYNCITEM_ADDCLIENT)
758                          + " - "
759                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
760                          + " " + syncItemEvent.getSourceUri()
761                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
762                          + " " + syncItemEvent.getItemKey().getKeyAsString()
763             );
764         }
765     }
766
767     /**
768      * Fired to notify that an item deletion has been sent.
769      *
770      * @param syncItemEvent the SyncItemEvent event
771      */

772     public void itemDeletedByClient(SyncItemEvent syncItemEvent) {
773         if (Logger.isLoggable(Logger.DEBUG)) {
774             Logger.debug(Language.getMessage(Language.LOG_SYNCITEM_DELETECLIENT)
775                          + " - "
776                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
777                          + " " + syncItemEvent.getSourceUri()
778                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
779                          + " " + syncItemEvent.getItemKey().getKeyAsString()
780             );
781         }
782     }
783
784     /**
785      * Fired to notify that an item update has been sent.
786      *
787      * @param syncItemEvent the SyncItemEvent event
788      */

789     public void itemUpdatedByClient(SyncItemEvent syncItemEvent) {
790         if (Logger.isLoggable(Logger.DEBUG)) {
791             Logger.debug(Language.getMessage(Language.LOG_SYNCITEM_UPDATECLIENT)
792                          + " - "
793                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
794                          + " " + syncItemEvent.getSourceUri()
795                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
796                          + " " + syncItemEvent.getItemKey().getKeyAsString()
797             );
798         }
799     }
800
801     /**
802      * Fired to notify a status command to send
803      *
804      * @param syncStatusEvent the SyncStatusEvent event
805      */

806     public void statusToSend(SyncStatusEvent syncStatusEvent) {
807         if (Logger.isLoggable(Logger.DEBUG)) {
808             Logger.debug(Language.getMessage(Language.LOG_SYNCSTATUS_SEND)
809                          + " - " + Language.getMessage(Language.LOG_SYNCSTATUS_CMD)
810                          + " " + syncStatusEvent.getCommand()
811                          + " - " + Language.getMessage(Language.LOG_SYNCSTATUS_STATUS)
812                          + " " + syncStatusEvent.getStatusCode()
813                          + " - "
814                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
815                          + " " + syncStatusEvent.getSourceUri()
816                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
817                          + " " + syncStatusEvent.getItemKey().getKeyAsString()
818             );
819         }
820     }
821
822     /**
823      * Fired to notify a received status command
824      *
825      * @param syncStatusEvent the SyncStatusEvent event
826      */

827     public void statusReceived(SyncStatusEvent syncStatusEvent) {
828         if (Logger.isLoggable(Logger.DEBUG)) {
829             Logger.debug(Language.getMessage(Language.LOG_SYNCSTATUS_RECEIVED)
830                          + " - " + Language.getMessage(Language.LOG_SYNCSTATUS_CMD)
831                          + " " + syncStatusEvent.getCommand()
832                          + " - " + Language.getMessage(Language.LOG_SYNCSTATUS_STATUS)
833                          + " " + syncStatusEvent.getStatusCode()
834                          + " - "
835                          + Language.getMessage(Language.LOG_SYNCSOURCE_SOURCEURI)
836                          + " " + syncStatusEvent.getSourceUri()
837                          + " - " + Language.getMessage(Language.LOG_SYNCITEM_KEY)
838                          + " " + syncStatusEvent.getItemKey().getKeyAsString()
839             );
840         }
841     }
842
843     //---------------------------------------------------------- Private methods
844

845     /**
846      * Returns a string representing the path of the specified dir
847      * concatenating it with the specified source directory
848      *
849      * @param sourceDirectory the source directory
850      * @param dir the directory
851      * @return the directory's path
852      */

853     private String JavaDoc buildPath(String JavaDoc sourceDirectory, String JavaDoc dir) {
854         return sourceDirectory+"/"+dir;
855     }
856
857     private String JavaDoc getBaseURL (String JavaDoc url) {
858
859         int firstSlash = url.indexOf("/" ) ;
860         int secondSlash = url.indexOf("/", firstSlash + 1 ) ;
861         int thirdSlash = url.indexOf("/", secondSlash + 1 ) ;
862         int portColon = url.indexOf(":", secondSlash + 1 ) ;
863
864         if (thirdSlash < portColon || portColon == -1) {
865
866             //
867
// No port is specified (ex: http://127.0.0.1/sync4j)
868
//
869
return url.substring(0, thirdSlash);
870         } else {
871             //
872
// A port is specified and must be removed
873
// (ex: http://127.0.0.1:8080/sync4j)
874
//
875
return url.substring(0, portColon);
876         }
877     }
878 }
879
Popular Tags