Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4e45883dcf173fcabf6fe62afb7746c61f162702 (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
/*******************************************************************************
 * Copyright (c) 2010 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.ote.server.internal;

import java.net.InetAddress;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.UUID;
import java.util.logging.Level;

import net.jini.core.lookup.ServiceID;
import net.jini.id.Uuid;
import net.jini.id.UuidFactory;

import org.eclipse.osee.framework.jdk.core.util.EnhancedProperties;
import org.eclipse.osee.framework.jini.service.interfaces.IService;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.NodeInfo;
import org.eclipse.osee.framework.messaging.services.RegisteredServiceReference;
import org.eclipse.osee.ote.core.ConnectionRequestResult;
import org.eclipse.osee.ote.core.IRemoteUserSession;
import org.eclipse.osee.ote.core.IUserSession;
import org.eclipse.osee.ote.core.OSEEPerson1_4;
import org.eclipse.osee.ote.core.OTESessionManager;
import org.eclipse.osee.ote.core.ReturnStatus;
import org.eclipse.osee.ote.core.environment.BundleConfigurationReport;
import org.eclipse.osee.ote.core.environment.BundleDescription;
import org.eclipse.osee.ote.core.environment.TestEnvironmentConfig;
import org.eclipse.osee.ote.core.environment.interfaces.IHostTestEnvironment;
import org.eclipse.osee.ote.core.environment.interfaces.IRuntimeLibraryManager;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironment;
import org.eclipse.osee.ote.message.MessageSystemTestEnvironment;
import org.eclipse.osee.ote.server.PropertyParamter;

public class OteService implements IHostTestEnvironment, IService {

   private final ServiceID serviceID;
   private final EnhancedProperties enhancedProperties;
   private MessageSystemTestEnvironment currentEnvironment;
   private ITestEnvironment remoteEnvironment;
   private final EnvironmentCreationParameter environmentCreation;
   private final IRuntimeLibraryManager runtimeLibraryManager;
   private RegisteredServiceReference registeredServiceReference;
   private OTESessionManager oteSessions;
   
   
   public OteService(IRuntimeLibraryManager runtimeLibraryManager, EnvironmentCreationParameter environmentCreation, OTESessionManager oteSessions, PropertyParamter parameterObject, EnhancedProperties properties) {
      this.runtimeLibraryManager = runtimeLibraryManager;
      this.environmentCreation = environmentCreation;
      this.oteSessions = oteSessions;
      
      Uuid uuid = UuidFactory.generate();
      Long lsb = Long.valueOf(uuid.getLeastSignificantBits());
      Long msb = Long.valueOf(uuid.getMostSignificantBits());
      serviceID = new ServiceID(msb.longValue(), lsb.longValue());

      enhancedProperties = properties;
      enhancedProperties.setProperty("name", environmentCreation.getServerTitle());
      enhancedProperties.setProperty("station", parameterObject.getStation());
      enhancedProperties.setProperty("version", parameterObject.getVersion());
      enhancedProperties.setProperty("type", parameterObject.getType());
      enhancedProperties.setProperty("maxUsers", Integer.toString(environmentCreation.getMaxUsersPerEnvironment()));
      enhancedProperties.setProperty("comment", parameterObject.getComment());
      enhancedProperties.setProperty("date", new Date().toString());
      enhancedProperties.setProperty("group", "OSEE Test Environment");
      enhancedProperties.setProperty("owner", System.getProperty("user.name"));
      enhancedProperties.setProperty("id", serviceID.toString());
      try {
         enhancedProperties.setProperty("appServerURI", String.format("http://%s:%s", InetAddress.getLocalHost().getHostAddress(), Integer.parseInt(System.getProperty("org.osgi.service.http.port"))));
      } catch (Exception e) {
         OseeLog.log(OteService.class, Level.SEVERE, "Failed to set the appServerURI", e);
      }
   }
   
   @Override
   public NodeInfo getBroker(){
      return environmentCreation.getBroker();
   }

   @Override
   public EnhancedProperties getProperties() throws RemoteException {
      return enhancedProperties;
   }

   @Override
   public ConnectionRequestResult requestEnvironment(IRemoteUserSession session, UUID sessionId, TestEnvironmentConfig config) throws RemoteException {
      try {
         OseeLog.log(OteService.class, Level.INFO,
            "received request for test environment from user " + session.getUser().getName());
         if (!isEnvironmentAvailable()) {
            createEnvironment();

         }
         
         oteSessions.add(sessionId, session);
         updateDynamicInfo();
         return new ConnectionRequestResult(remoteEnvironment, sessionId, new ReturnStatus("Success", true));
      } catch (Throwable ex) {
         OseeLog.log(OteService.class, Level.SEVERE,
            "Exception while requesting environment for user " + session.getUser().getName(), ex);
         throw new RemoteException("Exception while requesting environment for user ", ex);
      }
   }

   private void createEnvironment() throws Throwable {
      currentEnvironment = environmentCreation.createEnvironment();
      remoteEnvironment = environmentCreation.createRemoteTestEnvironment(currentEnvironment);
      currentEnvironment.startup(environmentCreation.getOutfileLocation());
   }

   private boolean isEnvironmentAvailable() {
      return remoteEnvironment != null;
   }

   public void updateDynamicInfo() throws RemoteException {
      Collection<OSEEPerson1_4> userList = new LinkedList<OSEEPerson1_4>();
      StringBuilder sb = new StringBuilder();
      if (isEnvironmentAvailable()) {
         for(UUID sessionId:oteSessions.get()){
            IUserSession session = oteSessions.get(sessionId);
            try {
               userList.add(session.getUser());
            } catch (Exception e) {
               OseeLog.log(OteService.class, Level.SEVERE, e);
            }
         }
      }
      for (OSEEPerson1_4 person : userList) {
         sb.append(person.getName());
         sb.append(", ");
      }
      if (sb.length() > 2) {
         String list = sb.toString().substring(0, sb.length() - 2);
         environmentCreation.getServiceConnector().setProperty("user_list", list);
      } else {
         environmentCreation.getServiceConnector().setProperty("user_list", "N/A");
      }
      if (registeredServiceReference != null) {
         registeredServiceReference.update();
      }
   }

   @Override
   public ServiceID getServiceID() throws RemoteException {
      return serviceID;
   }

   @Override
   public void kill() throws RemoteException {
      if(currentEnvironment != null){
         currentEnvironment.shutdown();
      }
   }

   public void set(RegisteredServiceReference ref) {
      this.registeredServiceReference = ref;
   }

   @Override
   public void disconnect(UUID sessionId) throws RemoteException {
      if (currentEnvironment != null) {
         oteSessions.remove(sessionId);
         updateDynamicInfo();
      }
   }

   @Override
   public String getHttpURL() throws RemoteException {
      return (String)enhancedProperties.getProperty("appServerURI");
   }
   
}

Back to the top