Merge branch 'master' of git@git.labs.intellij.net:idea/community into tool-window
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / XmlRpcServerImpl.java
blobc3250f103fd645a750e89d28f00d74d18d2dc897
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.components.ApplicationComponent;
20 import com.intellij.openapi.diagnostic.Logger;
21 import org.apache.xmlrpc.IdeaAwareWebServer;
22 import org.apache.xmlrpc.IdeaAwareXmlRpcServer;
23 import org.apache.xmlrpc.WebServer;
24 import org.jetbrains.annotations.NonNls;
25 import org.jetbrains.annotations.NotNull;
27 import java.io.IOException;
28 import java.net.BindException;
29 import java.net.ServerSocket;
31 /**
32 * @author mike
34 public class XmlRpcServerImpl implements XmlRpcServer, ApplicationComponent {
35 private static final Logger LOG = Logger.getInstance("#com.intellij.ide.XmlRpcServerImpl");
36 private static final int FIRST_PORT_NUMBER = 63342;
37 private static final int PORTS_COUNT = 20;
38 public static int detectedPortNumber = -1;
39 private WebServer myWebServer;
40 @NonNls private static final String PROPERTY_RPC_PORT = "rpc.port";
42 @NotNull
43 @NonNls
44 public String getComponentName() {
45 return "XmlRpcServer";
48 public void initComponent() {
49 if (ApplicationManager.getApplication().isUnitTestMode() || !checkPort()) return;
50 final Thread thread = Thread.currentThread();
51 final int currentPriority = thread.getPriority();
52 try {
53 thread.setPriority(Thread.NORM_PRIORITY - 2);
54 myWebServer = new IdeaAwareWebServer(getPortNumber(), null, new IdeaAwareXmlRpcServer());
55 myWebServer.start();
57 catch (Exception e) {
58 LOG.error(e);
59 myWebServer = null;
61 finally {
62 thread.setPriority(currentPriority);
66 public int getPortNumber() {
67 return detectedPortNumber == -1 ? getDefaultPort() : detectedPortNumber;
70 private static int getDefaultPort() {
71 if (System.getProperty(PROPERTY_RPC_PORT) != null) return Integer.parseInt(System.getProperty(PROPERTY_RPC_PORT));
72 return FIRST_PORT_NUMBER;
75 private static boolean checkPort() {
76 ServerSocket socket = null;
77 try {
78 final int firstPort = getDefaultPort();
79 for (int i = 0; i < PORTS_COUNT; i++) {
80 int port = firstPort + i;
81 try {
82 socket = new ServerSocket(port);
83 detectedPortNumber = port;
84 return true;
86 catch (BindException ignored) {
90 try {
91 // try any port
92 socket = new ServerSocket(0);
93 detectedPortNumber = socket.getLocalPort();
94 return true;
95 } catch (BindException ignored) {
98 catch (IOException e) {
99 LOG.info(e);
101 finally {
102 if (socket != null) {
103 try {
104 socket.close();
106 catch (IOException e) {
107 LOG.error(e);
111 return false;
114 public void disposeComponent() {
115 if (myWebServer != null) {
116 myWebServer.shutdown();
120 public void addHandler(String name, Object handler) {
121 if (myWebServer != null) {
122 myWebServer.addHandler(name, handler);
124 else {
125 LOG.info("Handler not registered because XML-RPC server is not running");
129 public void removeHandler(String name) {
130 if (myWebServer != null) {
131 myWebServer.removeHandler(name);