KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > hibernate > HibernatePingTargetManagerImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.business.hibernate;
20
21 import java.net.InetAddress JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
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 JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Collection JavaDoc;
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 /**
46  * Hibernate implementation of the PingTargetManager.
47  *
48  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
49  */

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         // remove contents and then target
66
this.removePingTargetContents(pingTarget);
67         this.strategy.remove(pingTarget);
68     }
69     
70     
71     /**
72      * Convenience method which removes any queued pings or auto pings that
73      * reference the given ping target.
74      */

75     private void removePingTargetContents(PingTargetData ping)
76             throws RollerException {
77         
78         Session session = this.strategy.getSession();
79         
80         // Remove the website's ping queue entries
81
Criteria criteria = session.createCriteria(PingQueueEntryData.class);
82         criteria.add(Expression.eq("pingTarget", ping));
83         List JavaDoc queueEntries = criteria.list();
84         Iterator JavaDoc qIT = queueEntries.iterator();
85         while(qIT.hasNext()) {
86             this.strategy.remove((PingQueueEntryData) qIT.next());
87         }
88         
89         // Remove the website's auto ping configurations
90
AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager();
91         List JavaDoc autopings = autoPingMgr.getAutoPingsByTarget(ping);
92         Iterator JavaDoc it = autopings.iterator();
93         while(it.hasNext()) {
94             this.strategy.remove((AutoPingData) it.next());
95         }
96     }
97     
98     
99     /**
100      * @see org.apache.roller.model.PingTargetManager#removeAllCustomPingTargets()
101      */

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 JavaDoc allCustomTargets = criteria.list();
109             removeTargets(allCustomTargets);
110         } catch (HibernateException e) {
111             throw new RollerException(e);
112         }
113     }
114     
115     
116     // Private helper to remove a collection of targets.
117
private void removeTargets(Collection JavaDoc customTargets) throws RollerException {
118         
119         // just go through the list and remove each auto ping
120
Iterator JavaDoc 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 JavaDoc id) throws RollerException {
133         return (PingTargetData) strategy.load(id, PingTargetData.class);
134     }
135
136     
137     public boolean isNameUnique(PingTargetData pingTarget) throws RollerException {
138         String JavaDoc name = pingTarget.getName();
139         if (name == null || name.trim().length() == 0) return false;
140         
141         String JavaDoc id = pingTarget.getId();
142         
143         // Determine the set of "brother" targets (custom or common) among which this name should be unique.
144
List JavaDoc brotherTargets = null;
145         WebsiteData website = pingTarget.getWebsite();
146         if (website == null) {
147             brotherTargets = getCommonPingTargets();
148         } else {
149             brotherTargets = getCustomPingTargets(website);
150         }
151         
152         // Within that set of targets, fail if there is a target with the same name and that target doesn't
153
// have the same id.
154
for (Iterator JavaDoc i = brotherTargets.iterator(); i.hasNext();) {
155             PingTargetData brother = (PingTargetData) i.next();
156             // Fail if it has the same name but not the same id.
157
if (brother.getName().equals(name) && (id == null || !brother.getId().equals(id))) {
158                 return false;
159             }
160         }
161         // No conflict found
162
return true;
163     }
164     
165     
166     public boolean isUrlWellFormed(PingTargetData pingTarget) throws RollerException {
167         String JavaDoc url = pingTarget.getPingUrl();
168         if (url == null || url.trim().length() == 0) return false;
169         try {
170             URL JavaDoc parsedUrl = new URL JavaDoc(url);
171             // OK. If we get here, it parses ok. Now just check that the protocol is http and there is a host portion.
172
boolean isHttp = parsedUrl.getProtocol().equals("http");
173             boolean hasHost = (parsedUrl.getHost() != null) && (parsedUrl.getHost().trim().length() > 0);
174             return isHttp && hasHost;
175         } catch (MalformedURLException JavaDoc e) {
176             return false;
177         }
178     }
179     
180     
181     public boolean isHostnameKnown(PingTargetData pingTarget) throws RollerException {
182         String JavaDoc url = pingTarget.getPingUrl();
183         if (url == null || url.trim().length() == 0) return false;
184         try {
185             URL JavaDoc parsedUrl = new URL JavaDoc(url);
186             String JavaDoc host = parsedUrl.getHost();
187             if (host == null || host.trim().length() == 0) return false;
188             InetAddress JavaDoc addr = InetAddress.getByName(host);
189             return true;
190         } catch (MalformedURLException JavaDoc e) {
191             return false;
192         } catch (UnknownHostException JavaDoc e) {
193             return false;
194         }
195     }
196     
197     
198     /**
199      * @see org.apache.roller.model.PingTargetManager#getCommonPingTargets()
200      */

201     public List JavaDoc 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     /**
216      * @see org.apache.roller.model.PingTargetManager#getCustomPingTargets(org.apache.roller.pojos.WebsiteData)
217      */

218     public List JavaDoc 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