aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs
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/ScriptEngine/ComponentFactory.cs
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/ScriptEngine/ComponentFactory.cs')
-rw-r--r--OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs144
1 files changed, 144 insertions, 0 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}