1 23 24 package org.infoglue.deliver.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.StringTokenizer ; 32 33 import org.apache.log4j.Logger; 34 import org.exolab.castor.jdo.Database; 35 import org.exolab.castor.jdo.OQLQuery; 36 import org.exolab.castor.jdo.QueryResults; 37 import org.infoglue.cms.entities.management.Repository; 38 import org.infoglue.cms.entities.management.RepositoryVO; 39 import org.infoglue.cms.exception.SystemException; 40 import org.infoglue.deliver.util.CacheController; 41 import org.infoglue.deliver.util.NullObject; 42 43 import com.opensymphony.module.propertyset.PropertySet; 44 import com.opensymphony.module.propertyset.PropertySetManager; 45 46 public class RepositoryDeliveryController extends BaseDeliveryController 47 { 48 private final static Logger logger = Logger.getLogger(RepositoryDeliveryController.class.getName()); 49 50 53 54 private RepositoryDeliveryController() 55 { 56 } 57 58 61 62 public static RepositoryDeliveryController getRepositoryDeliveryController() 63 { 64 return new RepositoryDeliveryController(); 65 } 66 67 68 71 72 public RepositoryVO getMasterRepository(Database db) throws SystemException, Exception 73 { 74 RepositoryVO repositoryVO = (RepositoryVO)CacheController.getCachedObject("masterRepository", "masterRepository"); 75 if(repositoryVO != null) 76 return repositoryVO; 77 78 OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r ORDER BY r.repositoryId"); 79 80 QueryResults results = oql.execute(Database.ReadOnly); 81 82 if (results.hasMore()) 83 { 84 Repository repository = (Repository)results.next(); 85 repositoryVO = repository.getValueObject(); 86 } 87 88 results.close(); 89 oql.close(); 90 91 if(repositoryVO != null) 92 CacheController.cacheObject("masterRepository", "masterRepository", repositoryVO); 93 94 return repositoryVO; 95 } 96 97 public List getRepositoryVOListFromServerName(Database db, String serverName, String portNumber, String repositoryName) throws SystemException, Exception 98 { 99 List repositories = new ArrayList (); 100 101 List cachedRepositories = (List )CacheController.getCachedObject("masterRepository", "allDNSRepositories"); 102 if(cachedRepositories == null) 103 { 104 cachedRepositories = new ArrayList (); 105 106 OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r WHERE is_defined(r.dnsName)"); 107 QueryResults results = oql.execute(Database.ReadOnly); 108 109 while (results.hasMore()) 110 { 111 Repository repository = (Repository) results.next(); 112 cachedRepositories.add(repository.getValueObject()); 113 } 114 115 results.close(); 116 oql.close(); 117 118 if(cachedRepositories.size() > 0) 119 CacheController.cacheObject("masterRepository", "allDNSRepositories", cachedRepositories); 120 } 121 122 Iterator repositoriesIterator = cachedRepositories.iterator(); 123 while (repositoriesIterator.hasNext()) 124 { 125 RepositoryVO repositoryVO = (RepositoryVO) repositoriesIterator.next(); 126 logger.info("repository:" + repositoryVO.getDnsName()); 127 String [] dnsNames = splitStrings(repositoryVO.getDnsName()); 128 logger.info("dnsNames:" + dnsNames); 129 for (int i=0;i<dnsNames.length;i++) 130 { 131 logger.info("dnsNames[i]:" + dnsNames[i]); 132 String dnsName = dnsNames[i]; 133 int protocolIndex = dnsName.indexOf("://"); 134 if(protocolIndex > -1) 135 dnsName = dnsName.substring(protocolIndex + 3); 136 137 logger.info("Matching only server name - removed protocol if there:" + dnsName); 138 139 if((dnsName.indexOf(":") == -1 && dnsName.indexOf(serverName) == 0) || dnsName.indexOf(serverName + ":" + portNumber) == 0) 140 { 141 if(repositoryName != null && repositoryName.length() > 0) 142 { 143 logger.info("Has to check repositoryName also:" + repositoryName); 144 if(repositoryVO.getName().equalsIgnoreCase(repositoryName)) 145 repositories.add(repositoryVO); 146 } 147 else 148 { 149 repositories.add(repositoryVO); 150 } 151 } 152 } 153 } 154 155 return repositories; 156 } 157 158 200 201 private String [] splitStrings(String str) 202 { 203 List list = new ArrayList (); 204 StringTokenizer st = new StringTokenizer (str, ","); 205 while (st.hasMoreTokens()) 206 { 207 String token = st.nextToken().trim(); 208 list.add(token); 209 } 210 211 return (String []) list.toArray(new String [0]); 212 } 213 214 217 218 public List getRepositoryVOList(Database db) throws SystemException, Exception 219 { 220 List repositoryVOList = new ArrayList (); 221 222 OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r ORDER BY r.repositoryId"); 223 224 QueryResults results = oql.execute(Database.ReadOnly); 225 226 if (results.hasMore()) 227 { 228 Repository repository = (Repository)results.next(); 229 RepositoryVO repositoryVO = repository.getValueObject(); 230 repositoryVOList.add(repositoryVO); 231 } 232 233 results.close(); 234 oql.close(); 235 236 return repositoryVOList; 237 } 238 239 242 243 public String getPropertyValue(Integer repositoryId, String propertyName) 244 { 245 String key = "parentRepository_" + repositoryId + "_" + propertyName; 246 logger.info("key:" + key); 247 Object object = CacheController.getCachedObject("parentRepository", key); 248 249 if(object instanceof NullObject) 250 { 251 logger.info("There was an cached property but it was null:" + object); 252 return null; 253 } 254 else if(object != null) 255 { 256 logger.info("There was an cached property:" + object); 257 return (String )object; 258 } 259 260 String propertyValue = null; 261 262 Map args = new HashMap (); 263 args.put("globalKey", "infoglue"); 264 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 265 266 propertyValue = ps.getString("repository_" + repositoryId + "_" + propertyName); 267 logger.info("propertyValue:" + propertyValue); 268 if(propertyValue != null) 269 CacheController.cacheObject("parentRepository", key, propertyValue); 270 else 271 CacheController.cacheObject("parentRepository", key, new NullObject()); 272 273 return propertyValue; 274 } 275 276 } | Popular Tags |