Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Hive/3.3/HiveServiceLocator.cs @ 8506

Last change on this file since 8506 was 8506, checked in by fschoepp, 12 years ago

#1888:

  • Web project now creates custom html for each type we want to enter / display
  • Added endpointConfigurationName to HiveServiceLocator (because Web Project contains more than one endpoint configuration)
  • Removed logging statement from ConfigurationService to prevent exception during failure of loading ConfigurationSettings
  • ApplicationManager: Changed default implementation to WebApplicationManager (instead of LightWeight) for testing purposes within Web Project
  • WebApplicationManager: The application manager which returns plugin descriptors from the currently loaded assemblies (instead of LightweightAppManager)
  • HiveService: Fixed a transaction bug
  • IControllerService: Created a method to dispatch Scenarios to certain IScenarioManager (in this case HiveScenarioManager)
  • Added more mappable types to ControllerModel
  • PlaceholderControllerService dispatches all Scenarios to the HiveScenarioManager
  • Web project now dispatches the scenario to the controller after pressing "Run Job"
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Security.Cryptography.X509Certificates;
24using System.ServiceModel;
25using HeuristicLab.Clients.Common;
26
27namespace HeuristicLab.Clients.Hive {
28  public class HiveServiceLocator : IHiveServiceLocator {
29    private static IHiveServiceLocator instance = null;
30    public static IHiveServiceLocator Instance {
31      get {
32        if (instance == null) {
33          instance = new HiveServiceLocator();
34        }
35        return instance;
36      }
37    }
38
39    private string username;
40    public string Username {
41      get { return username; }
42      set { username = value; }
43    }
44
45    private string password;
46    public string Password {
47      get { return password; }
48      set { password = value; }
49    }
50
51    private string endpointConfigurationName;
52
53    public string EndpointConfigurationName
54    {
55      get { return endpointConfigurationName; }
56      set { endpointConfigurationName = value; }
57    }
58
59
60    private string remoteAddress;
61    public string RemoteAddress {
62      get { return remoteAddress; }
63      set { remoteAddress = value; }
64    }
65
66    private string identityCertificate;
67    public string IdentityCertificate {
68      get { return identityCertificate; }
69      set { identityCertificate = value; }
70    }   
71
72    private HiveServiceClient NewServiceClient() {
73      HiveServiceClient cl;
74      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
75        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>();
76      else {
77        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
78      }
79
80      if (!string.IsNullOrEmpty(remoteAddress) && !string.IsNullOrEmpty(identityCertificate)) {
81
82        X509Certificate2Collection supportingCertificates = new X509Certificate2Collection();
83        supportingCertificates.Import(Convert.FromBase64String(identityCertificate));
84
85        X509Certificate2 primaryCertificate = supportingCertificates[0];
86        supportingCertificates.RemoveAt(0);
87        EndpointIdentity ei = EndpointIdentity.CreateX509CertificateIdentity(primaryCertificate, supportingCertificates);
88
89        cl.Endpoint.Address = new EndpointAddress(new Uri(remoteAddress), ei);
90      }
91
92      return cl;
93    }
94
95    public T CallHiveService<T>(Func<IHiveService, T> call) {
96      HiveServiceClient client = NewServiceClient();
97      HandleAnonymousUser(client);
98
99      try {
100        return call(client);
101      }
102      finally {
103        try {
104          client.Close();
105        }
106        catch (Exception) {
107          client.Abort();
108        }
109      }
110    }
111
112    public void CallHiveService(Action<IHiveService> call) {
113      HiveServiceClient client = NewServiceClient();
114      HandleAnonymousUser(client);
115
116      try {
117        call(client);
118      }
119      finally {
120        try {
121          client.Close();
122        }
123        catch (Exception) {
124          client.Abort();
125        }
126      }
127    }
128
129    private void HandleAnonymousUser(HiveServiceClient client) {
130      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
131        try {
132          client.Close();
133        }
134        catch (Exception) {
135          client.Abort();
136        }
137        throw new AnonymousUserException();
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.