001    /*
002    
003      $Id: SharpsterDaemon.java,v 1.15 2003/05/08 18:40:54 ndulgheru Exp $
004    
005    */
006    
007    package sharpster.daemon;
008    
009    import net.jxta.id.*;
010    import net.jxta.document.*;
011    import java.io.FileInputStream;
012    import java.io.FileOutputStream;
013    import java.io.StringWriter;
014    import java.util.Enumeration;
015    import java.util.LinkedList;
016    import java.util.ListIterator;
017    
018    import sharpster.daemon.externalcommunication.ExternalCommunication;
019    import sharpster.daemon.usermanagement.UserManager;
020    import sharpster.daemon.sharemanagement.ShareManager;
021    import sharpster.daemon.filemanagement.FileManager;
022    import sharpster.daemon.filemanagement.LocalFileManager;
023    import sharpster.daemon.filemanagement.CVSManager;
024    import sharpster.daemon.daemonplugin.DaemonPluginManager;
025    import sharpster.daemon.commandmanagement.ExternalCommandManager;
026    import sharpster.daemon.commandmanagement.InternalCommandManager;
027    import sharpster.daemon.clientcommunication.ClientCommunication;
028    import sharpster.daemon.groupmanagement.GroupController;
029    import sharpster.common.*;
030    
031    public class SharpsterDaemon {
032        ExternalCommunication externalCommunication;
033        FileManager fileManager;
034        ShareManager shareManager;
035        UserManager userManager;
036        ExternalCommandManager externalCommandManager;
037        InternalCommandManager internalCommandManager;
038        DaemonPluginManager pluginManager;
039        ClientCommunication clientCommunication;
040        Mutex globalMutex;
041        GroupController groupController;
042        
043        static String peerName = new String("N/A");
044        static String jxtaUsername = new String();
045        static String jxtaPassword = new String();
046        static String cvsRoot = new String();
047        static String cvsTemp = new String();
048    
049        public SharpsterDaemon() {
050        }
051    
052        public static void main(String[] args) {
053            SharpsterDaemon daemon = new SharpsterDaemon();
054            System.out.println("Starting Sharpster daemon...");
055            daemon.initialize();
056        }
057    
058        public static String getPeerName() {
059            return new String(peerName);
060        }
061    
062        public static void setPeerName(String name) {
063            peerName = new String(name);
064        }
065    
066        public static String getCVSRoot() {
067            return cvsRoot;
068        }
069    
070        public static String getCVSTemp() {
071            return cvsTemp;
072        }
073    
074        private void readConfigFile(String file) {
075            try {
076                FileInputStream input = new FileInputStream(file);
077                StructuredDocument document = (StructuredTextDocument) 
078                    StructuredDocumentFactory.newStructuredDocument(new MimeMediaType("text/xml"), input);
079                
080                Enumeration parameter = document.getChildren();
081    
082                while(parameter.hasMoreElements()) {
083                    Element element = (Element)parameter.nextElement();
084                    String key = (String)element.getKey();
085                    if(key.equals("cvs-repository")) {
086                        cvsRoot = (String)element.getValue();
087                    }
088                    else if(key.equals("cvs-temporary")) {
089                        cvsTemp = (String)element.getValue();
090                    }
091                    else if(key.equals("jxta-username")) {
092                        jxtaUsername = (String)element.getValue();
093                        System.setProperty("net.jxta.tls.principal", jxtaUsername);
094                    }
095                    else if(key.equals("jxta-password")) {
096                        jxtaPassword = (String)element.getValue();
097                        System.setProperty("net.jxta.tls.password", jxtaPassword);
098                    }
099                    else {
100                        System.out.println("Warning: unknown key found in configuration file ("+key+").");
101                    }
102                }
103                input.close();
104            }
105            catch(Exception e) {
106                System.out.println("Error: could'nt read the Sharpster config.");
107                System.exit(1);
108            }
109            
110            try {
111                java.io.File iofile = new java.io.File(cvsRoot);
112                if(!iofile.isDirectory() || !iofile.isAbsolute()) {
113                    System.out.println("Error: The CVS root path is not valid, please check your config/sharpster.cfg.");
114                    System.out.println("       Note that the CVS root path has to be absolute, i.e not relative.");
115                    System.exit(1);         
116                }
117    
118                iofile = new java.io.File(cvsTemp);
119                if(!iofile.isDirectory() || !iofile.isAbsolute()) {
120                    System.out.println("Error: The CVS temporary path is not valid, please check your config/sharpster.cfg.");
121                    System.exit(1);         
122                }
123            }
124            catch(Exception e) {
125            }
126        }
127    
128        private void initialize() {
129            readConfigFile("config/sharpster.cfg");
130    
131            //Create all objects
132            globalMutex = new Mutex();
133            externalCommunication = new ExternalCommunication(globalMutex);
134            clientCommunication = new ClientCommunication(globalMutex);
135            fileManager = new FileManager();
136            userManager = new UserManager();
137            shareManager = new ShareManager();
138            externalCommandManager = new ExternalCommandManager();
139            internalCommandManager = new InternalCommandManager();
140            pluginManager = new DaemonPluginManager();
141            groupController = new GroupController();
142    
143            //Initialize all objects
144            externalCommunication.initialize(userManager, 
145                                             externalCommandManager);
146            
147            clientCommunication.initialize(internalCommandManager);
148            
149            fileManager.initialize(pluginManager);
150            userManager.initialize();
151            shareManager.initialize(fileManager, userManager, groupController);
152            
153            externalCommandManager.initialize(fileManager, shareManager, 
154                                              userManager, externalCommunication,
155                                              globalMutex);
156            
157            internalCommandManager.initialize(fileManager, shareManager, 
158                                              userManager, externalCommunication,
159                                              globalMutex, groupController);
160            
161            groupController.initialize(userManager);
162            
163    
164            if(!pluginManager.loadPlugins("plugins")) {
165                System.out.println("Warning: error loading plugins.");      
166            }
167            else {
168                String[] names = pluginManager.getPluginNames();
169                for(int i=0;i<names.length;i++) {
170                    System.out.println("SD::Loaded plugin: "+names[i]);
171                }
172            }
173            
174            if(!userManager.loadFromFile("config/users.cfg")) {
175                System.out.println("Warning: error reading users config.");
176            }
177            if(!fileManager.loadFromFile("config/files.cfg")) {
178                System.out.println("Warning: error reading files config.");
179            }
180            if(!shareManager.loadFromFile("config/shares.cfg")) {
181                System.out.println("Warning: error reading shares config.");
182            }
183    
184            userManager.saveToFile(null);
185            fileManager.saveToFile(null);
186            shareManager.saveToFile(null);
187    
188            //Start the threads
189            clientCommunication.start();
190            externalCommunication.start();
191            
192            /*FileCollection files = new FileCollection();
193            SharedFile file = new SharedFile();
194            file.setFileName("test/test2/*.txt");
195            files.addFile(file);
196            ResponseCollection col = externalCommandManager.remoteCheckoutFiles(files,"grendel");
197            System.out.println(col.toString());*/
198        }
199    }
200    
201