1
package com.highcharts.export.pool;
3
import java.util.HashMap;
6
import javax.annotation.PostConstruct;
8
import org.apache.log4j.Logger;
10
import com.highcharts.export.server.Server;
11
import com.highcharts.export.server.ServerState;
12
import com.highcharts.export.util.TempDir;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.UnsupportedEncodingException;
18
import java.net.URLDecoder;
19
import java.nio.file.Files;
20
import java.nio.file.Path;
21
import java.nio.file.Paths;
22
import java.util.Enumeration;
23
import java.util.jar.JarEntry;
24
import java.util.jar.JarFile;
25
import org.apache.commons.io.IOUtils;
27
public class ServerObjectFactory implements ObjectFactory<Server> {
33
private int readTimeout;
34
private int connectTimeout;
35
private int maxTimeout;
36
private static HashMap<Integer, PortStatus> portUsage = new HashMap<Integer, PortStatus>();
37
protected static Logger logger = Logger.getLogger("pool");
39
private enum PortStatus {
45
public Server create() {
46
logger.debug("in makeObject, " + exec + ", " + script + ", " + host);
47
Integer port = this.getAvailablePort();
48
portUsage.put(port, PortStatus.BUSY);
49
return new Server(exec, script, host, port, connectTimeout, readTimeout, maxTimeout);
53
public boolean validate(Server server) {
54
boolean isValid = false;
56
if(server.getState() != ServerState.IDLE) {
57
logger.debug("server didn\'t pass validation");
60
String result = server.request("{\"status\":\"isok\"}");
61
if(result.indexOf("OK") > -1) {
63
logger.debug("server passed validation");
65
logger.debug("server didn\'t pass validation");
67
} catch (Exception e) {
68
logger.error("Error while validating object in Pool: " + e.getMessage());
74
public void destroy(Server server) {
75
ServerObjectFactory.releasePort(server.getPort());
80
public void activate(Server server) {
81
server.setState(ServerState.ACTIVE);
85
public void passivate(Server server) {
86
server.setState(ServerState.IDLE);
89
public static void releasePort(Integer port) {
90
logger.debug("Releasing port " + port);
91
portUsage.put(port, PortStatus.FREE);
94
public Integer getAvailablePort() {
95
for (Map.Entry<Integer, PortStatus> entry : portUsage.entrySet()) {
96
if (PortStatus.FREE == entry.getValue()) {
97
// return available port
98
logger.debug("Portusage " + portUsage.toString());
99
return entry.getKey();
102
// if no port is free
103
logger.debug("Nothing free in Portusage " + portUsage.toString());
104
return basePort + portUsage.size();
107
/*Getters and Setters*/
109
public String getExec() {
113
public void setExec(String exec) {
117
public String getScript() {
121
public void setScript(String script) {
122
this.script = script;
125
public String getHost() {
129
public void setHost(String host) {
133
public int getBasePort() {
137
public void setBasePort(int basePort) {
138
this.basePort = basePort;
141
public int getReadTimeout() {
145
public void setReadTimeout(int readTimeout) {
146
this.readTimeout = readTimeout;
149
public int getConnectTimeout() {
150
return connectTimeout;
153
public void setConnectTimeout(int connectTimeout) {
154
this.connectTimeout = connectTimeout;
157
public int getMaxTimeout() {
161
public void setMaxTimeout(int maxTimeout) {
162
this.maxTimeout = maxTimeout;
166
public void afterBeanInit() {
167
String jarLocation = getClass().getProtectionDomain().getCodeSource().getLocation().getPath().split("!")[0].replace("file:/", "");
169
jarLocation = URLDecoder.decode(jarLocation, "utf-8");
170
// get filesystem depend path
171
jarLocation = new File(jarLocation).getCanonicalPath();
172
} catch (UnsupportedEncodingException ueex) {
174
} catch (IOException ioex) {
179
JarFile jar = new JarFile(jarLocation);
180
for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
181
JarEntry entry = entries.nextElement();
182
String name = entry.getName();
183
if (name.startsWith("phantomjs/")) {
184
Path path = Paths.get(TempDir.getTmpDir().toString(), name);
185
if (name.endsWith("/")) {
186
Files.createDirectories(path);
188
File file = Files.createFile(path).toFile();
189
InputStream in = jar.getInputStream(entry);
190
IOUtils.copy(in, new FileOutputStream(file));
194
} catch (IOException ioex) {