KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > PluginManager


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.kernel;
47
48
49 import java.io.IOException JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Map JavaDoc;
53
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56 import org.mr.MantaAgent;
57 import org.mr.core.configuration.ConfigurationElement;
58
59
60 /**
61  * Loads all the plugins from the configuration file.
62  * Property "PluginList" is a CSV property that hold all the plugins to be loaded.
63  *
64  * @author Amir Shevat
65  *
66  */

67 public class PluginManager {
68
69     Log log = null;
70     MantaAgent agent = null;
71     private Map JavaDoc pluginsMap = null;
72
73     public PluginManager() {
74         agent = MantaAgent.getInstance();
75         pluginsMap = new HashMap JavaDoc();
76         log=LogFactory.getLog("PluginManager");
77         ConfigurationElement plugin = agent.getSingletonRepository().getConfigManager().getConfigurationElement("plug-ins");
78         
79         if(plugin == null ||plugin.getSubConfigurationElements() == null|| plugin.getSubConfigurationElements().size() == 0 ){
80             if(log.isInfoEnabled()) {
81                 log.info("PluginManager has no plugins to init.");
82             }//if
83
return;
84         }
85        ArrayList JavaDoc pluginList =plugin.getSubConfigurationElements();
86         
87
88         if(log.isInfoEnabled()) {
89             log.info("PluginManager is starting to load " + pluginList.size() + " plugins");
90         }//if
91

92         try {
93            initPlugins(pluginList);
94         }//try
95
catch (Exception JavaDoc e) {
96             if(log.isErrorEnabled()){
97                 log.error("IOException in PluginManager." + e);
98             }//if
99
}//catch
100
}//PluginManager
101

102
103     
104
105
106     private void initPlugins(ArrayList JavaDoc pluginList) throws IOException JavaDoc {
107
108         String JavaDoc classToLoad;
109         Object JavaDoc obj;
110         for (int i = 0; i < pluginList.size(); i++) {
111             ConfigurationElement pluginConf=(ConfigurationElement) pluginList.get(i);
112             if(pluginConf.getSubConfigurationElementByName("loader_class") == null){
113                 log.info("Configuration error missing loader_class in plugins");
114                 continue;
115             }
116             classToLoad = pluginConf.getSubConfigurationElementByName("loader_class").getValue();
117             classToLoad = classToLoad.trim();
118             try {
119                 Class JavaDoc c = Class.forName(classToLoad);
120                 Plugin p = (Plugin) c.newInstance();
121         
122                 //put the plugin into the map, so we'll be able to access it later.
123
pluginsMap.put(p.getName(),p);
124                 
125                 p.start();
126                 log.info("Loaded plugin " + classToLoad);
127             }//try
128
catch (Throwable JavaDoc e) {
129                 if(log.isErrorEnabled()){
130                     log.error("Plugin Manager was not able to load the " + classToLoad + " plugin.", e);
131                 }
132             }//catch
133
}//for
134
}//initPlugins
135
}//PluginManager
136
Popular Tags