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

import java.io.File;
import java.io.Serializable;
import java.net.URI;
import java.net.UnknownHostException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import net.jini.export.Exporter;
import net.jini.jeri.BasicILFactory;
import net.jini.jeri.BasicJeriExporter;
import net.jini.jeri.tcp.TcpServerEndpoint;

import org.eclipse.osee.connection.service.IServiceConnector;
import org.eclipse.osee.connection.service.IServicePropertyChangeListener;
import org.eclipse.osee.framework.jdk.core.util.EnhancedProperties;
import org.eclipse.osee.framework.jdk.core.util.Network;
import org.eclipse.osee.framework.plugin.core.util.ExportClassLoader;
import org.eclipse.osee.ote.core.environment.interfaces.IHostTestEnvironment;

/**
 * @author Andrew M. Finkbeiner
 */
public class JmsToJiniBridgeConnector implements IServiceConnector {
   public static final String TYPE = "jmstojini";
   private static final class ExportInfo {
      private final Exporter exporter;
      private final Object exportedObject;

      private ExportInfo(Exporter exporter, Object exportedObject) {
         this.exportedObject = exportedObject;
         this.exporter = exporter;
      }
   }

   private EnhancedProperties properties;
   private final HashMap<Object, ExportInfo> exports = new HashMap<Object, ExportInfo>();
   private final ExportClassLoader exportClassLoader;
   private final Object service;
   private final List<IServicePropertyChangeListener> propertyChangeListeners =
      new CopyOnWriteArrayList<IServicePropertyChangeListener>();
   private final String uniqueServerId;
   private boolean connected = false;

   public JmsToJiniBridgeConnector(ExportClassLoader exportClassLoader, Object service, String id) {
      this.uniqueServerId = id;
      this.exportClassLoader = exportClassLoader;
      this.service = service;
      if (service instanceof IHostTestEnvironment) {
         try {
            this.properties = ((IHostTestEnvironment) service).getProperties();
         } catch (RemoteException ex) {
            this.properties = new EnhancedProperties();
            ex.printStackTrace();
         }
      } else {
         this.properties = new EnhancedProperties();
      }
   }

   @Override
   public Object export(Object callback) throws ExportException {
      try {
         Exporter exporter = createExporter();
         Object exportedObject = exporter.export((Remote) callback);
         exports.put(callback, new ExportInfo(exporter, exportedObject));
         return exportedObject;
      } catch (UnknownHostException e) {
         throw new ExportException("failed to export", e);
      }
   }

   @Override
   public void unexport(Object callback) throws Exception {
      ExportInfo info = exports.remove(callback);
      if (info != null) {
         info.exporter.unexport(false);
      }
   }

   @Override
   public Object findExport(Object callback) {
      ExportInfo info = exports.get(callback);
      if (info != null) {
         return info.exportedObject;
      }
      return null;
   }

   @Override
   public void stop() throws Exception {
      for (ExportInfo info : exports.values()) {
         info.exporter.unexport(false);
      }
      exports.clear();
   }

   private Exporter createExporter() throws UnknownHostException {
      return new BasicJeriExporter(TcpServerEndpoint.getInstance(Network.getValidIP().getHostAddress(), 0),
         new BasicILFactory(null, null, exportClassLoader), false, false);
   }

   @Override
   public String getConnectorType() {
      return TYPE;
   }

   @Override
   public Serializable getProperty(String property, Serializable defaultValue) {
      return properties.getProperty(property, defaultValue);
   }

   @Override
   public Object getService() {
      return service;
   }

   @Override
   public boolean ping() {
      if (getService() != null && getService() instanceof IHostTestEnvironment) {
         try {
            ((IHostTestEnvironment) getService()).getProperties();
            return true;
         } catch (Throwable th) {
            return false;
         }
      }
      return false;
   }

   @Override
   public void addPropertyChangeListener(IServicePropertyChangeListener listener) {
      propertyChangeListeners.add(listener);
   }

   @Override
   public void removePropertyChangeListener(IServicePropertyChangeListener listener) {
      propertyChangeListeners.remove(listener);
   }

   @Override
   public void setProperty(String key, Serializable value) {
      properties.setProperty(key, value);
      for (IServicePropertyChangeListener listener : propertyChangeListeners) {
         listener.propertyChanged(this, key, value);
      }
   }

   @Override
   public URI upload(File file) throws Exception {
      return null;
   }

   @Override
   public String getUniqueServerId() {
      return uniqueServerId;
   }

   @Override
   public EnhancedProperties getProperties() {
      return properties;
   }

   @Override
   public void init(Object service) throws UnknownHostException, ExportException {

   }
   
   public void setConnected(boolean connected){
      this.connected = connected;
   }
   
   public boolean isConnected() {
      return this.connected;
   }

}

Back to the top