001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.servers;
005:
006: import com.tc.SessionIntegrator;
007: import com.tc.SessionIntegratorContext;
008: import com.tc.SessionIntegratorFrame;
009:
010: import java.io.File;
011: import java.io.FileInputStream;
012: import java.io.IOException;
013: import java.util.Arrays;
014: import java.util.Properties;
015: import java.util.prefs.Preferences;
016:
017: public class ServerSelection {
018: private ServerInfo[] m_serverInfos;
019: private int m_selectedServerIndex;
020:
021: // CDV-300: Disable 'was6.1' for the time being; configurator support for WebSphere is untested in a non-trivial environment
022: private static final String[] WEB_SERVERS = { "tomcat5.5",
023: "tomcat5.0", "tomcat6.0", "wls8.1", "wls9.2" /*, "was6.1"*/};
024: private static final String SERVER_SELECTION_PREF_KEY = "SessionIntegrator.ServerSelection";
025: private static final String SELECTED_SERVER_PREF_KEY = "SelectedServer";
026: private static final String SERVERS_PREF_KEY = "Servers";
027: private static final String SERVER_PROPS_FILENAME = "server.properties";
028: private static final String SERVER_ENV_FILENAME = "server.environment";
029: private static final String WEBAPP_PROPS_FILENAME = "webapps.properties";
030: private static final String DEFAULT_SERVER_NAME = WEB_SERVERS[0];
031:
032: private static ServerSelection m_instance;
033:
034: private ServerSelection() {
035: String sandBoxRoot = SessionIntegratorFrame.getSandBoxRoot();
036: int serverCount = WEB_SERVERS.length;
037: Preferences prefs = getPreferences();
038: Preferences serversPrefs = prefs.node(SERVERS_PREF_KEY);
039: Preferences serverPrefs;
040:
041: m_serverInfos = new ServerInfo[serverCount];
042:
043: for (int i = 0; i < serverCount; i++) {
044: File serverDir = new File(sandBoxRoot, WEB_SERVERS[i]);
045: File propsFile = new File(serverDir, SERVER_PROPS_FILENAME);
046: File envFile = new File(serverDir, SERVER_ENV_FILENAME);
047: Properties props = new Properties();
048: Properties env = new Properties();
049:
050: try {
051: props.load(new FileInputStream(propsFile));
052: env.load(new FileInputStream(envFile));
053:
054: m_serverInfos[i] = new ServerInfo(props, env);
055: serverPrefs = serversPrefs.node(m_serverInfos[i]
056: .getName());
057:
058: m_serverInfos[i].loadEnvironment(serverPrefs);
059: } catch (IOException ioe) {/**/
060: }
061: }
062:
063: String selectedServerName = prefs.get(SELECTED_SERVER_PREF_KEY,
064: DEFAULT_SERVER_NAME);
065: if ((m_selectedServerIndex = Arrays.asList(WEB_SERVERS)
066: .indexOf(selectedServerName)) == -1) {
067: m_selectedServerIndex = 0;
068: }
069: }
070:
071: public Properties getDefaultProperties(int i) {
072: String sandBoxRoot = SessionIntegratorFrame.getSandBoxRoot();
073: File serverDir = new File(sandBoxRoot, WEB_SERVERS[i]);
074: File envFile = new File(serverDir, SERVER_ENV_FILENAME);
075: Properties env = new Properties();
076:
077: try {
078: env.load(new FileInputStream(envFile));
079: } catch (IOException ioe) {/**/
080: }
081:
082: return env;
083: }
084:
085: public static ServerSelection getInstance() {
086: if (m_instance == null) {
087: m_instance = new ServerSelection();
088: }
089: return m_instance;
090: }
091:
092: public ServerInfo[] getServers() {
093: return m_serverInfos;
094: }
095:
096: public void setServers(ServerInfo[] servers) {
097: m_serverInfos = servers;
098: }
099:
100: public ServerInfo[] cloneServers() {
101: ServerInfo[] servers = getServers();
102: ServerInfo[] result = new ServerInfo[servers.length];
103:
104: for (int i = 0; i < servers.length; i++) {
105: result[i] = new ServerInfo(servers[i]);
106: }
107:
108: return result;
109: }
110:
111: public int getSelectedServerIndex() {
112: return m_selectedServerIndex;
113: }
114:
115: public ServerInfo getSelectedServer() {
116: return m_serverInfos[m_selectedServerIndex];
117: }
118:
119: public File getSelectedServerWebAppProperties() {
120: String sandBoxRoot = SessionIntegratorFrame.getSandBoxRoot();
121: File serverDir = new File(sandBoxRoot, getSelectedServer()
122: .getName());
123: File propsFile = new File(serverDir, WEBAPP_PROPS_FILENAME);
124:
125: return propsFile;
126: }
127:
128: public void setSelectedServerIndex(int selectedServerIndex) {
129: m_selectedServerIndex = selectedServerIndex;
130: storeServerEnvironments();
131: }
132:
133: public ServerInfo getServer(int index) {
134: return m_serverInfos[index];
135: }
136:
137: public void storeServerEnvironments() {
138: int serverCount = WEB_SERVERS.length;
139: Preferences prefs = getPreferences();
140: Preferences serversPrefs = prefs.node(SERVERS_PREF_KEY);
141: Preferences serverPrefs;
142:
143: for (int i = 0; i < serverCount; i++) {
144: serverPrefs = serversPrefs.node(m_serverInfos[i].getName());
145: m_serverInfos[i].storeEnvironment(serverPrefs);
146: }
147:
148: prefs.put(SELECTED_SERVER_PREF_KEY, getSelectedServer()
149: .getName());
150: storePreferences();
151: }
152:
153: private Preferences getPreferences() {
154: SessionIntegratorContext cntx = SessionIntegrator.getContext();
155: return cntx.prefs.node(SERVER_SELECTION_PREF_KEY);
156: }
157:
158: private void storePreferences() {
159: SessionIntegratorContext cntx = SessionIntegrator.getContext();
160: cntx.client.storePrefs();
161: }
162: }
|