Archive

Archive for February, 2012

将 Apache httpd 作为应用开发平台

February 24, 2012 Leave a comment

Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大,灵活而健壮,且容易扩展的开发平台。开发人员通过定制 Apache 模块,可以几乎无限制的扩展 Apache httpd,使其更好的与实际应用场景匹配,而又无需考虑底层的网络传输细节。这样既可以提高开发效率,节省开发成本,又能充分利用 Apache 本身的健壮性及可靠性。

from IBM developerWorks 中国 : 文档库 http://www.ibm.com/developerworks/cn/opensource/os-cn-apachehttpd/index.html?ca=drs-

Advertisements
Categories: GR Tags:

hacks.mozilla.org: Storing images and files in IndexedDB

February 23, 2012 Leave a comment

The other day we wrote about how to Save images and files in localStorage, and it was about being pragmatic with what we have available today. There are, however, a number of performance implications with localStorage – something that we will cover on this blog later – and the desired future approach is utilizing IndexedDB. Here I’ll walk you through how to store images and files in IndexedDB and then present them through an ObjectURL.

The general approach

First, let’s talk about the steps we will go through to create an IndexedDB data base, save the file into it and then read it out and present in the page:

  1. Create or open a database.
  2. Create an objectStore (if it doesn’t already exist)
  3. Retrieve an image file and create a blob from that image file
  4. Initiate a database transaction
  5. Save that blob into the database
  6. Read out that saved file and create an ObjectURL from it and set it as the src of an image element in the page

Creating the code

Let’s break down all parts of the code that we need to do this:

Create or open a database.

<noscript></p>
<pre>// IndexedDB
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction ||
window.OIDBTransaction || window.msIDBTransaction,
dbVersion = 1.0;

// Create/open database
var request = indexedDB.open(“elephantFiles”, dbVersion);</pre>
<p>request.onsuccess = function (event) {<br />
console.log(“Success creating/accessing IndexedDB database”);<br />
db = request.result;</p>
<p> db.onerror = function (event) {<br />
console.log(“Error creating/accessing IndexedDB database”);<br />
};</p>
<p> // Interim solution for Google Chrome to create an objectStore. Will be deprecated<br />
if (db.setVersion) {<br />
if (db.version != dbVersion) {<br />
var setVersion = db.setVersion(dbVersion);<br />
setVersion.onsuccess = function () {<br />
createObjectStore(db);<br />
getImageFile();<br />
};<br />
}<br />
else {<br />
getImageFile();<br />
}<br />
}<br />
else {<br />
getImageFile();<br />
}<br />
}</p>
<p>// For future use. Currently only in latest Firefox versions<br />
request.onupgradeneeded = function (event) {<br />
createObjectStore(event.target.result);<br />
};<br />
</noscript>

The intended way to use this is to have the onupgradeneeded event triggered when a database is created or gets a higher version number. This is currently only supported in Firefox, but will soon be in other web browsers. If the web browser doesn’t support this event, you can use the deprecated setVersion method and connect to its onsuccess event.

Create an objectStore (if it doesn’t already exist)

<noscript></p>
<pre>// Create an objectStore
console.log(“Creating objectStore”)
dataBase.createObjectStore(“elephants”);</pre>
<p></noscript>

Here you create an ObjectStore that you will store your data – or in our case, files – and once created you don’t need to recreate it, just update its contents.

Retrieve an image file and create a blob from that image file

<noscript></p>
<pre>// Create XHR and BlobBuilder
var xhr = new XMLHttpRequest(),
blobBuilder = new (window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.OBlobBuilder || window.msBlobBuilder),
blob;

xhr.open(“GET”, “elephant.png”, true);
// Set the responseType to arraybuffer. “blob” is an option too, rendering BlobBuilder unnecessary, but the support for “blob” is not widespread enough yet
xhr.responseType = “arraybuffer”;

xhr.addEventListener(“load”, function () {
if (xhr.status === 200) {
console.log(“Image retrieved”);

// File as response
var response = xhr.response;

if (blobBuilder) {
// Append the response to the BlobBuilder
blobBuilder.append(response);
// Create a blob with the desired MIME type
blob = blobBuilder.getBlob(“image/png”);
}
else {
blob = new Blob([response]);
}

if (blob) {
// Put the received blob into IndexedDB
putElephantInDb(blob);
}
}
}, false);
// Send XHR
xhr.send();</pre>
<p></noscript>

This code gets the contents of a file as an arraybuffer. You could also use responseType = "blob"; to get a blob back directly. However, currently that’s only supported in Firefox.
Once you have received the entire file, you create a blob and then send it to the function to store it in the database.

Initiate a database transaction

<noscript></p>
<pre>// Open a transaction to the database
var transaction = db.transaction([“elephants”], IDBTransaction.READ_WRITE);</pre>
<p></noscript>

To start writing something to the database, you need to initiate a transaction with an objectStore name and the type of action you want to do – in this case read and write.

Save that blob into the database

<noscript></p>
<pre>// Put the blob into the dabase
transaction.objectStore(“elephants”).put(blob, “image”);</pre>
<p></noscript>

Once the transaction is in place, you get a reference to the desired objectStore and then put your blob into it and give it a key.

Read out that saved file and create an ObjectURL from it and set it as the src of an image element in the page

<noscript></p>
<pre>// Retrieve the file that was just stored
transaction.objectStore(“elephants”).get(“image”).onsuccess = function (event) {
var imgFile = event.target.result;
console.log(“Got elephant!” + imgFile);

// Get window.URL object
var URL = window.URL || window.webkitURL;

// Create and revoke ObjectURL
var imgURL = URL.createObjectURL(imgFile);

// Set img src to ObjectURL
var imgElephant = document.getElementById(“elephant”);
imgElephant.setAttribute(“src”, imgURL);

// Revoking ObjectURL
URL.revokeObjectURL(imgURL);
};</pre>
<p></noscript>

Use the same transaction to get the image file you just stored, and then create an objectURL and set it to the src of an image in the page.
This could just as well, for instance, have been a JavaScript file that you attached to a script element, and then it would parse the JavaScript.

The complete code

So, here’s is the complete working code:

<noscript></p>
<pre>(function () {
// IndexedDB
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction,
dbVersion = 1.0;

// Create/open database
var request = indexedDB.open(“elephantFiles”, dbVersion),
db,
createObjectStore = function (dataBase) {
// Create an objectStore
console.log(“Creating objectStore”)
dataBase.createObjectStore(“elephants”);
},

getImageFile = function () {
// Create XHR and BlobBuilder
var xhr = new XMLHttpRequest(),
blobBuilder = new (window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.OBlobBuilder || window.msBlobBuilder),
blob;

xhr.open(“GET”, “elephant.png”, true);
// Set the responseType to arraybuffer. “blob” is an option too, rendering BlobBuilder unnecessary, but the support for “blob” is not widespread enough yet
xhr.responseType = “arraybuffer”;

xhr.addEventListener(“load”, function () {
if (xhr.status === 200) {
console.log(“Image retrieved”);

// File as response
var response = xhr.response;

if (blobBuilder) {
// Append the response to the BlobBuilder
blobBuilder.append(response);
// Create a blob with the desired MIME type
blob = blobBuilder.getBlob(“image/png”);
}
else {
blob = new Blob([response]);
}

if (blob) {
// Put the received blob into IndexedDB
putElephantInDb(blob);
}
}
}, false);
// Send XHR
xhr.send();
},

putElephantInDb = function (blob) {
console.log(“Putting elephants in IndexedDB”);

// Open a transaction to the database
var transaction = db.transaction([“elephants”], IDBTransaction.READ_WRITE);

// Put the blob into the dabase
transaction.objectStore(“elephants”).put(blob, “image”);

// Retrieve the file that was just stored
transaction.objectStore(“elephants”).get(“image”).onsuccess = function (event) {
var imgFile = event.target.result;
console.log(“Got elephant!” + imgFile);

// Get window.URL object
var URL = window.URL || window.webkitURL;

// Create and revoke ObjectURL
var imgURL = URL.createObjectURL(imgFile);

// Set img src to ObjectURL
var imgElephant = document.getElementById(“elephant”);
imgElephant.setAttribute(“src”, imgURL);

// Revoking ObjectURL
URL.revokeObjectURL(imgURL);
};
};

request.onerror = function (event) {
console.log(“Error creating/accessing IndexedDB database”);
};

request.onsuccess = function (event) {
console.log(“Success creating/accessing IndexedDB database”);
db = request.result;

db.onerror = function (event) {
console.log(“Error creating/accessing IndexedDB database”);
};

// Interim solution for Google Chrome to create an objectStore. Will be deprecated
if (db.setVersion) {
if (db.version != dbVersion) {
var setVersion = db.setVersion(dbVersion);
setVersion.onsuccess = function () {
createObjectStore(db);
getImageFile();
};
}
else {
getImageFile();
}
}
else {
getImageFile();
}
}

// For future use. Currently only in latest Firefox versions
request.onupgradeneeded = function (event) {
createObjectStore(event.target.result);
};
})();</pre>
<p></noscript>

Web browser support

IndexedDB
Supported since long (a number of versions back) in Firefox and Google Chrome. Planned to be in IE10, unclear about Safari and Opera.
onupgradeneeded
Supported in latest Firefox. Planned to be in Google Chrome soon and hopefully IE10. Unclear about Safari and Opera.
XMLHttpRequest Level 2
Supported in Firefox and Google Chrome since long, Safari 5+ and planned to be in IE10 and Opera 12.
BlobBuilder
Supported in Firefox and Google Chrome since long, and is planned to be in IE10. Unclear about Safari and Opera.
responseType “blob”
Currently only supported in Firefox. Will soon be in Google Chrome and is planned to be in IE10. Unclear about Safari and Opera.

Demo and code

I’ve put together a demo with IndexedDB and saving images and files in it where you can see it all in action. Make sure to use any Developer Tool to Inspect Element on the image to see the value of its src attribute. Also make sure to check the console.log messages to follow the actions.

The code for storing files in IndexedDB is also available on GitHub, so go play now!

from Planet Mozilla http://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/

Categories: GR Tags:

阿里巴巴面试宝典

February 23, 2012 Leave a comment

这是以前阿里巴巴Java面试的一些要点,下面我发的是一部分,其他的可以找我要2434298599:

面试要点:

了解一下教育背景,是否有真正做过文本分类的算法,可以了解一下分类算法是否熟悉

是否有过B/S的开发经验

传递参数时,什么时候是值传递,什么时候是引用传递

是否使用过JDBC,JDBC的使用方法

try-catch-finally中,如果在try或catch中有return过,是否还执行

简述快速排序

不是用中间变量实现swap(x,y)函数

1、 除了在从前的项目中使用了哪些开源框架开发外,需注重他们是否清楚框架的工作原理;

Jdk1.5新特性,

Spring的原理,事务如何管理,如何防止脏读数据,

你最有成就感的项目,

说说方法sysnchrnized 和方法块sysnchrnized区别,

Hibernate的延迟加载,session何时开启,何时关闭.

2、对java应用的内存处理机制,线程工作原理,并有没有在项目中实际解决该类问题的经验;

3、 有没有在项目中有过系统性能调优,性能跟踪,内存泄漏定位等相关经验,

4、 在解决实际问题时,是否有了解解决问题方法的本质(解决方案的系统原理);

5、 对关系型数据库工作原理,以及关键性能影响点的了解及项目中的实际经验;

6、 是否爱好技术,愿意不断投入时间去跟进,学习(学习的深入度,是否能结合系统原理来看一些新技术);

Java面试题

1.string,stringbuffer 和stringbuilder的区别

2.struts1和2的区别

3.ArrayList,HashMap,HashTable区别

4.谈谈NIO

5.Weblogic怎么发布一个JMS

6.OBJECT有些什么函数,都是做什么用的

7.JSP,SERVLE是线程安全的吗?为什么?

8.了解SOA吗?

9.网页中跨域访问的问题怎么解决

10.ibtas中怎么设置OS CACHE

JAVA面试基础测试题

1.抽象:

 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。

7、String 和StringBuffer的区别

 JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据。这个String类提供了数值不可改变的字符串。而这个StringBuffer类提供的字符串进行修改。当你知道字符数据要改变的时候你就可以使用StringBuffer。典型地,你可以使用 StringBuffers来动态构造字符数据。

9、说出Servlet的生命周期,并说出Servlet和CGI的区别。

 Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。

与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。

   (我2434298599)10、说出ArrayList,Vector, LinkedList的存储性能和特性

 ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),通常性能上较ArrayList差,而LinkedList使用双向链表实现存储,按序号索引数据需要进行前向或后向遍历,但是插入数据时只需要记录本项的前后项即可,所以插入速度较快。

 

14、HashMap和Hashtable的区别。

    HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。

HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。

HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为contains方法容易让人引起误解。

Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。

最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。

Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。

16、sleep() 和 wait() 有什么区别?

    sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。调用sleep不会释放对象锁。

wait是Object类的方法,对此对象调用wait方法导致本线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象发出notify方法(或notifyAll)后本线程才进入对象锁定池准备获得对象锁进入运行状态。

作者: 念想n 

声明: 本文系ITeye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!


已有 3 人发表回复,猛击->>这里<<-参与讨论

ITeye推荐

from ITeye论坛最新讨论 http://www.iteye.com/topic/1120830

Categories: GR Tags:

Brendan Eich: Mobile Web API Evolution

February 23, 2012 Leave a comment

Ragavan Srinivasan’s post about the forthcoming Mozilla Marketplace for Open Web Apps inspired me to write about Mozilla’s surging Web and Device API standards work.

A bit of background. Mozilla has always contributed to web standards, going back to the start of the project. We co-founded the WHAT-WG to kick off HTML5. As readers of this blog know, we are a leader in JS standardization. We have some of the top CSS and layout experts in the world.

In the last eight months, our efforts to extend the web standards to include new APIs needed to build compelling apps and OS components on mobile devices have really caught fire. B2G and Open Web Apps are the fuel for this fire.

So I thought I would compile a list of emerging APIs to which we’ve contributed. In citing Mozillans I do not mean to minimize the efforts of standardization colleagues at Google, Microsoft, Nokia, Opera, the W3C and elsewhere. Standards are a multi-vendor effort (although one shiny name is conspicuously absent from this list).

The Mozilla contributions are worth noting both to acknowledge the individuals involved, and to highlight how Mozilla is championing device APIs for the web without having a native application stack blessed with such APIs on offer. We see the Web as quickly evolving to match native stacks. We have no other agenda than improving the Web to improve its users’ lives, including Web developers’ lives — especially mobile users and developers.

As always, standards in progress are subject to change, yet require prototype implementation and user-testing. Mozilla remains committed to playing fairly by not forging de-facto standards out of prototypes, rather proposing before disposing and in the end tracking whatever is standardized.

Here is the list, starting with some 2011-era work:

  • Geolocation, with Google contributing the editor and Firefox (thanks to Jay Sullivan leading the charge) implementing early.
  • WebGL and typed arrays.
  • Gamepad API. Co-editor: Ted Mielczarek. Mozillans are also contributing to Pointer Lock.
  • Screen Orientation. Editor: Mounir Lamouri.
  • navigator.getUserMedia. Co-editor: Anant Narayanan
  • Battery Status (in Last Call). From the Acknowledgements:

    Big thanks to the Mozilla WebAPI team for their invaluable feedback based on prototype implementations.

  • Media Capture. Fabrice Desré prototype-implemented in Gecko.
  • Network API. Editor: Mounir Lamouri.
  • Web Telephony. Ben Turner, Jonas Sicking, Philipp von Weitershausen.
  • Web SMS. Mounir Lamouri, Jonas Sicking.
  • Vibration. From the Acknowledgements:

    The group is deeply indebted to Mounir Lamouri, Jonas Sicking, and the Mozilla WebAPI team in general for providing the WebVibrator prototype as an initial input.

  • File API. Editors: Arun Ranganathan, Jonas Sicking.
  • IndexedDB. Editors includes Jonas Sicking.

I did not list most of the HTML5 and Web API work aimed at Desktop Firefox, to focus on the new mobile-oriented additions. There’s more to say, including about bundled-permission follies and how to weave permission-granting (with memorization) into interactions, but not here.

One last note. The CSS vendor prefix brouhaha had, among many salutary effects, the benefit of shining light on an important requirement of competitive mobile web development: CSS style properties such as -webkit-animation-*, however you spell them, must have fast and beautiful implementations across devices for developers to find them usable: 60Hz, artifact-free rendering under touch control. This requires such work as off-main-thread compositing and GL layers.

This is a high technical bar, but we are in the process of meeting it in the latest Firefox for Android and B2G builds, thanks to hard work from many people, especially Patrick Walton, Robert O’Callahan, Chris Jones, and Andreas Gal. Onward!

/be

from Planet Mozilla http://brendaneich.com/2012/02/mobile-web-api-evolution/

Categories: GR Tags:

Social Media Monitoring Maturity Model

February 23, 2012 Leave a comment

These days it’s very important for brands to monitor what is being said about them across various social media channels such as blogs, Twitter and Facebook. But where should the tools used to do the monitoring reside? Should they be stand-alone offerings? Should they be integrated with your collaboration platform, your CRM/Marketing tool, or your Call Center applications?

Below is my first draft of a maturity model showing the key benefits and issues of social media monitoring based on which platform the tool is a part of.

Social Media Monitoring Maturity Model

I’d love your feedback and together we can improve this.

But who should be responsible for this monitoring, Customer Support or Marketing? (or both)

from In The Next Version http://www.alanlepofsky.net/alepofsky/alanblog.nsf/dx/social-media-monitoring-maturity-model

Categories: GR Tags:

Falling in love with the Google+ API

February 22, 2012 Leave a comment

By Melina Mattos, Program Manager for Google Africa and Bob Aman, Program Manager for Developer Relations

Cross-posted from the Google Africa Blog

Attention developers! Of the 90+ Google APIs, which is your favorite? We know that we fell in love with the Google+ API after we saw the amazing applications built from the Hackathons in South Africa and Kenya. We want to continue spreading the love!

This Valentine’s Day we’re thrilled to announce that we are holding three more Google+ Hackathons with the support of the Google Technology User Groups (GTUGs) in Accra, Kampala, and Lagos.

If you are ready to wow us with your application, please apply for the event using these forms: Kampala on March 10 at the 4th floor of Solis House, Lagos on March 17 at the CCHub Nigeria, and Accra on March 21 at the Meltwater Entrepreneurial School of Technology.

Remember to start today on getting those creative juices flowing! Familiarize yourself with the API and review these resources. Begin gathering ideas and coding a little. Take advantage of the Google+ Platform Office Hours on the 15th of February. If you have any questions, please address them to us in our Google+ Hangout on February 23. Use the Hackathon to perfect your application and win one of the multiple prizes we will be awarding – including a ticket for the overall winning application to Google’s premiere developer event, Google I/O!

Any updates relating to these Hackathons and the Hangout will be posted on Google+ (of course!) using the hashtag #hackgplus. Stay tuned!

Posted by Scott Knaster, Editor

from Google Developers Blog http://googledevelopers.blogspot.com/2012/02/falling-in-love-with-google-api.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+GDBcode+%28Google+Developers+Blog%29

Categories: GR Tags:

hacks.mozilla.org: Wiki Wednesday: February 22, 2012

February 22, 2012 Leave a comment

Here are today’s Wiki Wednesday articles! If you know about these topics, please try to find a few minutes to look over these articles that are marked as needing technical intervention and see if you can fix them up. You can do so either by logging into the wiki and editing the articles directly, or by emailing your notes, sample code, or feedback to mdnwiki@mozilla.org.

Contributors to Wiki Wednesday will get recognition in the next Wiki Wednesday announcement. Thanks in advance for your help!

JavaScript

SpiderMonkey

Developing Mozilla

Extensions

XUL

XPCOM

Interfaces

Thanks to Neil Rashbrook for contributing!

Plugins

CSS

Thanks to leeli and Panagiotis Tsalaportas for their contributions!

SVG

HTML

Thanks to Panagiotis Tsalaportas and Neil Rashbrook for their contributions since last time.

DOM

Thanks to Matt N. for his contribution to the DOM documentation.

from Planet Mozilla http://hacks.mozilla.org/2012/02/wiki-wednesday-february-22-2012/

Categories: GR Tags: