[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Clients.Common;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Clients.Hive {
|
---|
[7132] | 26 | public class HiveServiceLocator : IHiveServiceLocator {
|
---|
| 27 | private static IHiveServiceLocator instance = null;
|
---|
| 28 | public static IHiveServiceLocator Instance {
|
---|
[6976] | 29 | get {
|
---|
| 30 | if (instance == null) {
|
---|
[7132] | 31 | instance = new HiveServiceLocator();
|
---|
[6976] | 32 | }
|
---|
| 33 | return instance;
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[9665] | 37 | private HiveServiceLocator() {
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[6976] | 40 | private string username;
|
---|
| 41 | public string Username {
|
---|
| 42 | get { return username; }
|
---|
| 43 | set { username = value; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | private string password;
|
---|
| 47 | public string Password {
|
---|
| 48 | get { return password; }
|
---|
| 49 | set { password = value; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[9665] | 52 | public int EndpointRetries { get; private set; }
|
---|
| 53 |
|
---|
| 54 | public string WorkingEndpoint { get; private set; }
|
---|
| 55 |
|
---|
[6976] | 56 | private HiveServiceClient NewServiceClient() {
|
---|
[9665] | 57 | if (EndpointRetries >= Settings.Default.MaxEndpointRetries) {
|
---|
| 58 | return CreateClient(WorkingEndpoint);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | var configurations = Settings.Default.EndpointConfigurationPriorities;
|
---|
| 62 |
|
---|
| 63 | Exception exception = null;
|
---|
| 64 | foreach (var endpointConfigurationName in configurations) {
|
---|
| 65 | try {
|
---|
| 66 | var cl = CreateClient(endpointConfigurationName);
|
---|
| 67 | cl.Open();
|
---|
| 68 | WorkingEndpoint = endpointConfigurationName;
|
---|
| 69 | return cl;
|
---|
| 70 | }
|
---|
| 71 | catch (Exception exc) {
|
---|
| 72 | exception = exc;
|
---|
| 73 | EndpointRetries++;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | throw exception ?? new Exception("No endpoint for Hive service found.");
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private HiveServiceClient CreateClient(string endpointConfigurationName) {
|
---|
| 81 | HiveServiceClient cl = null;
|
---|
| 82 |
|
---|
[6976] | 83 | if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
|
---|
[9665] | 84 | cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
|
---|
[6976] | 85 | else
|
---|
[9665] | 86 | cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
|
---|
[7142] | 87 |
|
---|
[6976] | 88 | return cl;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public T CallHiveService<T>(Func<IHiveService, T> call) {
|
---|
| 92 | HiveServiceClient client = NewServiceClient();
|
---|
[7249] | 93 | HandleAnonymousUser(client);
|
---|
| 94 |
|
---|
[6976] | 95 | try {
|
---|
| 96 | return call(client);
|
---|
[7249] | 97 | }
|
---|
| 98 | finally {
|
---|
[6976] | 99 | try {
|
---|
| 100 | client.Close();
|
---|
| 101 | }
|
---|
| 102 | catch (Exception) {
|
---|
| 103 | client.Abort();
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public void CallHiveService(Action<IHiveService> call) {
|
---|
| 109 | HiveServiceClient client = NewServiceClient();
|
---|
[7249] | 110 | HandleAnonymousUser(client);
|
---|
| 111 |
|
---|
[6976] | 112 | try {
|
---|
| 113 | call(client);
|
---|
[7249] | 114 | }
|
---|
| 115 | finally {
|
---|
[6976] | 116 | try {
|
---|
| 117 | client.Close();
|
---|
| 118 | }
|
---|
| 119 | catch (Exception) {
|
---|
| 120 | client.Abort();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[7249] | 124 |
|
---|
| 125 | private void HandleAnonymousUser(HiveServiceClient client) {
|
---|
| 126 | if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
|
---|
| 127 | try {
|
---|
| 128 | client.Close();
|
---|
| 129 | }
|
---|
| 130 | catch (Exception) {
|
---|
| 131 | client.Abort();
|
---|
| 132 | }
|
---|
| 133 | throw new AnonymousUserException();
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[6976] | 136 | }
|
---|
| 137 | }
|
---|