Skip to main content
summaryrefslogtreecommitdiffstats
blob: b903854f809d028a76d489f3951390246d2cadd3 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*******************************************************************************
 * 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.io.ByteArrayInputStream;
import java.lang.Thread.State;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import org.eclipse.osee.framework.core.data.OseeCodeVersion;
import org.eclipse.osee.framework.core.data.OseeServerInfo;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.core.server.IApplicationServerManager;
import org.eclipse.osee.framework.core.server.OseeHttpServlet;
import org.eclipse.osee.framework.core.server.OseeServerProperties;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.OseeInfo;
import org.eclipse.osee.framework.jdk.core.util.ChecksumUtil;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.time.GlobalTime;
import org.eclipse.osee.logger.Log;

/**
 * @author Roberto E. Escobar
 */
public class ApplicationServerManager implements IApplicationServerManager {
   private final Map<String, OseeServerThreadFactory> threadFactories;
   private final Map<String, InternalOseeHttpServlet> oseeHttpServlets;

   private Log logger;
   private IOseeDatabaseService dbService;

   private ApplicationServerDataStore dataStore;
   private OseeServerInfoMutable serverInfo;
   private Timer timer;
   private final Set<String> defaultVersions = new HashSet<String>();

   public ApplicationServerManager() {
      this.oseeHttpServlets = new ConcurrentHashMap<String, InternalOseeHttpServlet>();
      this.threadFactories = new ConcurrentHashMap<String, OseeServerThreadFactory>();
   }

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

   public Log getLogger() {
      return logger;
   }

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

   private IOseeDatabaseService getDatabaseService() {
      return dbService;
   }

   public void start() throws Exception {
      defaultVersions.clear();
      String[] userSpecifiedVersion = OseeServerProperties.getOseeVersion();
      if (Conditions.hasValues(userSpecifiedVersion)) {
         for (String version : userSpecifiedVersion) {
            defaultVersions.add(version);
         }
      } else {
         defaultVersions.add(OseeCodeVersion.getVersion());
      }

      dataStore = new ApplicationServerDataStore(getLogger(), getDatabaseService());
      serverInfo = createOseeServerInfo(getLogger(), dataStore, defaultVersions);

      timer = new Timer();
      timer.schedule(new TimerTask() {
         @Override
         public void run() {
            if (isDbInitialized()) {
               try {
                  boolean wasRegistered = executeLookupRegistration();
                  setServletRequestsAllowed(wasRegistered);
               } catch (Exception ex) {
                  getLogger().error(ex, "Error during lookup registration");
               } finally {
                  timer.cancel();
               }
            } else {
               setServletRequestsAllowedNoDbUpdate(true);
            }
         }

         private boolean isDbInitialized() {
            boolean result = false;
            try {
               String id = OseeInfo.getDatabaseGuid();
               if (Strings.isValid(id)) {
                  result = true;
               }
            } catch (Exception ex) {
               // Do nothing - no need to log exception
            }
            return result;
         }
      }, 5 * 1000);
   }

   public void stop() {
      shutdown();
      defaultVersions.clear();
   }

   private static OseeServerInfoMutable createOseeServerInfo(Log logger, ApplicationServerDataStore dataStore, Set<String> defaultVersions) {
      String serverAddress = "127.0.0.1";
      try {
         serverAddress = InetAddress.getLocalHost().getCanonicalHostName();
      } catch (UnknownHostException ex) {
         //
      }
      int port = OseeServerProperties.getOseeApplicationServerPort();
      String scheme = OseeServerProperties.getOseeApplicationServerScheme();
      URI uri = null;
      try {
         uri = new URI(scheme, null, serverAddress, port, null, null, null);
      } catch (URISyntaxException ex) {
         logger.error(ex, "Error generating application server uri");
      }

      String checkSum = "-1";
      try {
         String address = String.format("%s:%s", serverAddress, port);
         ByteArrayInputStream inputStream = new ByteArrayInputStream(address.getBytes("UTF-8"));
         checkSum = ChecksumUtil.createChecksumAsString(inputStream, ChecksumUtil.MD5);
      } catch (Exception ex) {
         logger.error(ex, "Error generating application server id");
      }
      OseeServerInfoMutable toReturn =
         new OseeServerInfoMutable(checkSum, uri.toString(), new String[0], GlobalTime.GreenwichMeanTimestamp(), false);
      toReturn.setVersions(defaultVersions);
      return toReturn;
   }

   private void refreshData(OseeServerInfoMutable info) {
      dataStore.refresh(info);

      Set<String> supportedVersions = info.getVersionSet();
      if (!supportedVersions.containsAll(defaultVersions)) {
         supportedVersions.addAll(defaultVersions);
         info.setVersions(supportedVersions);
      }
   }

   @Override
   public boolean executeLookupRegistration() {
      OseeServerInfoMutable info = getApplicationServerInfo();
      refreshData(info);
      deregisterWithDb(info);
      boolean isRegistered = registerWithDb(info);
      if (isRegistered) {
         getLogger().info("Application Server: [%s] registered.", info.getServerId());
      }
      return isRegistered;
   }

   @Override
   public void register(String context, OseeHttpServlet servlet) {
      InternalOseeHttpServlet internalServlet = servlet;
      internalServlet.setRequestsAllowed(getApplicationServerInfo().isAcceptingRequests());
      this.oseeHttpServlets.put(context, internalServlet);
   }

   @Override
   public void unregister(String key) {
      this.oseeHttpServlets.remove(key);
      this.threadFactories.remove(key);
   }

   @Override
   public Collection<String> getRegisteredServlets() {
      return oseeHttpServlets.keySet();
   }

   private OseeServerInfoMutable getApplicationServerInfo() {
      return serverInfo;
   }

   @Override
   public ThreadFactory createNewThreadFactory(String name, int priority) {
      OseeServerThreadFactory factory = new OseeServerThreadFactory(name, priority);
      this.threadFactories.put(name, factory);
      return factory;
   }

   private List<OseeServerThread> getThreadsFromFactory(String key) {
      OseeServerThreadFactory factory = threadFactories.get(key);
      return factory.getThreads();
   }

   /**
    * This method expects that one OSEE server job is running, namely the job calling this method, so it will return
    * true if 1 or less jobs are running.
    */
   @Override
   public boolean isSystemIdle() {
      return !Operations.areOperationsScheduled();
   }

   private static enum PersistType {
      ALLOW_DB_PERSIST,
      DONT_DB_PERSIST;
   }

   private synchronized void setServletRequestsAllowed(final boolean value, PersistType persistType) throws OseeCoreException {
      OseeServerInfoMutable info = getApplicationServerInfo();
      info.setAcceptingRequests(value);
      if (PersistType.ALLOW_DB_PERSIST == persistType) {
         dataStore.update(Collections.singleton(info));
      }
      for (String contexts : oseeHttpServlets.keySet()) {
         InternalOseeHttpServlet servlets = oseeHttpServlets.get(contexts);
         servlets.setRequestsAllowed(value);
      }
   }

   private void setServletRequestsAllowedNoDbUpdate(final boolean value) {
      try {
         setServletRequestsAllowed(value, PersistType.DONT_DB_PERSIST);
      } catch (OseeCoreException ex) {
         logger.warn(ex, "Error updating servlet requests allowed to [%s]- current setting is [%s]", value,
            getApplicationServerInfo().isAcceptingRequests());
      }
   }

   @Override
   public synchronized void setServletRequestsAllowed(final boolean value) throws OseeCoreException {
      setServletRequestsAllowed(value, PersistType.ALLOW_DB_PERSIST);
   }

   @Override
   public void shutdown() {
      if (timer != null) {
         timer.cancel();
      }
      OseeServerInfoMutable info = getApplicationServerInfo();
      deregisterWithDb(info);
   }

   @Override
   public List<String> getCurrentProcesses() {
      List<String> processList = new ArrayList<String>();
      for (String key : threadFactories.keySet()) {
         for (OseeServerThread thread : getThreadsFromFactory(key)) {
            State state = thread.getState();
            if (!state.equals(State.TERMINATED)) {
               processList.add(thread.getName());
            }
         }
      }
      for (String contexts : oseeHttpServlets.keySet()) {
         InternalOseeHttpServlet servlets = oseeHttpServlets.get(contexts);
         if (servlets.getState().equals(ProcessingStateEnum.BUSY)) {
            processList.add(servlets.getCurrentRequest());
         }
      }
      return processList;
   }

   @Override
   public int getNumberOfActiveThreads() {
      int totalProcesses = 0;
      for (String contexts : oseeHttpServlets.keySet()) {
         InternalOseeHttpServlet servlet = oseeHttpServlets.get(contexts);
         if (servlet.getState().isBusy()) {
            totalProcesses++;
         }
      }

      for (String key : threadFactories.keySet()) {
         for (OseeServerThread thread : getThreadsFromFactory(key)) {
            State state = thread.getState();
            if (State.TERMINATED != state) {
               totalProcesses++;
            }
         }
      }
      return totalProcesses;
   }

   @Override
   public String getId() {
      return getApplicationServerInfo().getServerId();
   }

   @Override
   public URI getServerUri() {
      return getApplicationServerInfo().getUri();
   }

   @Override
   public Date getDateStarted() {
      return getApplicationServerInfo().getDateStarted();
   }

   @Override
   public boolean isAcceptingRequests() {
      return getApplicationServerInfo().isAcceptingRequests();
   }

   @Override
   public String[] getSupportedVersions() {
      OseeServerInfoMutable info = getApplicationServerInfo();
      refreshData(info);
      return getApplicationServerInfo().getVersion();
   }

   @Override
   public void addSupportedVersion(String version) throws OseeCoreException {
      Conditions.checkNotNull(version, "Osee Version");
      OseeServerInfoMutable info = getApplicationServerInfo();
      refreshData(info);
      info.addVersion(version);
      dataStore.update(Collections.singleton(info));
   }

   @Override
   public void removeSupportedVersion(String version) throws OseeCoreException {
      Conditions.checkNotNull(version, "Osee Version");
      Conditions.checkExpressionFailOnTrue(defaultVersions.contains(version),
         "Unable to remove default Osee version [%s]", version);

      OseeServerInfoMutable info = getApplicationServerInfo();
      refreshData(info);
      Set<String> versions = info.getVersionSet();
      boolean wasRemoved = versions.remove(version);
      if (wasRemoved) {
         info.setVersions(versions);
         dataStore.update(Collections.singleton(info));
      } else {
         throw new OseeStateException("Not part of the supported version [%s]", version);
      }
   }

   private boolean deregisterWithDb(OseeServerInfo info) {
      boolean status = false;
      try {
         dataStore.delete(Collections.singleton(info));
         status = true;
      } catch (OseeCoreException ex) {
         getLogger().info("Server lookup table is not initialized");
      }
      return status;
   }

   private boolean registerWithDb(OseeServerInfo info) {
      boolean status = false;
      try {
         dataStore.create(Collections.singleton(info));
         status = true;
      } catch (OseeCoreException ex) {
         getLogger().info("Server lookup table is not initialized");
      }
      return status;
   }

}

Back to the top