Pages

Wednesday, November 9, 2011

Adobe confirms Flash Player is dead for mobile devices


 
Adobe confirms Flash Player is dead for mobile devices
Published on Engadget | shared via feedly mobile
We heard the talk and now here's the confirmation: Flash Player for mobile devices is officially dead. Adobe is reaffirming its commitment to "aggressively contribute" to HTML5, a platform with broader support and capabilities than Flash was ever able to deliver. Adobe will of course also be pushing developers to work in its AIR platform for a more native experience, and the company will continue to work on Flash Player for desktop operating systems, but one can't help but see the platform as a whole standing on fairly shaky footing at this point.

Adobe confirms Flash Player is dead for mobile devices originally appeared on Engadget on Wed, 09 Nov 2011 09:47:00 EDT. Please see our terms for use of feeds.

Permalink | sourceAdobe | Email this | Comments



Sent from my iPhone

Monday, November 7, 2011

How To Encrypt Just Mail's Messages

This is kind of crazy, but it works like a charm. I was trying to find a way to just encrypt and password-protect my mail without going and putting my account in FileVault (evil). I noticed that Mac OS X would mount a disk image to open a program it knew was once on that disk image and, well, I put two and two together: what if my mail was on an encrypted disk image that also held the only working copy of the Mail application? Well, it would prompt for the image password, open Mail, and if I’d done things right then my mail would show up.
So that’s what I did.

Make the Image

You’ll need an encrypted disk image with your current mail accounts. Don’t use Disk Utility for this because its “Image from Folder” feature creates a static image and we want a sparse image. Do something like this:
$ cd Library
$ hdiutil create -size 2g -srcfolder Mail -format SPARSE -encryption -stdinpass Mail
Enter disk image passphrase:
....................................
created: /Users/ahknight/Library/Mail.sparseimage

Link the Image

For now, let’s just move the Library/Mail folder to the side rather than deleting it outright. Rename it to Mail-old or something. Then, open the image you just made (if you did the above, it’s in ~/Library/Mail.sparseimage) and then do the following:
$ ln -s /Volumes/Mail ~/Library/Mail
Now when Mail wants to use your mail, it will use the image. Of course, if you start mail without the image mounted you’ll get strange things happening, so we need to put Mail on the image.

Add and Lock Mail

Drag a copy of Mail into the image. Remove the Mail item from your Dock and add this copy to it instead (this is the important step). Now when you click on Mail, the system will know to go to this disk image.
However, if any other copy of Mail is available, it will go to that one, instead. To prevent this, we have to make the other Mail ineligible for launching in one way or another. Pick one:
  • Delete it off the system and symlink /Applications/Mail.app to the copy on the disk image while it’s mounted, and have the image mounted during updates (best idea, since it doesn’t break anything so long as the image is mounted)
  • sudo chown root /Applications/Mail.app; sudo chmod 700 /Applications/Mail.app and copy the program back to the image after updates
  • Trash it. You’ll need to copy it off the image and back into place before system updates and then back on the image when done updating.
The first is the easiest and preferable way to do it.

Test

Now the moment of truth. Unmount the image and then click on the Mail icon. You should get a password prompt for the image and then Mail should start with all of your mail.



from: http://www.macgeekery.com/hacks/software/how_to_encrypt_just_mails_messages

Saving Data Using JSTL in a JSP Page

When a JSP page needs to save data for its processing, it must specify a location, called the scope. See Saving Data in a JSP Page for an explanation of the four available scopes.
Data is saved using a mechanism called scoped variables. A scoped variable has a name, which is of type Stringand a value, which is of type Object. For non-page scoped variables, it is recommended that the name use the reverse domain name convention (e.g., prefixed with com_mycompany) to minimize unexpected collisions when integrating with third party modules.
When using the JSTL's expression language (see Enabling the JSTL Expression Language in a JSP Page), the variables in each scope are made available in the implicit objects pageScope, requestScope, sessionScope, andapplicationScope.
This example saves and retrieves values in scoped variables in each of the four scopes:
<%-- Declare the core library --%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>

<%-- Save data in scoped variables --%>
<c:set var="name1" value="value1" scope="page" />
<c:set var="com_mycompany_name2" value="value2" scope="request" />
<c:set var="com_mycompany_name3" value="value3" scope="session" />
<c:set var="com_mycompany_name4" value="value4" scope="application" />

<%-- Show the saved values --%>
<c:out value='${pageScope.name1}' />
<c:out value='${requestScope.com_mycompany_name2}' />
<c:out value='${sessionScope.com_mycompany_name3}' />
<c:out value='${applicationScope.com_mycompany_name4}' />
When retrieving a saved value, it is possible to omit the scope. If the scope is omitted, the variable name is automatically searched for in each of the scopes, in the order pageScope, requestScope, sessionScope, andapplicationScope.
<%-- Show the saved values without a specific scope --%>
<c:out value='${name1}' />
<c:out value='${com_mycompany_name2}' />
<c:out value='${com_mycompany_name3}' />
<c:out value='${com_mycompany_name4}' />
It is also possible to specify the value to save using the contents of the body, rather than through the value attribute:
<%-- Save data using body content --%>
<c:set var="name1" scope="page">
    value 1 in body
</c:set>
<c:set var="com_mycompany_name2" scope="request" >
    value 2 in body
</c:set>
<c:set var="com_mycompany_name3" scope="session" >
    value 3 in body
</c:set>
<c:set var="com_mycompany_name4" scope="application">
    value 4 in body
</c:set>
When specifying the value using body contents, the body contents is first trimmed of leading and trailing white space before it is saved. For example,
<c:set var="name1" scope="page">
    line 1
    line 2
</c:set>
would be saved as:
"line 1\n    line 2"

Saving Data in a JSP Page

When a JSP page needs to save data for its processing, it must specify a location, called the scope. There are four scopes available – page, request, session, and application. Page – scoped data is accessible only within the JSP page and is destroyed when the page has finished generating its output for the request. Request-scoped data is associated with the request and destroyed when the request is completed. Session-scoped data is associated with a session and destroyed when the session is destroyed. Application-scoped data is associated with the web application and destroyed when the web application is destroyed. Application-scoped data is not accessible to other web applications.
Data is saved using a mechanism called attributes. An attribute is a key/value pair where the key is a string and the value is any object. It is recommended that the key use the reverse domain name convention (e.g., prefixed with com.mycompany) to minimize unexpected collisions when integrating with third party modules.
This example uses attributes to save and retrieve data in each of the four scopes:
<%
    // Check if attribute has been set
    Object o = pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE);
    if (o == null) {
        // The attribute com.mycompany.name1 may not have a value or may have the value null
    }

    // Save data
    pageContext.setAttribute("com.mycompany.name1", "value0");  // PAGE_SCOPE is the default
    pageContext.setAttribute("com.mycompany.name1", "value1", PageContext.PAGE_SCOPE);
    pageContext.setAttribute("com.mycompany.name2", "value2", PageContext.REQUEST_SCOPE);
    pageContext.setAttribute("com.mycompany.name3", "value3", PageContext.SESSION_SCOPE);
    pageContext.setAttribute("com.mycompany.name4", "value4", PageContext.APPLICATION_SCOPE);
%>

<%-- Show the values --%>
<%= pageContext.getAttribute("com.mycompany.name1") %> <%-- PAGE_SCOPE --%>
<%= pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE) %>
<%= pageContext.getAttribute("com.mycompany.name2", PageContext.REQUEST_SCOPE) %>
<%= pageContext.getAttribute("com.mycompany.name3", PageContext.SESSION_SCOPE) %>
<%= pageContext.getAttribute("com.mycompany.name4", PageContext.APPLICATION_SCOPE) %>
See also Saving Data Using JSTL in a JSP Page.




from: http://www.exampledepot.com/egs/javax.servlet.jsp/attr.html

10 Most Useful Java Best Practice Quotes for Java Developers

Quote 1: Avoid creating unnecessary objects and always prefer to do Lazy Initialization
Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code.


public class Countries {

private List countries;

public List getCountries() {

//initialize only when required
if(null == countries) {
countries = new ArrayList();
}
return countries;
}
}
Quote 2: Never make an instance fields of class public
Making a class field public can cause lot of issues in a program. For instance you may have a class called MyCalender. This class contains an array of String weekdays. You may have assume that this array will always contain 7 names of weekdays. But as this array is public, it may be accessed by anyone. Someone by mistake also may change the value and insert a bug!


public class MyCalender {

public String[] weekdays =
{"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};

//some code

}
Best approach as many of you already know is to always make the field private and add a getter method to access the elements.

private String[] weekdays =
{"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};

public String[] getWeekdays() {
return weekdays;
}
But writing getter method does not exactly solve our problem. The array is still accessible. Best way to make it unmodifiable is to return a clone of array instead of array itself. Thus the getter method will be changed to.


public String[] getWeekdays() {
return weekdays.clone();
}
Quote 3: Always try to minimize Mutability of a class
Making a class immutable is to make it unmodifiable. The information the class preserve will stay as it is through out the lifetime of the class. Immutable classes are simple, they are easy to manage. They are thread safe. They makes great building blocks for other objects.

However creating immutable objects can hit performance of an app. So always choose wisely if you want your class to be immutable or not. Always try to make a small class with less fields immutable.

To make a class immutable you can define its all constructors private and then create a public static method
to initialize and object and return it.


public class Employee {

private String firstName;
private String lastName;

//private default constructor
private Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public static Employee valueOf (String firstName, String lastName) {
return new Employee(firstName, lastName);
}
}
Quote 4: Try to prefer Interfaces instead of Abstract classes
First you can not inherit multiple classes in Java but you can definitely implements multiple interfaces. Its very easy to change the implementation of an existing class and add implementation of one more interface rather then changing full hierarchy of class.

Again if you are 100% sure what methods an interface will have, then only start coding that interface. As it is very difficult to add a new method in an existing interface without breaking the code that has already implemented it. On contrary a new method can be easily added in Abstract class without breaking existing functionality.

Quote 5: Always try to limit the scope of Local variable
Local variables are great. But sometimes we may insert some bugs due to copy paste of old code. Minimizing the scope of a local variable makes code more readable, less error prone and also improves the maintainability of the code.

Thus, declare a variable only when needed just before its use.

Always initialize a local variable upon its declaration. If not possible at least make the local instance assigned null value.

Quote 6: Try to use standard library instead of writing your own from scratch
Writing code is fun. But “do not reinvent the wheel”. It is very advisable to use an existing standard library which is already tested, debugged and used by others. This not only improves the efficiency of programmer but also reduces chances of adding new bugs in your code. Also using a standard library makes code readable and maintainable.

For instance Google has just released a new library Google Collections that can be used if you want to add advance collection functionality in your code.

Quote 7: Wherever possible try to use Primitive types instead of Wrapper classes
Wrapper classes are great. But at same time they are slow. Primitive types are just values, whereas Wrapper classes are stores information about complete class.

Sometimes a programmer may add bug in the code by using wrapper due to oversight. For example, in below example:


int x = 10;
int y = 10;

Integer x1 = new Integer(10);
Integer y1 = new Integer(10);

System.out.println(x == y);
System.out.println(x1 == y1);
The first sop will print true whereas the second one will print false. The problem is when comparing two wrapper class objects we cant use == operator. It will compare the reference of object and not its actual value.

Also if you are using a wrapper class object then never forget to initialize it to a default value. As by default all wrapper class objects are initialized to null.

?
1
2
3
4
5
6
7
Boolean flag;

if(flag == true) {
System.out.println("Flag is set");
} else {
System.out.println("Flag is not set");
}
The above code will give a NullPointerException as it tries to box the values before comparing with true and as its null.

Quote 8: Use Strings with utmost care.
Always carefully use Strings in your code. A simple concatenation of strings can reduce performance of program. For example if we concatenate strings using + operator in a for loop then everytime + is used, it creates a new String object. This will affect both memory usage and performance time.

Also whenever you want to instantiate a String object, never use its constructor but always instantiate it directly. For example:


//slow instantiation
String slow = new String("Yet another string object");

//fast instantiation
String fast = "Yet another string object";
Quote 9: Always return empty Collections and Arrays instead of null
Whenever your method is returning a collection element or an array, always make sure you return empty array/collection and not null. This will save a lot of if else testing for null elements. For instance in below example we have a getter method that returns employee name. If the name is null it simply return blank string “”.


public String getEmployeeName() {
return (null==employeeName ? "": employeeName);
}
Quote 10: Defensive copies are savior
Defensive copies are the clone objects created to avoid mutation of an object. For example in below code we have defined a Student class which has a private field birth date that is initialized when the object is constructed.


public class Student {
private Date birthDate;

public Student(birthDate) {
this.birthDate = birthDate;
}

public Date getBirthDate() {
return this.birthDate;
}
}
Now we may have some other code that uses the Student object.


public static void main(String []arg) {

Date birthDate = new Date();
Student student = new Student(birthDate);

birthDate.setYear(2019);

System.out.println(student.getBirthDate());
}
In above code we just created a Student object with some default birthdate. But then we changed the value of year of the birthdate. Thus when we print the birth date, its year was changed to 2019!

To avoid such cases, we can use Defensive copies mechanism. Change the constructor of Student class to following.


public Student(birthDate) {
this.birthDate = new Date(birthDate);
}
This ensure we have another copy of birthdate that we use in Student class.

Two bonus quotes
Here are two bonus Java best practice quotes for you.

Quote 11: Never let exception come out of finally block
Finally blocks should never have code that throws exception. Always make sure finally clause does not throw exception. If you have some code in finally block that does throw exception, then log the exception properly and never let it come out :)

Quote 12: Never throw “Exception”
Never throw java.lang.Exception directly. It defeats the purpose of using checked Exceptions. Also there is no useful information getting conveyed in caller method.

More Quotes from Java Developers
Do you have a quote that is not included in above list? Well, feel free to add your Java best practice quote using comment below. Write your quote and explain it in 2-3 lines. I will add all those user generated quotes in this section.

Quote #13: Avoid floating point numbers
It is a bad idea to use floating point to try to represent exact quantities like monetary amounts. Using floating point for dollars-and-cents calculations is a recipe for disaster. Floating point numbers are best reserved for values such as measurements, whose values are fundamentally inexact to begin with. For calculations of monetary amounts it is better to use BigDecimal.

Wednesday, November 2, 2011

深入了解java虚拟机(JVM)

1   Java技术与Java虚拟机
说起Java,人们首先想到的是Java编程语言,然而事实上,Java是一种技术,它由四方面组成: Java编程语言、Java类文件格式、Java虚拟机和Java应用程序接口(Java API)。它们的关系如下图所示:
图1   Java四个方面的关系

JRE VS JVM

凡是接触过 Java 的人都知道 JRE 的概念,即 Java 运行时环境( Java Runtime Environment ),因为它是运行 Java 程序必不可少的(除非程序用 GCJ 等编译,但我怀疑这样处理后还能不能称之为“ Java 程序”了)。  

Java 喊出的带有标志性的口号“ Write Once , Run Anywhere (一次编写,到处运行)”(记得某老师给俺们上课讲到这里时还不忘幽一默:到处运行?没有计算机就运行不了……),正是建立在 JRE 的基础之上。何以实现?就是在 Java 应用程序和操作系统之间增加了一虚拟层—— JRE 。程序源代码不是直接编译、链接成机器代码,而是先转化到字节码( bytecode )这种特殊的中间形式,字节码再转换成机器码或系统调用。前者是传统的编译方法,生成的机器代码就不可避免地跟特殊的操作系统和特殊的机器结构相关,很多装双系统的用户无法在 Linux 运行 Windows 下的大型游戏,心里那个郁闷(于是很多虚拟软件和模拟程序应运而生)。而 Java 程序的字节码文件可以放到任意装有 JRE 的计算机运行,再由不同 JRE 的将它们转化成相应的机器代码,这就实现了 Java 程序的可移植性。这样程序员也不用去关心程序运行的具体环境,而可以专心编写软件。这种分层抽象、隐藏细节的思想在计算机科学中处处可见,比如机器组织结构的设计、网络协议的实现等。 Pascal 语言的发明者 Niklaus Wirth ,就富有预见性地指出应该有这样一种可移植的语言,其生成的中间代码可以在一台假想的机器( a hypothetical machine )上运行。而 Java 虚拟机( Java virtual machine 或 JVM )就是这样的一台机器,它模拟实际处理器的结构,解释字节码。怎么一会说是 JRE ,一会儿又成了 JVM ,两者是否同物不同名?  

回答是否定的。  

JVM 是 Java 平台的基础,和实际的机器一样,它也有自己的指令集,并且在运行时操作不同的内存区域。 JVM 通过抽象操作系统和 CPU 结构,提供了一种与平台无关的代码执行方法,即与特殊的实现方法、主机硬件、主机操作系统无关。但是在一些小的方面, JVM 的实现也是互不相同的,比如垃圾回收算法,线程调度算法(可能不同 OS 有不同的实现)。 JVM 的主要工作是解释自己的指令集(即字节码)到 CPU 的指令集或 OS 的系统调用,保护用户免被恶意程序骚扰。 JVM 对上层的 Java 源文件是不关心的,它关注的只是由源文件生成的类文件( class file )。类文件的组成包括 JVM 指令集,符号表以及一些补助信息。  

而 JRE 是 Sun 公司发布的一个更大的系统,它里面就有一个 JVM 。 JRE 就与具体的 CPU 结构和操作系统有关,我们从 Sun 下载 JRE 的时候就看到了不同的各种版本。同 JVM 一起组成 JRE 的还有一些 API (如 awt , swing 等)。 JRE 是运行 Java 程序必不可少的。