KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > admin > AdminManager


1 /**
2  * Copyright (C) 2003-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.server.admin;
19
20 import java.beans.XMLEncoder JavaDoc;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import java.sql.*;
28 import java.io.*;
29 import java.util.logging.Logger JavaDoc;
30 import java.util.logging.Level JavaDoc;
31
32 import sync4j.framework.core.*;
33 import sync4j.framework.logging.Sync4jLogger;
34 import sync4j.framework.config.ConfigurationConstants;
35 import sync4j.framework.config.LoggingConfiguration;
36
37 import sync4j.framework.engine.source.SyncSource;
38 import sync4j.framework.engine.source.SyncSourceException;
39
40 import sync4j.framework.security.Sync4jPrincipal;
41
42 import sync4j.framework.server.Sync4jUser;
43 import sync4j.framework.server.Sync4jDevice;
44 import sync4j.framework.server.Sync4jModule;
45 import sync4j.framework.server.Sync4jSourceType;
46 import sync4j.framework.server.Sync4jConnector;
47 import sync4j.framework.server.Sync4jSource;
48 import sync4j.framework.server.error.*;
49
50 import sync4j.framework.server.store.Clause;
51 import sync4j.framework.server.store.WhereClause;
52 import sync4j.framework.server.store.LogicalClause;
53 import sync4j.framework.server.store.PersistentStore;
54 import sync4j.framework.server.store.PersistentStoreException;
55
56 import sync4j.framework.tools.beans.BeanFactory;
57 import sync4j.framework.tools.beans.BeanException;
58 import sync4j.framework.tools.beans.BeanNotFoundException;
59
60 import sync4j.server.admin.UserManager;
61 import sync4j.server.admin.AdminException;
62
63 import sync4j.server.config.Configuration;
64 import sync4j.server.config.ServerConfiguration;
65
66 /**
67  * This is the session enterprise java bean that handles the administration
68  * console. It is designed to be a stateless session bean.
69  * <p>
70  * This server accepts the requests addressed to the hostname
71  * indicated by the configuration property pointed by {CONFIG_SERVER_URI} (see
72  * Sync4j.properties).
73  * <p>
74  * AdminBean uses the system property sync4j.configpath to set the base of the
75  * config path.
76  *
77  * @author Luigia Fassina @ Funambol
78  *
79  * @version $Id: AdminManager.java,v 1.12 2005/07/19 12:35:49 nichele Exp $
80  *
81  */

82 public class AdminManager
83 implements ConfigurationConstants, java.io.Serializable JavaDoc {
84     // ------------------------------------------------------- Private constants
85

86     // This role is used only for authentication purposes
87
private static final String JavaDoc ROLE_SPECIAL = "special_sync_admin";
88
89     // ------------------------------------------------------------ Private data
90

91     private transient Logger JavaDoc log = null;
92
93     private Configuration config = null;
94     private PersistentStore ps = null;
95     private UserManager userManager = null;
96
97     // ------------------------------------------------------------ Constructors
98

99     public AdminManager() {
100
101         log = Sync4jLogger.getLogger("admin");
102
103         try {
104             config = Configuration.getConfiguration();
105             ps = (PersistentStore)config.getStore();
106             userManager = (UserManager)config.getUserManager();
107         } catch (Throwable JavaDoc t) {
108             log.severe("Unable to create the admin manager: " + t.getMessage());
109             log.throwing(getClass().getName(), "constructor", t);
110         }
111     }
112
113     // ---------------------------------------------------------- Public methods
114

115     public Configuration getConfig() {
116         return config;
117     }
118
119     /**
120      * Read the list of roles available.
121      *
122      * @return names of roles available
123      *
124      * @throws ServerException
125      * @throws AdminException
126      */

127     public String JavaDoc[] getRoles()
128     throws ServerException, AdminException {
129         String JavaDoc[] roles = null;
130
131         try {
132
133             roles = userManager.getRoles();
134
135             if (roles != null) {
136                 List JavaDoc l = Arrays.asList(roles);
137                 Vector JavaDoc v = new Vector JavaDoc(l);
138                 int size = v.size();
139                 for (int i=0; i<size; i++) {
140                     String JavaDoc role = (String JavaDoc)v.get(i);
141                     if (role.startsWith(ROLE_SPECIAL)) {
142                         v.remove(i);
143                         size = v.size();
144                     }
145                 }
146                 roles = (String JavaDoc[])v.toArray(new String JavaDoc[0]);
147             }
148
149         } catch (PersistentStoreException e) {
150             String JavaDoc msg = "Error reading roles: " + e.getMessage();
151
152             if (log.isLoggable(Level.SEVERE)) {
153                 log.severe(msg);
154             }
155             log.throwing(getClass().getName(), "getRoles", e);
156
157             throw new ServerException(msg, e);
158         }
159         return roles;
160     }
161
162     /**
163      * Read all users that satisfy the parameter of search.
164      *
165      * @param clause array of conditions for the query
166      *
167      * @return array of Sync4jUser
168      *
169      * @throws ServerException
170      * @throws AdminException
171      */

172     public Sync4jUser[] getUsers(Clause clause)
173     throws ServerException, AdminException {
174         Sync4jUser[] users = null;
175         try {
176
177             users = userManager.getUsers(clause);
178
179             for (int i=0; (users != null) && i<users.length; i++) {
180                 userManager.getUserRoles(users[i]);
181             }
182
183         } catch (PersistentStoreException e) {
184             String JavaDoc msg = "Error reading Users: " + e.getMessage();
185
186             if (log.isLoggable(Level.SEVERE)) {
187                 log.severe(msg);
188             }
189             log.throwing(getClass().getName(), "getUsers", e);
190
191             throw new ServerException(msg, e);
192         }
193
194         return users;
195     }
196
197     /**
198      * Adds a new user and the assigned role to it
199      *
200      * @param user the user to insert
201      *
202      * @return the username
203      *
204      * @throws ServerException
205      * @throws AdminException
206      */

207     public String JavaDoc addUser(Sync4jUser user)
208     throws ServerException, AdminException {
209         try {
210
211             userManager.insertUser(user);
212
213             return user.getUsername();
214
215         } catch (PersistentStoreException e) {
216             String JavaDoc msg = "Error inserting User: " + e.getMessage();
217             if (log.isLoggable(Level.SEVERE)) {
218                 log.severe(msg);
219             }
220             log.throwing(getClass().getName(), "addUser", e);
221
222             throw new ServerException(msg, e);
223         }
224     }
225
226     /**
227      * Update the information of the specific user
228      *
229      * @param user the user with informations updated
230      *
231      * @throws ServerException
232      * @throws AdminException
233      */

234     public void setUser(Sync4jUser user)
235     throws ServerException, AdminException {
236         try {
237
238             userManager.setUser(user);
239
240         } catch (PersistentStoreException e) {
241             String JavaDoc msg = "Error updating User: " + e.getMessage();
242
243             if (log.isLoggable(Level.SEVERE)) {
244                 log.severe(msg);
245             }
246             log.throwing(getClass().getName(), "setUser", e);
247
248             throw new ServerException(msg, e);
249         }
250     }
251
252     /**
253      * Delete the user
254      *
255      * @param userName the name of user to delete
256      *
257      * @throws ServerException
258      * @throws AdminException
259      */

260     public void deleteUser(String JavaDoc userName)
261     throws ServerException, AdminException {
262         try {
263             Sync4jUser user = new Sync4jUser(userName,null,null,null,null,null);
264             userManager.deleteUser(user);
265
266         } catch (PersistentStoreException e) {
267             String JavaDoc msg = "Error deleting User: " + e.getMessage();
268             if (log.isLoggable(Level.SEVERE)) {
269                 log.severe(msg);
270             }
271             log.throwing(getClass().getName(), "deleteUser", e);
272
273             throw new ServerException(msg, e);
274         }
275     }
276
277     /**
278      * Count the number of users that satisfy the specif clauses.
279      *
280      * @param clause the conditions of the search
281      *
282      * @return number of users
283      *
284      * @throws ServerException
285      * @throws AdminException
286      */

287     public int countUsers(Clause clause)
288     throws ServerException, AdminException {
289         int n = 0;
290
291         try {
292
293             n = userManager.countUsers(clause);
294
295         } catch (PersistentStoreException e) {
296             String JavaDoc msg = "Error counting users: " + e.getMessage();
297             if (log.isLoggable(Level.SEVERE)) {
298                 log.severe(msg);
299             }
300
301             log.throwing(getClass().getName(), "countUsers", e);
302
303             throw new ServerException(msg, e);
304         }
305         return n;
306     }
307
308     /**
309      * Read a list of device that satisfy the specific conditions.
310      *
311      * @param clauses the conditions foe search informations.
312      *
313      * @return array of device
314      *
315      * @throws ServerException
316      * @throws AdminException
317      */

318     public Sync4jDevice[] getDevices(Clause clauses)
319     throws ServerException, AdminException {
320         Sync4jDevice[] devices = null;
321
322         try {
323
324             devices = (Sync4jDevice[])ps.read(new Sync4jDevice(), clauses);
325
326         } catch (PersistentStoreException e) {
327             String JavaDoc msg = "Error reading devices: " + e.getMessage();
328             if (log.isLoggable(Level.SEVERE)) {
329                 log.severe(msg);
330             }
331
332             log.throwing(getClass().getName(), "getDevices", e);
333
334             throw new ServerException(msg, e);
335         }
336         return devices;
337     }
338
339     /**
340      * Adds a new device.
341      *
342      * @param d the new device
343      *
344      * @return the device id
345      *
346      * @throws ServerException
347      * @throws AdminException
348      */

349     public String JavaDoc addDevice(Sync4jDevice d)
350     throws ServerException, AdminException {
351         try {
352
353             ps.store(d);
354
355         } catch (PersistentStoreException e) {
356             String JavaDoc msg = "Error adding device: " + e.getMessage();
357             if (log.isLoggable(Level.SEVERE)) {
358                 log.severe(msg);
359             }
360
361             log.throwing(getClass().getName(), "addDevice", e);
362
363             throw new ServerException(msg, e);
364         }
365         return d.getDeviceId();
366     }
367
368     /**
369      * Update the information of specific device.
370      *
371      * @param d the device to update
372      *
373      * @throws ServerException
374      * @throws AdminException
375      */

376     public void setDevice(Sync4jDevice d)
377     throws ServerException, AdminException {
378         try {
379
380             ps.store(d);
381
382         } catch (PersistentStoreException e) {
383             String JavaDoc msg = "Error updating device: " + e.getMessage();
384             if (log.isLoggable(Level.SEVERE)) {
385                 log.severe(msg);
386             }
387
388             log.throwing(getClass().getName(), "setDevice", e);
389
390             throw new ServerException(msg, e);
391         }
392     }
393
394     /**
395      * Delete the specific device.
396      *
397      * @param deviceId the device id that identifies the device
398      *
399      * @throws ServerException
400      * @throws AdminException
401      */

402     public void deleteDevice(String JavaDoc deviceId)
403     throws ServerException, AdminException{
404         try {
405             Sync4jDevice sd = new Sync4jDevice(deviceId);
406             ps.delete(sd);
407
408         } catch (PersistentStoreException e) {
409             String JavaDoc msg = "Error deleting device: " + e.getMessage();
410
411             if (log.isLoggable(Level.SEVERE)) {
412                 log.severe(msg);
413             }
414
415             log.throwing(getClass().getName(), "deleteDevice", e);
416
417             throw new ServerException(msg, e);
418         }
419     }
420
421     /**
422      * Count the number of device that satisfy the specific clauses.
423      *
424      * @param clauses the specific conditions for counting devices
425      *
426      * @return the number of device finds
427      *
428      * @throws ServerException
429      * @throws AdminException
430      */

431     public int countDevices(Clause clauses)
432     throws ServerException, AdminException {
433         int n = 0;
434
435         try {
436
437             n = ps.count(new Sync4jDevice(), clauses);
438
439         } catch (PersistentStoreException e) {
440             String JavaDoc msg = "Error counting devices: " + e.getMessage();
441             if (log.isLoggable(Level.SEVERE)) {
442                 log.severe(msg);
443             }
444
445             log.throwing(getClass().getName(), "countDevices", e);
446
447             throw new ServerException(msg, e);
448         }
449         return n;
450     }
451
452     /**
453      * Read all principal that satisfy the clauses.
454      *
455      * @param clauses the specific conditions for search principal
456      *
457      * @return array of principal
458      *
459      * @throws ServerException
460      * @throws AdminException
461      */

462     public Sync4jPrincipal[] getPrincipals(Clause clauses)
463     throws ServerException, AdminException {
464         Sync4jPrincipal[] principals = null;
465
466         try {
467
468             principals = (Sync4jPrincipal[])ps.read(new Sync4jPrincipal(null,null,null), clauses);
469
470         } catch (PersistentStoreException e) {
471             String JavaDoc msg = "Error reading principals: " + e.getMessage();
472
473             if (log.isLoggable(Level.SEVERE)) {
474                 log.severe(msg);
475             }
476
477             log.throwing(getClass().getName(), "getPrincipals", e);
478
479             throw new ServerException(msg, e);
480         }
481         return principals;
482     }
483
484     /**
485      * Adds a new principal.
486      *
487      * @param p the new principal
488      *
489      * @return the principal id
490      *
491      * @throws ServerException
492      * @throws AdminException
493      */

494     public String JavaDoc addPrincipal(Sync4jPrincipal p)
495     throws ServerException, AdminException {
496         try {
497
498             ps.store(p);
499
500         } catch (PersistentStoreException e) {
501             String JavaDoc msg = "Error adding rincipal: " + e.getMessage();
502
503             if (log.isLoggable(Level.SEVERE)) {
504                 log.severe(msg);
505             }
506
507             log.throwing(getClass().getName(), "addPrincipal", e);
508
509             throw new ServerException(msg, e);
510         }
511         return p.getId();
512     }
513
514     /**
515      * Delete the specific principal.
516      *
517      * @param principalId the principal id that identifies the principal
518      *
519      * @throws ServerException
520      * @throws AdminException
521      */

522     public void deletePrincipal(String JavaDoc principalId)
523     throws ServerException, AdminException {
524         try {
525             Sync4jPrincipal sp = new Sync4jPrincipal(principalId, null, null);
526             ps.delete(sp);
527
528         } catch (PersistentStoreException e) {
529             String JavaDoc msg = "Error deleting principal: " + e.getMessage();
530             if (log.isLoggable(Level.SEVERE)) {
531                 log.severe(msg);
532             }
533
534             log.throwing(getClass().getName(), "deletePrincipal", e);
535
536             throw new ServerException(msg, e);
537         }
538     }
539
540     /**
541      * Count the number of principal that satisfy the specific clauses.
542      *
543      * @param clauses the specific conditions for counting principals
544      *
545      * @return the number of principal finds
546      *
547      * @throws ServerException
548      * @throws AdminException
549      */

550     public int countPrincipals(Clause clauses)
551     throws ServerException, AdminException {
552         int n = 0;
553
554         try {
555
556             n = ps.count(new Sync4jPrincipal(null,null,null), clauses);
557
558         } catch (PersistentStoreException e) {
559             String JavaDoc msg = "Error counting principals: " + e.getMessage();
560             if (log.isLoggable(Level.SEVERE)) {
561                 log.severe(msg);
562             }
563
564             log.throwing(getClass().getName(), "countPrincipals", e);
565
566             throw new ServerException(msg, e);
567         }
568         return n;
569     }
570
571     /**
572      * Read all modules information
573      *
574      * @return an array with the modules information (empty if no objects are found)
575      *
576      * @throws ServerException
577      * @throws AdminException
578      */

579     public Sync4jModule[] getModulesName()
580     throws ServerException, AdminException {
581         Sync4jModule[] modules = null;
582
583         try {
584
585             modules = (Sync4jModule[])ps.read(Sync4jModule.class);
586
587         } catch (PersistentStoreException e) {
588             String JavaDoc msg = "Error reading modules: " + e.getMessage();
589             if (log.isLoggable(Level.SEVERE)) {
590                 log.severe(msg);
591             }
592
593             log.throwing(getClass().getName(), "getModulesName", e);
594
595             throw new ServerException(msg, e);
596         }
597
598         return modules;
599     }
600
601     /**
602      * Read the information of the specific module
603      *
604      * @param moduleId the module id that identifies the module to search
605      *
606      * @return the relative information to the module
607      *
608      * @throws ServerException
609      * @throws AdminException
610      */

611     public Sync4jModule getModule(String JavaDoc moduleId)
612     throws ServerException, AdminException {
613         Sync4jModule module = null;
614
615         try {
616
617             module = new Sync4jModule(moduleId, null, null);
618             ps.read(module);
619
620             //for each SyncConnector read the SyncSourceType and
621
//for each SyncSourceType read the SyncSource
622
Sync4jConnector[] syncConnectors = module.getConnectors();
623             for (int i=0; (syncConnectors != null) && i<syncConnectors.length; i++) {
624                 Sync4jConnector sc = syncConnectors[i];
625                 Sync4jSourceType[] syncSourceTypes = sc.getSourceTypes();
626
627                 for (int y=0; (syncSourceTypes != null) && y<syncSourceTypes.length; y++) {
628                     Sync4jSource[] sync4jSources = (Sync4jSource[])ps.read(syncSourceTypes[y], null);
629
630                     ArrayList JavaDoc syncSources = new ArrayList JavaDoc();
631                     ArrayList JavaDoc syncSourcesFailed = new ArrayList JavaDoc();
632
633                     for (int z=0; z<sync4jSources.length; z++) {
634
635                         try {
636                             SyncSource syncSource = (SyncSource)BeanFactory.getNoInitBeanInstance(
637                             config.getClassLoader(),
638                             sync4jSources[z].getConfig()
639                             );
640                             syncSources.add(syncSource);
641
642                         } catch (BeanException e) {
643                             Throwable JavaDoc t = e.getCause();
644
645                             String JavaDoc msg = "Error creating SyncSource "
646                             + module.getModuleName()
647                             + "/"
648                             + sc.getConnectorName()
649                             + "/"
650                             + syncSourceTypes[y].getDescription()
651                             + "/"
652                             + sync4jSources[z].getUri();
653
654                             if ( t != null) {
655                                 msg += ": " + t.getMessage();
656                             }
657
658                             log.throwing(getClass().getName(), "getModule", t);
659
660                             syncSourcesFailed.add(
661                             new SyncSourceException(
662                             sync4jSources[z].getUri(),
663                             sync4jSources[z].getConfig(),
664                             t)
665                             );
666                         }
667                     }
668
669                     SyncSource[] syncSourcesOK = (SyncSource[])syncSources.toArray(new SyncSource[syncSources.size()]);
670                     syncSourceTypes[y].setSyncSources(syncSourcesOK);
671
672                     SyncSourceException[] syncSourcesNO = (SyncSourceException[])syncSourcesFailed.toArray(new SyncSourceException[syncSourcesFailed.size()]);
673                     syncSourceTypes[y].setSyncSourcesFailed(syncSourcesNO);
674                 }
675             }
676
677         } catch (PersistentStoreException e) {
678             String JavaDoc msg = "Error getting module: " + e.getMessage();
679             if (log.isLoggable(Level.SEVERE)) {
680                 log.severe("Error getting Module: " + e.getMessage());
681             }
682             log.throwing(getClass().getName(), "getModule", e);
683
684             throw new ServerException(msg, e);
685         }
686
687         return module;
688     }
689
690     /**
691      * Adds a new source into datastore and create a relative file xml with configuration.
692      * The source must have a defined source type.
693      * The source type must refer to a connector.
694      * The connector must refer to a module.
695      *
696      * @param moduleId the module id
697      * @param connectorId the connector id
698      * @param sourceTypeId the source type id
699      * @param source the information of the new source
700      *
701      * @throws ServerException
702      * @throws AdminException
703      */

704     public void addSource(String JavaDoc moduleId, String JavaDoc connectorId, String JavaDoc sourceTypeId, SyncSource source)
705     throws ServerException, AdminException {
706         String JavaDoc uri = source.getSourceURI();
707         String JavaDoc sourceName = source.getName();
708
709         String JavaDoc configFile = moduleId
710                           + File.separator
711                           + connectorId
712                           + File.separator
713                           + sourceTypeId
714                           ;
715
716         Sync4jSource s4j = new Sync4jSource(
717                                    uri,
718                                    configFile
719                                    + File.separator
720                                    + sourceName
721                                    + ".xml",
722                                    sourceTypeId,
723                                    sourceName);
724
725         //
726
//checking for the existance of the source before inserting
727
//
728
Sync4jSource existSource[] = null;
729         try {
730
731             WhereClause[] wc = new WhereClause[2];
732             String JavaDoc value[] = new String JavaDoc[1];
733             value[0] = uri;
734             wc[0] = new WhereClause("uri",value, WhereClause.OPT_EQ, false);
735
736             value = new String JavaDoc[1];
737             value[0] = sourceName;
738             wc[1] = new WhereClause("name",value, WhereClause.OPT_EQ, false);
739             LogicalClause lc = new LogicalClause(LogicalClause.OPT_OR, wc);
740
741             existSource = (Sync4jSource[])ps.read(s4j, lc);
742
743         } catch (PersistentStoreException e) {
744             String JavaDoc msg = "Error reading sources existing: " + e.getMessage();
745
746             if (log.isLoggable(Level.SEVERE)) {
747                 log.severe(msg);
748             }
749
750             log.throwing(getClass().getName(), "addSource", e);
751
752             throw new ServerException(msg, e);
753         }
754
755         if (existSource == null || existSource.length == 0) {
756             try {
757
758                 ps.store(s4j);
759
760             } catch (PersistentStoreException e) {
761                 String JavaDoc msg = "Error adding the SyncSource: " + e.getMessage();
762                 if (log.isLoggable(Level.SEVERE)) {
763                     log.severe(msg);
764                 }
765
766                 log.throwing(getClass().getName(), "addSource", e);
767
768                 throw new ServerException(msg, e);
769             }
770         } else {
771             String JavaDoc msg = "A SyncSource with URI "
772             + uri + " or with Name " + sourceName
773             + " is already present.";
774
775             throw new AdminException(msg);
776         }
777
778         try {
779
780             String JavaDoc path = config.getConfigPath() + File.separator + configFile;
781             if (path.startsWith("file:")) {
782                 path = path.substring(6);
783             }
784
785             File f = new File(path);
786             f.mkdirs();
787
788             XMLEncoder JavaDoc encoder = null;
789             encoder = new XMLEncoder JavaDoc(new FileOutputStream(path+File.separator+sourceName+".xml"));
790             encoder.writeObject((Object JavaDoc)source);
791             encoder.flush();
792             encoder.close();
793
794         } catch(FileNotFoundException e) {
795             String JavaDoc msg = "Error storing the SyncSource on file system: " + e.getMessage();
796             if (log.isLoggable(Level.SEVERE)) {
797                 log.severe(msg);
798             }
799
800             log.throwing(getClass().getName(), "addSource", e);
801
802             throw new ServerException(msg, e);
803         }
804     }
805
806     /**
807      * Read a list of syncSource that satisfy the specific conditions.
808      *
809      * @param clauses the conditions foe search informations.
810      *
811      * @return array of syncSource
812      *
813      * @throws ServerException
814      * @throws AdminException
815      */

816     public Sync4jSource[] getSync4jSources(Clause clauses)
817     throws ServerException, AdminException {
818         Sync4jSource[] sources = null;
819
820         try {
821
822             sources = (Sync4jSource[])ps.read(new Sync4jSource(), clauses);
823
824         } catch (PersistentStoreException e) {
825             String JavaDoc msg = "Error reading sources: " + e.getMessage();
826             if (log.isLoggable(Level.SEVERE)) {
827                 log.severe(msg);
828             }
829
830             log.throwing(getClass().getName(), "getSync4jSources", e);
831
832             throw new ServerException(msg, e);
833         }
834         return sources;
835     }
836
837
838     /**
839      * Update a specific source into datastore and create a relative file xml with configuration.
840      * The source must have a defined source type.
841      * The source type must refer to a connector.
842      * The connector must refer to a module.
843      *
844      * @param moduleId the module id
845      * @param connectorId the connector id
846      * @param sourceTypeId the source type id
847      * @param source the information of the new source
848      *
849      * @throws ServerException
850      * @throws AdminException
851      */

852     public void setSource(String JavaDoc moduleId, String JavaDoc connectorId, String JavaDoc sourceTypeId, SyncSource source)
853     throws ServerException, AdminException{
854         String JavaDoc uri = source.getSourceURI();
855         String JavaDoc sourceName = source.getName();
856
857         String JavaDoc configFile = moduleId
858                           + File.separator
859                           + connectorId
860                           + File.separator
861                           + sourceTypeId
862                           ;
863
864         Sync4jSource s4j = new Sync4jSource(uri,null,sourceTypeId,null);
865
866         try {
867
868             ps.read(s4j);
869
870         } catch (PersistentStoreException e) {
871             String JavaDoc msg = "Error reading sources existing: " + e.getMessage();
872
873             if (log.isLoggable(Level.SEVERE)) {
874                 log.severe(msg);
875             }
876
877             log.throwing(getClass().getName(), "addSource", e);
878
879             throw new ServerException(msg, e);
880         }
881
882         s4j.setSourceName(sourceName);
883
884         String JavaDoc nameFileXml = s4j.getConfig().substring(configFile.length());
885
886         try {
887
888             ps.store(s4j);
889
890         } catch (PersistentStoreException e) {
891             String JavaDoc msg = "Error storing SyncSource: " + e.getMessage();
892             if (log.isLoggable(Level.SEVERE)) {
893                 log.severe(msg);
894             }
895
896             log.throwing(getClass().getName(), "addSource", e);
897
898             throw new ServerException(msg, e);
899         }
900
901         try {
902
903             String JavaDoc path = config.getConfigPath() + File.separator + configFile;
904             if (path.startsWith("file:")) {
905                 path = path.substring(6);
906             }
907
908             File f = new File(path);
909             f.mkdirs();
910
911             XMLEncoder JavaDoc encoder = null;
912             encoder = new XMLEncoder JavaDoc(new FileOutputStream(path+File.separator+nameFileXml));
913             encoder.writeObject((Object JavaDoc)source);
914             encoder.flush();
915             encoder.close();
916
917         } catch(FileNotFoundException e) {
918             String JavaDoc msg = "Error storing SyncSource: " + e.getMessage();
919             if (log.isLoggable(Level.SEVERE)) {
920                 log.severe(msg);
921             }
922
923             log.throwing(getClass().getName(), "addSource", e);
924
925             throw new ServerException(msg, e);
926         }
927     }
928
929     /**
930      * Delete a specific source and the relative file of configuration.
931      *
932      * @param sourceUri the uri that identifies the source
933      *
934      * @throws ServerException
935      * @throws AdminException
936      */

937     public void deleteSource(String JavaDoc sourceUri)
938     throws ServerException, AdminException{
939         Sync4jSource s4j = new Sync4jSource(sourceUri,null,null,null);
940         try {
941
942             ps.read(s4j);
943
944         } catch (PersistentStoreException e) {
945             String JavaDoc msg = "Error reading source: " + e.getMessage();
946
947             if (log.isLoggable(Level.SEVERE)) {
948                 log.severe(msg);
949             }
950
951             log.throwing(getClass().getName(), "deleteSource", e);
952
953             throw new ServerException(msg, e);
954         }
955
956         try {
957             ps.delete(s4j);
958
959         } catch (PersistentStoreException e) {
960             String JavaDoc msg = "Error deleting SyncSource: " + e.getMessage();
961
962             if (log.isLoggable(Level.SEVERE)) {
963                 log.severe(msg);
964             }
965
966             log.throwing(getClass().getName(), "deleteSource", e);
967
968             throw new ServerException(msg, e);
969         }
970
971         String JavaDoc path = config.getConfigPath() + File.separator + s4j.getConfig();
972         if (path.startsWith("file:")) {
973             path = path.substring(6);
974         }
975
976         File f = new File(path);
977         f.delete();
978     }
979
980     /**
981      * Returns the server logging configuration
982      *
983      * @return the server logging configuration as a <i>LoggingConfiguration</i>
984      * object.
985      *
986      * @throws ServerException, AdminException
987      */

988     public LoggingConfiguration getLoggingConfiguration()
989     throws ServerException, AdminException {
990         return config.getLoggingConfiguration();
991     }
992
993     /**
994      * Saves and apply the new logging configuration
995      *
996      * @param settings the new logging configuration
997      *
998      * @throws ServerException
999      * @throws AdminException
1000     */

1001    public void setLoggingConfiguration(LoggingConfiguration settings)
1002    throws ServerException, AdminException {
1003        File f = new File(config.getConfigPath(),
1004                          config.getServerConfig().getEngineConfiguration().getLoggingConfiguration());
1005
1006        try {
1007            BeanFactory.saveBeanInstance(settings, f);
1008            config.setLoggingConfiguration();
1009        } catch (Exception JavaDoc e) {
1010            throw new ServerException(e.getMessage(), e);
1011        }
1012
1013    }
1014
1015    /**
1016     * Returns the server configuration
1017     *
1018     * @return the server configuration as a <i>ServerConfiguration</i>
1019     * object.
1020     *
1021     * @throws ServerException, AdminException
1022     */

1023    public ServerConfiguration getServerConfiguration()
1024    throws ServerException, AdminException {
1025        return config.getServerConfig();
1026    }
1027
1028    /**
1029     * Saves and apply the new server configuration
1030     *
1031     * @param serverConfig the new server configuration
1032     *
1033     * @throws ServerException
1034     * @throws AdminException
1035     */

1036    public void setServerConfiguration(ServerConfiguration serverConfig)
1037        throws ServerException, AdminException {
1038        File f = new File(config.getConfigPath(), BEAN_SERVER_CONFIGURATION);
1039        try {
1040            BeanFactory.saveBeanInstance(serverConfig, f);
1041            config.loadServerConfig();
1042        } catch (Exception JavaDoc e) {
1043            throw new ServerException(e.getMessage(), e);
1044        }
1045    }
1046
1047
1048    /**
1049     * Returns the server version.
1050     *
1051     * @return the server version
1052     *
1053     */

1054    public String JavaDoc getServerVersion() {
1055        return config.getServerConfig().getServerInfo().getSwV();
1056    }
1057    
1058    /**
1059     * Returns the server bean with the given name. If the bean does not exist,
1060     * am AdminException is thrown.
1061     *
1062     * @param bean the server bean
1063     *
1064     * @throws ServerException, AdminException
1065     */

1066    public Object JavaDoc getServerBean(String JavaDoc bean)
1067    throws ServerException, AdminException {
1068        try {
1069            return Configuration.getConfiguration().getBeanInstanceByName(bean);
1070        } catch (BeanNotFoundException e) {
1071            throw new AdminException(e.getMessage());
1072        } catch (Exception JavaDoc e) {
1073            throw new ServerException(e);
1074        }
1075    }
1076    
1077    /**
1078     * Sets the server bean with the given name.
1079     *
1080     * @param bean the server bean
1081     * @param obj the bean instance
1082     *
1083     * @throws ServerException, AdminException
1084     */

1085    public void setServerBean(String JavaDoc bean, Object JavaDoc obj)
1086    throws ServerException, AdminException {
1087        try {
1088            Configuration.getConfiguration().setBeanInstance(bean, obj);
1089        } catch (Exception JavaDoc e) {
1090            throw new ServerException(e);
1091        }
1092    }
1093
1094    // --------------------------------------------------------- private methods
1095
}
1096
Popular Tags