1 18 package sync4j.server.admin; 19 20 import java.beans.XMLEncoder ; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Arrays ; 25 import java.util.Vector ; 26 27 import java.sql.*; 28 import java.io.*; 29 import java.util.logging.Logger ; 30 import java.util.logging.Level ; 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 82 public class AdminManager 83 implements ConfigurationConstants, java.io.Serializable { 84 86 private static final String ROLE_SPECIAL = "special_sync_admin"; 88 89 91 private transient Logger log = null; 92 93 private Configuration config = null; 94 private PersistentStore ps = null; 95 private UserManager userManager = null; 96 97 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 t) { 108 log.severe("Unable to create the admin manager: " + t.getMessage()); 109 log.throwing(getClass().getName(), "constructor", t); 110 } 111 } 112 113 115 public Configuration getConfig() { 116 return config; 117 } 118 119 127 public String [] getRoles() 128 throws ServerException, AdminException { 129 String [] roles = null; 130 131 try { 132 133 roles = userManager.getRoles(); 134 135 if (roles != null) { 136 List l = Arrays.asList(roles); 137 Vector v = new Vector (l); 138 int size = v.size(); 139 for (int i=0; i<size; i++) { 140 String role = (String )v.get(i); 141 if (role.startsWith(ROLE_SPECIAL)) { 142 v.remove(i); 143 size = v.size(); 144 } 145 } 146 roles = (String [])v.toArray(new String [0]); 147 } 148 149 } catch (PersistentStoreException e) { 150 String 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 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 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 207 public String 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 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 234 public void setUser(Sync4jUser user) 235 throws ServerException, AdminException { 236 try { 237 238 userManager.setUser(user); 239 240 } catch (PersistentStoreException e) { 241 String 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 260 public void deleteUser(String 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 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 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 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 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 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 349 public String addDevice(Sync4jDevice d) 350 throws ServerException, AdminException { 351 try { 352 353 ps.store(d); 354 355 } catch (PersistentStoreException e) { 356 String 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 376 public void setDevice(Sync4jDevice d) 377 throws ServerException, AdminException { 378 try { 379 380 ps.store(d); 381 382 } catch (PersistentStoreException e) { 383 String 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 402 public void deleteDevice(String deviceId) 403 throws ServerException, AdminException{ 404 try { 405 Sync4jDevice sd = new Sync4jDevice(deviceId); 406 ps.delete(sd); 407 408 } catch (PersistentStoreException e) { 409 String 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 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 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 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 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 494 public String addPrincipal(Sync4jPrincipal p) 495 throws ServerException, AdminException { 496 try { 497 498 ps.store(p); 499 500 } catch (PersistentStoreException e) { 501 String 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 522 public void deletePrincipal(String 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 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 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 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 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 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 611 public Sync4jModule getModule(String 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 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 syncSources = new ArrayList (); 631 ArrayList syncSourcesFailed = new ArrayList (); 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 t = e.getCause(); 644 645 String 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 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 704 public void addSource(String moduleId, String connectorId, String sourceTypeId, SyncSource source) 705 throws ServerException, AdminException { 706 String uri = source.getSourceURI(); 707 String sourceName = source.getName(); 708 709 String 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 Sync4jSource existSource[] = null; 729 try { 730 731 WhereClause[] wc = new WhereClause[2]; 732 String value[] = new String [1]; 733 value[0] = uri; 734 wc[0] = new WhereClause("uri",value, WhereClause.OPT_EQ, false); 735 736 value = new String [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 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 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 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 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 encoder = null; 789 encoder = new XMLEncoder (new FileOutputStream(path+File.separator+sourceName+".xml")); 790 encoder.writeObject((Object )source); 791 encoder.flush(); 792 encoder.close(); 793 794 } catch(FileNotFoundException e) { 795 String 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 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 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 852 public void setSource(String moduleId, String connectorId, String sourceTypeId, SyncSource source) 853 throws ServerException, AdminException{ 854 String uri = source.getSourceURI(); 855 String sourceName = source.getName(); 856 857 String 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 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 nameFileXml = s4j.getConfig().substring(configFile.length()); 885 886 try { 887 888 ps.store(s4j); 889 890 } catch (PersistentStoreException e) { 891 String 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 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 encoder = null; 912 encoder = new XMLEncoder (new FileOutputStream(path+File.separator+nameFileXml)); 913 encoder.writeObject((Object )source); 914 encoder.flush(); 915 encoder.close(); 916 917 } catch(FileNotFoundException e) { 918 String 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 937 public void deleteSource(String 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 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 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 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 988 public LoggingConfiguration getLoggingConfiguration() 989 throws ServerException, AdminException { 990 return config.getLoggingConfiguration(); 991 } 992 993 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 e) { 1010 throw new ServerException(e.getMessage(), e); 1011 } 1012 1013 } 1014 1015 1023 public ServerConfiguration getServerConfiguration() 1024 throws ServerException, AdminException { 1025 return config.getServerConfig(); 1026 } 1027 1028 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 e) { 1043 throw new ServerException(e.getMessage(), e); 1044 } 1045 } 1046 1047 1048 1054 public String getServerVersion() { 1055 return config.getServerConfig().getServerInfo().getSwV(); 1056 } 1057 1058 1066 public Object getServerBean(String 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 e) { 1073 throw new ServerException(e); 1074 } 1075 } 1076 1077 1085 public void setServerBean(String bean, Object obj) 1086 throws ServerException, AdminException { 1087 try { 1088 Configuration.getConfiguration().setBeanInstance(bean, obj); 1089 } catch (Exception e) { 1090 throw new ServerException(e); 1091 } 1092 } 1093 1094 } 1096 | Popular Tags |