001    //Copyright (C) 2006  A. Nelson
002    //
003    //This program is free software; you can redistribute it and/or
004    //modify it under the terms of the GNU General Public License
005    //as published by the Free Software Foundation; either version 2
006    //of the License, or (at your option) any later version.
007    //
008    //This program is distributed in the hope that it will be useful,
009    //but WITHOUT ANY WARRANTY; without even the implied warranty of
010    //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    //GNU General Public License for more details.
012    //
013    //You should have received a copy of the GNU General Public License
014    //along with this program; if not, write to the Free Software
015    //Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
016    
017    package pluginCore;
018    
019    /**
020     * The core handler provides a controlled interface that is handed
021     * to the plug-ins to allow them communication with the main core.
022     * All calls that are made by plug-ins are run through the CoreHandeler.
023     **/
024    public class CoreHandler {
025    
026        /**
027         * The pointer to the active PluginCore.
028         **/
029        private PluginCore runningCore;
030    
031        /**
032         * The plug-in that this CoreHandeler is associated with.
033         **/
034        private Plugin myPlugin;
035    
036        /**
037         * Initializes the CoreHandeler
038         * @param coreIn A pointer to the running core.
039         **/
040        public CoreHandler( PluginCore coreIn, Plugin plugIn ) {
041            
042            runningCore = coreIn;
043            myPlugin    = plugIn;
044        }
045    
046        /**
047         *  Checks if a plugin has a unstable lock held on it.
048         * @param id The id of the plugin to ckeck.
049         * @return True if the plugin is locked, false if it is unlocked.
050         */
051        public boolean isLocked( int id ) {
052            
053            return runningCore.isLocked( id );
054        }
055        
056        
057        /**
058         * Gets the Id of a plug-in, this ID can then be used to send messages to that plug-in. 
059         * If the plug-in is not already loaded then the system will attempt to load it, then return the ID.
060         * @param plugin The name of the plugin you are looking for, the PluginCore checks the field Plug-in.pluginName.
061         * @return The Id of the plug-in, or a -1 on failure.
062         **/
063        public int pluginIDRequest( String plugin ){
064            
065            return runningCore.pluginIDRequest( plugin );
066        }
067    
068        
069        /**
070         * Loads a new plug-in into the system and starts its thread running.
071         * @param plugin The full name of the object that you wish to load into the system.
072         * @return The plugin's Id is returned if successful and -1 is returned on failure.
073         **/
074        public int loadPlugin(String plugin ){
075    
076            return runningCore.loadPlugin( plugin );
077        }
078    
079            /**
080             * Loads a new plug-in into the system and initalizes it with the data
081             * from a previous plugin, that is contained in the stateIn array.
082             * 
083             * @param pluginName
084             *            The full name of the object that you wish to load into the
085             *            system.
086             * @return The plugin's Id is returned if successful and -1 is returned on
087             *         failure.
088             */
089        public int loadPlugin( String plugin, Object[] state ) {
090            
091            return runningCore.loadPlugin( plugin, state );
092        }
093    
094        public boolean unloadPlugin( int plugId ) {
095    
096            return runningCore.unloadPlugin( plugId );
097        }
098    
099    
100        
101        /**
102         * Stops the system running, first calls the 'shutdown()' call for all loaded plug-ins
103         * then calls a join() on them before finally 'exiting itself. 
104         **/
105        public void shutdown() {
106    
107                    /**
108                     * I could set permissions here to make it so that only 
109                     * some plug-ins can shutdown everything.
110                     **/
111            runningCore.shutdown();
112        }
113    
114        /**
115         * Lists the plug-ins that are currently loaded in the system.
116         * @return A list of loaded plug-ins with newlines separating them.
117         **/
118        public String[] listLoadedPlugins() {
119    
120            return runningCore.listLoadedPlugins();
121        }
122    
123            public Object[] savePlugin(int pluginId) {
124    
125                    return runningCore.savePlugin( pluginId );
126            }
127            
128        /**
129         * Requests that the core send the message 'messIn' 
130         * to the plug-in with the same Id as the destID field in the Message object.
131         * @param messIn The message to be sent.
132         * @return Returns 1 on success and 0 on failure.
133         **/
134        public int postMessage( Message messIn ){
135            
136                    if( messIn.sourceID == myPlugin.getID() ) {
137                        
138                        return runningCore.postMessage( messIn );
139                    }
140                    if( myPlugin.debugStatus() == true) { 
141                        
142                        System.out.println( "Plugin attempting to send Message with out correct source ID");
143                    }
144                    return 0;
145        }
146    }