001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business.hibernate;
020:
021: import java.net.InetAddress;
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.net.UnknownHostException;
025: import org.hibernate.Criteria;
026: import org.hibernate.HibernateException;
027: import org.hibernate.Session;
028: import org.hibernate.criterion.Expression;
029: import org.hibernate.criterion.Order;
030: import org.apache.roller.RollerException;
031: import org.apache.roller.pojos.PingTargetData;
032: import org.apache.roller.pojos.WebsiteData;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Collection;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.roller.business.pings.AutoPingManager;
039: import org.apache.roller.business.pings.PingTargetManager;
040: import org.apache.roller.business.RollerFactory;
041: import org.apache.roller.pojos.AutoPingData;
042: import org.apache.roller.pojos.PingQueueEntryData;
043:
044: /**
045: * Hibernate implementation of the PingTargetManager.
046: *
047: * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
048: */
049: public class HibernatePingTargetManagerImpl implements
050: PingTargetManager {
051:
052: static final long serialVersionUID = 121008492583382718L;
053:
054: private static Log log = LogFactory
055: .getLog(HibernatePingTargetManagerImpl.class);
056:
057: private HibernatePersistenceStrategy strategy = null;
058:
059: public HibernatePingTargetManagerImpl(
060: HibernatePersistenceStrategy strat) {
061: this .strategy = strat;
062: }
063:
064: public void removePingTarget(PingTargetData pingTarget)
065: throws RollerException {
066: // remove contents and then target
067: this .removePingTargetContents(pingTarget);
068: this .strategy.remove(pingTarget);
069: }
070:
071: /**
072: * Convenience method which removes any queued pings or auto pings that
073: * reference the given ping target.
074: */
075: private void removePingTargetContents(PingTargetData ping)
076: throws RollerException {
077:
078: Session session = this .strategy.getSession();
079:
080: // Remove the website's ping queue entries
081: Criteria criteria = session
082: .createCriteria(PingQueueEntryData.class);
083: criteria.add(Expression.eq("pingTarget", ping));
084: List queueEntries = criteria.list();
085: Iterator qIT = queueEntries.iterator();
086: while (qIT.hasNext()) {
087: this .strategy.remove((PingQueueEntryData) qIT.next());
088: }
089:
090: // Remove the website's auto ping configurations
091: AutoPingManager autoPingMgr = RollerFactory.getRoller()
092: .getAutopingManager();
093: List autopings = autoPingMgr.getAutoPingsByTarget(ping);
094: Iterator it = autopings.iterator();
095: while (it.hasNext()) {
096: this .strategy.remove((AutoPingData) it.next());
097: }
098: }
099:
100: /**
101: * @see org.apache.roller.model.PingTargetManager#removeAllCustomPingTargets()
102: */
103: public void removeAllCustomPingTargets() throws RollerException {
104:
105: try {
106: Session session = strategy.getSession();
107: Criteria criteria = session
108: .createCriteria(PingTargetData.class);
109: criteria.add(Expression.isNotNull("website"));
110: List allCustomTargets = criteria.list();
111: removeTargets(allCustomTargets);
112: } catch (HibernateException e) {
113: throw new RollerException(e);
114: }
115: }
116:
117: // Private helper to remove a collection of targets.
118: private void removeTargets(Collection customTargets)
119: throws RollerException {
120:
121: // just go through the list and remove each auto ping
122: Iterator targets = customTargets.iterator();
123: while (targets.hasNext()) {
124: this .strategy.remove((PingTargetData) targets.next());
125: }
126: }
127:
128: public void savePingTarget(PingTargetData pingTarget)
129: throws RollerException {
130: strategy.store(pingTarget);
131: }
132:
133: public PingTargetData getPingTarget(String id)
134: throws RollerException {
135: return (PingTargetData) strategy.load(id, PingTargetData.class);
136: }
137:
138: public boolean isNameUnique(PingTargetData pingTarget)
139: throws RollerException {
140: String name = pingTarget.getName();
141: if (name == null || name.trim().length() == 0)
142: return false;
143:
144: String id = pingTarget.getId();
145:
146: // Determine the set of "brother" targets (custom or common) among which this name should be unique.
147: List brotherTargets = null;
148: WebsiteData website = pingTarget.getWebsite();
149: if (website == null) {
150: brotherTargets = getCommonPingTargets();
151: } else {
152: brotherTargets = getCustomPingTargets(website);
153: }
154:
155: // Within that set of targets, fail if there is a target with the same name and that target doesn't
156: // have the same id.
157: for (Iterator i = brotherTargets.iterator(); i.hasNext();) {
158: PingTargetData brother = (PingTargetData) i.next();
159: // Fail if it has the same name but not the same id.
160: if (brother.getName().equals(name)
161: && (id == null || !brother.getId().equals(id))) {
162: return false;
163: }
164: }
165: // No conflict found
166: return true;
167: }
168:
169: public boolean isUrlWellFormed(PingTargetData pingTarget)
170: throws RollerException {
171: String url = pingTarget.getPingUrl();
172: if (url == null || url.trim().length() == 0)
173: return false;
174: try {
175: URL parsedUrl = new URL(url);
176: // OK. If we get here, it parses ok. Now just check that the protocol is http and there is a host portion.
177: boolean isHttp = parsedUrl.getProtocol().equals("http");
178: boolean hasHost = (parsedUrl.getHost() != null)
179: && (parsedUrl.getHost().trim().length() > 0);
180: return isHttp && hasHost;
181: } catch (MalformedURLException e) {
182: return false;
183: }
184: }
185:
186: public boolean isHostnameKnown(PingTargetData pingTarget)
187: throws RollerException {
188: String url = pingTarget.getPingUrl();
189: if (url == null || url.trim().length() == 0)
190: return false;
191: try {
192: URL parsedUrl = new URL(url);
193: String host = parsedUrl.getHost();
194: if (host == null || host.trim().length() == 0)
195: return false;
196: InetAddress addr = InetAddress.getByName(host);
197: return true;
198: } catch (MalformedURLException e) {
199: return false;
200: } catch (UnknownHostException e) {
201: return false;
202: }
203: }
204:
205: /**
206: * @see org.apache.roller.model.PingTargetManager#getCommonPingTargets()
207: */
208: public List getCommonPingTargets() throws RollerException {
209: try {
210: Session session = ((HibernatePersistenceStrategy) strategy)
211: .getSession();
212: Criteria criteria = session
213: .createCriteria(PingTargetData.class);
214: criteria.add(Expression.isNull("website"));
215: criteria.addOrder(Order.asc("name"));
216: return criteria.list();
217: } catch (HibernateException e) {
218: throw new RollerException(e);
219: }
220:
221: }
222:
223: /**
224: * @see org.apache.roller.model.PingTargetManager#getCustomPingTargets(org.apache.roller.pojos.WebsiteData)
225: */
226: public List getCustomPingTargets(WebsiteData website)
227: throws RollerException {
228: try {
229: Session session = ((HibernatePersistenceStrategy) strategy)
230: .getSession();
231: Criteria criteria = session
232: .createCriteria(PingTargetData.class);
233: criteria.add(Expression.eq("website", website));
234: criteria.addOrder(Order.asc("name"));
235: return criteria.list();
236: } catch (HibernateException e) {
237: throw new RollerException(e);
238: }
239: }
240:
241: public void release() {
242: }
243:
244: }
|