~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/system/plugins/PluginManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.herac.tuxguitar.gui.system.plugins;
2
 
 
3
 
import java.io.File;
4
 
import java.io.FileInputStream;
5
 
import java.io.FileNotFoundException;
6
 
import java.io.FilenameFilter;
7
 
import java.io.IOException;
8
 
import java.net.URL;
9
 
import java.net.URLClassLoader;
10
 
import java.util.ArrayList;
11
 
import java.util.Iterator;
12
 
import java.util.List;
13
 
import java.util.Properties;
14
 
 
15
 
import org.herac.tuxguitar.gui.TuxGuitar;
16
 
import org.herac.tuxguitar.gui.util.TuxGuitarFileUtils;
17
 
 
18
 
public class PluginManager {
19
 
 
20
 
        public static final String PROPERTY_PLUGIN_PATH = "plugin.path";
21
 
        
22
 
        public static final String PROPERTY_PLUGIN_CLASS = "plugin.class";
23
 
        
24
 
        private static final String LANGUAGE_FOLDER = "lang";
25
 
 
26
 
        private List plugins;
27
 
 
28
 
        private List pluginItems;
29
 
        
30
 
        public PluginManager(){
31
 
                this.plugins = new ArrayList();
32
 
                this.pluginItems = new ArrayList();             
33
 
        }
34
 
    
35
 
        public List getPlugins(){
36
 
                return this.plugins;
37
 
        }
38
 
        
39
 
        public List getPluginItems(){
40
 
                return this.pluginItems;
41
 
        }
42
 
        
43
 
    /**
44
 
     * Load language files from lang folder
45
 
     *
46
 
     */
47
 
    public void loadPlugins(){          
48
 
        // we need plugin_*.properties files only
49
 
        final FilenameFilter filter = new FilenameFilter() {
50
 
                public boolean accept(File dir, String name) {
51
 
                        return (name.indexOf("plugin_") != -1 && name.indexOf(".properties") != -1);
52
 
                }
53
 
        };          
54
 
        // get the files
55
 
        String prefix = TuxGuitarFileUtils.PLUGINS_PREFIX;
56
 
        if(prefix != null){             
57
 
                File path = new File(prefix);
58
 
                if(path != null && path.exists()){
59
 
                        String[] files = path.list(filter);        
60
 
                        for(int i = 0;i < files.length;i++){                                            
61
 
                                try {
62
 
                                        loadPLugin(prefix + File.separator + files[i]);
63
 
                                } catch (Exception e) {
64
 
                                        e.printStackTrace();
65
 
                                } catch (Error e) {
66
 
                                        e.printStackTrace();
67
 
                                }                                               
68
 
                        }
69
 
                }
70
 
        }                      
71
 
    }    
72
 
    
73
 
    private void loadPLugin(String pluginProperties) throws FileNotFoundException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException{         
74
 
        try{
75
 
                Properties properties = new Properties();               
76
 
                properties.load(new FileInputStream(pluginProperties));
77
 
                        
78
 
                String pluginPath = properties.getProperty(PROPERTY_PLUGIN_PATH,new String());
79
 
                String pluginClass = properties.getProperty(PROPERTY_PLUGIN_CLASS,new String());
80
 
                        
81
 
                File file = new File(pluginPath);
82
 
                
83
 
                if(file != null && file.exists()){              
84
 
                        ClassLoader loader = new URLClassLoader(new URL[]{ new File(pluginPath).toURL() },getClass().getClassLoader()); 
85
 
                        if(loader != null){
86
 
                                Class loadedClass = loader.loadClass(pluginClass);
87
 
                                if(loadedClass != null){
88
 
                                        Object object = loadedClass.newInstance();
89
 
                                        if(object instanceof TGPlugin){         
90
 
                                                TGPlugin plugin = (TGPlugin)object;
91
 
                                                plugins.add(plugin);
92
 
                                                if(plugin.getItem().isEnabled()){
93
 
                                                        pluginItems.add(plugin);
94
 
                                                }
95
 
                                        }
96
 
                                }
97
 
                        }
98
 
                }
99
 
        }catch(Exception e){                                    
100
 
                TuxGuitar.instance().showErrorMessage(e);
101
 
        }catch(Error e){                
102
 
                TuxGuitar.instance().showErrorMessage(e);
103
 
        }
104
 
    }
105
 
        
106
 
    public void initPLugins(){          
107
 
        Iterator it = plugins.iterator();
108
 
        while(it.hasNext()){
109
 
                try{
110
 
                        TGPlugin plugin = (TGPlugin)it.next();
111
 
                        plugin.init(TuxGuitar.instance());
112
 
                }catch(Exception e){                    
113
 
                        TuxGuitar.instance().showErrorMessage(e);
114
 
                }catch(Error e){                
115
 
                        TuxGuitar.instance().showErrorMessage(e);
116
 
                }  
117
 
        }       
118
 
    }
119
 
    
120
 
    public void closePLugins(){         
121
 
        Iterator it = plugins.iterator();
122
 
        while(it.hasNext()){
123
 
                try{    
124
 
                        TGPlugin plugin = (TGPlugin)it.next();
125
 
                        plugin.close();
126
 
                }catch(Exception e){                    
127
 
                        TuxGuitar.instance().showErrorMessage(e);
128
 
                }catch(Error e){                
129
 
                        TuxGuitar.instance().showErrorMessage(e);
130
 
                }
131
 
        }               
132
 
    }
133
 
 
134
 
}