Skip to main content
summaryrefslogtreecommitdiffstats
blob: 656f847f415276f4886efe91cec3b2b7b014170e (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
/*******************************************************************************
 * 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.client.internal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.client.CoreClientActivator;
import org.eclipse.osee.framework.core.client.OseeClientProperties;
import org.eclipse.osee.framework.core.client.server.HttpUrlBuilderClient;
import org.eclipse.osee.framework.core.data.OseeCodeVersion;
import org.eclipse.osee.framework.core.data.OseeServerContext;
import org.eclipse.osee.framework.core.data.OseeServerInfo;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.core.util.HttpProcessor;
import org.eclipse.osee.framework.core.util.HttpProcessor.AcquireResult;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.BaseStatus;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Andrew M. Finkbeiner
 */
public class OseeApplicationServer {

   private static String oseeServer = null;
   private static boolean isServerAlive = false;
   private static OseeServerInfo serverInfo = null;
   private static final String ArbitrationService = "Arbitration Server";
   private static final String ApplicationServer = "Application Server";

   private OseeApplicationServer() {
      // private constructor
   }

   public static String getOseeApplicationServer() throws OseeCoreException {
      checkAndUpdateStatus();
      Conditions.checkNotNullOrEmpty(oseeServer, "resource server address");
      return oseeServer;
   }

   public static boolean isApplicationServerAlive() {
      checkAndUpdateStatus();
      return isServerAlive;
   }

   private static void checkAndUpdateStatus() {
      isServerAlive = false;
      if (serverInfo == null) {
         String overrideValue = OseeClientProperties.getOseeApplicationServer();
         if (Strings.isValid(overrideValue)) {
            serverInfo = fromString(overrideValue);
         } else {
            serverInfo = getOseeServerAddress();
         }
         if (serverInfo != null) {
            oseeServer = String.format("http://%s:%s/", serverInfo.getServerAddress(), serverInfo.getPort());
         }
      }
      DateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
      if (serverInfo == null) {
         OseeLog.reportStatus(new BaseStatus(ApplicationServer, Level.SEVERE, "Application server address was null"));
      } else {
         isServerAlive = HttpProcessor.isAlive(serverInfo.getServerAddress(), serverInfo.getPort());
         if (isServerAlive) {
            OseeLog.reportStatus(new BaseStatus(ApplicationServer, Level.INFO, "%s %s Running Since: %s", oseeServer,
               Arrays.deepToString(serverInfo.getVersion()), format.format(serverInfo.getDateStarted())));
         } else {
            OseeLog.reportStatus(new BaseStatus(ApplicationServer, Level.SEVERE, "Unable to Connect to [%s]",
               oseeServer));
         }
      }
   }

   public static OseeServerInfo fromString(String value) {
      OseeServerInfo toReturn = null;
      String rawAddress = value;
      if (rawAddress.startsWith("http")) {
         rawAddress = value.replace("http://", "");
      }
      Pattern pattern = Pattern.compile("(.*):(\\d+)");
      Matcher matcher = pattern.matcher(rawAddress);
      if (matcher.find()) {
         String address = matcher.group(1);
         int port = Integer.valueOf(matcher.group(2));
         toReturn =
            new OseeServerInfo("OVERRIDE", address, port, new String[] {"OVERRIDE"},
               new Timestamp(new Date().getTime()), true);
      }
      return toReturn;
   }

   private static OseeServerInfo getOseeServerAddress() {
      OseeServerInfo oseeServerInfo = null;
      ByteArrayOutputStream outputStream = null;
      InputStream inputStream = null;
      try {
         Map<String, String> parameters = new HashMap<String, String>();
         parameters.put("version", OseeCodeVersion.getVersion());
         String url =
            HttpUrlBuilderClient.getInstance().getOsgiArbitrationServiceUrl(OseeServerContext.LOOKUP_CONTEXT,
               parameters);

         outputStream = new ByteArrayOutputStream();
         AcquireResult result = HttpProcessor.acquire(new URL(url), outputStream);
         try {
            OseeLog.reportStatus(new BaseStatus(ArbitrationService, Level.INFO, "%s",
               HttpUrlBuilderClient.getInstance().getArbitrationServerPrefix()));
         } catch (OseeDataStoreException ex) {
            OseeLog.log(CoreClientActivator.class, Level.SEVERE, ex);
         }
         if (result.getCode() == HttpURLConnection.HTTP_OK) {
            inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            oseeServerInfo = OseeServerInfo.fromXml(inputStream);
         }
      } catch (Exception ex) {
         OseeLog.log(CoreClientActivator.class, Level.SEVERE, ex);
         OseeLog.reportStatus(new BaseStatus(ArbitrationService, Level.SEVERE, ex,
            "Error requesting application server for version [%s]", OseeCodeVersion.getVersion()));
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close();
            } catch (IOException ex) {
               OseeLog.log(CoreClientActivator.class, Level.SEVERE, ex);
            }
         }
         if (outputStream != null) {
            try {
               outputStream.close();
            } catch (IOException ex) {
               OseeLog.log(CoreClientActivator.class, Level.SEVERE, ex);
            }
         }
      }
      return oseeServerInfo;
   }
}

Back to the top