Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c51d2cbf68d9b8a0092f61327a7ca763e5a2403 (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
/*******************************************************************************
 * 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.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;

import org.apache.activemq.broker.BrokerService;
import org.eclipse.osee.connection.service.IConnectionService;
import org.eclipse.osee.connection.service.IServiceConnector;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.network.PortUtil;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.MessageService;
import org.eclipse.osee.framework.messaging.NodeInfo;
import org.eclipse.osee.framework.messaging.OseeMessagingListener;
import org.eclipse.osee.framework.messaging.OseeMessagingStatusCallback;
import org.eclipse.osee.framework.messaging.ReplyConnection;
import org.eclipse.osee.framework.messaging.services.RegisteredServiceReference;
import org.eclipse.osee.framework.messaging.services.RemoteServiceRegistrar;
import org.eclipse.osee.framework.messaging.services.ServiceInfoPopulator;
import org.eclipse.osee.framework.messaging.services.messages.ServiceDescriptionPair;
import org.eclipse.osee.ote.core.OTESessionManager;
import org.eclipse.osee.ote.core.OteBaseMessages;
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.ITestEnvironmentServiceConfig;
import org.eclipse.osee.ote.server.OteServiceStarter;
import org.eclipse.osee.ote.server.PropertyParamter;
import org.eclipse.osee.ote.server.TestEnvironmentFactory;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.packageadmin.PackageAdmin;

/**
 * @author Andrew M. Finkbeiner
 */
public class OteServiceStarterImpl implements OteServiceStarter, ServiceInfoPopulator, OseeMessagingStatusCallback {

	private PackageAdmin packageAdmin;
	private IRuntimeLibraryManager runtimeLibraryManager;
	private IConnectionService connectionService;
	private RemoteServiceRegistrar remoteServiceRegistrar;
	private MessageService messageService;

	private BrokerService brokerService;
	private OteService service;
	private final ListenForHostRequest listenForHostRequest;

	private IServiceConnector serviceSideConnector;
   private OTESessionManager oteSessions;

	
   public void bindOTESessionManager(OTESessionManager oteSessions){
      this.oteSessions = oteSessions;
   }
   
   public void unbindOTESessionManager(OTESessionManager oteSessions){
      this.oteSessions = null;
   } 
   
	public void bindIRuntimeLibraryManager(IRuntimeLibraryManager runtimeLibraryManager){
	   this.runtimeLibraryManager = runtimeLibraryManager;
	}
	
	public void unbindIRuntimeLibraryManager(IRuntimeLibraryManager runtimeLibraryManager){
      this.runtimeLibraryManager = null;
   } 
	
	public void bindRemoteServiceRegistrar(RemoteServiceRegistrar remoteServiceRegistrar){
	   this.remoteServiceRegistrar = remoteServiceRegistrar;
	}
	
	public void unbindRemoteServiceRegistrar(RemoteServiceRegistrar remoteServiceRegistrar){
      this.remoteServiceRegistrar = null;
   }
	
	public void bindMessageService(MessageService messageService){
	   this.messageService = messageService;
	}

	public void unbindMessageService(MessageService messageService){
	   this.messageService = null;
	}
	
	public void bindIConnectionService(IConnectionService connectionService){
	   this.connectionService = connectionService;
	}
	
	public void unbindIConnectionService(IConnectionService connectionService){
	   this.connectionService = null;
	}
	   
	public void bindPackageAdmin(PackageAdmin packageAdmin){
	   this.packageAdmin = packageAdmin;
	}
	
	public void unbindPackageAdmin(PackageAdmin packageAdmin){
      this.packageAdmin = null;
   }
	
	public OteServiceStarterImpl() {
		listenForHostRequest = new ListenForHostRequest();
	}

	@Override
	public IHostTestEnvironment start(IServiceConnector serviceSideConnector, ITestEnvironmentServiceConfig config, PropertyParamter propertyParameter, String environmentFactoryClass) throws Exception {
		return start(serviceSideConnector, config, propertyParameter, null, environmentFactoryClass);
	}

	@Override
	public IHostTestEnvironment start(IServiceConnector serviceSideConnector, ITestEnvironmentServiceConfig config, PropertyParamter propertyParameter, TestEnvironmentFactory factory) throws Exception {
		return start(serviceSideConnector, config, propertyParameter, factory, null);
	}

	private IHostTestEnvironment start(IServiceConnector serviceSideConnector, ITestEnvironmentServiceConfig config, PropertyParamter propertyParameter, TestEnvironmentFactory factory, String environmentFactoryClass) throws Exception {
		if (service != null) {
			throw new OseeStateException("An ote Server has already been started.");
		}

		this.serviceSideConnector = serviceSideConnector;
		brokerService = new BrokerService();

		String strUri;
		try {
			String addressAsString = getAddress();
			int port = getServerPort();
			strUri = String.format("tcp://%s:%d", addressAsString, port);
			try {
				brokerService.addConnector(strUri);
				OseeLog.log(getClass(), Level.INFO, "Added TCP connector: " + strUri);			
			} catch (Exception e) {
				OseeLog.log(getClass(), Level.SEVERE, "could not add connector for " + strUri, e);
				strUri = "vm://localhost?broker.persistent=false";
			}
		} catch (Exception e) {
			OseeLog.log(getClass(), Level.SEVERE, "could acquire a TCP address", e);
			strUri = "vm://localhost?broker.persistent=false";
		}
		
		brokerService.setEnableStatistics(false);
		brokerService.setBrokerName("OTEServer");
		brokerService.setPersistent(false);
		brokerService.setUseJmx(false);
		brokerService.start();
		URI uri = new URI(strUri);

		NodeInfo nodeInfo = new NodeInfo("OTEEmbeddedBroker", uri);

		EnvironmentCreationParameter environmentCreationParameter =
				new EnvironmentCreationParameter(runtimeLibraryManager, nodeInfo, serviceSideConnector, config, factory,
						environmentFactoryClass, packageAdmin);

		service =
				new OteService(runtimeLibraryManager, environmentCreationParameter, oteSessions, propertyParameter,
						serviceSideConnector.getProperties());

		serviceSideConnector.init(service);

		if (propertyParameter.isLocalConnector() || propertyParameter.useJiniLookup()) {
			connectionService.addConnector(serviceSideConnector);
		}
		if (!propertyParameter.isLocalConnector()) {
			messageService.get(nodeInfo).subscribe(OteBaseMessages.RequestOteHost, listenForHostRequest, this);
			RegisteredServiceReference ref = remoteServiceRegistrar.registerService("osee.ote.server", "1.0", service.getServiceID().toString(), uri, this, 60 * 3);
			service.set(ref);
		} else {
			serviceSideConnector.setProperty("OTEEmbeddedBroker", nodeInfo);
		}
		
		FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(IHostTestEnvironment.class, service, null);
		
		return service;
	}

	private int getServerPort() throws IOException {
		String portFromLaunch = System.getProperty("ote.server.broker.uri.port");
		int port = 0;
		if (portFromLaunch != null) {
			try {
				port = Integer.parseInt(portFromLaunch);
			} catch (NumberFormatException ex) {
			}
		}
		if (port == 0) {
			port = PortUtil.getInstance().getValidPort();
		}
		return port;
	}

	@Override
	public void stop() {
		if (service != null) {
			try {
				service.updateDynamicInfo();
				remoteServiceRegistrar.unregisterService("osee.ote.server", "1.0", service.getServiceID().toString());
			} catch (RemoteException ex) {
				OseeLog.log(getClass(), Level.SEVERE, ex);
			}
		}
		if (brokerService != null) {
			try {
				brokerService.stop();
			} catch (Exception ex) {
				OseeLog.log(getClass(), Level.SEVERE, ex);
			}
		}
		if (serviceSideConnector != null) {
			try {
				connectionService.removeConnector(serviceSideConnector);
			} catch (Exception ex) {
				OseeLog.log(getClass(), Level.SEVERE, ex);
			}
		}
		service = null;
		brokerService = null;
	}

	private String getAddress() throws UnknownHostException {
		InetAddress[] all = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
		String defaultAddress = all[0].getHostAddress();
		for (InetAddress address : all ) {
		   if(!address.isSiteLocalAddress())
		   {
		      String firstRealLocalAddress = address.getHostAddress();
		      if (address instanceof Inet6Address) {
		         firstRealLocalAddress = "[" + firstRealLocalAddress + "]";
		      }
		      return firstRealLocalAddress;
		   }
		}
		return defaultAddress;
	}

	private class ListenForHostRequest extends OseeMessagingListener {

		@Override
		public void process(Object message, Map<String, Object> headers, ReplyConnection replyConnection) {
			if (replyConnection.isReplyRequested()) {
				try {
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					ObjectOutputStream oos = new ObjectOutputStream(baos);
					oos.writeObject(message);
					oos.writeObject(serviceSideConnector.getService());
					replyConnection.send(baos.toByteArray(), null, OteServiceStarterImpl.this);
				} catch (OseeCoreException ex) {
					OseeLog.log(getClass(), Level.SEVERE, ex);
				} catch (IOException ex) {
					OseeLog.log(getClass(), Level.SEVERE, ex);
				}
			}
		}
	}

	@Override
	public void updateServiceInfo(List<ServiceDescriptionPair> serviceDescription) {
		for (Entry<String, Serializable> entry : serviceSideConnector.getProperties().entrySet()) {
			ServiceDescriptionPair pair = new ServiceDescriptionPair();
			if (entry.getKey() != null && entry.getValue() != null) {
				pair.setName(entry.getKey());
				pair.setValue(entry.getValue().toString());
				serviceDescription.add(pair);
			}
		}
	}

	@Override
	public void fail(Throwable th) {
		OseeLog.log(getClass(), Level.SEVERE, th);
	}

	@Override
	public void success() {
	}

}

Back to the top