有多好用?
在任何窗口,只要敲Alt+space,就会激活launchy的小窗口,比如说你想运行firefox,直接敲firefox,firefox就会被运行了,比用书标点快多了,是吧?还有自动补全和提示。想打开文件夹?没问题,直接在launchy的窗口里输入地址就好了。甚至于网页也是ok的。在小窗口里输入http://www.blogjava.net/stingh711/就会直接到我的blog上啦(谁会记得这么长的...不过常用的网站就很快了)
launchy默认的只到Start Menu目录下面搜索.lnk文件,如果想通过launchy运行其他程序,比如说我常用的eclipse,该怎样做呢?首先,在launchy的窗口上右键,选Directories, 然后加入eclipse的目录,并在File Types里面加入.exe,这样launchy就会搜到eclipse了。
是不是真的很好用呢?赶快到http://www.launchy.net/上去下载吧。

Technorati Tags:

Technorati :

posted @ 2007-09-29 16:52 django 阅读(319) | 评论 (0)编辑 收藏
 
发觉我还真是个不能坚持的人。一直觉得应该好好写好一个blog,在用了blogjava之后,又用了donews,百度的blog,donews的把帐号忘记了,百度的好像不支持blog editor,而且...在百度上面的人感觉都是小孩子,实在是不适合我用...今天又到blogjava上面一看,发觉原来留下来的文章访问量还挺高,决定还是用这儿好了。

有的时候,在过很久来再来看以前写的东西,发觉写的还真是不太好,又没有心情去修改(写blog是要心情的,对吧?)以后写,一定要好好酝酿。希望可以坚持下去。

Blogged with Flock

Tags:

posted @ 2007-09-29 16:23 django 阅读(277) | 评论 (1)编辑 收藏
 

Hibernate.groovy


import  org.hibernate.cfg.Configuration
import  org.hibernate.Session
import  org.hibernate.SessionFactory
import  org.hibernate.Transaction
import  org.hibernate.tool.hbm2ddl.SchemaUpdate

class  Hibernate {
    def 
static  sessionFactory
   
static  { 
       
try  { 
            Configuration cfg 
=   new  Configuration()
            cfg.configure()            
           
new  SchemaUpdate(cfg).execute( true true )      
            sessionFactory 
=  cfg.buildSessionFactory()    
        } 
catch  (Exception e) {     
            e.printStackTrace()      
        }    
    }    

    Hibernate() {}   

    
private  Session getSession() {    
        
return  sessionFactory.openSession()    
    }    

    Object execute(closure) {       
        def s 
=  getSession()        
            def tr 
=   null         
            def result 
=   null         
            
try  {            
                tr 
=  s.beginTransaction()          
                    result 
=  closure.call(s)       
                    tr.commit()      
            } 
catch  (Exception e) {     
                e.printStackTrace()      
                    
if  (tr  !=   null ) {     
                        tr.rollback()    
                    }        
            } 
finally  {     
                s.close()   
            }        
        
return  result   
    }    

    
void  saveOrUpdate(obj) {
        def saveClosure 
=  { s  ->  s.saveOrUpdate(obj) }      
        execute(saveClosure)    
    }    

    List executeQuery(hql) {     
        execute({ s 
->  s.createQuery(hql).list() })   
    }    

    List executeQuery(hql, parameters) {    
        def query 
=  { s  ->         
            def q 
=  s.createQuery(hql)        
                
if  (parameters  !=   null ) {         
                    
for  (i in  0 ..parameters.size() - 1 ) {       
                        q.setParameter(i, parameters[i])       
                    }            
                }            
            q.list()       
        }        
        execute(query)    
    }    

    def get(clazz, id) {    
        
return  execute({ s  ->  s.get(clazz, id) })   
    }    

    
void  delete(obj) {       
        execute({ s 
->  s.delete(obj) })   
    }
}

Instead of interface, I use Closure for callback

Blogged with Flock

Tags:

posted @ 2006-12-20 22:54 django 阅读(465) | 评论 (0)编辑 收藏
 

这两天在写一个tr069的simulator,原理很简单啦,用httpclient模拟tr069的client端,发送soap message到我们的acs server.

发送soap 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里面,只有一个PostMethod,这样每次会一直keep一个http连接.因为在server端,只直接用http session来保存server的状态的,所以必须要是保持是一个session.用一个PostMethod可以做到这点.不过奇怪的是,在发第二次请求的时候,怎么都拿不到http connection.也不知道是不是httpclient的bug.后来才每次调用都重新create一个PostMethod,但是把第一次得到的sessionID add进去.

posted @ 2006-12-18 23:30 django 阅读(5669) | 评论 (0)编辑 收藏
 

所有双向的关联必须有一端被设置为inverse.在一对多关联中,它必须代表多的一端.在多对多中,可以任意选取一端.

inverse会影响到save时候的行为。如果一端的inverse设为true,则save应该在另一端进行

posted @ 2006-12-11 23:14 django 阅读(280) | 评论 (0)编辑 收藏
 

Introduction

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.

1 - Formatting

    1.1 - Indentation

    All indents are four spaces. All indenting is done with spaces, not tabs. (examples and reasoning)
    Matching braces always line up vertically in the same column as their construct. (examples)
    All if, while and for statements must use braces even if they control just one statement. (reasoning and examples)

    1.2 - Spacing

    All method names should be immediately followed by a left parenthesis.
    All array dereferences should be immediately followed by a left square bracket.
    Binary operators should have a space on either side.
    Unary operators should be immediately preceded or followed by their operand.
    Commas and semicolons are always followed by whitespace.
    All casts should be written with no spaces.
    The keywords if, while, for, switch, and catch must be followed by a space.
    (examples)

    1.3 - Class Member Ordering

    class Order
    {
    // fields

    // constructors

    // methods
    }

    1.4 - Maximum Line Length

    Avoid making lines longer than 120 characters. (reasoning)

    1.5 - Parentheses

    Parentheses should be used in expressions not only to specify order of precedence, but also to help simplify the expression. When in doubt, parenthesize.

2 - Identifiers

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.

    2.1 - Classes and Interfaces

    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)

    2.2 - Packages

    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)

    2.3 - All Other Identifiers

    All other identifiers, including (but not limited to) fields, local variables, methods and parameters, will use the following naming convention. This includes identifiers for constants.

    The first letter of each word in the name will be uppercase, except for the first letter of the name. All other letters will be in lowercase, except in the case of an embedded acronym, which will be all uppercase. Leading acronyms are all lower case. (examples and reasoning)

    Hungarian notation and scope identification are not allowed. (reasoning and examples)

    Test code is permitted to use underscores in identifiers for methods and fields. (reasoning and examples)

3 - Coding

    3.1 - Constructs to Avoid

    Never use do..while. (examples and reasoning)
    Never use return in the middle of a method. (reasoning)
    Never use continue. (reasoning)
    Never use break other than in a switch statement. (reasoning)

    3.2 - Do Not Compound Increment Or Decrement Operators

    Use a separate line for an increment or decrement. (examples and reasoning)

    Never use pre-increment or pre-decrement (examples and reasoning)

    3.3 - Initialization

    Declare variables as close as possible to where they are used. (examples)

    3.4 - Access

    All fields must be private, except for some constants.

4 - Self-Documenting Code

				"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

posted @ 2006-11-27 16:18 django 阅读(406) | 评论 (0)编辑 收藏
 
Pro spring
The art of project management
Analyse patterns
posted @ 2006-11-23 23:55 django 阅读(282) | 评论 (0)编辑 收藏
 
First of all, add a bean in spring's configuration files like this:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderconfigurer">
    <property name="locations">
        <list>
            <value>...property files</value>
        </list>
    </property>
</bean>
Put the property files under classpath, then you can use ${property name} to reference properties in your property files in your spring configuration files.
Also, system properties and properties sent it by -D are also available through ${property name}.
posted @ 2006-11-23 23:53 django 阅读(481) | 评论 (0)编辑 收藏
 
  1. mysql.exe -uroot -ptest -e "source create.sql"
To be continued...
posted @ 2006-11-22 17:42 django 阅读(290) | 评论 (0)编辑 收藏
 

在项目中,经常要用到读系统文件.在项目的遗留代码中,都是在系统启动是传入一个APP_HOME,然后根据相对路径去读文件.这样做的缺点是比较难测试,而且自动化的测试更难.

比如说有这样一个类Server,要根据server.properties来初始化,一开始的代码是这样的:

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,很难测试. 我们首先把initial()重构一下,代码如下:


public void initial(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
// initial
}

至少,测试时,我们可以传进来自己的InputStream,也可以方便的时候测试用的server.properties,或者干脆使用内联的文件,代码如下:

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
}
}

但是,在实际工作的代码中,文件名和路径依然要hard code进代码中.这时,我们可以使用spring中的Resource接口来进一步改进我们的代码.

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
}
}

再加一段spring的配置文件:

<beans>
<bean id="server" class="Server">
<property name="resource" value="classpath:server.properties"/>
</bean>
</beans>

这样,Server的代码完全与文件的具体路径和文件名无关,仅仅用配置文件就可以指定,表达更清楚,也更易于测试.

当然,仅限于已经使用spring的项目.

posted @ 2006-11-19 11:54 django 阅读(3654) | 评论 (0)编辑 收藏
仅列出标题
共12页: First 上一页 4 5 6 7 8 9 10 11 12 下一页