Skip to main content
summaryrefslogtreecommitdiffstats
blob: c80ef0e1fdb5a5f906cd52fef220d482f9a65b9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.server.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.framework.core.data.OseeServerInfo;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.server.IApplicationServerLookup;
import org.eclipse.osee.framework.core.util.HttpProcessor;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.logger.Log;

/**
 * @author Roberto E. Escobar
 */
public class ApplicationServerLookup implements IApplicationServerLookup {

   private Log logger;
   private IOseeDatabaseService dbService;

   private ApplicationServerDataStore serverDataStore;

   public void setLogger(Log logger) {
      this.logger = logger;
   }

   private Log getLogger() {
      return logger;
   }

   public void setDatabaseService(IOseeDatabaseService dbService) {
      this.dbService = dbService;
   }

   private IOseeDatabaseService getDatabaseService() {
      return dbService;
   }

   public void start() {
      serverDataStore = new ApplicationServerDataStore(getLogger(), getDatabaseService());
   }

   public void stop() {
      serverDataStore = null;
   }

   private ApplicationServerDataStore getDataStore() {
      return serverDataStore;
   }

   @Override
   public Collection<OseeServerInfo> getAvailableServers() throws OseeCoreException {
      Collection<? extends OseeServerInfo> infos = getDataStore().getAll();
      return getHealthyServers(infos);
   }

   private Collection<OseeServerInfo> getHealthyServers(Collection<? extends OseeServerInfo> infos) {
      List<OseeServerInfo> healthyServers = new ArrayList<OseeServerInfo>();
      for (OseeServerInfo info : infos) {
         if (info.isAcceptingRequests()) {
            if (isServerAlive(info)) {
               healthyServers.add(info);
            }
         }
      }
      return healthyServers;
   }

   @Override
   public OseeServerInfo getServerInfoBy(String version) throws OseeCoreException {
      Collection<? extends OseeServerInfo> infos = getVersionCompatibleServers(version);
      Collection<OseeServerInfo> healthyServers = getHealthyServers(infos);
      return getBestAvailable(healthyServers);
   }

   public Collection<OseeServerInfo> getVersionCompatibleServers(String clientVersion) throws OseeCoreException {
      Set<OseeServerInfo> toReturn = new HashSet<OseeServerInfo>();
      if (Strings.isValid(clientVersion)) {
         Collection<? extends OseeServerInfo> infos = getDataStore().getAll();
         for (OseeServerInfo info : infos) {
            if (isServerCompatible(info, clientVersion)) {
               toReturn.add(info);
            }
         }
      }
      return toReturn;
   }

   private boolean isCompatibleVersion(String serverVersion, String clientVersion) {
      boolean result = false;
      if (serverVersion.equals(clientVersion)) {
         result = true;
      } else {
         result = clientVersion.matches(serverVersion);
         if (!result) {
            result = serverVersion.matches(clientVersion);
         }
      }
      return result;
   }

   private boolean isServerCompatible(OseeServerInfo info, String clientVersion) {
      boolean result = false;
      for (String version : info.getVersion()) {
         result = isCompatibleVersion(version, clientVersion);
         if (result) {
            break;
         }
      }
      return result;
   }

   private OseeServerInfo getBestAvailable(Collection<OseeServerInfo> infos) throws OseeCoreException {
      OseeServerInfo result = null;
      if (infos.size() == 1) {
         result = infos.iterator().next();
      } else {
         int minSessions = Integer.MAX_VALUE;
         for (OseeServerInfo info : infos) {
            try {
               int numberOfSessions = serverDataStore.getNumberOfSessions(info.getServerId());
               if (minSessions > numberOfSessions) {
                  result = info;
                  minSessions = numberOfSessions;
               }
            } catch (OseeDataStoreException ex) {
               logger.error(ex, "Error getting number of sessions");
            }
         }
      }
      return result;
   }

   private boolean isServerAlive(OseeServerInfo info) {
      return HttpProcessor.isAlive(info.getUri());
   }

}

Back to the top