aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ApplicationPlugins
diff options
context:
space:
mode:
authorTedd Hansen2008-11-09 10:30:46 +0000
committerTedd Hansen2008-11-09 10:30:46 +0000
commit853ba745b517f8dfb5c5b955637454e4c7d25507 (patch)
treea11ac849bcf4f80c91e87c4fb72a32f7d591e486 /OpenSim/ApplicationPlugins
parentUpdate svn properties. Add copyright headers. Minor formatting cleanup. (diff)
downloadopensim-SC-853ba745b517f8dfb5c5b955637454e4c7d25507.zip
opensim-SC-853ba745b517f8dfb5c5b955637454e4c7d25507.tar.gz
opensim-SC-853ba745b517f8dfb5c5b955637454e4c7d25507.tar.bz2
opensim-SC-853ba745b517f8dfb5c5b955637454e4c7d25507.tar.xz
Refactoring: Moved component creation to "ComponentFactory" as dictated by convention
Diffstat (limited to 'OpenSim/ApplicationPlugins')
-rw-r--r--OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs144
-rw-r--r--OpenSim/ApplicationPlugins/ScriptEngine/RegionEngineLoader.cs6
-rw-r--r--OpenSim/ApplicationPlugins/ScriptEngine/ScriptEnginePlugin.cs99
3 files changed, 148 insertions, 101 deletions
diff --git a/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs b/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs
new file mode 100644
index 0000000..48cd1f9
--- /dev/null
+++ b/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs
@@ -0,0 +1,144 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.IO;
30using System.Reflection;
31using log4net;
32using OpenSim.ScriptEngine.Shared;
33
34namespace OpenSim.ApplicationPlugins.ScriptEngine
35{
36 public static class ComponentFactory
37 {
38 // Component providers are registered here wit a name (string)
39 // When a script engine is created the components are instanciated
40 public static Dictionary<string, Type> providers = new Dictionary<string, Type>();
41 public static Dictionary<string, Type> scriptEngines = new Dictionary<string, Type>();
42
43 internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 private readonly static string nameIScriptEngineComponent = typeof(IScriptEngineComponent).Name; // keep interface name in managed code
45 private readonly static string nameIScriptEngine = typeof(IScriptEngine).Name; // keep interface name in managed code
46
47 public static string Name
48 {
49 get { return "SECS.ComponentFactory"; }
50 }
51
52 /// <summary>
53 /// Load components from directory
54 /// </summary>
55 /// <param name="directory"></param>
56 internal static void Load(string directory, string filter)
57 {
58 // We may want to change how this functions as currently it required unique class names for each component
59
60 foreach (string file in Directory.GetFiles(directory, filter))
61 {
62 //m_log.DebugFormat("[ScriptEngine]: Loading: [{0}].", file);
63 Assembly componentAssembly = null;
64 try
65 {
66 componentAssembly = Assembly.LoadFrom(file);
67 }
68 catch (Exception e)
69 {
70 m_log.ErrorFormat("[{0}] Error loading: \"{1}\".", Name, file);
71 }
72 if (componentAssembly != null)
73 {
74 try
75 {
76 // Go through all types in the assembly
77 foreach (Type componentType in componentAssembly.GetTypes())
78 {
79 if (componentType.IsPublic
80 && !componentType.IsAbstract)
81 {
82 //if (componentType.IsSubclassOf(typeof(ComponentBase)))
83 if (componentType.GetInterface(nameIScriptEngineComponent) != null)
84 {
85 // We have found an type which is derived from ProdiverBase, add it to provider list
86 m_log.InfoFormat("[{0}] Adding component: {1}", Name, componentType.Name);
87 lock (providers)
88 {
89 providers.Add(componentType.Name, componentType);
90 }
91 }
92 //if (componentType.IsSubclassOf(typeof(ScriptEngineBase)))
93 if (componentType.GetInterface(nameIScriptEngine) != null)
94 {
95 // We have found an type which is derived from RegionScriptEngineBase, add it to engine list
96 m_log.InfoFormat("[{0}] Adding script engine: {1}", Name, componentType.Name);
97 lock (scriptEngines)
98 {
99 scriptEngines.Add(componentType.Name, componentType);
100 }
101 }
102 }
103 }
104 }
105 catch
106 (ReflectionTypeLoadException re)
107 {
108 m_log.ErrorFormat("[{0}] Could not load component \"{1}\": {2}", Name, componentAssembly.FullName, re.ToString());
109 int c = 0;
110 foreach (Exception e in re.LoaderExceptions)
111 {
112 c++;
113 m_log.ErrorFormat("[{0}] LoaderException {1}: {2}", Name, c, e.ToString());
114 }
115 }
116 } //if
117 } //foreach
118 }
119
120 public static IScriptEngineComponent GetComponentInstance(string name, params Object[] args)
121 {
122 lock (providers)
123 {
124 if (!providers.ContainsKey(name))
125 throw new Exception("ScriptEngine requested component named \"" + name +
126 "\" that does not exist.");
127 return Activator.CreateInstance(providers[name], args) as IScriptEngineComponent;
128 }
129 }
130
131 private readonly static string nameIScriptEngineRegionComponent = typeof(IScriptEngineRegionComponent).Name; // keep interface name in managed code
132 public static IScriptEngineComponent GetComponentInstance(RegionInfoStructure info, string name, params Object[] args)
133 {
134 IScriptEngineComponent c = GetComponentInstance(name, args);
135
136 // If module is IScriptEngineRegionComponent then it will have one instance per region and we will initialize it
137 if (c.GetType().GetInterface(nameIScriptEngineRegionComponent) != null)
138 ((IScriptEngineRegionComponent)c).Initialize(info);
139
140 return c;
141 }
142
143 }
144}
diff --git a/OpenSim/ApplicationPlugins/ScriptEngine/RegionEngineLoader.cs b/OpenSim/ApplicationPlugins/ScriptEngine/RegionEngineLoader.cs
index 1023238..998f017 100644
--- a/OpenSim/ApplicationPlugins/ScriptEngine/RegionEngineLoader.cs
+++ b/OpenSim/ApplicationPlugins/ScriptEngine/RegionEngineLoader.cs
@@ -97,16 +97,16 @@ namespace OpenSim.ApplicationPlugins.ScriptEngine
97 m_log.DebugFormat("[{0}] Loading region script engine engine \"{1}\".", Name, tempScriptEngineName); 97 m_log.DebugFormat("[{0}] Loading region script engine engine \"{1}\".", Name, tempScriptEngineName);
98 try 98 try
99 { 99 {
100 lock (ScriptEnginePlugin.scriptEngines) 100 lock (ComponentFactory.scriptEngines)
101 { 101 {
102 if (!ScriptEnginePlugin.scriptEngines.ContainsKey(tempScriptEngineName)) 102 if (!ComponentFactory.scriptEngines.ContainsKey(tempScriptEngineName))
103 { 103 {
104 m_log.ErrorFormat("[{0}] Unable to load region script engine: Script engine \"{1}\" does not exist.", Name, tempScriptEngineName); 104 m_log.ErrorFormat("[{0}] Unable to load region script engine: Script engine \"{1}\" does not exist.", Name, tempScriptEngineName);
105 } 105 }
106 else 106 else
107 { 107 {
108 scriptEngine = 108 scriptEngine =
109 Activator.CreateInstance(ScriptEnginePlugin.scriptEngines[tempScriptEngineName]) as 109 Activator.CreateInstance(ComponentFactory.scriptEngines[tempScriptEngineName]) as
110 IScriptEngine; 110 IScriptEngine;
111 } 111 }
112 } 112 }
diff --git a/OpenSim/ApplicationPlugins/ScriptEngine/ScriptEnginePlugin.cs b/OpenSim/ApplicationPlugins/ScriptEngine/ScriptEnginePlugin.cs
index f81b848..ae4e2f9 100644
--- a/OpenSim/ApplicationPlugins/ScriptEngine/ScriptEnginePlugin.cs
+++ b/OpenSim/ApplicationPlugins/ScriptEngine/ScriptEnginePlugin.cs
@@ -26,11 +26,7 @@
26 */ 26 */
27using System; 27using System;
28using System.Collections.Generic; 28using System.Collections.Generic;
29using System.IO;
30using System.Reflection; 29using System.Reflection;
31using System.Text;
32using System.Threading;
33using OpenSim.ScriptEngine.Shared;
34using log4net; 30using log4net;
35 31
36namespace OpenSim.ApplicationPlugins.ScriptEngine 32namespace OpenSim.ApplicationPlugins.ScriptEngine
@@ -43,10 +39,6 @@ namespace OpenSim.ApplicationPlugins.ScriptEngine
43 internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 39 internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 internal OpenSimBase m_OpenSim; 40 internal OpenSimBase m_OpenSim;
45 41
46 // Component providers are registered here wit a name (string)
47 // When a script engine is created the components are instanciated
48 public static Dictionary<string, Type> providers = new Dictionary<string, Type>();
49 public static Dictionary<string, Type> scriptEngines = new Dictionary<string, Type>();
50 42
51 43
52 public ScriptEnginePlugin() 44 public ScriptEnginePlugin()
@@ -62,7 +54,7 @@ namespace OpenSim.ApplicationPlugins.ScriptEngine
62 54
63 // Load all modules from current directory 55 // Load all modules from current directory
64 // We only want files named OpenSim.ScriptEngine.*.dll 56 // We only want files named OpenSim.ScriptEngine.*.dll
65 Load(".", "OpenSim.ScriptEngine.*.dll"); 57 ComponentFactory.Load(".", "OpenSim.ScriptEngine.*.dll");
66 } 58 }
67 59
68 public void Initialise(OpenSimBase openSim) 60 public void Initialise(OpenSimBase openSim)
@@ -73,95 +65,6 @@ namespace OpenSim.ApplicationPlugins.ScriptEngine
73 //m_OpenSim.Shutdown(); 65 //m_OpenSim.Shutdown();
74 } 66 }
75 67
76 private readonly static string nameIScriptEngineComponent = typeof(IScriptEngineComponent).Name; // keep interface name in managed code
77 private readonly static string nameIScriptEngine = typeof(IScriptEngine).Name; // keep interface name in managed code
78 /// <summary>
79 /// Load components from directory
80 /// </summary>
81 /// <param name="directory"></param>
82 public void Load(string directory, string filter)
83 {
84 // We may want to change how this functions as currently it required unique class names for each component
85
86 foreach (string file in Directory.GetFiles(directory, filter))
87 {
88 //m_log.DebugFormat("[ScriptEngine]: Loading: [{0}].", file);
89 Assembly componentAssembly = null;
90 try
91 {
92 componentAssembly = Assembly.LoadFrom(file);
93 } catch (Exception e)
94 {
95 m_log.ErrorFormat("[{0}] Error loading: \"{1}\".", Name, file);
96 }
97 if (componentAssembly != null)
98 {
99 try
100 {
101 // Go through all types in the assembly
102 foreach (Type componentType in componentAssembly.GetTypes())
103 {
104 if (componentType.IsPublic
105 && !componentType.IsAbstract)
106 {
107 //if (componentType.IsSubclassOf(typeof(ComponentBase)))
108 if (componentType.GetInterface(nameIScriptEngineComponent) != null)
109 {
110 // We have found an type which is derived from ProdiverBase, add it to provider list
111 m_log.InfoFormat("[{0}] Adding component: {1}", Name, componentType.Name);
112 lock (providers)
113 {
114 providers.Add(componentType.Name, componentType);
115 }
116 }
117 //if (componentType.IsSubclassOf(typeof(ScriptEngineBase)))
118 if (componentType.GetInterface(nameIScriptEngine) != null)
119 {
120 // We have found an type which is derived from RegionScriptEngineBase, add it to engine list
121 m_log.InfoFormat("[{0}] Adding script engine: {1}", Name, componentType.Name);
122 lock (scriptEngines)
123 {
124 scriptEngines.Add(componentType.Name, componentType);
125 }
126 }
127 }
128 }
129 }
130 catch
131 (ReflectionTypeLoadException re)
132 {
133 m_log.ErrorFormat("[{0}] Could not load component \"{1}\": {2}", Name, componentAssembly.FullName, re.ToString());
134 int c = 0;
135 foreach (Exception e in re.LoaderExceptions)
136 {
137 c++;
138 m_log.ErrorFormat("[{0}] LoaderException {1}: {2}", Name, c, e.ToString());
139 }
140 }
141 } //if
142 } //foreach
143 }
144
145 public static IScriptEngineComponent GetComponentInstance(string name, params Object[] args)
146 {
147 if (!providers.ContainsKey(name))
148 throw new Exception("ScriptEngine requested component named \"" + name +
149 "\" that does not exist.");
150
151 return Activator.CreateInstance(providers[name], args) as IScriptEngineComponent;
152 }
153
154 private readonly static string nameIScriptEngineRegionComponent = typeof(IScriptEngineRegionComponent).Name; // keep interface name in managed code
155 public static IScriptEngineComponent GetComponentInstance(RegionInfoStructure info, string name, params Object[] args)
156 {
157 IScriptEngineComponent c = GetComponentInstance(name, args);
158
159 // If module is IScriptEngineRegionComponent then it will have one instance per region and we will initialize it
160 if (c.GetType().GetInterface(nameIScriptEngineRegionComponent) != null)
161 ((IScriptEngineRegionComponent)c).Initialize(info);
162
163 return c;
164 }
165 68
166 #region IApplicationPlugin stuff 69 #region IApplicationPlugin stuff
167 /// <summary> 70 /// <summary>