KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > PingTargetManagerImpl


1 /*
2  * Copyright (c) 2005
3  * Anil R. Gangolli. All rights reserved.
4  *
5  * Distributed with the Roller Weblogger Project under the terms of the Roller Software
6  * License
7  */

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 JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Abstract implementation of PingTargetManager.
29  */

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 JavaDoc name, String JavaDoc pingUrl) throws RollerException
47     {
48         return new PingTargetData(null, name, pingUrl, null);
49     }
50
51     public PingTargetData createCustomPingTarget(String JavaDoc name, String JavaDoc pingUrl, WebsiteData website) throws RollerException
52     {
53         if (website == null) throw new RollerException(new IllegalArgumentException JavaDoc("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 JavaDoc id) throws RollerException
63     {
64         return (PingTargetData) persistenceStrategy.load(id, PingTargetData.class);
65     }
66
67     public void removePingTarget(String JavaDoc id) throws RollerException
68     {
69         // The retrieval is necessary in order to do the necessary cleanup of references in pingTarget.remove().
70
PingTargetData pingTarget = retrievePingTarget(id);
71         pingTarget.remove();
72     }
73
74     public boolean isNameUnique(PingTargetData pingTarget) throws RollerException
75     {
76         String JavaDoc name = pingTarget.getName();
77         if (name == null || name.trim().length() == 0) return false;
78
79         String JavaDoc id = pingTarget.getId();
80
81         // Determine the set of "brother" targets (custom or common) among which this name should be unique.
82
List JavaDoc 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         // Within that set of targets, fail if there is a target with the same name and that target doesn't
94
// have the same id.
95
for (Iterator JavaDoc i = brotherTargets.iterator(); i.hasNext();)
96         {
97             PingTargetData brother = (PingTargetData) i.next();
98             // Fail if it has the same name but not the same id.
99
if (brother.getName().equals(name) && (id == null || !brother.getId().equals(id)))
100             {
101                 return false;
102             }
103         }
104         // No conflict found
105
return true;
106     }
107
108     public boolean isUrlWellFormed(PingTargetData pingTarget) throws RollerException
109     {
110         String JavaDoc url = pingTarget.getPingUrl();
111         if (url == null || url.trim().length() == 0) return false;
112         try
113         {
114             URL JavaDoc parsedUrl = new URL JavaDoc(url);
115             // OK. If we get here, it parses ok. Now just check that the protocol is http and there is a host portion.
116
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 JavaDoc e)
121         {
122             return false;
123         }
124     }
125
126     public boolean isHostnameKnown(PingTargetData pingTarget) throws RollerException
127     {
128         String JavaDoc url = pingTarget.getPingUrl();
129         if (url == null || url.trim().length() == 0) return false;
130         try
131         {
132             URL JavaDoc parsedUrl = new URL JavaDoc(url);
133             String JavaDoc host = parsedUrl.getHost();
134             if (host == null || host.trim().length() == 0) return false;
135             InetAddress JavaDoc addr = InetAddress.getByName(host);
136             return true;
137         }
138         catch (MalformedURLException JavaDoc e)
139         {
140             return false;
141         }
142         catch (UnknownHostException JavaDoc e)
143         {
144             return false;
145         }
146     }
147 }
148
Popular Tags