KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > appserver > ConcreteReadOnlyAppServerInstallation


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test.server.appserver;
5
6 import com.tc.util.ZipBuilder;
7 import com.tc.util.runtime.Os;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileReader JavaDoc;
12 import java.io.FileWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.LineNumberReader JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLConnection JavaDoc;
18 import java.text.DateFormat JavaDoc;
19 import java.util.Date JavaDoc;
20
21 /**
22  * This class manages the installation of the read-only portion of a running appserver. A timestamp is used to cache the
23  * installation for future reference. This portion constitutes the majority of an appserver's filesize. This class
24  * should not be referenced by any class other than {@link AbstractAppServerInstallation}.
25  */

26 final class ConcreteReadOnlyAppServerInstallation {
27
28   private static final DateFormat JavaDoc df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
29
30   static File JavaDoc create(URL JavaDoc host, File JavaDoc serverDir, String JavaDoc serverType, String JavaDoc majorVersion, String JavaDoc minorVersion) throws Exception JavaDoc {
31     File JavaDoc serverInstallDir = new File JavaDoc(serverDir + File.separator + serverType + "-" + majorVersion + "." + minorVersion + "-install");
32     serverInstallDir.mkdir();
33     File JavaDoc timestampFile = new File JavaDoc(serverInstallDir + File.separator + "timestamp");
34     if (equalTimestamps(host, serverType, majorVersion, minorVersion, timestampFile)) { return serverInstallDir; }
35     File JavaDoc deleteServer = new File JavaDoc(serverInstallDir + File.separator + serverType + "-" + majorVersion + "." + minorVersion);
36     if (deleteServer.exists()) {
37       deleteServer.delete();
38     }
39     BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(appendPath(host, serverType, majorVersion, minorVersion).openStream());
40     ZipBuilder.unzip(in, serverInstallDir);
41     in.close();
42     return serverInstallDir;
43   }
44
45   private static boolean equalTimestamps(URL JavaDoc host, String JavaDoc serverType, String JavaDoc majorVersion, String JavaDoc minorVersion, File JavaDoc timestampFile) throws Exception JavaDoc {
46     URLConnection JavaDoc conn = appendPath(host, serverType, majorVersion, minorVersion).openConnection();
47     System.out.println("**Connection=" + host);
48     System.out.println("**Connection=" + conn);
49     long modified = conn.getLastModified();
50     if (!timestampFile.exists()) {
51       writeTimestamp(modified, timestampFile);
52       return false;
53     }
54     LineNumberReader JavaDoc in = new LineNumberReader JavaDoc(new FileReader JavaDoc(timestampFile));
55     String JavaDoc serverStamp = in.readLine();
56     in.close();
57     if (df.parse(serverStamp).getTime() != df.parse(df.format(new Date JavaDoc(modified))).getTime()) {
58       writeTimestamp(modified, timestampFile);
59       return false;
60     }
61     return true;
62   }
63
64   private static void writeTimestamp(long timestamp, File JavaDoc timestampFile) throws IOException JavaDoc {
65     PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(timestampFile));
66     out.println(df.format(new Date JavaDoc(timestamp)));
67     out.close();
68   }
69
70   private static String JavaDoc resolvePlatform() {
71     return Os.platform();
72   }
73
74   private static URL JavaDoc appendPath(URL JavaDoc host, String JavaDoc serverType, String JavaDoc majorVersion, String JavaDoc minorVersion) throws Exception JavaDoc {
75     String JavaDoc baseUrl = host.toExternalForm();
76     String JavaDoc appendedPath = serverType + "/" + resolvePlatform() + "/" + serverType.toLowerCase() + "-" + majorVersion.toLowerCase()
77                           + "." + minorVersion.toLowerCase() + ".zip";
78     return new URL JavaDoc(baseUrl + "/" + appendedPath);
79   }
80 }
81
Popular Tags