Monday, September 28, 2009

request.getRequestURL()和request.getRequestURI()有什么区别

更多精彩请到 http://www.139ya.com

Request.getRequestURL返回的是请求的全部,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。
request.getRequestURI得到的是request URL的部分值,并且web容器没有decode过的
getRequestURL:
public java.lang.StringBuffer getRequestURL()
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

Because
this method returns a StringBuffer, not a string, you can modify the URL easily, for example, to append query parameters.

This method is useful
for creating redirect messages and for reporting errors.

Returns:
a StringBuffer object containing the reconstructed URL

getRequestURI:
public java.lang.String getRequestURI()
Returns the part of
this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:
First line of HTTP request Returned Value
POST
/some/path.html HTTP/1.1 /some/path.html
GET http:
//foo.bar/a.html HTTP/1.0 /a.html
HEAD /xyz?a=b HTTP/1.1 /xyz
To reconstruct an URL with a scheme and host, use HttpUtils.getRequestURL(javax.servlet.http.HttpServletRequest).
Returns:
a String containing the part of the URL from the protocol name up to the query string
See Also:
HttpUtils.getRequestURL(javax.servlet.http.HttpServletRequest)

request.getRequestDispatcher(url).forward(request,response)

更多精彩请到 http://www.139ya.com


基础的东西,还是要打牢固的
response.sendRedirect(url)和request.getRequestDispatcher(url).forward(request,response)

response.sendRedirect(url) -- 重定向到指定URL
request.getRequestDispatcher(url).forward(request,response) -- 请求转发到指定URL

比如说有人打电话给公司前台找小王,这时候,公司的前台会有两种选择
一:通过转接的方式把电话转到小王那里(forward)
二:通过告诉对方小王的电话号码的方式让对方打给小王(sendRedirect)

涉及到电话挂断不挂断的问题
就像程序里面一样,请求是否有中断,决定参数是否能传递

response.sendRedirect(url)跳转到指定的URL地址,产生一个新的request,所以要传递参数只有在url后加参数,如:
url?id=1.
request.getRequestDispatcher(url).forward(request,response) 是直接将请求转发到指定URL,所以该请求能够直接获得上一个请求的数据,也就是说采用请求转发,request对象始终存在,不会重新创建。而 sendRedirect()会新建request对象,所以上一个request中的数据会丢失。
2....

redirect 会首先发一个response给浏览器, 然后浏览器收到这个response后再发一个requeset给服务器, 然后服务器发新的response给浏览器. 这时页面收到的request是一个新从浏览器发来的.

forward 发生在服务器内部, 在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面收到的request不是从浏览器直接发来了,可能己经放了数据.

遭遇了"Call to undefined function socket_create()"

更多精彩请到 http://www.139ya.com

今天在试验php socket时,在c:\window\system32\cmd.exe里遭遇了Fatal error: Call to undefined function socket_create()。

google一下,发现跟我有同样问题的人还不少。一般的解决方案是:

1. 找到php.ini,看 extension=php_gd2.dll 和 extension=php_sockets.dll 扩展是否打开;

2. 看phpInfo()显示的内容里,socket模块是否为enable;



我检查了一下,发现都是符合的。但错误仍然出现?怎么回事呢?



后来我才发现,原来是我在phpInfo()里看到的和在cmd窗口里使用的php不是同一个东西。



原因是我多次安装过php. 先前的php在系统的环境变量里面注册了path。所以在cmd窗口里使用的是以前的php. 而在phpInfo()里显示的是现在的php的设置。



解决的办法很简单了,就把系统环境变量里的path里,指向老的Php的路径改为指向正在使用的Php的路径。这样在cmd里的php和在浏览器里的php就是同一个东西了。

Thursday, September 24, 2009

10 hot and useful tips to maximize iPhone's battery life

更多精彩请到 http://www.139ya.com


http://alaminahmed.com/blog/read/10-hot-and-useful-tips-to-maximize-iphone-s-battery-life


10 hot and useful tips to maximize iPhone's battery life

posted on: Monday 7th July 2008
Apple Inc. claims that iPhone has 8 hours talk time, 250 hours of standby, 6 hours of Internet, 7 hours of video playback and 24 hours of audio playback!! These are very impressive numbers, except that claims has lots of strings attached which no one actually reads it.

Although, you may not get even close to these numbers, but I will attempt to provide all tweaks and settings to get as close as possible. I have collected most of these tips from Apple's fine prints and some from support page of an iPod. Don't worry, I am not going to ask you to turn of Wi-Fi or other features that would simply cripple your iPhone.

  1. Brightness setting: this alone can do the trick of getting most out your iPhone's (or any device for that matter) battery life. Even though iPhone has bright and gorgeous screen, you may want to turn it down to get that screen playable little longer!
    How-to: "Settings" -> "Brightness" -> slide it to about one seventh
  2. Auto brightness: iPhone has built-in sensor that checks the lighting condition where ever you are and adjust the brightness accordingly. This is sweet feature, but the sensor eats up battery life.
    How-to: "Settings" -> "Brightness" -> turn the "Auto-Brightness" switch "OFF"
  3. Auto-Lock: turn that display off as frequently as possible. Make sure it is set to 1 Minute
    How-to: "Settings" -> "General" -> "Auto-Lock" -> "1 Minute"
  4. Auto detect Wi-Fi: iPhone has feature to
    How-to: "Settings" -> "Wi-Fi" -> "Ask to Join Networks" -> turn "OFF"
  5. Email Check frequency: this may not be suitable for lots of business or frequent email users, but it does save battery life. You can set iPhone to check email every 15, 30, 60 minutes or do manual check. Of course, ideal setting for saving battery would be "Manual", but you can also set to "Every hour"
    How-to: "Settings" -> "Mail" -> "Auto-Check" -> select "Manual" or "Every hour"
  6. Turn of some sounds: iPhone makes noise for about everything you do on it. You can turn off some of these sound effects such as keyboard click noise and Lock Sound noise. These two are really unnecessary for the most part.
    How-to: "Settings" -> "Sounds" -> "Lock Sounds" turn "OFF" and "Keyboard Clicks" turn OFF
  7. Turn of that Equalizer: yes, according to Apple, turning of Equalizer will save little battery time!
    How-to: "Settings" -> "iPod" -> "EQ" -> "Off"
  8. Turn of Sound Check: iPhone provides Sound Check feature to adjust music volume to keep consistent range for all songs. Most of the time it does not work (at least for my songs). So you can turn it off to save some juice.
    How-to: "Settings" -> "iPod -> "Sound Check" turn "OFF"
  9. Audio encoding: compress size of your music file for better cache purpose and encode using MP3 or AAC encoding. AIFF and some other formats are not good and eats up CPU cycles
  10. Room Temperature: Keep iPhone in room temperature and do not charge your iPhone while it is in carrying case or cover. This keeps battery in ideal temperature even when charging.
Hope these tricks help you get more life out of your iPhone's battery. Just as an additional note, Apple has also announced iPhone's battery replacement program for $79.99 + shipping. read it here: http://www.apple.com/support/iphone/service/battery/

Wednesday, September 23, 2009

jmock2.5 使用总结

更多精彩请到 http://www.139ya.com

jmock是写单元测试时需要生成mock对象时很好的辅助库。

软件地址: http://www.jmock.org

本文是我今天摸索使用jmock(v2.4)的总结。不是初学指南,当你入门了,我想可以作为简单手册。是原版文档补充。

一般使用模式:
生成Mockery对象,可以全局共享
Java代码
  1. Mockery context = new JUnit4Mockery() {
  2. {
  3. //声明针对类mock(采用cglib生成子类),final方法无法mock,针对接口则会采用动态代理,不需要声明下面的代码
  4. setImposteriser(ClassImposteriser.INSTANCE);
  5. }
  6. };


例子
Java代码
  1. final UserDAO dao = context.mock(UserDAO.class);
  2. final Integer id1 = 1;
  3. final User user1 = new User();
  4. user1.setId(1);
  5. user1.setUserId(1);
  6. user1.setName("1");
  7. final Integer id2 = 2;
  8. final User user2 = new User();
  9. user2.setId(2);
  10. user2.setUserId(2);
  11. user2.setName("2");
  12. context.checking(new Expectations(){
  13. {
  14. //one表示调用一次。
  15. one(dao).find(id1);will(returnValue(user1));
  16. //再用另一参数做一次mock
  17. one(dao).find(id2);will(returnValue(user2));
  18. //可以忽略mock对象
  19. //ignoring(dao);
  20. }
  21. });

context.checking(new Expectations(){中的语法总结

1.
Java代码
  1. context.checking(new Expectations(){
  2. {
  3. //另一种连续调用: 同个方法同样参数,调用多次返回值各不同,声明了调用次序
  4. atLeast(1).of(dao).find(id1);
  5. will(onConsecutiveCalls(
  6. returnValue(user1),
  7. returnValue(user2)));
  8. }
  9. });

2.
Java代码
  1. context.checking(new Expectations(){
  2. {
  3. allowing(dao).find(id);will(throwException(new FindException()));//也可以throw RuntimeException
  4. }
  5. });

3.
Java代码
  1. {
  2. //allowing不限定调用次数
  3. allowing(dao).find(id);will(returnValue(user));
  4. never(dao).findByUserId(userId);
  5. //下面两行是从jmock官网copy下来的,可以为相同的方法用不同的参数mock多次
  6. //one(calculator).load("x"); will(returnValue(10));
  7. //never(calculator).load("y");
  8. }
  9. });

4.
Java代码
  1. context.checking(new Expectations(){
  2. {
  3. //可以在mock方法时做一些参数操作
  4. allowing(srv).findUser(with(lessThan(10)));will(returnValue(user1));
  5. allowing(srv).findUser(with(greaterThan(10)));will(returnValue(user2));
  6. }
  7. });

5.
Java代码
  1. context.checking(new Expectations(){
  2. {
  3. //inSequence(sequence),会把声明的方法放进序列,调用次序必须符合声明次序
  4. one(dao).find(id1);will(returnValue(user1));inSequence(sequence);
  5. one(dao).find(id2);will(returnValue(user2));inSequence(sequence);
  6. }
  7. });
  8. //调用次序必须符合声明次序
  9. assertEquals(user1,dao.find(id1));
  10. assertEquals(user2,dao.find(id2));

Friday, September 18, 2009

science & software

更多精彩请到 http://www.139ya.com

science & software http://www.randelshofer.ch/

上一篇的翻译

更多精彩请到 http://www.139ya.com


翻译:

史蒂夫 乔布斯(Steve Jobs)在斯坦福大学2005年毕业典礼上的演讲

我今天很荣幸能和你们一起参加毕业典礼,斯坦福大学是世界上最好的大学之一。我从来没有从大学中毕业。说实话,今天也许是在我的生命中离大学毕业最近的一天了。今天我想向你们讲述我生活中的三个故事。不是什么大不了的事情,只是三个故事而已。
  
  第一个故事是关于如何把生命中的点点滴滴串连起来。
  
  我在Reed大学读了六个月之后就退学了,但是在十八个月以后——我真正的作出退学决定之前,我还经常去学校。我为什么要退学呢?
  
  故事从我出生的时候讲起。我的亲生母亲是一个年轻的,没有结婚的大学毕业生。她决定让别人收养我, 她十分想让我被大学毕业生收养。所以在我出生的时候,她已经做好了一切的准备工作,能使得我被一个律师和他的妻子所收养。但是她没有料到,当我出生之后, 律师夫妇突然决定他们想要一个女孩。所以我的生养父母(他们在待选名单上)突然在半夜接到了一个电话:“我们现在这儿有一个不小心生出来的男婴,你们想要他吗?”他们回答道: “当然!”但是我亲生母亲随后发现,我的养母从来没有上过大学,我的养父 甚至从没有读过高中。她拒绝签这个收养合同。只是在几个月以后,我的父母答应她一定要让我上大学,那个时候她才软化同意。
  
  在十七岁那年,我真的上了大学。但是我很愚蠢的选择了一个几乎和你们斯坦福大学一样贵的学校, 我父母还处于蓝领阶层,他们几乎把所有积蓄都花在了我的学费上面。在六个月后, 我已经看不到其中的价值所在。我不知道我真正想要做什么,我也不知道大学能怎样帮助我找到答案。但是在这里,我几乎花光了我父母这一辈子的 全部积蓄。所以我决定要退学,我觉得这是个正确的决定。不能否认,我当时确实非常的害怕, 但是现在回头看看,那的确是我这一生中最棒的一个决定。在我做出退学决定的那一刻, 我终于可以不必去读那些令我提不起丝毫兴趣的课程了。然后我可以开始去修那些看起来有点意思的课程。

但是这并不是那么罗曼蒂克。我失去了我的宿舍,所以我只能在朋友房间的地板上面睡觉,我去捡可以换5美分的可乐罐,仅仅为了填饱肚子, 在星期天的晚上,我需要走七英里的路程,穿过这个城市到Hare Krishna神庙(注:位于纽约Brooklyn下城),只是为了能吃上好饭——这个星期唯一一顿好一点的饭,我喜欢那里的饭菜。

  我跟着我的直觉和好奇心走, 遇到的很多东西,此后被证明是无价之宝。让我给你们举一个例子吧:
  
  Reed大学在那时提供也许是全美最好的美术字课程。在这个大学里面的每个海报, 每个抽屉的标签上面全都是漂亮的美术字。因为我退学了, 不必去上正规的课程, 所以我决定去参加这个课程,去学学怎样写出漂亮的美术字。我学到了san serif 和serif字体, 我学会了怎么样在不同的字母组合之中改变空白间距, 还有怎么样才能作出最棒的印刷式样。那种美好、历史感和艺术精妙,是科学永远不能捕捉到的, 我发现那实在是太迷人了。
  
  当时看起来这些东西在我的生命中,好像都没有什么实际应用的可能。但是十年之后,当我们在设计第一台Macintosh电脑的时候,就不是那样了。我把当时我学的那些 东西全都设计进了Mac。那是第一台使用了漂亮的印刷字体的电脑。如果我当时没有退学, 就不会有机会去参加这个我感兴趣的美术字课程, Mac就不会有这么多丰富的字体,以及赏心悦目的字体间距。因为Windows只是抄袭了Mac,所以现在个人电脑就不会有现在这么美妙的字型了。
  
  当然我在大学的时候,还不可能把从前的点点滴滴串连起来,但是当我十年后回顾这一切的时候,真的豁然开朗了。

  再次说明的是,你在向前展望的时候不可能将这些片断串连起来;你只能在回顾的时候将点点滴滴串连起来。所以你必须相信这些片断会在你未来的某一天串连起来。你必须要相信某些东西:你的勇气、目的、生命、因缘......这个过程从来没有令我失望,只是让我的生命更加地与众不同。
  
  我的第二个故事是关于爱和失去。
  
  我非常幸运, 因为我在很早的时候就找到了我钟爱的东西。Woz和我在二十岁的时候就在父母的车库里面开创了苹果公司。我们工作得很努力, 十年之后, 这个公司从那两个车库中的穷小子发展到了超过四千名的雇员、价值超过二十亿的大公司。在公司成立的第九年,我们刚刚发布了最好的产品,那就是Macintosh。我也快要到三十岁了。在那一年, 我被炒了鱿鱼。你怎么可能被你自己创立的公司炒了鱿鱼呢? 嗯,在苹果快速成长的时候,我们雇用了一个很有天分的家伙和我一起管理这个公司, 在最初的几年,公司运转的很好。但是后来我们对未来的看法发生了分歧, 最终我们吵了起来。当争吵不可开交的时候, 董事会站在了他的那一边。所以在三十岁的时候, 我被炒了。在这么多人目光下我被炒了。在而立之年,我生命的全部支柱离自己远去, 这真是毁灭性的打击。
  
  在最初的几个月里,我真是不知道该做些什么。我觉得我很令上一代的创业家们很失望,我把他们交给我的接力棒弄丢了。我和创办惠普的David Pack、创办Intel的Bob Noyce见面,并试图向他们道歉。我把事情弄得糟糕透顶了。但是我渐渐发现了曙光, 我仍然喜爱我从事的这些东西。苹果公司发生的这些事情丝毫的没有改变这些, 一点也没有。我被驱逐了,但是我仍然钟爱我所做的事情。所以我决定从头再来。
  
  我当时没有觉察, 但是事后证明, 从苹果公司被炒是我这辈子发生的最棒的事情。因为,作为一个成功者的负重感被作为一个创业者的轻松感觉所重新代替, 没有比这更确定的事情了。这让我觉得如此自由, 进入了我生命中最有创造力的一个阶段。

  在接下来的五年里, 我创立了一个名叫NeXT的公司, 还有一个叫Pixar的公司, 然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“玩具总动员”,Pixar现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中,Apple收购了NeXT, 然后我又回到了Apple公司。我们在NeXT发展的技术在Apple的今天的复兴之中发挥了关键的作用。而且,我还和Laurence 一起建立了一个幸福完美的家庭。
  
  我可以非常肯定,如果我不被Apple开除的话, 这其中一件事情也不会发生的。这个良药的味道实在是太苦了,但是我想病人需要这个药。有些时候, 生活会拿起一块砖头向你的脑袋上猛拍一下。不要失去信仰。我很清楚唯一使我一直走下去的,就是我做的事情令我无比钟爱。你需要去找到你所爱的东西。对于工作是如此, 对于你的爱人也是如此。你的工作将会占据生活中很大的一部分。你只有相信自己所做的是伟大的工作, 你才能怡然自得。如果你现在还没有找到, 那么继续找、不要停下来,只要全心全意的去找, 在你找到的时候,你的心会告诉你的。就像任何真诚的关系, 随着岁月的流逝只会越来越紧密。所以继续找,直到你找到它,不要停下来!
  
  我的第三个故事是关于死亡的。
  
  当我十七岁的时候, 我读到了一句话:“如果你把每一天都当作生命中最后一天去生活的话,那么有一天你会发现你是正确的。”这句话给我留下了一个印象。从那时开始,过了33 年,我在每天早晨都会对着镜子问自己:“如果今天是我生命中的最后一天, 你会不会完成你今天想做的事情呢?”当答案连续多天是“No”的时候, 我知道自己需要改变某些事情了。
  
  “记住你即将死去”是我一生中遇到的最重要箴言。它帮我指明了生命中重要的选择。因为几乎所有的事情, 包括所有的荣誉、所有的骄傲、所有对难堪和失败的恐惧,这些在死亡面前都会消失。我看到的是留下的真正重要的东西。你有时候会思考你将会失去某些东西, “记住你即将死去”是我知道的避免这些想法的最好办法。你已经赤身裸体了, 你没有理由不去跟随自己内心的声音。
  
  大概一年以前, 我被诊断出癌症。我在早晨七点半做了一个检查, 检查清楚的显示在我的胰腺有一个肿瘤。我当时都不知道胰腺是什么东西。医生告诉我那很可能是一种无法治愈的癌症, 我还有三到六个月的时间活在这个世界上。我的医生叫我回家, 然后整理好我的一切, 那是医生对临终病人的标准程序。那意味着你将要把未来十年对你小孩说的话在几个月里面说完.;那意味着把每件事情都安排好, 让你的家人会尽可能轻松的生活;那意味着你要说“再见了”。
  
  我拿着那个诊断书过了一整天,那天晚上我作了一个活切片检查,医生将一个内窥镜从我的喉咙伸进去,通过我的胃, 然后进入我的肠子, 用一根针在我的胰腺上的肿瘤上取了几个细胞。我当时是被麻醉的,但是我的妻子在那里, 后来告诉我,当医生在显微镜下观察这些细胞的时候他们开始尖叫, 因为这些细胞最后竟然是一种非常罕见的可以用手术治愈的胰腺癌症细胞。我做了这个手术, 现在我痊愈了。
  
  那是我最接近死亡的时候, 我希望这也是以后的几十年最接近的一次。从死亡线上又活了过来, 我可以比以前把死亡只当成一 种想象中的概念的时候,更肯定一点地对你们说:

  没有人愿意死, 即使人们想上天堂, 也不会为了去那里而死。但是死亡是我们每个人共同的终点。从来没有人能够逃脱它。也应该如此。因为死亡就是生命中最好的一个发明。它将旧的清除以便给新的让路。你们现在是新的, 但是从现在开始不久以后, 你们将会逐渐的变成旧的然后被送离人生舞台。我很抱歉这很戏剧性, 但是这十分的真实。
  
  你们的时间很有限, 所以不要将他们浪费在重复其他人的生活上。不要被教条束缚,那意味着你和其他人思考的结果一起生活。不要被其他人喧嚣的观点掩盖你真正的内心的声音。还有最重要的是, 你要有勇气去听从你直觉和心灵的指示——它们在某种程度上知道你想要成为什么样子,所有其他的事情都是次要的。
  
  当我年轻的时候, 有一本叫做“整个地球的目录”振聋发聩的杂志,它是我们那一代人的圣经之一。它是一个叫Stewart Brand的家伙在离这里不远的Menlo Park编辑的, 他象诗一般神奇地将这本书带到了这个世界。那是六十年代后期, 在个人电脑出现之前, 所以这本书全部是用打字机,、剪刀还有偏光镜制造的。有点像用软皮包装的google, 在google出现三十五年之前:这是理想主义的,其中有许多灵巧的工具和伟大的想法。

Stewart和他的伙伴出版了几期的“整个地球的目录”,当它完成了自己使命的时候, 他们做出了最后一期的目录。那是在七十年代的中期, 我正是你们的年纪。在最后一期的封底上是清晨乡村公路的照片(如果你有冒险精神的话,你可以自己找到这条路的),在照片之下有这样一段话:“求知若饥,虚心若愚。”这是他们停止了发刊的告别语。“求知若饥,虚心若愚。”我总是希望自己能够那样,现在, 在你们即将毕业,开始新的旅程的时候, 我也希望你们能这样:
  
  求知若饥,虚心若愚。
  
  非常感谢你们

'You've got to find what you love,' Jobs says

更多精彩请到 http://www.139ya.com

http://news-service.stanford.edu/news/2005/june15/jobs-061505.html



This is the text of the Commencement address by Steve Jobs, CEO of Apple Computer and of Pixar Animation Studios, delivered on June 12, 2005.

I am honored to be with you today at your commencement from one of the finest universities in the world. I never graduated from college. Truth be told, this is the closest I've ever gotten to a college graduation. Today I want to tell you three stories from my life. That's it. No big deal. Just three stories.

The first story is about connecting the dots.

I dropped out of Reed College after the first 6 months, but then stayed around as a drop-in for another 18 months or so before I really quit. So why did I drop out?

It started before I was born. My biological mother was a young, unwed college graduate student, and she decided to put me up for adoption. She felt very strongly that I should be adopted by college graduates, so everything was all set for me to be adopted at birth by a lawyer and his wife. Except that when I popped out they decided at the last minute that they really wanted a girl. So my parents, who were on a waiting list, got a call in the middle of the night asking: "We have an unexpected baby boy; do you want him?" They said: "Of course." My biological mother later found out that my mother had never graduated from college and that my father had never graduated from high school. She refused to sign the final adoption papers. She only relented a few months later when my parents promised that I would someday go to college.

And 17 years later I did go to college. But I naively chose a college that was almost as expensive as Stanford, and all of my working-class parents' savings were being spent on my college tuition. After six months, I couldn't see the value in it. I had no idea what I wanted to do with my life and no idea how college was going to help me figure it out. And here I was spending all of the money my parents had saved their entire life. So I decided to drop out and trust that it would all work out OK. It was pretty scary at the time, but looking back it was one of the best decisions I ever made. The minute I dropped out I could stop taking the required classes that didn't interest me, and begin dropping in on the ones that looked interesting.

It wasn't all romantic. I didn't have a dorm room, so I slept on the floor in friends' rooms, I returned coke bottles for the 5¢ deposits to buy food with, and I would walk the 7 miles across town every Sunday night to get one good meal a week at the Hare Krishna temple. I loved it. And much of what I stumbled into by following my curiosity and intuition turned out to be priceless later on. Let me give you one example:

Reed College at that time offered perhaps the best calligraphy instruction in the country. Throughout the campus every poster, every label on every drawer, was beautifully hand calligraphed. Because I had dropped out and didn't have to take the normal classes, I decided to take a calligraphy class to learn how to do this. I learned about serif and san serif typefaces, about varying the amount of space between different letter combinations, about what makes great typography great. It was beautiful, historical, artistically subtle in a way that science can't capture, and I found it fascinating.

None of this had even a hope of any practical application in my life. But ten years later, when we were designing the first Macintosh computer, it all came back to me. And we designed it all into the Mac. It was the first computer with beautiful typography. If I had never dropped in on that single course in college, the Mac would have never had multiple typefaces or proportionally spaced fonts. And since Windows just copied the Mac, its likely that no personal computer would have them. If I had never dropped out, I would have never dropped in on this calligraphy class, and personal computers might not have the wonderful typography that they do. Of course it was impossible to connect the dots looking forward when I was in college. But it was very, very clear looking backwards ten years later.

Again, you can't connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future. You have to trust in something — your gut, destiny, life, karma, whatever. This approach has never let me down, and it has made all the difference in my life.

My second story is about love and loss.

I was lucky — I found what I loved to do early in life. Woz and I started Apple in my parents garage when I was 20. We worked hard, and in 10 years Apple had grown from just the two of us in a garage into a $2 billion company with over 4000 employees. We had just released our finest creation — the Macintosh — a year earlier, and I had just turned 30. And then I got fired. How can you get fired from a company you started? Well, as Apple grew we hired someone who I thought was very talented to run the company with me, and for the first year or so things went well. But then our visions of the future began to diverge and eventually we had a falling out. When we did, our Board of Directors sided with him. So at 30 I was out. And very publicly out. What had been the focus of my entire adult life was gone, and it was devastating.

I really didn't know what to do for a few months. I felt that I had let the previous generation of entrepreneurs down - that I had dropped the baton as it was being passed to me. I met with David Packard and Bob Noyce and tried to apologize for screwing up so badly. I was a very public failure, and I even thought about running away from the valley. But something slowly began to dawn on me — I still loved what I did. The turn of events at Apple had not changed that one bit. I had been rejected, but I was still in love. And so I decided to start over.

I didn't see it then, but it turned out that getting fired from Apple was the best thing that could have ever happened to me. The heaviness of being successful was replaced by the lightness of being a beginner again, less sure about everything. It freed me to enter one of the most creative periods of my life.

During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I returned to Apple, and the technology we developed at NeXT is at the heart of Apple's current renaissance. And Laurene and I have a wonderful family together.

I'm pretty sure none of this would have happened if I hadn't been fired from Apple. It was awful tasting medicine, but I guess the patient needed it. Sometimes life hits you in the head with a brick. Don't lose faith. I'm convinced that the only thing that kept me going was that I loved what I did. You've got to find what you love. And that is as true for your work as it is for your lovers. Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

My third story is about death.

When I was 17, I read a quote that went something like: "If you live each day as if it was your last, someday you'll most certainly be right." It made an impression on me, and since then, for the past 33 years, I have looked in the mirror every morning and asked myself: "If today were the last day of my life, would I want to do what I am about to do today?" And whenever the answer has been "No" for too many days in a row, I know I need to change something.

Remembering that I'll be dead soon is the most important tool I've ever encountered to help me make the big choices in life. Because almost everything — all external expectations, all pride, all fear of embarrassment or failure - these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die is the best way I know to avoid the trap of thinking you have something to lose. You are already naked. There is no reason not to follow your heart.

About a year ago I was diagnosed with cancer. I had a scan at 7:30 in the morning, and it clearly showed a tumor on my pancreas. I didn't even know what a pancreas was. The doctors told me this was almost certainly a type of cancer that is incurable, and that I should expect to live no longer than three to six months. My doctor advised me to go home and get my affairs in order, which is doctor's code for prepare to die. It means to try to tell your kids everything you thought you'd have the next 10 years to tell them in just a few months. It means to make sure everything is buttoned up so that it will be as easy as possible for your family. It means to say your goodbyes.

I lived with that diagnosis all day. Later that evening I had a biopsy, where they stuck an endoscope down my throat, through my stomach and into my intestines, put a needle into my pancreas and got a few cells from the tumor. I was sedated, but my wife, who was there, told me that when they viewed the cells under a microscope the doctors started crying because it turned out to be a very rare form of pancreatic cancer that is curable with surgery. I had the surgery and I'm fine now.

This was the closest I've been to facing death, and I hope its the closest I get for a few more decades. Having lived through it, I can now say this to you with a bit more certainty than when death was a useful but purely intellectual concept:

No one wants to die. Even people who want to go to heaven don't want to die to get there. And yet death is the destination we all share. No one has ever escaped it. And that is as it should be, because Death is very likely the single best invention of Life. It is Life's change agent. It clears out the old to make way for the new. Right now the new is you, but someday not too long from now, you will gradually become the old and be cleared away. Sorry to be so dramatic, but it is quite true.

Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma — which is living with the results of other people's thinking. Don't let the noise of others' opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.

When I was young, there was an amazing publication called The Whole Earth Catalog, which was one of the bibles of my generation. It was created by a fellow named Stewart Brand not far from here in Menlo Park, and he brought it to life with his poetic touch. This was in the late 1960's, before personal computers and desktop publishing, so it was all made with typewriters, scissors, and polaroid cameras. It was sort of like Google in paperback form, 35 years before Google came along: it was idealistic, and overflowing with neat tools and great notions.

Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: "Stay Hungry. Stay Foolish." It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.

Stay Hungry. Stay Foolish.

Thank you all very much

Monday, September 14, 2009

漫谈OCL概念、特征和实践

更多精彩请到 http://www.139ya.com

漫谈OCL概念、特征和实践

作者:大雁北飞 出处:csdn

OCL概念

我的BLOG上面两篇都是介绍OCL的,导致人气低迷,本来关注MDA技术的人就不多,关注OCL的就更少了。不过无论如何,OCL是MDA技术中不可缺少的部分。OCL虽然号称“对象约束语言”,不过实际上可以用来约束MOF四层模型中任意一层的模型以及实例。它真正的意义是建模相关领域约束语言。

除了约束模型以外,OCL的一个重要用途是可以用来描述模型转换规则。虽然这并不是OCL的主要用途(我没有仔细查阅OCL规范,不知道是否在规范中正式提出过这个用途),但是很多研究者进行了研究和探索。其中Jos Warmer专门在一节中讨论了这个问题。下面是摘自他文章中的一个用OCL描述模型转换的例子:

1. 自然语言描述的转换规则

· For each class named className in the PIM, there is a class named className in the PSM.

· For each public attribute named attributeName : Type of class className in the PIM the following attributes and operations are part of the class className in the target model.

- A private attribute with the same name: attributeName : Type

- A public operation named with the attribute name, preceded with 'get' and the attribute type as return type: getAttributeName() : Type

- A public operation named with the attribute name, preceded with 'set' and with the attribute as parameter and no return value: setAttributeName(att : Type)

从上面可以知道,这是一个将PIM中的class转换为PSM中class的规则,以及为private属性添加getter和setter方法。

2. 用OCL写的转换规则,其中用到了作者自己发明的伪符号


Transformation ClassToClass (UML, UML) {

source c1: UML::Class;

target c2: UML::Class;

source condition -- none

target condition -- none

mapping

try PublicToPrivateAttribute on

c1.features <~> c2.features;

-- everything else remains the same

}

Transformation PublicToPrivateAttribute (UML, UML) {

source sourceAttribute : UML::Attribute;

target targetAttribute : UML::Attribute;

getter : UML::Operation;

setter : UML::Operation;

source condition

sourceAttribute.visibility = VisibilityKind::public;

target condition

targetAttribute.visibility = VisibilityKind::private

and -- define the set operation

setter.name = 'set'.concat(targetAttribute.name)

and

setter.parameters->exists( p |

p.name = 'new'.concat(targetAttribute.name)

and

p.type = targetAttribute.type )

and

setter.type = OclVoid

and -- define the get operation

getter.name = 'get'.concat(targetAttribute.name)

and

getter.parameters->isEmpty()

and

getter.returntype = targetAttribute.type;

mapping

try StringToString on

sourceAttribute.name <~> targetAttribute.name;

try ClassifierToClassifier on

sourceAttribute.type <~> targetAttribute.type;

}


-- somewhere the rules StringToString and ClassifierToClassifier

-- need to be defined

看来作者很有兴趣扩展OCL,将它变为Model Transformation Language。

另外,Kent大学的研究者D.H.Akehurst在《Relations in OCL》(http://www.cs.kent.ac.uk/pubs/2004/2007/index.html)一文中专门做出了探索。这篇文章的大意是:扩展目前的OCL,将Relation(关系)作为first class(第一性的)元素加入OCL语言,然后利用Relation和目前的OCL相结合来描述模型转换规则(这篇文章我也许会在以后专门讨论)。中文的例子比较长,相关知识较多,就不列举了。
OCL特征

关于OCL的特征书中做出了总结,这里我没有回头仔细找,而是就脑海中最深的一些映像说几点。

OCL是一个查询性的语言,也就是说任何OCL的动作都不会对模型本身造成任何的影响或者改变。例如select操作,选出原来Set的一个子集,collect操作,将原来集合中的一些元素的值组成另一个集合。这些操作都不会对模型本身造成影响,最多就是构建了另外的对象或者集合。

OCL是一个强类型的语言,任何一个元素,都是有类型的,并且任何操作的返回值一定有一个确定的类型。如果不能确定类型,那么此元素属于OclVoid类型的值Undefined。OCL的类型有三种:基本类型(Integer,Boolean等)、Collection类型(五种,虚类型Collection,以及它的子类型Set,OrderedSet,Bag,Sequence)和自定义类型(UML类,Association,Enumeration等等)。

OCL里面很强调时间点,任何操作都定义为瞬时完成的,即操作中模型的状态不会改变。基于实现考虑,这么规定有一定的道理,不然在多线程系统中,OCL约束很有可能失效。另外precondition和postcondition也明确规定是在方法执行的前后时间点才有约束,时间点不对约束无效。

OCL是一个宣言式(Declarative)的语言,描述了what to do,没有描述how to do。例如self.attribute->select(i| i.name = ‘wxb_nudt’)描述了将某个类的所有attribute组成一个集合,然后将属性名为wxb_nudt的属性提取出来组成一个子集(显然这样的属性不会多于一个,但这并不是我们关心的问题)。这个表达式描述以上的目的,但是没有给出执行过程。

OCL是基于集合论和谓词逻辑的,这点从它的表达式中可以很轻易的看出来。但是并不是集合论中所有的集合操作在OCL中都具有相应的符号表达。例如映射(project)就没有。而且OCL没有证明集合论中的所有集合操作都可以用OCL中现有的操作组合出来。但是我们相信这一点(盲目的,我没有时间去证明这个,呵呵)。另外关于OCL操作的中止性没有得到证明,也就是说“不能确定每个OCL操作都可以在有限时间内完成”,并且OCL并不能保证任意的OCL表达式是可中止的(我感觉自己简直就在说废话)。其实OCL已经说明了,无论如何实现,OCL假定所有表达式的计算都在瞬间完成。

虽然这部分内容在前面的blog中提到过,不过那个时候仅仅是照本宣科,和现在心有所感是不一样的。
OCL实践

目前OCL没有标准的实现,Jos Warmer在他的个人网站上列出了目前可用的OCL实现列表http://www.klasse.nl/ocl/ocl-services.html。

其中我选择了Kent大学的OCL实现。还是kent大学的D.H.Akehurst,他们的research team开发了一个KMF(Kent Model Framework),其中有一个OCL的实现。可以用来体验一下用OCL来编程(编程?不是建模么?)。下载地址http://www.cs.kent.ac.uk/projects/ocl/

需要给他们写email才能得到下载地址。

然后Zurich大学的一位研究人员写了这个版本的OCL的简单实践http://www.zurich.ibm.com/~wah/doc/emf-ocl/,源代码如下:


import java.util.List;



import org.eclipse.emf.ecore.EAttribute;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.EcoreFactory;

import org.eclipse.emf.ecore.EcorePackage;



import uk.ac.kent.cs.kmf.util.ILog;

import uk.ac.kent.cs.kmf.util.OutputStreamLog;

import uk.ac.kent.cs.ocl20.OclProcessor;

import uk.ac.kent.cs.ocl20.bridge4emf.EmfOclProcessorImpl;



public class OCLDemo {



public static boolean checkOCLConstraint(OclProcessor processor, String expr, Object model) {

List l = processor.evaluate(expr, model);

return Boolean.valueOf(l.get(0).toString()).booleanValue();

}



public static void main(String[] args) {

ILog log = new OutputStreamLog(System.err);

OclProcessor processor = new EmfOclProcessorImpl(log);

System.out.println(processor.evaluate("1+1"));



processor.addModel(EcorePackage.eINSTANCE);

EClass eClass = EcoreFactory.eINSTANCE.createEClass();

eClass.setName("Library");

EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();

attr.setName("books");

attr.setEType(EcorePackage.eINSTANCE.getEInt());

eClass.getEStructuralFeatures().add(attr);



System.out.println(processor.evaluate("context ecore::EClass " +

"inv:self.eAttributes->select(x|x.name='books')", eClass));



System.out.println(processor.evaluate("context ecore::EClass " +

"inv:self.eAttributes->exists(x|x.name='books' and x.eType.name = 'EInt')", eClass));



boolean pre = checkOCLConstraint(processor, "context ecore::EClass inv: not self.oclIsUndefined()",eClass);

// do something with eClass

boolean post = checkOCLConstraint(processor, "context ecore::EClass inv: self.eAttributes->forAll(c| not c.changeable)",eClass);

if (!(!pre | post))

System.out.println("OK.");

else

System.out.println("Ooops.");



}

}


我在Eclipse3.0.1和EMF2.0以及上面下载的OCLjava包环境下运行了这个例子,结果如下:

[2]

[[org.eclipse.emf.ecore.impl.EAttributeImpl@62937c (name: books) (ordered: true, unique: true, lowerBound: 0, upperBound: 1) (changeable: true, volatile: false, transient: false, defaultValueLiteral: null, unsettable: false, derived: false) (iD: false)]]

[true]

OK.

例子很简单,详细的解释在上面链接的文章中解释了。但是这个例子仅仅构造了一个简单的ECore模型,而且是在程序中构造的,不是使用EclipseUML或者EMF画出来的,另外如何将OCL和模型连接起来也没有提到。如果有时间,我会看看KMF的文档,应该有答案。

Wednesday, September 9, 2009

java 开源内容管理系统

更多精彩请到 http://www.139ya.com


http://www.open-open.com/39.htm

http://www.yeeyan.com/articles/view/oc/39540

An artical about Globalization, Localization, Internationalization and Translation

更多精彩请到 http://www.139ya.com

http://atlas.pristine.com.tw/~philip/glit/en/index.html


Localization

L10n is the process of adapting the text and applications of a product or service to enable its acceptability for a particular cultural or linguistic market. Translation is the central activity of localization. Localization goes beyond literal translation, in addition to idiomatic language translation, numerous locale details such as currency, national regulations and holidays, cultural sensitivities, product or service names, gender roles, and geographic examples among many other details must all be considered. A successfully localized service or product is one that seems to have been developed within the local culture.

Localization primarily includes:

  • Translating text content, software source code, web sites, or database content; machine translation may be used in early stages.
  • Adjusting graphic and visual elements and examples to make them culturally appropriate
  • Post-production quality control of content, systems and the integrated product

The localization market is focused on software, documentation (packaging information, technical booklets, user manuals, training equipment, etc.), web sites and applications.

Internationalization

I18n is planning and implementing products and services so that they can easily be localized for specific languages and cultures.

This process requires a combination of both international and technical expertise, and generally involves both deploying new systems and reengineering existing ones. Once the internationalized platform is in place, rollouts in new countries or cultures should be significantly more cost efficient, timely and market effective.

Internationalization may include:

  • Creating illustrations for documents in which the text can easily be changed to another language and allowing expansion room for this purpose
  • Allowing space in user interfaces (for example, hardware labels, help pages, and online menus) for translation into languages that require more space
  • Creating print or web site graphic images so that their text labels can be translated inexpensively
  • Leaving enough space in a brochure to drop in different length languages
  • Separating the language elements from the graphic elements, or abstracting content from markup in a web application and software
  • Using written examples that have global meaning
  • Insuring that the tools and product can support international character sets
  • For software, ensuring data space so that messages can be translated from languages with single-byte character codes (such as English) into languages requiring multiple-byte character codes (such as Japanese Kanji)

Globalization

Globalization is an approach to business strategy that aims to address all of the logistical and organizational challenges an enterprise faces as it expands its supporting content, assets and message across cultures and markets to new clients. Globalization incorporates internationalization and localization to achieve this goal.

Globalization describes a comprehensive process that incorporates, augments, and extends:

  • Research on and identification of global markets
  • Market validation and selection
  • Identification and formalization of global business requirements
  • Language translation and cultural integration (a.k.a. Internationalization and Localization)
  • Identification of technology standards and solutions (format and structure)
  • Identification of cross-market affinities (to enable marketing and technology asset reuse)
  • Alignment with and support for Internationalization (i18n) and Localization (l10n) processes.

Ebooks libres et gratuits

更多精彩请到 http://www.139ya.com


http://www.ebooksgratuits.com/

http://www.pitbook.com/

http://www.cite-sciences.fr/francais/ala_cite/expositions/jules_verne/livres/afrique.html

Tuesday, September 8, 2009

英文Locale下使用中文输入法

更多精彩请到 http://www.139ya.com

http://wiki.ubuntu.org.cn/%E8%8B%B1%E6%96%87Locale%E4%B8%8B%E4%BD%BF%E7%94%A8%E4%B8%AD%E6%96%87%E8%BE%93%E5%85%A5%E6%B3%95


本文作用:

如果你的locale设置为英文,并安装了中文输入法,但它工作不正常,那么本文是你需要的。本文只关注英文locale下的问题。

本文作者:chinakr

授权许可:


如果你在英文locale下,正确安装scim或fcitx后仍无法正常使用输入法,解决的方法如下:


sudo aptitude install im-switch

如果你用scim,运行

sudo im-switch -s scim -z all_ALL
im-switch -s scim -z all_ALL

如果你用fcitx,运行

sudo im-switch -s fcitx -z all_ALL
im-switch -s fcitx -z all_ALL

这样,以后你就可以任意locale(英文、法文、藏文、维吾尔文,等等)下使用你喜欢的输入法了。

其实im-switch不过是帮你修改了几个配置文件而已。如果你实在不想装im-switch,也可以手动修改配置文件(比如下面就是一个加入en locale支持的例子,不过小心,有省力的软件帮你配置不用,一定要自己手工改,会被别人说你在装B哦)。

编辑 /etc/gtk-2.0/gtk.immodules(如果存在的话) 或者 /usr/lib/gtk-2.0/2.10.0/immodule-files.d/libgtk2.0-0.immodules 文件,在xim 的 local 增加 en 也就是说:

"xim" "X Input Method" "gtk20" "/usr/share/locale" "ko:ja:th:zh"

改成

"xim" "X Input Method" "gtk20" "/usr/share/locale" "en:ko:ja:th:zh"

保存退出,重启后再进就ok了.


[编辑] fcitx输入法

1. 安装

sudo apt-get install fcitx

2. 设置环境变量

echo -e "export XMODIFIERS=@im=fcitx\nexport GTK_IM_MODULE=\"xim\"\nexport QT_IM_MODULE=\"xim\"\nfcitx &">>~/.profile

3. 重启X

4. 方块字

gedit --encoding gbk ~/.fcitx/config

显示字体(中)=*

改为:

显示字体(中)=AR PL UMing CN #填你喜欢的中文字体

在fcitx激活状态( Ctrl+Space )下按 Ctrl+5 启用配置,重启X亦可。


以下待整理

补充作者:aBiNg

声明:你必须知道你在执行什么,请慎重操作!


如果是像本人一样,在英文locale环境下,只使用fcitx作为默认输入法的五笔用户,可以考虑删除scim。

删除scim

sudo apt-get remove --purge scim-*
sudo apt-get autoremove
sudo apt-get install -f
注:系统自动清理时,会提示删除ubuntu-desktop以及个别语言包之类,请自行google,再作决定。


安装fcitx

sudo apt-get update
sudo apt-get install fcitx
注:有的源中的fcitx存有bug(能调出输入法,但中文无法输入),请自行到fcitx官网下载页下载,按照说明文档编译安装 。


设置中文输入环境

echo 'LC_CTYPE=zh_CN.UTF-8' | sudo tee - -a /etc/environment


保存你的所有工作,重启X(ctrl+alt+backspace)。

ctrl+space调出/隐藏fcitx界面。


注:有些桌面环境(比如Fluxbox)可能并不能自动启动fcitx,可以在相应的启动脚本中(比如Fluxbox的启动脚本位于 ~/.fluxbox/startup)加入启动命令(比如 fcitx &)。也有其它的启动方法,请google。

注意,在纯英文环境下(没有安装任何中文支持),可能做了如上修改后fcitx仍然显示方块,这就是你的电脑中没有生成cn环境,可能进行如下修改以生成cn环境。

sudo gvim /var/lib/locales/supported.d/zh

加入如下内容
zh_SG.UTF-8 UTF-8
zh_HK.UTF-8 UTF-8
zh_TW.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8
zh_CN.GB2312 GB2312
zh_CN.GBK GBK
zh_CN.GB18030 GB18030
zh_HK.Big5 Big5
zh_TW.Big5 Big5

然后
sudo locale-gen
生成可能要几分钟,等等就行了,这样的话,在纯英文环境中fcitx也可正常显示状态栏上的文字了。