OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ServiceBase.cs
Go to the documentation of this file.
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 OpenSimulator 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  */
27 
28 using System;
29 using System.IO;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using log4net;
33 using Nini.Config;
34 using OpenSim.Services.Interfaces;
35 
36 namespace OpenSim.Services.Base
37 {
38  public class ServiceBase
39  {
40  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
41 
42  public T LoadPlugin<T>(string dllName) where T:class
43  {
44  return LoadPlugin<T>(dllName, new Object[0]);
45  }
46 
47  public T LoadPlugin<T>(string dllName, Object[] args) where T:class
48  {
49  // The path:type separator : is unfortunate because it collides
50  // with Windows paths like C:\...
51  // When the path provided includes the drive, this fails.
52  // Hence the root/noroot thing going on here.
53  string pathRoot = Path.GetPathRoot(dllName);
54  string noRoot = dllName.Substring(pathRoot.Length);
55  string[] parts = noRoot.Split(new char[] {':'});
56 
57 
58  dllName = pathRoot + parts[0];
59 
60  string className = String.Empty;
61 
62  if (parts.Length > 1)
63  className = parts[1];
64 
65  return LoadPlugin<T>(dllName, className, args);
66  }
67 
68  public T LoadPlugin<T>(string dllName, string className, Object[] args) where T:class
69  {
70  string interfaceName = typeof(T).ToString();
71 
72  try
73  {
74  Assembly pluginAssembly = Assembly.LoadFrom(dllName);
75 
76 // m_log.DebugFormat("[SERVICE BASE]: Found assembly {0}", dllName);
77 
78  foreach (Type pluginType in pluginAssembly.GetTypes())
79  {
80 // m_log.DebugFormat("[SERVICE BASE]: Found type {0}", pluginType);
81 
82  if (pluginType.IsPublic)
83  {
84  if (className != String.Empty &&
85  pluginType.ToString() !=
86  pluginType.Namespace + "." + className)
87  continue;
88 
89  Type typeInterface =
90  pluginType.GetInterface(interfaceName);
91  if (typeInterface != null)
92  {
93  T plug = (T)Activator.CreateInstance(pluginType,
94  args);
95 
96  return plug;
97  }
98  }
99  }
100 
101  return null;
102  }
103  catch (Exception e)
104  {
105  List<string> strArgs = new List<string>();
106  foreach (Object arg in args)
107  strArgs.Add(arg.ToString());
108 
109  m_log.Error(
110  string.Format(
111  "[SERVICE BASE]: Failed to load plugin {0} from {1} with args {2}",
112  interfaceName, dllName, string.Join(", ", strArgs.ToArray())), e);
113 
114  return null;
115  }
116  }
117 
118  public ServiceBase(IConfigSource config)
119  {
120  }
121  }
122 }
ServiceBase(IConfigSource config)
Definition: ServiceBase.cs:118
Interactive OpenSim region server
Definition: OpenSim.cs:55