1 5 package com.tc; 6 7 import org.apache.commons.io.CopyUtils; 8 import org.apache.commons.io.IOUtils; 9 import org.apache.xmlbeans.XmlException; 10 import org.apache.xmlbeans.XmlOptions; 11 12 import com.tc.config.Loader; 13 import com.tc.config.schema.migrate.ConfigUpdateException; 14 import com.tc.object.tools.BootJarSignature; 15 import com.tc.servers.ServerSelection; 16 import com.terracottatech.config.Application; 17 import com.terracottatech.config.DsoApplication; 18 import com.terracottatech.config.Server; 19 import com.terracottatech.config.Servers; 20 import com.terracottatech.config.TcConfigDocument; 21 import com.terracottatech.config.WebApplication; 22 import com.terracottatech.config.WebApplications; 23 import com.terracottatech.config.TcConfigDocument.TcConfig; 24 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import javax.swing.SwingUtilities ; 34 35 public class ConfigHelper { 36 private Loader m_configLoader; 37 private XmlOptions m_xmlOptions; 38 private File m_configFile; 39 private TcConfig m_config; 40 41 private static final String TC_INSTALL_DIR = SessionIntegratorFrame.getTCInstallDir(); 42 43 private static final String TOMCAT_SANDBOX = SessionIntegratorFrame.getSandBoxRoot(); 44 45 private static final String FS = System.getProperty("file.separator"); 46 47 private static final String CUSTOM_BOOT_JAR_PATH = TC_INSTALL_DIR + FS + "lib" + FS + "dso-boot" + FS 48 + getBootJarNameForThisVM(); 49 50 private static final int DEFAULT_JMX_PORT = 9520; 51 52 public ConfigHelper(ServerSelection selection) { 53 super(); 54 55 String serverName = selection.getSelectedServer().getName(); 56 57 m_configLoader = new Loader(); 58 m_xmlOptions = createXmlOptions(); 59 m_configFile = new File (TOMCAT_SANDBOX + FS + serverName + FS + "tc-config.xml"); 60 61 testUpdateConfig(); 62 } 63 64 private void testUpdateConfig() { 65 try { 66 if (m_configFile.exists()) { 67 if (m_configLoader.testIsOld(m_configFile)) { 68 m_configLoader.updateToCurrent(m_configFile); 69 } 70 } 71 } catch (ConfigUpdateException cue) { 72 } catch (Exception e) { 74 } 75 } 76 77 public String getConfigFilePath() { 78 return m_configFile.getAbsolutePath(); 79 } 80 81 public File getConfigFile() { 82 return m_configFile; 83 } 84 85 public TcConfig getConfig() { 86 return m_config; 87 } 88 89 public TcConfig ensureConfig() { 90 TcConfig config = getConfig(); 91 92 if (config == null) { 93 try { 94 config = load(); 95 } catch (Exception e) { 96 m_config = TcConfig.Factory.newInstance(); 97 } 98 } 99 100 return config; 101 } 102 103 public int getJmxPort() { 104 TcConfig config = ensureConfig(); 105 Servers servers = config.getServers(); 106 107 if (servers == null) { 108 servers = config.addNewServers(); 109 } 110 111 if (servers.sizeOfServerArray() == 0) { 112 servers.addNewServer(); 113 save(); 114 } 115 116 Server server = servers.getServerArray(0); 117 int port = server.isSetJmxPort() ? server.getJmxPort() : DEFAULT_JMX_PORT; 118 119 return port; 120 } 121 122 public DsoApplication getDsoApplication() { 123 TcConfig config = getConfig(); 124 125 if (config != null) { 126 Application app = config.getApplication(); 127 return app != null ? app.getDso() : null; 128 } 129 130 return null; 131 } 132 133 public DsoApplication ensureDsoApplication() { 134 DsoApplication dsoApp = null; 135 TcConfig config = ensureConfig(); 136 137 if (config != null) { 138 Application app = config.getApplication(); 139 140 if (app == null) { 141 app = config.addNewApplication(); 142 } 143 144 if ((dsoApp = app.getDso()) == null) { 145 dsoApp = app.addNewDso(); 146 dsoApp.addNewInstrumentedClasses(); 147 } 148 } 149 150 return dsoApp; 151 } 152 153 public TcConfig load() throws Exception { 154 File configFile = getConfigFile(); 155 TcConfigDocument configDoc; 156 157 configDoc = m_configLoader.parse(configFile, m_xmlOptions); 158 m_config = configDoc.getTcConfig(); 159 160 return m_config; 161 } 162 163 public List validate(String xmlText) throws IOException , XmlException { 164 TcConfigDocument configDoc = m_configLoader.parse(xmlText, m_xmlOptions); 165 TcConfig config = configDoc.getTcConfig(); 166 List errors = new ArrayList (); 167 168 if (config != null) { 169 m_xmlOptions.setErrorListener(errors); 170 configDoc.validate(m_xmlOptions); 171 m_xmlOptions.setErrorListener(null); 172 } 173 174 return errors; 175 } 176 177 public void save() { 178 TcConfigDocument configDoc = TcConfigDocument.Factory.newInstance(); 179 InputStream inStream = null; 180 OutputStream outStream = null; 181 182 try { 183 if (m_config != null) { 184 configDoc.setTcConfig(m_config); 185 186 inStream = configDoc.newInputStream(getXmlOptions()); 187 outStream = new FileOutputStream (m_configFile); 188 189 CopyUtils.copy(inStream, outStream); 190 } 191 } catch (Exception e) { 192 openError("Error saving '" + m_configFile.getName() + "'", e); 193 } finally { 194 IOUtils.closeQuietly(inStream); 195 IOUtils.closeQuietly(outStream); 196 } 197 } 198 199 public String getConfigText() { 200 TcConfig config = getConfig(); 201 InputStream inStream = null; 202 String text = null; 203 204 try { 205 if (config != null) { 206 TcConfigDocument configDoc = TcConfigDocument.Factory.newInstance(); 207 208 configDoc.setTcConfig(m_config); 209 210 inStream = configDoc.newInputStream(getXmlOptions()); 211 text = IOUtils.toString(inStream); 212 } 213 } catch (Exception e) { 214 openError("Error getting config text", e); 215 } finally { 216 IOUtils.closeQuietly(inStream); 217 } 218 219 return text; 220 } 221 222 public void save(String xmlText) { 223 TcConfigDocument configDoc = null; 224 InputStream inStream = null; 225 OutputStream outStream = null; 226 227 try { 228 configDoc = m_configLoader.parse(xmlText, m_xmlOptions); 229 m_config = configDoc.getTcConfig(); 230 231 if (m_config != null) { 232 inStream = configDoc.newInputStream(getXmlOptions()); 233 outStream = new FileOutputStream (m_configFile); 234 235 CopyUtils.copy(inStream, outStream); 236 } 237 } catch (Exception e) { 238 openError("Error saving '" + m_configFile.getName() + "'", e); 239 } finally { 240 IOUtils.closeQuietly(inStream); 241 IOUtils.closeQuietly(outStream); 242 } 243 } 244 245 public void saveAs(File file, String xmlText) { 246 TcConfigDocument configDoc = null; 247 InputStream inStream = null; 248 OutputStream outStream = null; 249 250 try { 251 configDoc = m_configLoader.parse(xmlText, m_xmlOptions); 252 m_config = configDoc.getTcConfig(); 253 254 if (m_config != null) { 255 inStream = configDoc.newInputStream(getXmlOptions()); 256 outStream = new FileOutputStream (file); 257 258 CopyUtils.copy(inStream, outStream); 259 } 260 } catch (Exception e) { 261 openError("Error saving '" + file.getName() + "'", e); 262 } finally { 263 IOUtils.closeQuietly(inStream); 264 IOUtils.closeQuietly(outStream); 265 } 266 } 267 268 public void openError(final String msg, final Throwable t) { 269 if (SwingUtilities.isEventDispatchThread()) { 270 ErrorDialog d = new ErrorDialog(msg, t); 271 d.setVisible(true); 272 } else { 273 SwingUtilities.invokeLater(new Runnable () { 274 public void run() { 275 ErrorDialog d = new ErrorDialog(msg, t); 276 d.setVisible(true); 277 } 278 }); 279 } 280 } 281 282 public static String getCustomBootJarPath() { 283 return CUSTOM_BOOT_JAR_PATH; 284 } 285 286 public static File getCustomBootJarFile() { 287 return new File (getCustomBootJarPath()); 288 } 289 290 public boolean ensureWebApplication(String name) { 291 DsoApplication dsoApp = ensureDsoApplication(); 292 293 if (dsoApp != null) { 294 WebApplications apps = dsoApp.getWebApplications(); 295 296 if (apps == null) { 297 apps = dsoApp.addNewWebApplications(); 298 } 299 300 WebApplication[] webApps = apps.getWebApplicationArray(); 301 for (int i = 0; i < webApps.length; i++) { 302 if (webApps[i].getStringValue().equals(name)) { return false; } 303 } 304 305 WebApplication webApp = apps.addNewWebApplication(); 306 webApp.setStringValue(name); 307 308 return true; 309 } 310 311 return false; 312 } 313 314 public boolean removeWebApplication(String name) { 315 DsoApplication dsoApp = ensureDsoApplication(); 316 317 if (dsoApp != null) { 318 WebApplications apps = dsoApp.getWebApplications(); 319 320 if (apps != null) { 321 WebApplication[] appNames = apps.getWebApplicationArray(); 322 323 for (int i = 0; i < appNames.length; i++) { 324 if (appNames[i].getStringValue().equals(name)) { 325 apps.removeWebApplication(i); 326 return true; 327 } 328 } 329 } 330 } 331 332 return false; 333 } 334 335 private XmlOptions createXmlOptions() { 336 XmlOptions opts = new XmlOptions(); 337 338 opts.setLoadLineNumbers(); 339 opts.setSavePrettyPrint(); 340 opts.setSavePrettyPrintIndent(2); 341 opts.remove(XmlOptions.LOAD_STRIP_WHITESPACE); 342 opts.remove(XmlOptions.LOAD_STRIP_COMMENTS); 343 opts.remove(XmlOptions.VALIDATE_ON_SET); 344 345 return opts; 346 } 347 348 public XmlOptions getXmlOptions() { 349 return m_xmlOptions; 350 } 351 352 private static String getBootJarNameForThisVM() { 353 try { 354 return BootJarSignature.getBootJarNameForThisVM(); 355 } catch (Exception e) { 356 return "dso-boot.jar"; 357 } 358 } 359 } 360 | Popular Tags |