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
;
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";
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();
53 thread
.setPriority(Thread
.NORM_PRIORITY
- 2);
54 myWebServer
= new IdeaAwareWebServer(getPortNumber(), null, new IdeaAwareXmlRpcServer());
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;
78 final int firstPort
= getDefaultPort();
79 for (int i
= 0; i
< PORTS_COUNT
; i
++) {
80 int port
= firstPort
+ i
;
82 socket
= new ServerSocket(port
);
83 detectedPortNumber
= port
;
86 catch (BindException ignored
) {
92 socket
= new ServerSocket(0);
93 detectedPortNumber
= socket
.getLocalPort();
95 } catch (BindException ignored
) {
98 catch (IOException e
) {
102 if (socket
!= null) {
106 catch (IOException e
) {
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
);
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
);