7f3fcc7811ab144537220c517ba7e376727378e2
[idea/community.git] / platform / built-in-server / src / org / jetbrains / builtInWebServer / BuiltInServerOptions.java
1 package org.jetbrains.builtInWebServer;
2
3 import com.intellij.notification.NotificationType;
4 import com.intellij.openapi.application.ApplicationNamesInfo;
5 import com.intellij.openapi.components.PersistentStateComponent;
6 import com.intellij.openapi.components.ServiceManager;
7 import com.intellij.openapi.components.State;
8 import com.intellij.openapi.components.Storage;
9 import com.intellij.openapi.options.Configurable;
10 import com.intellij.openapi.options.SimpleConfigurable;
11 import com.intellij.openapi.util.Getter;
12 import com.intellij.util.xmlb.XmlSerializerUtil;
13 import com.intellij.util.xmlb.annotations.Attribute;
14 import com.intellij.xdebugger.settings.DebuggerConfigurableProvider;
15 import com.intellij.xdebugger.settings.DebuggerSettingsCategory;
16 import com.intellij.xml.XmlBundle;
17 import org.jetbrains.annotations.NotNull;
18 import org.jetbrains.annotations.Nullable;
19 import org.jetbrains.ide.BuiltInServerManager;
20 import org.jetbrains.ide.BuiltInServerManagerImpl;
21 import org.jetbrains.ide.CustomPortServerManager;
22 import org.jetbrains.io.CustomPortServerManagerBase;
23
24 import java.util.Collection;
25 import java.util.Collections;
26
27 @State(
28   name = "BuiltInServerOptions",
29   storages = @Storage("other.xml")
30 )
31 public class BuiltInServerOptions implements PersistentStateComponent<BuiltInServerOptions>, Getter<BuiltInServerOptions> {
32   private static final int DEFAULT_PORT = 63342;
33
34   @Attribute
35   public int builtInServerPort = DEFAULT_PORT;
36   @Attribute
37   public boolean builtInServerAvailableExternally = false;
38
39   public static BuiltInServerOptions getInstance() {
40     return ServiceManager.getService(BuiltInServerOptions.class);
41   }
42
43   @Override
44   public BuiltInServerOptions get() {
45     return this;
46   }
47
48   static final class BuiltInServerDebuggerConfigurableProvider extends DebuggerConfigurableProvider {
49     @NotNull
50     @Override
51     public Collection<? extends Configurable> getConfigurables(@NotNull DebuggerSettingsCategory category) {
52       if (category == DebuggerSettingsCategory.GENERAL) {
53         return Collections.singletonList(SimpleConfigurable.create("builtInServer", XmlBundle
54           .message("setting.builtin.server.category.label"), BuiltInServerConfigurableUi.class, getInstance()));
55       }
56       return Collections.emptyList();
57     }
58   }
59
60   @Nullable
61   @Override
62   public BuiltInServerOptions getState() {
63     return this;
64   }
65
66   @Override
67   public void loadState(BuiltInServerOptions state) {
68     XmlSerializerUtil.copyBean(state, this);
69   }
70
71   public int getEffectiveBuiltInServerPort() {
72     MyCustomPortServerManager portServerManager = CustomPortServerManager.EP_NAME.findExtension(MyCustomPortServerManager.class);
73     if (!portServerManager.isBound()) {
74       return BuiltInServerManager.getInstance().getPort();
75     }
76     return builtInServerPort;
77   }
78
79   public static final class MyCustomPortServerManager extends CustomPortServerManagerBase {
80     @Override
81     public void cannotBind(Exception e, int port) {
82       BuiltInServerManagerImpl.NOTIFICATION_GROUP.getValue().createNotification("Cannot start built-in HTTP server on custom port " +
83                                                                                 port + ". " +
84                                                                                 "Please ensure that port is free (or check your firewall settings) and restart " +
85                                                                                 ApplicationNamesInfo.getInstance().getFullProductName(),
86                                                                                 NotificationType.ERROR).notify(null);
87     }
88
89     @Override
90     public int getPort() {
91       int port = getInstance().builtInServerPort;
92       return port == DEFAULT_PORT ? -1 : port;
93     }
94
95     @Override
96     public boolean isAvailableExternally() {
97       return getInstance().builtInServerAvailableExternally;
98     }
99   }
100
101   public static void onBuiltInServerPortChanged() {
102     CustomPortServerManager.EP_NAME.findExtension(MyCustomPortServerManager.class).portChanged();
103   }
104 }