1 18 19 package org.apache.roller.business.hibernate; 20 21 import java.net.InetAddress ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.UnknownHostException ; 25 import org.hibernate.Criteria; 26 import org.hibernate.HibernateException; 27 import org.hibernate.Session; 28 import org.hibernate.criterion.Expression; 29 import org.hibernate.criterion.Order; 30 import org.apache.roller.RollerException; 31 import org.apache.roller.pojos.PingTargetData; 32 import org.apache.roller.pojos.WebsiteData; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Collection ; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.roller.model.AutoPingManager; 39 import org.apache.roller.model.PingTargetManager; 40 import org.apache.roller.model.RollerFactory; 41 import org.apache.roller.pojos.AutoPingData; 42 import org.apache.roller.pojos.PingQueueEntryData; 43 44 45 50 public class HibernatePingTargetManagerImpl implements PingTargetManager { 51 52 static final long serialVersionUID = 121008492583382718L; 53 54 private static Log log = LogFactory.getLog(HibernatePingTargetManagerImpl.class); 55 56 private HibernatePersistenceStrategy strategy = null; 57 58 59 public HibernatePingTargetManagerImpl(HibernatePersistenceStrategy strat) { 60 this.strategy = strat; 61 } 62 63 64 public void removePingTarget(PingTargetData pingTarget) throws RollerException { 65 this.removePingTargetContents(pingTarget); 67 this.strategy.remove(pingTarget); 68 } 69 70 71 75 private void removePingTargetContents(PingTargetData ping) 76 throws RollerException { 77 78 Session session = this.strategy.getSession(); 79 80 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 82 criteria.add(Expression.eq("pingTarget", ping)); 83 List queueEntries = criteria.list(); 84 Iterator qIT = queueEntries.iterator(); 85 while(qIT.hasNext()) { 86 this.strategy.remove((PingQueueEntryData) qIT.next()); 87 } 88 89 AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager(); 91 List autopings = autoPingMgr.getAutoPingsByTarget(ping); 92 Iterator it = autopings.iterator(); 93 while(it.hasNext()) { 94 this.strategy.remove((AutoPingData) it.next()); 95 } 96 } 97 98 99 102 public void removeAllCustomPingTargets() throws RollerException { 103 104 try { 105 Session session = strategy.getSession(); 106 Criteria criteria = session.createCriteria(PingTargetData.class); 107 criteria.add(Expression.isNotNull("website")); 108 List allCustomTargets = criteria.list(); 109 removeTargets(allCustomTargets); 110 } catch (HibernateException e) { 111 throw new RollerException(e); 112 } 113 } 114 115 116 private void removeTargets(Collection customTargets) throws RollerException { 118 119 Iterator targets = customTargets.iterator(); 121 while (targets.hasNext()) { 122 this.strategy.remove((PingTargetData) targets.next()); 123 } 124 } 125 126 127 public void savePingTarget(PingTargetData pingTarget) throws RollerException { 128 strategy.store(pingTarget); 129 } 130 131 132 public PingTargetData getPingTarget(String id) throws RollerException { 133 return (PingTargetData) strategy.load(id, PingTargetData.class); 134 } 135 136 137 public boolean isNameUnique(PingTargetData pingTarget) throws RollerException { 138 String name = pingTarget.getName(); 139 if (name == null || name.trim().length() == 0) return false; 140 141 String id = pingTarget.getId(); 142 143 List brotherTargets = null; 145 WebsiteData website = pingTarget.getWebsite(); 146 if (website == null) { 147 brotherTargets = getCommonPingTargets(); 148 } else { 149 brotherTargets = getCustomPingTargets(website); 150 } 151 152 for (Iterator i = brotherTargets.iterator(); i.hasNext();) { 155 PingTargetData brother = (PingTargetData) i.next(); 156 if (brother.getName().equals(name) && (id == null || !brother.getId().equals(id))) { 158 return false; 159 } 160 } 161 return true; 163 } 164 165 166 public boolean isUrlWellFormed(PingTargetData pingTarget) throws RollerException { 167 String url = pingTarget.getPingUrl(); 168 if (url == null || url.trim().length() == 0) return false; 169 try { 170 URL parsedUrl = new URL (url); 171 boolean isHttp = parsedUrl.getProtocol().equals("http"); 173 boolean hasHost = (parsedUrl.getHost() != null) && (parsedUrl.getHost().trim().length() > 0); 174 return isHttp && hasHost; 175 } catch (MalformedURLException e) { 176 return false; 177 } 178 } 179 180 181 public boolean isHostnameKnown(PingTargetData pingTarget) throws RollerException { 182 String url = pingTarget.getPingUrl(); 183 if (url == null || url.trim().length() == 0) return false; 184 try { 185 URL parsedUrl = new URL (url); 186 String host = parsedUrl.getHost(); 187 if (host == null || host.trim().length() == 0) return false; 188 InetAddress addr = InetAddress.getByName(host); 189 return true; 190 } catch (MalformedURLException e) { 191 return false; 192 } catch (UnknownHostException e) { 193 return false; 194 } 195 } 196 197 198 201 public List getCommonPingTargets() throws RollerException { 202 try { 203 Session session = ((HibernatePersistenceStrategy) strategy).getSession(); 204 Criteria criteria = session.createCriteria(PingTargetData.class); 205 criteria.add(Expression.isNull("website")); 206 criteria.addOrder(Order.asc("name")); 207 return criteria.list(); 208 } catch (HibernateException e) { 209 throw new RollerException(e); 210 } 211 212 } 213 214 215 218 public List getCustomPingTargets(WebsiteData website) throws RollerException { 219 try { 220 Session session = ((HibernatePersistenceStrategy) strategy).getSession(); 221 Criteria criteria = session.createCriteria(PingTargetData.class); 222 criteria.add(Expression.eq("website", website)); 223 criteria.addOrder(Order.asc("name")); 224 return criteria.list(); 225 } catch (HibernateException e) { 226 throw new RollerException(e); 227 } 228 } 229 230 231 public void release() {} 232 233 } 234 | Popular Tags |