aboutsummaryrefslogtreecommitdiff
path: root/subsonic-booter/src/main/java/net/sourceforge/subsonic/booter/deployer/SubsonicDeployer.java
blob: b066c6352f3cbb5c3d4b42360a9d1e5dc6bad247 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package net.sourceforge.subsonic.booter.deployer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.BindException;
import java.util.Date;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.apache.commons.io.IOUtils;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.Constraint;
import org.mortbay.jetty.security.ConstraintMapping;
import org.mortbay.jetty.security.SslSocketConnector;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

/**
 * Responsible for deploying the Subsonic web app in
 * the embedded Jetty container.
 * <p/>
 * The following system properties may be used to customize the behaviour:
 * <ul>
 * <li><code>subsonic.contextPath</code> - The context path at which Subsonic is deployed.  Default "/".</li>
 * <li><code>subsonic.port</code> - The port Subsonic will listen to.  Default 4040.</li>
 * <li><code>subsonic.httpsPort</code> - The port Subsonic will listen to for HTTPS.  Default 0, which disables HTTPS.</li>
 * <li><code>subsonic.war</code> - Subsonic WAR file, or exploded directory.  Default "subsonic.war".</li>
 * <li><code>subsonic.createLinkFile</code> - If set to "true", a Subsonic.url file is created in the working directory.</li>
 * <li><code>subsonic.ssl.keystore</code> - Path to an alternate SSL keystore.</li>
 * <li><code>subsonic.ssl.password</code> - Password of the alternate SSL keystore.</li>
 * </ul>
 *
 * @author Sindre Mehus
 */
public class SubsonicDeployer implements SubsonicDeployerService {

    public static final String DEFAULT_HOST = "0.0.0.0";
    public static final int DEFAULT_PORT = 4040;
    public static final int DEFAULT_HTTPS_PORT = 0;
    public static final int DEFAULT_MEMORY_LIMIT = 150;
    public static final String DEFAULT_CONTEXT_PATH = "/";
    public static final String DEFAULT_WAR = "subsonic.war";
    private static final int MAX_IDLE_TIME_MILLIS = 7 * 24 * 60 * 60 * 1000; // One week.
    private static final int HEADER_BUFFER_SIZE = 64 * 1024;

    // Subsonic home directory.
    private static final File SUBSONIC_HOME_WINDOWS = new File("c:/subsonic");
    private static final File SUBSONIC_HOME_OTHER = new File("/var/subsonic");

    private Throwable exception;
    private File subsonicHome;
    private final Date startTime;

    public SubsonicDeployer() {

        // Enable shutdown hook for Ehcache.
        System.setProperty("net.sf.ehcache.enableShutdownHook", "true");

        startTime = new Date();
        createLinkFile();
        deployWebApp();
    }

    private void createLinkFile() {
        if ("true".equals(System.getProperty("subsonic.createLinkFile"))) {
            Writer writer = null;
            try {
                writer = new FileWriter("subsonic.url");
                writer.append("[InternetShortcut]");
                writer.append(System.getProperty("line.separator"));
                writer.append("URL=").append(getUrl());
                writer.flush();
            } catch (Throwable x) {
                System.err.println("Failed to create subsonic.url.");
                x.printStackTrace();
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException x) {
                        // Ignored
                    }
                }
            }
        }
    }

    private void deployWebApp() {
        try {
            Server server = new Server();
            SelectChannelConnector connector = new SelectChannelConnector();
            connector.setMaxIdleTime(MAX_IDLE_TIME_MILLIS);
            connector.setHeaderBufferSize(HEADER_BUFFER_SIZE);
            connector.setHost(getHost());
            connector.setPort(getPort());
            if (isHttpsEnabled()) {
                connector.setConfidentialPort(getHttpsPort());
            }
            server.addConnector(connector);

            if (isHttpsEnabled()) {
                SslSocketConnector sslConnector = new SslSocketConnector();
                sslConnector.setMaxIdleTime(MAX_IDLE_TIME_MILLIS);
                sslConnector.setHeaderBufferSize(HEADER_BUFFER_SIZE);
                sslConnector.setHost(getHost());
                sslConnector.setPort(getHttpsPort());
                sslConnector.setKeystore(System.getProperty("subsonic.ssl.keystore", getClass().getResource("/subsonic.keystore").toExternalForm()));
                sslConnector.setPassword(System.getProperty("subsonic.ssl.password", "subsonic"));
                server.addConnector(sslConnector);
            }

            WebAppContext context = new WebAppContext();
            context.setTempDirectory(getJettyDirectory());
            context.setContextPath(getContextPath());
            context.setWar(getWar());

            if (isHttpsEnabled()) {
                ConstraintMapping constraintMapping = new ConstraintMapping();
                Constraint constraint = new Constraint();
                constraint.setDataConstraint(Constraint.DC_CONFIDENTIAL);
                constraintMapping.setPathSpec("/");
                constraintMapping.setConstraint(constraint);
                context.getSecurityHandler().setConstraintMappings(new ConstraintMapping[]{constraintMapping});
            } 

            server.addHandler(context);
            server.start();

            System.err.println("Subsonic running on: " + getUrl());
            if (isHttpsEnabled()) {
                System.err.println("                and: " + getHttpsUrl());
            }

        } catch (Throwable x) {
            x.printStackTrace();
            exception = x;
        }
    }

    private File getJettyDirectory() {
        File dir = new File(getSubsonicHome(), "jetty");
        String buildNumber = getSubsonicBuildNumber();
        if (buildNumber != null) {
            dir = new File(dir, buildNumber);
        }
        System.err.println("Extracting webapp to " + dir);

        if (!dir.exists() && !dir.mkdirs()) {
            System.err.println("Failed to create directory " + dir);
        }

        return dir;
    }

    private String getSubsonicBuildNumber() {
        File war = new File(getWar());
        InputStream in = null;
        try {
            if (war.isFile()) {
                JarFile jar = new JarFile(war);
                ZipEntry entry = jar.getEntry("WEB-INF\\classes\\build_number.txt");
                if (entry == null) {
                    entry = jar.getEntry("WEB-INF/classes/build_number.txt");
                }
                in = jar.getInputStream(entry);
            } else {
                in = new FileInputStream(war.getPath() + "/WEB-INF/classes/build_number.txt");
            }
            return IOUtils.toString(in);

        } catch (Exception x) {
            System.err.println("Failed to resolve build number from WAR: " + war);
            x.printStackTrace();
            return null;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    private String getContextPath() {
        return System.getProperty("subsonic.contextPath", DEFAULT_CONTEXT_PATH);
    }


    private String getWar() {
        String war = System.getProperty("subsonic.war");
        if (war == null) {
            war = DEFAULT_WAR;
        }

        File file = new File(war);
        if (file.exists()) {
            System.err.println("Using WAR file: " + file.getAbsolutePath());
        } else {
            System.err.println("Error: WAR file not found: " + file.getAbsolutePath());
        }

        return war;
    }

    private String getHost() {
        return System.getProperty("subsonic.host", DEFAULT_HOST);
    }

    private int getPort() {
        int port = DEFAULT_PORT;

        String portString = System.getProperty("subsonic.port");
        if (portString != null) {
            port = Integer.parseInt(portString);
        }

        // Also set it so that the webapp can read it.
        System.setProperty("subsonic.port", String.valueOf(port));

        return port;
    }

    private int getHttpsPort() {
        int port = DEFAULT_HTTPS_PORT;

        String portString = System.getProperty("subsonic.httpsPort");
        if (portString != null) {
            port = Integer.parseInt(portString);
        }

        // Also set it so that the webapp can read it.
        System.setProperty("subsonic.httpsPort", String.valueOf(port));

        return port;
    }

    private boolean isHttpsEnabled() {
        return getHttpsPort() > 0;
    }

    public String getErrorMessage() {
        if (exception == null) {
            return null;
        }
        if (exception instanceof BindException) {
            return "Address already in use. Please change port number.";
        }

        return exception.toString();
    }

    public int getMemoryUsed() {
        long freeBytes = Runtime.getRuntime().freeMemory();
        long totalBytes = Runtime.getRuntime().totalMemory();
        long usedBytes = totalBytes - freeBytes;
        return (int) Math.round(usedBytes / 1024.0 / 1024.0);
    }

    private String getUrl() {
        String host = DEFAULT_HOST.equals(getHost()) ? "localhost" : getHost();
        StringBuffer url = new StringBuffer("http://").append(host);
        if (getPort() != 80) {
            url.append(":").append(getPort());
        }
        url.append(getContextPath());
        return url.toString();
    }

    private String getHttpsUrl() {
        if (!isHttpsEnabled()) {
            return null;
        }

        String host = DEFAULT_HOST.equals(getHost()) ? "localhost" : getHost();
        StringBuffer url = new StringBuffer("https://").append(host);
        if (getHttpsPort() != 443) {
            url.append(":").append(getHttpsPort());
        }
        url.append(getContextPath());
        return url.toString();
    }

    /**
     * Returns the Subsonic home directory.
     *
     * @return The Subsonic home directory, if it exists.
     * @throws RuntimeException If directory doesn't exist.
     */
    private File getSubsonicHome() {

        if (subsonicHome != null) {
            return subsonicHome;
        }

        File home;

        String overrideHome = System.getProperty("subsonic.home");
        if (overrideHome != null) {
            home = new File(overrideHome);
        } else {
            boolean isWindows = System.getProperty("os.name", "Windows").toLowerCase().startsWith("windows");
            home = isWindows ? SUBSONIC_HOME_WINDOWS : SUBSONIC_HOME_OTHER;
        }

        // Attempt to create home directory if it doesn't exist.
        if (!home.exists() || !home.isDirectory()) {
            boolean success = home.mkdirs();
            if (success) {
                subsonicHome = home;
            } else {
                String message = "The directory " + home + " does not exist. Please create it and make it writable. " +
                        "(You can override the directory location by specifying -Dsubsonic.home=... when " +
                        "starting the servlet container.)";
                System.err.println("ERROR: " + message);
            }
        } else {
            subsonicHome = home;
        }

        return home;
    }

    public DeploymentStatus getDeploymentInfo() {
        return new DeploymentStatus(startTime, getUrl(), getHttpsUrl(), getMemoryUsed(), getErrorMessage());
    }
}