1 8 9 package org.roller.business; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.roller.RollerException; 14 import org.roller.model.AutoPingManager; 15 import org.roller.model.PingTargetManager; 16 import org.roller.model.RollerFactory; 17 import org.roller.pojos.PingTargetData; 18 import org.roller.pojos.WebsiteData; 19 20 import java.net.InetAddress ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.net.UnknownHostException ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 30 public abstract class PingTargetManagerImpl implements PingTargetManager 31 { 32 protected PersistenceStrategy persistenceStrategy; 33 34 private static Log mLogger = 35 LogFactory.getFactory().getInstance(PingTargetManagerImpl.class); 36 37 public PingTargetManagerImpl(PersistenceStrategy persistenceStrategy) 38 { 39 this.persistenceStrategy = persistenceStrategy; 40 } 41 42 public void release() 43 { 44 } 45 46 public PingTargetData createCommonPingTarget(String name, String pingUrl) throws RollerException 47 { 48 return new PingTargetData(null, name, pingUrl, null); 49 } 50 51 public PingTargetData createCustomPingTarget(String name, String pingUrl, WebsiteData website) throws RollerException 52 { 53 if (website == null) throw new RollerException(new IllegalArgumentException ("website == null")); 54 return new PingTargetData(null, name, pingUrl, website); 55 } 56 57 public void storePingTarget(PingTargetData pingTarget) throws RollerException 58 { 59 persistenceStrategy.store(pingTarget); 60 } 61 62 public PingTargetData retrievePingTarget(String id) throws RollerException 63 { 64 return (PingTargetData) persistenceStrategy.load(id, PingTargetData.class); 65 } 66 67 public void removePingTarget(String id) throws RollerException 68 { 69 PingTargetData pingTarget = retrievePingTarget(id); 71 pingTarget.remove(); 72 } 73 74 public boolean isNameUnique(PingTargetData pingTarget) throws RollerException 75 { 76 String name = pingTarget.getName(); 77 if (name == null || name.trim().length() == 0) return false; 78 79 String id = pingTarget.getId(); 80 81 List brotherTargets = null; 83 WebsiteData website = pingTarget.getWebsite(); 84 if (website == null) 85 { 86 brotherTargets = getCommonPingTargets(); 87 } 88 else 89 { 90 brotherTargets = getCustomPingTargets(website); 91 } 92 93 for (Iterator i = brotherTargets.iterator(); i.hasNext();) 96 { 97 PingTargetData brother = (PingTargetData) i.next(); 98 if (brother.getName().equals(name) && (id == null || !brother.getId().equals(id))) 100 { 101 return false; 102 } 103 } 104 return true; 106 } 107 108 public boolean isUrlWellFormed(PingTargetData pingTarget) throws RollerException 109 { 110 String url = pingTarget.getPingUrl(); 111 if (url == null || url.trim().length() == 0) return false; 112 try 113 { 114 URL parsedUrl = new URL (url); 115 boolean isHttp = parsedUrl.getProtocol().equals("http"); 117 boolean hasHost = (parsedUrl.getHost() != null) && (parsedUrl.getHost().trim().length() > 0); 118 return isHttp && hasHost; 119 } 120 catch (MalformedURLException e) 121 { 122 return false; 123 } 124 } 125 126 public boolean isHostnameKnown(PingTargetData pingTarget) throws RollerException 127 { 128 String url = pingTarget.getPingUrl(); 129 if (url == null || url.trim().length() == 0) return false; 130 try 131 { 132 URL parsedUrl = new URL (url); 133 String host = parsedUrl.getHost(); 134 if (host == null || host.trim().length() == 0) return false; 135 InetAddress addr = InetAddress.getByName(host); 136 return true; 137 } 138 catch (MalformedURLException e) 139 { 140 return false; 141 } 142 catch (UnknownHostException e) 143 { 144 return false; 145 } 146 } 147 } 148 | Popular Tags |