1 4 package com.openedit.archive.cumulus; 5 6 import java.util.Date ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.dom4j.Element; 11 12 import com.canto.cumulus.Cumulus; 13 import com.canto.cumulus.Server; 14 import com.canto.cumulus.ServerCatalogs; 15 import com.openedit.OpenEditException; 16 import com.openedit.ShutdownList; 17 import com.openedit.Shutdownable; 18 import com.openedit.modules.xml.XmlFile; 19 import com.openedit.page.Page; 20 import com.openedit.page.manage.PageManager; 21 import com.openedit.store.StoreException; 22 import com.openedit.util.XmlUtil; 23 24 public class CumulusConnectionPool implements Shutdownable 25 { 26 private static final Log log = LogFactory.getLog(CumulusConnectionPool.class); 27 28 protected XmlFile fieldXmlFile; 29 protected PageManager fieldPageManager; 30 protected long fieldLastUsed; 31 protected Server fieldServerConnection; 32 protected static boolean hasStarted = false; 33 public Server getAvailableServer() throws StoreException 34 { 35 if( fieldServerConnection == null) 36 { 37 reconnect(); 38 } 39 return fieldServerConnection; 40 } 41 public ServerCatalogs getServerCatalogs() throws StoreException 42 { 43 ServerCatalogs catalogs = null; 44 try 45 { 46 catalogs = getAvailableServer().getServerCatalogs(); 47 long now = new Date ().getTime(); 48 if( now > getLastUsed() + (1000 * 60 * 5) ) 50 { 51 log.info("Cumulus connection being tested"); 52 catalogs.getServerCatalog("$Users"); 53 } 54 } 55 catch ( Exception ex) 56 { 57 log.error(ex); 58 reconnect(); 59 catalogs = getAvailableServer().getServerCatalogs(); 60 } 61 setLastUsed(System.currentTimeMillis()); 62 return catalogs; 63 } 64 65 public void reconnect() throws StoreException 66 { 67 68 String username = getFile().getRoot().elementText("username"); 69 String password = getFile().getRoot().elementText("password"); 70 String server = getFile().getRoot().elementText("server"); 71 String readonly = getFile().getRoot().elementText("readonly"); 72 try 73 { 74 boolean read = Boolean.parseBoolean(readonly); 75 read = !read; 76 if( !hasStarted) 77 { 78 log.info("Starting Cumulus"); 79 Cumulus.CumulusStart(); 80 hasStarted = true; 81 } 82 log.info("Connecting to cumulus " + server); 83 fieldServerConnection = Server.connectToServer(read,server, username,password); 84 } 85 catch ( Exception ex ) 86 { 87 log.error(ex); 88 throw new StoreException(ex); 89 } 90 } 91 92 public void expireConnection() 93 { 94 long now = new Date ().getTime(); 95 96 if( now > getLastUsed() + (1000 * 60 * 15) ) 98 { 99 fieldServerConnection = null; 100 } 101 } 102 protected XmlFile getFile() throws StoreException 103 { 104 if( fieldXmlFile == null) 105 { 106 fieldXmlFile = new XmlFile(); 107 String path = "/WEB-INF/cumulus.xml"; 108 fieldXmlFile.setPath(path); 109 } 110 try 111 { 112 Page config = getPageManager().getPage(fieldXmlFile.getPath()); 113 if( config.exists() && config.getLastModified().getTime() != fieldXmlFile.getLastModified() ) 114 { 115 Element root = new XmlUtil().getXml(config.getReader(), config.getCharacterEncoding()); 116 fieldXmlFile.setRoot(root); 117 fieldXmlFile.setLastModified(config.getLastModified().getTime()); 118 } 119 } 120 catch( OpenEditException ex) 121 { 122 throw new StoreException(ex); 123 } 124 return fieldXmlFile; 125 } 126 public PageManager getPageManager() 127 { 128 return fieldPageManager; 129 } 130 public void setPageManager(PageManager inPageManager) 131 { 132 fieldPageManager = inPageManager; 133 } 134 protected long getLastUsed() 135 { 136 return fieldLastUsed; 137 } 138 protected void setLastUsed(long inLastUsed) 139 { 140 fieldLastUsed = inLastUsed; 141 } 142 public Server getServerConnection() 143 { 144 return fieldServerConnection; 145 } 146 public void setServerConnection(Server inServerConnection) 147 { 148 fieldServerConnection = inServerConnection; 149 } 150 public void setShutdownList(ShutdownList inList) 151 { 152 inList.addForShutdown(this); } 154 public void shutdown() 155 { 156 if( hasStarted) 157 Cumulus.CumulusStop(); 158 } 159 protected void finalize() throws Throwable 160 { 161 shutdown(); 162 super.finalize(); 163 } 164 public boolean isEnabled() throws StoreException 165 { 166 return getFile().getLastModified() > -1; 167 } 168 169 } 170 | Popular Tags |