Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7363287137f49b2c375ca04092479bc0867ffc0b (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
/*******************************************************************************
 * 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.ote.service.core;

import java.net.InetAddress;
import java.rmi.RemoteException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;

import org.eclipse.osee.connection.service.IServiceConnector;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.Message;
import org.eclipse.osee.framework.plugin.core.util.ExportClassLoader;
import org.eclipse.osee.ote.core.AbstractRemoteSession;
import org.eclipse.osee.ote.core.ConnectionRequestResult;
import org.eclipse.osee.ote.core.IRemoteUserSession;
import org.eclipse.osee.ote.core.OSEEPerson1_4;
import org.eclipse.osee.ote.core.environment.TestEnvironmentConfig;
import org.eclipse.osee.ote.core.environment.interfaces.IHostTestEnvironment;
import org.eclipse.osee.ote.core.framework.prompt.IPassFailPromptResponse;
import org.eclipse.osee.ote.core.framework.prompt.IResumeResponse;
import org.eclipse.osee.ote.core.framework.prompt.IUserInputPromptResponse;
import org.eclipse.osee.ote.core.framework.prompt.IYesNoPromptResponse;
import org.eclipse.osee.ote.service.Activator;
import org.eclipse.osee.ote.service.SessionDelegate;

/**
 * @author Ken J. Aguilar
 */
public class ClientSession extends AbstractRemoteSession {
   private static final int TIMEOUT = 1; // 1 minute timeout
   private static final String LOCK_ERROR_MSG = "could not acquire lock";
   private final InetAddress address;
   private SessionDelegate sessionDelegate = null;
   private final ReentrantLock lock = new ReentrantLock();
   private final OteClientEndpointReceive receive;

   public ClientSession(OSEEPerson1_4 user, InetAddress address, OteClientEndpointReceive receive) {
      super(user);
      this.address = address;
      this.receive = receive;
      Activator.log(Level.INFO,
         String.format("Created OTE session for %s. Address=%s\n ", user.getName(), address.toString()));
   }

   @Override
   public String getAddress() throws RemoteException {
      return address.getHostAddress();
   }

   @Override
   public byte[] getFile(String workspacePath) throws RemoteException {
      if (sessionDelegate != null) {
         try {
            return sessionDelegate.getFile(workspacePath);
         } catch (Exception ex) {
            throw new RemoteException("failed to get the file " + workspacePath, ex);
         }
      }
      throw new IllegalStateException("session delegate not set");
   }

   @Override
   public long getFileDate(String workspacePath) throws RemoteException {
      if (sessionDelegate != null) {
         try {
            return sessionDelegate.getFileDate(workspacePath);
         } catch (Exception ex) {
            throw new RemoteException("failed to get the file date" + workspacePath, ex);
         }
      }
      throw new IllegalStateException("session delegate not set");
   }

   @Override
   public boolean isAlive() throws RemoteException {
      try {
         if (lock.tryLock(TIMEOUT, TimeUnit.MINUTES)) {
            try {
               return true;
            } finally {
               lock.unlock();
            }
         }
         return false;
      } catch (InterruptedException ex) {
         throw new RemoteException(LOCK_ERROR_MSG, ex);
      }
   }

   @Override
   public void initiateInformationalPrompt(String message) throws RemoteException {
      assert sessionDelegate != null : "delegate is null";
      try {
         sessionDelegate.handleInformationPrompt(message);
      } catch (Exception ex) {
         System.out.println(message);
      }
   }

   @Override
   public void initiatePassFailPrompt(IPassFailPromptResponse prompt) throws RemoteException {
      assert sessionDelegate != null : "delegate is null";
      try {
         sessionDelegate.handlePassFail(prompt);
      } catch (Exception ex) {
         throw new RemoteException("exception initiating prompt", ex);
      }
   }

   @Override
   public void initiateResumePrompt(IResumeResponse prompt) throws RemoteException {
      assert sessionDelegate != null : "delegate is null";
      try {
         sessionDelegate.handlePause(prompt);
      } catch (Exception ex) {
         throw new RemoteException("exception initiating prompt", ex);
      }
   }

   @Override
   public void cancelPrompts() throws RemoteException {
      assert sessionDelegate != null : "delegate is null";
      try {
         sessionDelegate.cancelPrompts();
      } catch (Exception ex) {
         throw new RemoteException("exception canceling prompt", ex);
      }
   }

   @Override
   public void initiateUserInputPrompt(IUserInputPromptResponse prompt) throws RemoteException {
      assert sessionDelegate != null : "delegate is null";
      try {
         sessionDelegate.handleUserInput(prompt);
      } catch (Exception ex) {
         throw new RemoteException("exception initiating prompt", ex);
      }
   }

   /**
    * this must be called prior to establishing a test host connection
    */
   synchronized void setSessionDelegate(SessionDelegate sessionDelegate) {
      // intentionally package-private
      this.sessionDelegate = sessionDelegate;
   }

   /**
    * closes this session
    */
   void close() {

   }

   TestHostConnection connect(IServiceConnector connector, IHostTestEnvironment testHost, TestEnvironmentConfig config) throws Exception {
      // intentionally package-private
      if (lock.tryLock(TIMEOUT, TimeUnit.MINUTES)) {
         try {
            IRemoteUserSession exportedSession = (IRemoteUserSession) connector.export(this);
            UUID id = UUID.randomUUID();
            Thread.currentThread().setContextClassLoader(ExportClassLoader.getInstance());
            ConnectionRequestResult result = testHost.requestEnvironment(exportedSession, id, config);
            if (result.getStatus().getStatus()) {
               return new TestHostConnection(connector, testHost, result.getEnvironment(), result.getSessionKey());
            } else {
               OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, "Error Connecting to the OTE Test Server.",
                  new Exception(result.getStatus().getMessage()));
            }
            return null;
         } finally {
            lock.unlock();
         }
      }
      throw new IllegalStateException(LOCK_ERROR_MSG);

   }

   void disconnect(TestHostConnection connection) throws InterruptedException, RemoteException {
      // intentionally package-private
      if (lock.tryLock(TIMEOUT, TimeUnit.MINUTES)) {
         try {
            connection.endConnection();
            return;
         } finally {
            lock.unlock();
         }
      }
      throw new IllegalStateException(LOCK_ERROR_MSG);
   }

   @Override
   public void sendMessageToClient(Message message) throws RemoteException {
      receive.receivedMessage(message);
   }

   @Override
   public void initiateYesNoPrompt(IYesNoPromptResponse prompt) throws Exception {
      assert sessionDelegate != null : "delegate is null";
      try {
         sessionDelegate.handleYesNo(prompt);
      } catch (Exception ex) {
         throw new RemoteException("exception initiating prompt", ex);
      }
   }
}

Back to the top