锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Blogged with Flock Blogged with Flock Tags: java Blogged with Flock Tags: java
聽聽private聽long聽id;
聽聽private聽List<WebSite>聽trustedSites;
聽聽private聽List<WebSite>聽blockedSites;
聽聽
聽聽public聽CustomService()聽{
聽聽聽聽this.trustedSites聽=聽new聽ArrayList<WebSite>();
聽聽聽聽this.blockedSites聽=聽new聽ArrayList<WebSite>();
聽聽}
聽聽public聽List<WebSite>聽getBlockedSites()聽{
聽聽聽聽return聽blockedSites;
聽聽}
聽聽public聽void聽setBlockedSites(List<WebSite>聽blockedSites)聽{
聽聽聽聽this.blockedSites聽=聽blockedSites;
聽聽}
聽聽public聽long聽getId()聽{
聽聽聽聽return聽id;
聽聽}
聽聽public聽void聽setId(long聽id)聽{
聽聽聽聽this.id聽=聽id;
聽聽}
聽聽public聽List<WebSite>聽getTrustedSites()聽{
聽聽聽聽return聽trustedSites;
聽聽}
聽聽public聽void聽setTrustedSites(List<WebSite>聽trustedSites)聽{
聽聽聽聽this.trustedSites聽=聽trustedSites;
聽聽}
聽聽
聽聽public聽void聽addTrustedSites(String聽site,聽boolean聽isNewData)聽{
聽聽聽聽this.trustedSites.add(new聽WebSite(site,聽isNewData));
聽聽}
聽聽
聽聽public聽void聽addBlockedSites(String聽site,聽boolean聽isNewData)聽{
聽聽聽聽this.blockedSites.add(new聽WebSite(site,聽isNewData));
聽聽}
}
聽聽private聽String聽website;
聽聽private聽boolean聽isNewData;
聽聽
聽聽public聽WebSite()聽{
聽聽聽聽
聽聽}
聽聽public聽WebSite(String聽website)聽{
聽聽聽聽this.website聽=聽website;
聽聽}
聽聽public聽WebSite(String聽website,聽boolean聽isNewData)聽{
聽聽聽聽this.website聽=聽website;
聽聽聽聽this.isNewData聽=聽isNewData;
聽聽}
聽聽public聽boolean聽isIsNewData()聽{
聽聽聽聽return聽isNewData;
聽聽}
聽聽public聽void聽setIsNewData(boolean聽isNewData)聽{
聽聽聽聽this.isNewData聽=聽isNewData;
聽聽}
聽聽public聽String聽getWebsite()聽{
聽聽聽聽return聽website;
聽聽}
聽聽public聽void聽setWebsite(String聽website)聽{
聽聽聽聽this.website聽=聽website;
聽聽}
聽聽@Override
聽聽public聽boolean聽equals(Object聽obj)聽{
聽聽聽聽if聽(this聽==聽obj)聽{
聽聽聽聽聽聽return聽true;
聽聽聽聽}
聽聽聽聽if聽(obj聽==聽null)聽{
聽聽聽聽聽聽return聽false;
聽聽聽聽}
聽聽聽聽if聽(getClass()聽!=聽obj.getClass())聽{
聽聽聽聽聽聽return聽false;
聽聽聽聽}
聽聽聽聽final聽WebSite聽other聽=聽(WebSite)聽obj;
聽聽聽聽if聽(this.website聽==聽null聽||聽!this.website.equals(other.website))聽{
聽聽聽聽聽聽return聽false;
聽聽聽聽}
聽聽聽聽if聽(this.isNewData聽!=聽other.isNewData)聽{
聽聽聽聽聽聽return聽false;
聽聽聽聽}
聽聽聽聽return聽true;
聽聽}
聽聽@Override
聽聽public聽int聽hashCode()聽{
聽聽聽聽int聽hash聽=聽7;
聽聽聽聽hash聽=聽13聽*聽hash聽+聽(this.website聽!=聽null聽?聽this.website.hashCode()聽:聽0);
聽聽聽聽hash聽=聽13聽*聽hash聽+聽(this.isNewData聽?聽1聽:聽0);
聽聽聽聽return聽hash;
聽聽}
聽聽
}
<!DOCTYPE聽hibernate-mapping聽PUBLIC
聽聽聽聽"-//Hibernate/Hibernate聽Mapping聽DTD聽3.0//EN"
聽聽聽聽"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping聽package="model.cnm.cf">
聽聽<class聽name="CustomService"聽table="custom_services">
聽聽聽聽<id聽name="id"聽column="id"聽type="long">
聽聽聽聽聽聽<generator聽class="native"/>
聽聽聽聽</id>
聽聽聽聽<list聽name="trustedSites"聽table="cf_trusted_sites"聽cascade="all">
聽聽聽聽聽聽<key聽column="service_id"/>
聽聽聽聽聽聽<list-index聽column="pos"/>
聽聽聽聽聽聽<composite-element聽class="WebSite">
聽聽聽聽聽聽聽聽<property聽name="website"聽column="site"聽type="string"/>
聽聽聽聽聽聽聽聽<property聽name="isNewData"聽column="isNewData"聽type="boolean"/>
聽聽聽聽聽聽</composite-element>
聽聽聽聽</list>
聽聽聽聽<list聽name="blockedSites"聽table="cf_blocked_sites">
聽聽聽聽聽聽<key聽column="service_id"/>
聽聽聽聽聽聽<index聽column="pos"/>
聽聽聽聽聽聽<composite-element聽class="WebSite">
聽聽聽聽聽聽聽聽<property聽name="website"聽column="site"聽type="string"/>
聽聽聽聽聽聽聽聽<property聽name="isNewData"聽column="isNewData"聽type="boolean"/>
聽聽聽聽聽聽</composite-element>
聽聽聽聽</list>
聽聽</class>
</hibernate-mapping>
聽2聽聽聽private聽CustomService聽service;
聽3聽聽聽private聽long聽id;
聽4聽聽聽
聽5聽聽聽@Test
聽6聽聽聽public聽void聽save()聽{
聽7聽聽聽聽聽session聽=聽HibernateUtil.getSession();
聽8聽聽聽聽聽service聽=聽new聽CustomService();
聽9聽聽聽聽聽service.addBlockedSites("www.whitehouse.com",聽true);
10聽聽聽聽聽service.addBlockedSites("www.whitehouse.com",聽false);
11聽聽聽聽聽
12聽聽聽聽聽service.addTrustedSites("www.google.cn",聽true);
13聽聽聽聽聽service.addTrustedSites("www.google.cn",聽false);
14聽聽聽聽聽
15聽聽聽聽聽
16聽聽聽聽聽session.save(service);
17聽聽聽聽聽id聽=聽service.getId();
18聽聽聽聽聽session.flush();
19聽聽聽聽聽session.close();
20聽聽聽聽聽
21聽聽聽聽聽session聽=聽HibernateUtil.getSession();
22聽聽聽聽聽Transaction聽tr聽=聽null;
23聽聽聽聽聽try聽{
24聽聽聽聽聽聽聽tr聽=聽session.beginTransaction();
25聽聽聽聽聽聽聽session.save(service);
26聽聽聽聽聽}聽catch聽(Exception聽e)聽{
27聽聽聽聽聽聽聽if聽(tr聽!=聽null)聽{
28聽聽聽聽聽聽聽聽聽tr.commit();
29聽聽聽聽聽聽聽}
30聽聽聽聽聽}聽finally聽{
31聽聽聽聽聽聽聽session.close();
32聽聽聽聽聽}
33聽聽聽聽聽
34聽聽聽聽聽session聽=聽HibernateUtil.getSession();
35聽聽聽聽聽service聽=聽getService();
36聽聽聽聽聽assertEquals(2,聽service.getBlockedSites().size());
37聽聽聽聽聽assertEquals(2,聽service.getTrustedSites().size());
38聽聽聽}
39聽聽聽
40聽聽聽private聽CustomService聽getService()聽{
41聽聽聽聽聽return聽(CustomService)聽session.get(CustomService.class,聽id);
42聽聽聽}
43聽聽聽聽聽
44聽
45聽}
2聽session.save(service);
3聽session.flush();
4聽session.close();
鍙堢粰CustomService鍔犱簡涓涓猲ame瀛楁,緇撴灉琛ㄦ槑,鍦ㄧ洿鎺ョ敤session.flush()鍜宻ession.close()鐨勬椂鍊?鏍規湰灝辨病鏈夋彁浜?涓嶈繃濂囨殑鏄?絎竴嬈¤繖鏍峰仛鎬庝箞浼氭彁浜や簡.
]]>
浼樼偣錛?br />
]]>
]]>
]]>
鏈夌┖瀛︿範涓涓媘anifest鐨勫叾浠栫殑鐢ㄩ?
]]>
Tags: java
Hibernate.groovy
Instead of interface, I use Closure for callback
Blogged with Flock
Tags: hibernategroovy
鍙戦乻oap message鐨勪唬鐮佸涓?
public class MessageSender {
/**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(MessageSender.class);
private HttpClient httpClient;
private PostMethod postMethod;
private MessageFactory messageFactory;
private String url;
private NameValuePair sessionId;
public MessageSender(String ip){
this.httpClient = new HttpClient();
try {
this.messageFactory = MessageFactory.newInstance();
} catch (SOAPException e) {
logger.error(e.getMessage());
}
this.url = generateRequestUrl(ip);
}
private String generateRequestUrl(String ip) {
return "http://" + ip + ":8080/vantage/TR069";
}
public SOAPMessage sendMessage(SOAPMessage input) throws IOException, SOAPException {
this.postMethod = new PostMethod(this.url);
byte[] dataAsBytes = null;
if (input == null) {
logger.debug("Send a empty post");
dataAsBytes = new byte[0];
} else {
ByteArrayOutputStream data = new ByteArrayOutputStream();
input.writeTo(data);
dataAsBytes = data.toByteArray();
}
RequestEntity entity = new ByteArrayRequestEntity(dataAsBytes);
this.postMethod.setRequestEntity(entity);
if (this.sessionId != null) {
this.postMethod.addParameter(this.sessionId);
}
this.httpClient.executeMethod(this.postMethod);
sessionId = this.postMethod.getParameter("SessionID");
InputStream in = this.postMethod.getResponseBodyAsStream();
if (null == in) {
return null;
}
return this.messageFactory.createMessage(null, in);
}
}
鏈鍒濈殑code閲岄潰,鍙湁涓涓狿ostMethod,榪欐牱姣忔浼氫竴鐩磌eep涓涓猦ttp榪炴帴.鍥犱負鍦╯erver绔?鍙洿鎺ョ敤http session鏉ヤ繚瀛榮erver鐨勭姸鎬佺殑,鎵浠ュ繀欏昏鏄繚鎸佹槸涓涓猻ession.鐢ㄤ竴涓狿ostMethod鍙互鍋氬埌榪欑偣.涓嶈繃濂囨殑鏄?鍦ㄥ彂絎簩嬈¤姹傜殑鏃跺?鎬庝箞閮芥嬁涓嶅埌http connection.涔熶笉鐭ラ亾鏄笉鏄痟ttpclient鐨刡ug.鍚庢潵鎵嶆瘡嬈¤皟鐢ㄩ兘閲嶆柊create涓涓狿ostMethod,浣嗘槸鎶婄涓嬈″緱鍒扮殑sessionID add榪涘幓.
inverse浼氬獎鍝嶅埌save鏃跺欑殑琛屼負銆傚鏋滀竴绔殑inverse璁句負true錛屽垯save搴旇鍦ㄥ彟涓绔繘琛?/p>
The Java language gives you all the room you need to write code that would be very difficult for others to understand. Java also permits you to write code that is very easy to understand. Most development teams would prefer the latter.
A style guide provides provides a map so that the code generated by a group of programmers will be consistent and, therefore, easier to read and maintain. Many people do not care for the style guide offered by Sun. This document is one alternative.
This document covers most areas where there could be confusion or difference of opinion. Areas that have never been a problem in our experience are undocumented.
if
, while
, for
, switch
, and catch
must be followed by a space.class Order
{
// fields
// constructors
// methods
}
All identifiers use letters ('A' through 'Z' and 'a' through 'z') and numbers ('0' through '9') only. No underscores, dollar signs or non-ascii characters.
All class and interface identifiers will use mixed case. The first letter of each word in the name will be uppercase, including the first letter of the name. All other letters will be in lowercase, except in the case of an acronym, which will be all upper case. (examples)
Package names will use lower case characters only. Try to keep the length under eight (8) characters. Multi-word package names should be avoided. (examples)
do..while
.
(examples and reasoning)return
in the middle of a method.
(reasoning)continue
.
(reasoning)break
other than in a switch statement.
(reasoning)
Use a separate line for an increment or decrement. (examples and reasoning)
Never use pre-increment or pre-decrement (examples and reasoning)
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand."
--- Martin Fowler, Refactoring: Improving the Design of Existing Code
Rather than trying to document how you perform a complex algorithm, try to make the algorithm easier to read by introducing more identifiers. This helps in the future in case the algorithm changes but someone forgets to change the documentation. (examples and reasoning)
鍘熸枃閾炬帴鍦?http://www.javaranch.com/style.jsp
姣斿璇存湁榪欐牱涓涓被Server,瑕佹牴鎹畇erver.properties鏉ュ垵濮嬪寲,涓寮濮嬬殑浠g爜鏄繖鏍風殑:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author sting
*/
public class Server {
private static final String FILE = "conf" + File.separator + "server.properties";
public void initial() throws IOException {
FileInputStream in = new FileInputStream(System.getProperty("APP_HOME") + File.separator + FILE);
Properties properties = new Properties();
properties.load(in);
// initial
}
}
鏂囦歡璺緞鍜屾枃浠跺悕閮芥槸hard code,寰堥毦嫻嬭瘯. 鎴戜滑棣栧厛鎶奿nitial()閲嶆瀯涓涓?浠g爜濡備笅:
public void initial(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
// initial
}
鑷沖皯,嫻嬭瘯鏃?鎴戜滑鍙互浼犺繘鏉ヨ嚜宸辯殑InputStream,涔熷彲浠ユ柟渚跨殑鏃跺欐祴璇曠敤鐨剆erver.properties,鎴栬呭共鑴嗕嬌鐢ㄥ唴鑱旂殑鏂囦歡,浠g爜濡備笅:
class ServerTest extends TestCase {
private Server server;
public void setUp() throws Exception {
this.server = new Server();
}
public void testInitial() throws Exception {
String serverProperties = "port=8080\n" +
"run_mode=normal";
InputStream in = new ByteArrayInputStream(serverProperties.getBytes());
this.server.initial(in);
// assert
}
}
浣嗘槸,鍦ㄥ疄闄呭伐浣滅殑浠g爜涓?鏂囦歡鍚嶅拰璺緞渚濈劧瑕乭ard code榪涗唬鐮佷腑.榪欐椂,鎴戜滑鍙互浣跨敤spring涓殑Resource鎺ュ彛鏉ヨ繘涓姝ユ敼榪涙垜浠殑浠g爜.
public class Server {
private Resource resource;
public void setResource(Resource r) {
this.resource = r;
}
public void initial() throws IOException {
Properties properties = new Properties();
properties.load(this.resource.getInputStream());
// initial
}
}
鍐嶅姞涓孌祍pring鐨勯厤緗枃浠?
<beans>
<bean id="server" class="Server">
<property name="resource" value="classpath:server.properties"/>
</bean>
</beans>
榪欐牱,Server鐨勪唬鐮佸畬鍏ㄤ笌鏂囦歡鐨勫叿浣撹礬寰勫拰鏂囦歡鍚嶆棤鍏?浠呬粎鐢ㄩ厤緗枃浠跺氨鍙互鎸囧畾,琛ㄨ揪鏇存竻妤?涔熸洿鏄撲簬嫻嬭瘯.
褰撶劧,浠呴檺浜庡凡緇忎嬌鐢╯pring鐨勯」鐩?