2011年4月17日 星期日

[API] JPA

* PersistenceUnitManager
  • Interface that defines an abstraction for finding and managing JPA PersistenceUnitInfos.
  • Used by LocalContainerEntityManagerFactoryBean in order to obtain a PersistenceUnitInfo for building a concrete EntityManagerFactory.

* DefaultPersistenceUnitManager
  • Used as internal default by LocalContainerEntityManagerFactoryBean.
  • Supports standard JPA scanning for persistence.xml files, with configurable file locations, JDBC DataSource lookup and load-time weaving.
  • The default XML file location is classpath*:META-INF/persistence.xml, scanning for all matching files in the class path (as defined in the JPA specification).
  • DataSource names are by default interpreted as JNDI names, and no load time weaving is available (which requires weaving to be turned off in the persistence provider).

* ResourceBundleMessageSource
  • MessageSource implementation that accesses resource bundles using specified basenames.
  • This class relies on the underlying JDK's ResourceBundle implementation, in combination with the JDK's standard message parsing provided by MessageFormat.

* OpenEntityManagerInViewFilter
  • Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.
  • This filter makes JPA EntityManagers available via the current thread, which will be autodetected by transaction managers.
  • It is suitable for service layer transactions via JpaTransactionManager or JtaTransactionManager as well as for non-transactional read-only execution.

* JpaTransactionManager
  • Is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access.
  • JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. 
  • Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.
  • Supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services which access JPA and services which use plain JDBC (without being aware of JPA)!
  • Application code needs to stick to the same simple Connection lookup pattern as with DataSourceTransactionManager (i.e. DataSourceUtils.getConnection(javax.sql.DataSource) or going through a TransactionAwareDataSourceProxy). Note that this requires a vendor-specific JpaDialect to be configured.
  • Will autodetect the DataSource used as known connection factory of the EntityManagerFactory, so you usually don't need to explicitly specify the "dataSource" property.

* transaction-type
  • Is defined in persistence unit (persistence.xml file)
  • Default to JTA in a Java EE environment
  • Default to RESOURCE_LOCAL in a Java SE environment
  • Two different transaction types:
    • JTA:
      • most important
      • Multiple participating resources
      • Distributed XA transactions
      • 透過設置容器端管理的Data Source之JNDI名稱,這必須在容器端先定好,JDBC資源與Connection Pool等。
      • For distributed transactions
    • Resource-local



* Reference
- EJB3 Gossip: 第一個 JPA(容器管理)
- Java Persistence API (JPA): Transaction Transaction
- transaction-type="JTA" / "RESOURCE_LOCAL"

[Introduction] SpringMVC

* Spring MVC
  • Is essentially a request dispatcher framework, with a Servlet API variant and Portlet API variant.
  • As providing foundational facilities and conveniences on top of the Servlet/Portlet container, ex:
    • Flexible request mappings: separation between controller processing and view rendering phase
    • Data binding: basic JSP tag libraries that complement the JSTL, etc.
    • The building blocks of sophisticated HTTP request processing.

* The class is annotated with the @Controller stereotype. This indicates that its methods should be scanned for request mappings.

* @RequestMapping
  • The actual request mappings are expressed through this at the method level.
  • Each of those mappings binds to a specific HTTP path within the containing DispatcherServlet.
  • Allows for specifying HTTP request methods (e.g. method = RequestMapping.GET) or specific request parameters (e.g. params = "action=save"), all narrowing the type-level mapping for specific methods. Alternatively,

* Reference
- Chapter 13. Web MVC framework
- Annotated Web MVC Controllers in Spring 2.5

[Configuration] classpath with Spring

* J2EE default classpath:
- /WEB-INF/classes

* Spring 默认会使用当前线程的 ClassLoader 的 getResource 方法获取资源的 URL,如果无法获得当前线程的ClassLoader,Spring 将使用加载类 org.springframework.util.ClassUtils 的 ClassLoader。

* "classpath*:"
  • 是為了從多個 jar 文件中加載相同的文件。
  • 使用 classpath* 前缀可以获取所有与给定路径匹配的 classpath 资源,从而避免出现两个不同位置有相同名字的文件,Spring 只加载其中一个的情况。
  • You know you could actually have these context files loaded by different class loaders but in same application. 
    • Ex: EAR loading contexts in war and META-INF jars in separate class loaders. As Long as these are in the same VM, You can do a multi classloader resource.
  • ex: "classpath*:META-INF/*-beans.xml": a ClassLoader.getResources() call is used on the last non-wildcard path segment to get all the matching resources in the class loader hierarchy, and then off each resource the same PathMatcher resoltion strategy described above is used for the wildcard subpath.

* "classpath:"
- 只能加載找到的第一個文件。


* Reference
- Spring加载resource时classpath*:与classpath:的区别
- Chapter 4. Resources
- classpath*: making your Modular Spring Resources
- Spring中使用classpath加载配置文件浅析 **
- Spring中使用classpath加载配置文件浅析(续

2011年4月10日 星期日

[Shortcut] 執行

* services.msc: 開啟 "服務"

* msconfig: 開啟 "系統設定"

[Configuration] log4j.xml

* 有 property and xml 兩種設定方式

* Log level
FATAL      0  
  ERROR      3  
  WARN       4  
  INFO       6  
  DEBUG      7 

* Appender: log 輸出位置
org.apache.log4j.ConsoleAppender: 控制台

org.apache.log4j.FileAppender: 文件
 - . Threshold: log 最低 levle
. ImmediateFlush: default is true, 所有 log 都會馬上被 output
. Target: default is System.out, output on console,
   to choose which console stream to print messages to, System.out or System.err.

org.apache.log4j.DailyRollingFileAppender: 每天產生一個 log file
- . Append: default is true, 將 log 增加到指定 file, false is 覆蓋 log
. File: 指定文件
......

org.apache.log4j.RollingFileAppender: 文件大小到達指定大小的时候產生一個新的文件

org.apache.log4j.WriterAppender: 將 log 信息以串流格式發送到任意指定的地方

* Layout: log 輸出格式
org.apache.log4j.HTMLLayout: 以HTML表格形式布局

org.apache.log4j.PatternLayout: 可任意自定格式

org.apache.log4j.SimpleLayout: 包含 log 信息的级别和信息字符串

org.apache.log4j.TTCCLayout: 包含產生 log 的時間、thread、class 等等信息

* 列印參數的格式類似於 C
%m   程式中的 message

  %p   log level

  %r   啟動應用到輸出 log 所花費的毫秒數

  %c   class name

  %t   產生該 log 的 thread

  %n   換行

  %d   日期或時間,ex: %d{yyy MMM dd HH:mm:ss , SSS}

  %l   log 的發生位置,包括 class name, thread, line number,ex: Testlog4.main(TestLog4.java: 10 )。

* Logger: 對應 package 和 appender
additivity: 是否往上查找未設定的屬性
appender-ref: appender reference,可以有多個
level: log level priority,default is "debug"
  //  若沒有設定則是繼承 root logger 的設定
  // everything of spring log level is default "info"

* root: root Logger
以 root 的 LEVEL 為主,ex: 假設 root level 為 INFO,那麼即使設為 DEBUG 也只會輸出到 INFO level。

* API
  • PatternLayout
    • The goal of this class is to format a LoggingEvent and return the results as a String. The results depend on the conversion pattern.
    • The left justification flag which is just the minus (-) character comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output.
    • The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated.
    • This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end.
  • ConsoleAppender
    • Appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.
  • LevelRangeFilter
    • A very simple filter based on level matching, which can be used to reject messages with priorities outside a certain range.
    • The filter admits three options LevelMin, LevelMax and AcceptOnMatch.
    • If LevelMinw/LevelMax is not defined, then there is no minimum/maximum acceptable level (ie a level is never rejected for being too "low"/unimportant// "high"/important).
  • DailyRollingFileAppender
    • The rolling schedule is specified by the DatePattern option. This pattern should follow the SimpleDateFormat conventions. In particular, you must escape literal text within a pair of single quotes. A formatted version of the date pattern is used as the suffix for the rolled file name.
  • Logger
  • Level
    • Defines the minimum set of levels recognized by the system, that is OFF, FATAL, ERROR, WARN, INFODEBUG and ALL.
    • TRACE: The TRACE Level designates finer-grained informational events than the DEBUG

* Reference
- log4j 基本配置
- Apache Log4j 1.2.16 API
- log4j使用指南 ***
- Apache Log4j 学习笔记 **
- org.apache.log4j.Logger 详解 **
- wiki

2011年4月7日 星期四

[J2EE] WEB-INF

外界無法經由網址直接存取 WEB-INF 下的檔案

故若是在 WEB-INF 下的檔案必得經由 controller 存取

[Configuration] pom.xml

* Project Object Model (POM)
  • The fundamental unit of work in Maven.
  • An XML file that contains information about the project and configuration details used by Maven to build the project.
  • Contains default values for most projects, ex: the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/main/test; and so on.

* 在 Maven 中,有三個內建的建構生命週期:
  • Default: 處理專案部署。
  • Clean: 處理專案資源清除。
  • Site: 處理專案文件。

* 使用 mvn 指令呼叫執行某個階段,都是呼叫某個 plugin 來執行,像是編譯時會使用 mvn compile,這會呼叫 Maven 的 maven-compiler-plugin,這個 plugin 在 Super POM 中會有預設值。

* plugin
  • maven-resources-plugin: 處理專案資源的複製。
  • maven-compiler-plugin: 負責專案的編譯。
  • maven-surefire-plugin: 負責專案的單元測試。
  • maven-jar-plugin: 負責專案的包裝。
  • maven-clean-plugin
    • Will delete the target directory by default. You may configure it to delete additional directories and files.
    • The directory in the fileset is a relative path inside a project,


* 可有以下關係
  • 繼承
  •   
          
           目前使用的 POM 版本
           組織或項目的唯一標誌,會成為預設的套件名稱  
           項目的通用名稱,這將成為你存放製品的資料夾名稱,通常就是專案名稱  
           產品版本  
           打包出來的檔案類別, ex: war, jar...  
      
    
  • 依賴
  •   
          
          組織或項目的唯一標誌  
          項目的通用名稱  
          版本  
          檔案類型 (war, jar...)  
          用於限制相應的依賴範圍,ex: compile, runtime...  
          用於連續依賴時  
          
        ...  
        
    
  • 合成

* Build
  
   
      打包後的檔案名稱
        
      打包檔案目錄  
        
        
            
        
      

        
            
              資源所在位置  
            
          
    


* Reference
- Introduction to the POM
- 我的Maven2之旅:五.pom.xml基本元素介绍
- maven 配置篇 之pom.xml(一) **
- JUnit Gossip: 使用 Maven
- JUnit Gossip: POM 設定檔
- JUnit Gossip: 建構生命週期
- Maven Clean plugin - Delete Additional Files Not Exposed to Maven

Bind Sevice

* 若有相同的 service 時, 則只能 bind 到第一個安裝的 service。

2011年4月6日 星期三

[Configuration] web.xml

* 以下會影響正確性
  • 大小寫
  • 元素順序,ex: servlet元素必須出現在所有servlet-mapping元素之前。

* 元素配置


    
    










    



    
    
    
        




    
    




    
























* Reference
- Tomcat中用web.xml控制Web应用详解(1)
- web.xml元素:常见设定值一览 **

Spring and Struts2

Spring 像是 framework 的工具


Struts2則是實現 mvc framework

2011年4月5日 星期二

Code Style

@(#) STATIC ATTRIBUTES

@(#) ATTRIBUTES

@(#) INNER CLASSES

@(#) CONSTRUCTORS

@(#) PUBLIC METHODS

@(#) PROTECTED METHODS

@(#) PRIVATE METHODS

2011年4月4日 星期一

[DevGuide] Designing for Responsiveness

* Application Not Responding (ANR)
The system displays an ANR if an application cannot respond to user input.

* What Triggers ANR?
Application responsiveness is monitored by the Activity Manager and Window Manager system services.
  • No response to an input event (e.g. key press, screen touch) within 5 seconds
  • A BroadcastReceiver hasn't finished executing within 10 seconds

* Android applications normally run entirely on a single (i.e. main) thread.
This means that anything your application is doing in the main thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or Intent broadcast.


[How to Avoid ANR]

* The recommended approach is to create a child thread and do most of your work there.
  • This keeps the main thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen. 
  • Since such threading usually is accomplished at the class level.

* Potentially long running operations (ex: network or database operations, or computationally expensive calculations such as resizing bitmaps) should be done in a child thread (or in the case of databases operations, via an asynchronous request). However, this does not mean that your main thread should block while waiting for the child thread to complete.

* Should also avoid starting an Activity from an Intent Receiver (it will spawn a new screen that will steal focus from whatever application the user is currently has running). If your application has something to show the user in response to an Intent broadcast, it should do so using the Notification Manager.



* Reference
- Designing for Responsiveness

eclipse ini setting

* eclipse JVM
-vm
C:/(jdk path)/bin/javaw.exe

* The priority of eclipse using JVM
  1. eclipse.ini
  2. jre in eclipse
  3. jre that set in system


* Reference
- Rex
- Eclipse.ini
- FAQ How do I run Eclipse?
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s are also using JDK installs

2011年4月3日 星期日

[API] Alarm

* Alarm 會被取消的狀態

1. 重新開機

2. 重新安裝 app


---- Update on 2013/06/20 ----

更新不影響其狀態。


---- Update on 2013/08/25 ----

如果時間已經超過設定的 trigger time 那麼會馬上執行。

2011年4月2日 星期六

Security 的三個層次

1. 管道安全性: ex: https

2. 語言通不通: ex: 內文加密 PK

3. 認不認識對方: ex: pwd