File Source: JPAPingTargetManagerImpl.java
1
2 /*
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. The ASF licenses this file to You
5 * under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License. For additional information regarding
16 * copyright in this work, please see the NOTICE file in the top level
17 * directory of this distribution.
18 */
19 package org.apache.roller.weblogger.business.jpa;
20
21 import java.net.InetAddress;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.net.UnknownHostException;
25 import java.util.List;
26 import java.util.Iterator;
27 import javax.persistence.Query;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.roller.weblogger.business.jpa.JPAPersistenceStrategy;
32
33 import org.apache.roller.weblogger.WebloggerException;
34 import org.apache.roller.weblogger.business.Weblogger;
35
36 import org.apache.roller.weblogger.business.pings.PingTargetManager;
37 import org.apache.roller.weblogger.pojos.PingTarget;
38 import org.apache.roller.weblogger.pojos.Weblog;
39
40 /*
41 * JPAPingTargetManagerImpl.java
42 *
43 * Created on May 29, 2006, 2:24 PM
44 *
45 */
46 @com.google.inject.Singleton
47 public class JPAPingTargetManagerImpl implements PingTargetManager {
48
49 /** The logger instance for this class. */
/*
P/P * Method: org.apache.roller.weblogger.business.jpa.JPAPingTargetManagerImpl__static_init
*
* Postconditions:
* init'ed(log)
*/
50 private static Log log = LogFactory.getLog(
51 JPAPingTargetManagerImpl.class);
52
53 private final Weblogger roller;
54 private final JPAPersistenceStrategy strategy;
55
56
57 @com.google.inject.Inject
/*
P/P * Method: void org.apache.roller.weblogger.business.jpa.JPAPingTargetManagerImpl(Weblogger, JPAPersistenceStrategy)
*
* Postconditions:
* this.roller == roller
* init'ed(this.roller)
* this.strategy == strategy
* init'ed(this.strategy)
*/
58 protected JPAPingTargetManagerImpl(Weblogger roller, JPAPersistenceStrategy strategy) {
59 this.roller = roller;
60 this.strategy = strategy;
61 }
62
63
64 public void removePingTarget(PingTarget pingTarget)
65 throws WebloggerException {
66 // remove contents and then target
/*
P/P * Method: void removePingTarget(PingTarget)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*/
67 this.removePingTargetContents(pingTarget);
68 this.strategy.remove(pingTarget);
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(PingTarget ping)
76 throws WebloggerException {
77 // Remove the website's ping queue entries
/*
P/P * Method: void removePingTargetContents(PingTarget)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Presumptions:
* javax.persistence.EntityManager:createNamedQuery(...)@301 != null
*/
78 Query q = strategy.getNamedUpdate("PingQueueEntry.removeByPingTarget");
79 q.setParameter(1, ping);
80 q.executeUpdate();
81
82 // Remove the website's auto ping configurations
83 q = strategy.getNamedUpdate("AutoPing.removeByPingTarget");
84 q.setParameter(1, ping);
85 q.executeUpdate();
86 }
87
88 public void removeAllCustomPingTargets()
89 throws WebloggerException {
/*
P/P * Method: void removeAllCustomPingTargets()
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Presumptions:
* javax.persistence.EntityManager:createNamedQuery(...)@301 != null
*/
90 Query q = strategy.getNamedUpdate(
91 "PingTarget.removeByWebsiteNotNull");
92 q.executeUpdate();
93 }
94
95 public void savePingTarget(PingTarget pingTarget)
96 throws WebloggerException {
/*
P/P * Method: void savePingTarget(PingTarget)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*/
97 strategy.store(pingTarget);
98 }
99
100 public PingTarget getPingTarget(String id)
101 throws WebloggerException {
/*
P/P * Method: PingTarget getPingTarget(String)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Postconditions:
* init'ed(return_value)
*/
102 return (PingTarget)strategy.load(PingTarget.class, id);
103 }
104
105 public boolean isNameUnique(PingTarget pingTarget)
106 throws WebloggerException {
/*
P/P * Method: bool isNameUnique(PingTarget)
*
* Preconditions:
* pingTarget != null
* (soft) this.strategy != null
* (soft) this.strategy.emf != null
* (soft) this.strategy.threadLocalEntityManager != null
*
* Presumptions:
* java.util.Iterator:next(...)@126 != null
* javax.persistence.Query:getResultList(...)@177 != null
* javax.persistence.Query:getResultList(...)@185 != null
* org.apache.roller.weblogger.pojos.PingTarget:getId(...)@128 != null
* org.apache.roller.weblogger.pojos.PingTarget:getName(...)@128 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:equals(...)@128: {0}, {1}
* java.lang.String:equals(...)@128: {1}, {0}
* java.lang.String:length(...)@108: {1..232-1}, {0}
* java.util.Iterator:hasNext(...)@125: {0}, {1}
* org.apache.roller.weblogger.pojos.PingTarget:getId(...)@110: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.pojos.PingTarget:getName(...)@107: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.pojos.PingTarget:getWebsite(...)@115: Inverse{null}, Addr_Set{null}
*/
107 String name = pingTarget.getName();
108 if (name == null || name.trim().length() == 0) return false;
109
110 String id = pingTarget.getId();
111
112 // Determine the set of "brother" targets (custom or common)
113 // among which this name should be unique.
114 List brotherTargets = null;
115 Weblog website = pingTarget.getWebsite();
116 if (website == null) {
117 brotherTargets = getCommonPingTargets();
118 } else {
119 brotherTargets = getCustomPingTargets(website);
120 }
121
122 // Within that set of targets, fail if there is a target
123 // with the same name and that target doesn't
124 // have the same id.
125 for (Iterator i = brotherTargets.iterator(); i.hasNext();) {
126 PingTarget brother = (PingTarget) i.next();
127 // Fail if it has the same name but not the same id.
128 if (brother.getName().equals(name) &&
129 (id == null || !brother.getId().equals(id))) {
130 return false;
131 }
132 }
133 // No conflict found
134 return true;
135 }
136
137
138 public boolean isUrlWellFormed(PingTarget pingTarget)
139 throws WebloggerException {
/*
P/P * Method: bool isUrlWellFormed(PingTarget)
*
* Preconditions:
* pingTarget != null
*
* Presumptions:
* java.net.URL:getHost(...)@147 != null
* java.net.URL:getProtocol(...)@146 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:length(...)@141: {1..232-1}, {0}
* org.apache.roller.weblogger.pojos.PingTarget:getPingUrl(...)@140: Addr_Set{null}, Inverse{null}
*/
140 String url = pingTarget.getPingUrl();
141 if (url == null || url.trim().length() == 0) return false;
142 try {
143 URL parsedUrl = new URL(url);
144 // OK. If we get here, it parses ok. Now just check
145 // that the protocol is http and there is a host portion.
146 boolean isHttp = parsedUrl.getProtocol().equals("http");
147 boolean hasHost = (parsedUrl.getHost() != null) &&
148 (parsedUrl.getHost().trim().length() > 0);
149 return isHttp && hasHost;
150 } catch (MalformedURLException e) {
151 return false;
152 }
153 }
154
155
156 public boolean isHostnameKnown(PingTarget pingTarget)
157 throws WebloggerException {
/*
P/P * Method: bool isHostnameKnown(PingTarget)
*
* Preconditions:
* pingTarget != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:length(...)@159: {1..232-1}, {0}
* java.lang.String:length(...)@163: {1..232-1}, {0}
* java.net.URL:getHost(...)@162: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.pojos.PingTarget:getPingUrl(...)@158: Addr_Set{null}, Inverse{null}
*/
158 String url = pingTarget.getPingUrl();
159 if (url == null || url.trim().length() == 0) return false;
160 try {
161 URL parsedUrl = new URL(url);
162 String host = parsedUrl.getHost();
163 if (host == null || host.trim().length() == 0) return false;
+ 164 InetAddress addr = InetAddress.getByName(host);
165 return true;
166 } catch (MalformedURLException e) {
167 return false;
168 } catch (UnknownHostException e) {
169 return false;
170 }
171 }
172
173 public List getCommonPingTargets()
174 throws WebloggerException {
/*
P/P * Method: List getCommonPingTargets()
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Postconditions:
* init'ed(return_value)
*/
175 Query q = strategy.getNamedQuery(
176 "PingTarget.getByWebsiteNullOrderByName");
177 return q.getResultList();
178 }
179
180 public List getCustomPingTargets(Weblog website)
181 throws WebloggerException {
/*
P/P * Method: List getCustomPingTargets(Weblog)
*
* Preconditions:
* this.strategy != null
* this.strategy.threadLocalEntityManager != null
* (soft) this.strategy.emf != null
*
* Postconditions:
* init'ed(return_value)
*/
182 Query q = strategy.getNamedQuery(
183 "PingTarget.getByWebsiteOrderByName");
184 q.setParameter(1, website);
185 return q.getResultList();
186 }
187
/*
P/P * Method: void release()
*/
188 public void release() {}
189
190 }
SofCheck Inspector Build Version : 2.18479
| JPAPingTargetManagerImpl.java |
2009-Jan-02 14:25:12 |
| JPAPingTargetManagerImpl.class |
2009-Sep-04 03:12:31 |