Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8251 was 8251, checked in by spimming, 12 years ago

#1888:

  • Change hive server from service configuration file
  • Created service configuration settings for server address and certificate
  • Added HeuristicLab.Clients.Hive-3.3 project to solution
File size: 3.9 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 remoteAddress;
52    public string RemoteAddress {
53      get { return remoteAddress; }
54      set { remoteAddress = value; }
55    }
56
57    private string identityCertificate;
58    public string IdentityCertificate {
59      get { return identityCertificate; }
60      set { identityCertificate = value; }
61    }
62
63    private HiveServiceClient NewServiceClient() {
64      HiveServiceClient cl;
65      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
66        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>();
67      else {
68        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(null, null, username, password);
69      }
70
71      if (!string.IsNullOrEmpty(remoteAddress) && !string.IsNullOrEmpty(identityCertificate)) {
72
73        X509Certificate2Collection supportingCertificates = new X509Certificate2Collection();
74        supportingCertificates.Import(Convert.FromBase64String(identityCertificate));
75
76        X509Certificate2 primaryCertificate = supportingCertificates[0];
77        supportingCertificates.RemoveAt(0);
78        EndpointIdentity ei = EndpointIdentity.CreateX509CertificateIdentity(primaryCertificate, supportingCertificates);
79
80        cl.Endpoint.Address = new EndpointAddress(new Uri(remoteAddress), ei);
81      }
82
83      return cl;
84    }
85
86    public T CallHiveService<T>(Func<IHiveService, T> call) {
87      HiveServiceClient client = NewServiceClient();
88      HandleAnonymousUser(client);
89
90      try {
91        return call(client);
92      }
93      finally {
94        try {
95          client.Close();
96        }
97        catch (Exception) {
98          client.Abort();
99        }
100      }
101    }
102
103    public void CallHiveService(Action<IHiveService> call) {
104      HiveServiceClient client = NewServiceClient();
105      HandleAnonymousUser(client);
106
107      try {
108        call(client);
109      }
110      finally {
111        try {
112          client.Close();
113        }
114        catch (Exception) {
115          client.Abort();
116        }
117      }
118    }
119
120    private void HandleAnonymousUser(HiveServiceClient client) {
121      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
122        try {
123          client.Close();
124        }
125        catch (Exception) {
126          client.Abort();
127        }
128        throw new AnonymousUserException();
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.