1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Clients.Common;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Clients.Hive {
|
---|
26 | public class HiveServiceLocator : IHiveServiceLocator {
|
---|
27 | private static IHiveServiceLocator instance = null;
|
---|
28 | public static IHiveServiceLocator Instance {
|
---|
29 | get {
|
---|
30 | if (instance == null) {
|
---|
31 | instance = new HiveServiceLocator();
|
---|
32 | }
|
---|
33 | return instance;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | private string username;
|
---|
38 | public string Username {
|
---|
39 | get { return username; }
|
---|
40 | set { username = value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | private string password;
|
---|
44 | public string Password {
|
---|
45 | get { return password; }
|
---|
46 | set { password = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private HiveServiceClient NewServiceClient() {
|
---|
50 | HiveServiceClient cl;
|
---|
51 | if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
|
---|
52 | cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>();
|
---|
53 | else
|
---|
54 | cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(null, null, username, password);
|
---|
55 |
|
---|
56 | return cl;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public T CallHiveService<T>(Func<IHiveService, T> call) {
|
---|
60 | HiveServiceClient client = NewServiceClient();
|
---|
61 | try {
|
---|
62 | return call(client);
|
---|
63 | } finally {
|
---|
64 | try {
|
---|
65 | client.Close();
|
---|
66 | }
|
---|
67 | catch (Exception) {
|
---|
68 | client.Abort();
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void CallHiveService(Action<IHiveService> call) {
|
---|
74 | HiveServiceClient client = NewServiceClient();
|
---|
75 | try {
|
---|
76 | call(client);
|
---|
77 | } finally {
|
---|
78 | try {
|
---|
79 | client.Close();
|
---|
80 | }
|
---|
81 | catch (Exception) {
|
---|
82 | client.Abort();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|