/
ServerPortProvider.java
126 lines (103 loc) · 4.6 KB
/
ServerPortProvider.java
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
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* 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:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.command.valueproviders;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import org.eclipse.che.api.machine.gwt.client.MachineServiceClient;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.machine.shared.dto.ServerDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStartedEvent;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStartedHandler;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStoppedEvent;
import org.eclipse.che.api.workspace.gwt.client.event.WorkspaceStoppedHandler;
import org.eclipse.che.api.workspace.shared.dto.UsersWorkspaceDto;
import org.eclipse.che.ide.api.app.AppContext;
import java.util.Map;
import java.util.Set;
/**
* Provide mapping internal port, i.e. ${server.port.8080} to 127.0.0.1:21212.
*
* @author Vlad Zhukovskiy
*/
@Singleton
public class ServerPortProvider implements WorkspaceStartedHandler, WorkspaceStoppedHandler {
public static final String KEY_TEMPLATE = "${server.port.%}";
private final MachineServiceClient machineServiceClient;
private final CommandPropertyValueProviderRegistry commandPropertyRegistry;
private final AppContext appContext;
private Set<CommandPropertyValueProvider> providers;
private final Operation<MachineDto> registerProviders = new Operation<MachineDto>() {
@Override
public void apply(MachineDto machine) throws OperationException {
providers = getProviders(machine);
commandPropertyRegistry.register(providers);
}
};
@Inject
public ServerPortProvider(EventBus eventBus,
MachineServiceClient machineServiceClient,
CommandPropertyValueProviderRegistry commandPropertyRegistry,
AppContext appContext) {
this.machineServiceClient = machineServiceClient;
this.commandPropertyRegistry = commandPropertyRegistry;
this.appContext = appContext;
eventBus.addHandler(WorkspaceStartedEvent.TYPE, this);
eventBus.addHandler(WorkspaceStoppedEvent.TYPE, this);
registerProviders();
}
private void registerProviders() {
String devMachineId = appContext.getDevMachineId();
if (devMachineId != null) {
machineServiceClient.getMachine(devMachineId).then(registerProviders);
}
}
private Set<CommandPropertyValueProvider> getProviders(MachineDto machine) {
Set<CommandPropertyValueProvider> providers = Sets.newHashSet();
for (Map.Entry<String, ServerDto> entry : machine.getRuntime().getServers().entrySet()) {
providers.add(new AddressProvider(entry.getKey(), entry.getValue().getAddress()));
if (entry.getKey().endsWith("/tcp")) {
providers.add(new AddressProvider(entry.getKey().substring(0, entry.getKey().length() - 4), entry.getValue().getAddress()));
}
}
return providers;
}
@Override
public void onWorkspaceStarted(UsersWorkspaceDto workspace) {
registerProviders();
}
@Override
public void onWorkspaceStopped(WorkspaceStoppedEvent event) {
for (CommandPropertyValueProvider provider : providers) {
commandPropertyRegistry.unregister(provider);
}
providers.clear();
}
private class AddressProvider implements CommandPropertyValueProvider {
String variable;
String address;
AddressProvider(String internalPort, String address) {
this.variable = KEY_TEMPLATE.replaceAll("%", internalPort);
this.address = address;
}
@Override
public String getKey() {
return variable;
}
@Override
public String getValue() {
return address;
}
}
}