~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to timeline/Highstock-1.3.10/exporting-server/java/highcharts-export/highcharts-export-convert/src/main/java/com/highcharts/export/pool/ServerObjectFactory.java

  • Committer: Jeff Stys
  • Date: 2014-04-21 12:46:26 UTC
  • Revision ID: jstys@sesda3.com-20140421124626-2332pb2dyjc33jxi
Proof-of-concept version of Data Coverage Timeline using Highchart/Highstock javascript library.  Changes to getDataCoverage API in order to feed the necessary data to the Timeline

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.highcharts.export.pool;
 
2
 
 
3
import java.util.HashMap;
 
4
import java.util.Map;
 
5
 
 
6
import javax.annotation.PostConstruct;
 
7
 
 
8
import org.apache.log4j.Logger;
 
9
 
 
10
import com.highcharts.export.server.Server;
 
11
import com.highcharts.export.server.ServerState;
 
12
import com.highcharts.export.util.TempDir;
 
13
import java.io.File;
 
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;
 
26
 
 
27
public class ServerObjectFactory implements ObjectFactory<Server> {
 
28
 
 
29
        public String exec;
 
30
        public String script;
 
31
        private String host;
 
32
        private int basePort;
 
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");
 
38
 
 
39
        private enum PortStatus {
 
40
        BUSY,
 
41
        FREE;
 
42
        }
 
43
 
 
44
        @Override
 
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);
 
50
        }
 
51
 
 
52
        @Override
 
53
        public boolean validate(Server server) {
 
54
                boolean isValid = false;
 
55
                try {
 
56
                        if(server.getState() != ServerState.IDLE) {
 
57
                                logger.debug("server didn\'t pass validation");
 
58
                                return false;
 
59
                        }
 
60
                        String result = server.request("{\"status\":\"isok\"}");
 
61
                        if(result.indexOf("OK") > -1) {
 
62
                                isValid = true;
 
63
                                logger.debug("server passed validation");
 
64
                        } else {
 
65
                                logger.debug("server didn\'t pass validation");
 
66
                        }
 
67
                } catch (Exception e) {
 
68
                        logger.error("Error while validating object in Pool: " + e.getMessage());
 
69
                }
 
70
                return isValid;
 
71
        }
 
72
 
 
73
        @Override
 
74
        public void destroy(Server server) {
 
75
                ServerObjectFactory.releasePort(server.getPort());
 
76
                server.cleanup();
 
77
        }
 
78
 
 
79
        @Override
 
80
        public void activate(Server server) {
 
81
                server.setState(ServerState.ACTIVE);
 
82
        }
 
83
 
 
84
        @Override
 
85
        public void passivate(Server server) {
 
86
                server.setState(ServerState.IDLE);
 
87
        }
 
88
 
 
89
        public static void releasePort(Integer port) {
 
90
                logger.debug("Releasing port " + port);
 
91
                portUsage.put(port, PortStatus.FREE);
 
92
        }
 
93
 
 
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();
 
100
                   }
 
101
                }
 
102
                // if no port is free
 
103
                logger.debug("Nothing free in Portusage " + portUsage.toString());
 
104
                return basePort + portUsage.size();
 
105
        }
 
106
 
 
107
        /*Getters and Setters*/
 
108
 
 
109
        public String getExec() {
 
110
                return exec;
 
111
        }
 
112
 
 
113
        public void setExec(String exec) {
 
114
                this.exec = exec;
 
115
        }
 
116
 
 
117
        public String getScript() {
 
118
                return script;
 
119
        }
 
120
 
 
121
        public void setScript(String script) {
 
122
                this.script = script;
 
123
        }
 
124
 
 
125
        public String getHost() {
 
126
                return host;
 
127
        }
 
128
 
 
129
        public void setHost(String host) {
 
130
                this.host = host;
 
131
        }
 
132
 
 
133
        public int getBasePort() {
 
134
                return basePort;
 
135
        }
 
136
 
 
137
        public void setBasePort(int basePort) {
 
138
                this.basePort = basePort;
 
139
        }
 
140
 
 
141
        public int getReadTimeout() {
 
142
                return readTimeout;
 
143
        }
 
144
 
 
145
        public void setReadTimeout(int readTimeout) {
 
146
                this.readTimeout = readTimeout;
 
147
        }
 
148
 
 
149
        public int getConnectTimeout() {
 
150
                return connectTimeout;
 
151
        }
 
152
 
 
153
        public void setConnectTimeout(int connectTimeout) {
 
154
                this.connectTimeout = connectTimeout;
 
155
        }
 
156
 
 
157
        public int getMaxTimeout() {
 
158
                return maxTimeout;
 
159
        }
 
160
 
 
161
        public void setMaxTimeout(int maxTimeout) {
 
162
                this.maxTimeout = maxTimeout;
 
163
        }
 
164
 
 
165
        @PostConstruct
 
166
        public void afterBeanInit() {
 
167
                String jarLocation = getClass().getProtectionDomain().getCodeSource().getLocation().getPath().split("!")[0].replace("file:/", "");
 
168
                try {
 
169
                        jarLocation = URLDecoder.decode(jarLocation, "utf-8");
 
170
                        // get filesystem depend path
 
171
                        jarLocation = new File(jarLocation).getCanonicalPath();
 
172
                } catch (UnsupportedEncodingException ueex) {
 
173
                        logger.error(ueex);
 
174
                } catch (IOException ioex) {
 
175
                        logger.error(ioex);
 
176
                }
 
177
 
 
178
                try {
 
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);
 
187
                                        } else {
 
188
                                                File file = Files.createFile(path).toFile();
 
189
                                                InputStream in = jar.getInputStream(entry);
 
190
                                                IOUtils.copy(in, new FileOutputStream(file));
 
191
                                        }
 
192
                                }
 
193
                        }
 
194
                } catch (IOException ioex) {
 
195
                        logger.error(ioex);
 
196
                }
 
197
        }
 
198
 
 
199
 
 
200
 
 
201
}