1 4 package com.tc.servers; 5 6 import com.tc.SessionIntegrator; 7 import com.tc.SessionIntegratorContext; 8 import com.tc.SessionIntegratorFrame; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.IOException ; 13 import java.util.Arrays ; 14 import java.util.Properties ; 15 import java.util.prefs.Preferences ; 16 17 public class ServerSelection { 18 private ServerInfo[] m_serverInfos; 19 private int m_selectedServerIndex; 20 21 private static final String [] WEB_SERVERS = {"tomcat5.5", "tomcat5.0", "tomcat6.0", "wls8.1"}; 22 private static final String SERVER_SELECTION_PREF_KEY = "SessionIntegrator.ServerSelection"; 23 private static final String SELECTED_SERVER_PREF_KEY = "SelectedServer"; 24 private static final String SERVERS_PREF_KEY = "Servers"; 25 private static final String SERVER_PROPS_FILENAME = "server.properties"; 26 private static final String SERVER_ENV_FILENAME = "server.environment"; 27 private static final String WEBAPP_PROPS_FILENAME = "webapps.properties"; 28 private static final String DEFAULT_SERVER_NAME = WEB_SERVERS[0]; 29 30 private static ServerSelection m_instance; 31 32 private ServerSelection() { 33 String sandBoxRoot = SessionIntegratorFrame.getSandBoxRoot(); 34 int serverCount = WEB_SERVERS.length; 35 Preferences prefs = getPreferences(); 36 Preferences serversPrefs = prefs.node(SERVERS_PREF_KEY); 37 Preferences serverPrefs; 38 39 m_serverInfos = new ServerInfo[serverCount]; 40 41 for(int i = 0; i < serverCount; i++) { 42 File serverDir = new File (sandBoxRoot, WEB_SERVERS[i]); 43 File propsFile = new File (serverDir, SERVER_PROPS_FILENAME); 44 File envFile = new File (serverDir, SERVER_ENV_FILENAME); 45 Properties props = new Properties (); 46 Properties env = new Properties (); 47 48 try { 49 props.load(new FileInputStream (propsFile)); 50 env.load(new FileInputStream (envFile)); 51 52 m_serverInfos[i] = new ServerInfo(props, env); 53 serverPrefs = serversPrefs.node(m_serverInfos[i].getName()); 54 55 m_serverInfos[i].loadEnvironment(serverPrefs); 56 } catch(IOException ioe) {} 57 } 58 59 String selectedServerName = prefs.get(SELECTED_SERVER_PREF_KEY, DEFAULT_SERVER_NAME); 60 if((m_selectedServerIndex = Arrays.asList(WEB_SERVERS).indexOf(selectedServerName)) == -1) { 61 m_selectedServerIndex = 0; 62 } 63 } 64 65 public Properties getDefaultProperties(int i) { 66 String sandBoxRoot = SessionIntegratorFrame.getSandBoxRoot(); 67 File serverDir = new File (sandBoxRoot, WEB_SERVERS[i]); 68 File envFile = new File (serverDir, SERVER_ENV_FILENAME); 69 Properties env = new Properties (); 70 71 try { 72 env.load(new FileInputStream (envFile)); 73 } catch(IOException ioe) {} 74 75 return env; 76 } 77 78 public static ServerSelection getInstance() { 79 if(m_instance == null) { 80 m_instance = new ServerSelection(); 81 } 82 return m_instance; 83 } 84 85 public ServerInfo[] getServers() { 86 return m_serverInfos; 87 } 88 89 public void setServers(ServerInfo[] servers) { 90 m_serverInfos = servers; 91 } 92 93 public ServerInfo[] cloneServers() { 94 ServerInfo[] servers = getServers(); 95 ServerInfo[] result = new ServerInfo[servers.length]; 96 97 for(int i = 0; i < servers.length; i++) { 98 result[i] = new ServerInfo(servers[i]); 99 } 100 101 return result; 102 } 103 104 public int getSelectedServerIndex() { 105 return m_selectedServerIndex; 106 } 107 108 public ServerInfo getSelectedServer() { 109 return m_serverInfos[m_selectedServerIndex]; 110 } 111 112 public File getSelectedServerWebAppProperties() { 113 String sandBoxRoot = SessionIntegratorFrame.getSandBoxRoot(); 114 File serverDir = new File (sandBoxRoot, getSelectedServer().getName()); 115 File propsFile = new File (serverDir, WEBAPP_PROPS_FILENAME); 116 117 return propsFile; 118 } 119 120 public void setSelectedServerIndex(int selectedServerIndex) { 121 m_selectedServerIndex = selectedServerIndex; 122 storeServerEnvironments(); 123 } 124 125 public ServerInfo getServer(int index) { 126 return m_serverInfos[index]; 127 } 128 129 public void storeServerEnvironments() { 130 int serverCount = WEB_SERVERS.length; 131 Preferences prefs = getPreferences(); 132 Preferences serversPrefs = prefs.node(SERVERS_PREF_KEY); 133 Preferences serverPrefs; 134 135 for(int i = 0; i < serverCount; i++) { 136 serverPrefs = serversPrefs.node(m_serverInfos[i].getName()); 137 m_serverInfos[i].storeEnvironment(serverPrefs); 138 } 139 140 prefs.put(SELECTED_SERVER_PREF_KEY, getSelectedServer().getName()); 141 storePreferences(); 142 } 143 144 private Preferences getPreferences() { 145 SessionIntegratorContext cntx = SessionIntegrator.getContext(); 146 return cntx.prefs.node(SERVER_SELECTION_PREF_KEY); 147 } 148 149 private void storePreferences() { 150 SessionIntegratorContext cntx = SessionIntegrator.getContext(); 151 cntx.client.storePrefs(); 152 } 153 } 154 | Popular Tags |