forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterServiceServletAdapter.java
More file actions
148 lines (120 loc) · 5.07 KB
/
ClusterServiceServletAdapter.java
File metadata and controls
148 lines (120 loc) · 5.07 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.cluster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.Properties;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.cluster.dao.ManagementServerHostDao;
import com.cloud.configuration.Config;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.PropertiesUtil;
import com.cloud.utils.component.AdapterBase;
@Component
@Local(value={ClusterServiceAdapter.class})
public class ClusterServiceServletAdapter extends AdapterBase implements ClusterServiceAdapter {
private static final Logger s_logger = Logger.getLogger(ClusterServiceServletAdapter.class);
private static final int DEFAULT_SERVICE_PORT = 9090;
private static final int DEFAULT_REQUEST_TIMEOUT = 300; // 300 seconds
@Inject private ClusterManager _manager;
@Inject private ManagementServerHostDao _mshostDao;
@Inject private ConfigurationDao _configDao;
private ClusterServiceServletContainer _servletContainer;
private int _clusterServicePort = DEFAULT_SERVICE_PORT;
private int _clusterRequestTimeoutSeconds = DEFAULT_REQUEST_TIMEOUT;
@Override
public ClusterService getPeerService(String strPeer) throws RemoteException {
try {
init();
} catch (ConfigurationException e) {
s_logger.error("Unable to init ClusterServiceServletAdapter");
throw new RemoteException("Unable to init ClusterServiceServletAdapter");
}
String serviceUrl = getServiceEndpointName(strPeer);
if(serviceUrl == null)
return null;
return new ClusterServiceServletImpl(serviceUrl, _clusterRequestTimeoutSeconds);
}
@Override
public String getServiceEndpointName(String strPeer) {
try {
init();
} catch (ConfigurationException e) {
s_logger.error("Unable to init ClusterServiceServletAdapter");
return null;
}
long msid = Long.parseLong(strPeer);
ManagementServerHostVO mshost = _mshostDao.findByMsid(msid);
if(mshost == null)
return null;
return composeEndpointName(mshost.getServiceIP(), mshost.getServicePort());
}
@Override
public int getServicePort() {
return _clusterServicePort;
}
private String composeEndpointName(String nodeIP, int port) {
StringBuffer sb = new StringBuffer();
sb.append("http://").append(nodeIP).append(":").append(port).append("/clusterservice");
return sb.toString();
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
init();
return true;
}
@Override
public boolean start() {
_servletContainer = new ClusterServiceServletContainer();
_servletContainer.start(new ClusterServiceServletHttpHandler(_manager), _clusterServicePort);
return true;
}
@Override
public boolean stop() {
if(_servletContainer != null)
_servletContainer.stop();
return true;
}
private void init() throws ConfigurationException {
if(_mshostDao != null)
return;
String value = _configDao.getValue(Config.ClusterMessageTimeOutSeconds.key());
_clusterRequestTimeoutSeconds = NumbersUtil.parseInt(value, DEFAULT_REQUEST_TIMEOUT);
s_logger.info("Configure cluster request time out. timeout: " + _clusterRequestTimeoutSeconds + " seconds");
File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(new FileInputStream(dbPropsFile));
} catch (FileNotFoundException e) {
throw new ConfigurationException("Unable to find db.properties");
} catch (IOException e) {
throw new ConfigurationException("Unable to load db.properties content");
}
_clusterServicePort = NumbersUtil.parseInt(dbProps.getProperty("cluster.servlet.port"), DEFAULT_SERVICE_PORT);
if(s_logger.isInfoEnabled())
s_logger.info("Cluster servlet port : " + _clusterServicePort);
}
}