星期二, 四月 02, 2013
编写自己的Wrapper Class
A URL Connection to a Java ARchive (JAR) file or an entry in a JAR file.
The syntax of a JAR URL is:
jar:<url>!/{entry}
参考: java.net.JarURLConnection
http://java.sun.com/j2se/1.4.2/docs/api/java/net/JarURLConnection.html
目的:要解决在可执行Jar文件中使用包含在其他地方的Jar文件中的class.
初级方法:使用Java extension机制,通过编写启动脚本来解决问题:
把第三方的Jar Library复制到{java home}\jre\lib\ext 目录下
或使用java ?Xbootclasspath 命令行参数:
-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
大多数的Java 应用服务器都使用了制作启动脚本的方法.
例子: 脚本run.bat 可以参看Jboss Application的run.bat或run.sh脚本
中级方法: 利用Jar 扩展机制规范,通过制作安装包来解决问题.
利用Jar包扩展(extension)机制通过Manifest Specification 来编写
参考: http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html
利用META-INF/MANIFEST.MF文件中的Class-Path:来指定需要引用的第三包,
将需要的第三方包放到在Class-Path:指定的目录下,然后把主程序jar包和第三包
制作安装包,在安装的时候按照目录结构安装。然后SystemClassLoader(sun.misc.Launcher)
自动加载
高级方法,编写自己的Container类或bootStrap Wrapper类.
思路:实现一个Custom ClassLoader-系统类java.开头的类让SysemClassLoader来载入,
其他的类用CustomClassLoader读取给出的Jar Library 包来实现.
最终实现 Class.forName(String name)自动使用Custom ClassLoader来寻找和加载class.
这里需要介绍一些Java ClassLoader的一些限制的知识,ClassLoader的详细原理,可以参看
其他相关介绍.
Hack Java:
强行设置系统默认的ClassLoader,有作用吗?No
Field tclass = ClassLoader.class.getDeclaredField("scl");
tclass.setAccessible(true);
tclass.set(null,Mydefault);
JVM的 SystemClassLoader是 sun.misc.Luncher的实例,这里面包含native的本地方法,
其中工作算法不得而之,JVM唯一给我们留下的扩展机制就是改变ContextClassLoader来实现
JVM的ClassLoader委托模式(delegation mode).
详细介绍:
Custom ClassLoader代码
/**
* Don't Reinvent wheel
* 使用 URLClassLoader 来载入Jar Library
*
*/
import java.io.InputStream;
import java.io.File;
import java.net.URLClassLoader;
import java.util.*;
import java.net.URL;
public class myClassLoaderByURL extends java.lang.ClassLoader
{
static java.net.URL _url=null;
static String libpath=null;
public static myClassLoaderByURL defaultld=null;
public myClassLoaderByURL(String liburl,ClassLoader parent) throws Exception
{
super(parent);
if(!checkJar(liburl))
{
throw new java.lang.RuntimeException("Jar File "+liburl+" not exist");
}
try
{
_url = new URL("jar:file:/"+liburl+"!/");
}catch(Exception e)
{
e.printStackTrace();
}
}
protected Class findClass(String name) throws ClassNotFoundException
{
Class speClass=null;
try
{
speClass = getParent().loadClass(name );
System.out.println("myClassLoaderByURL Msg: parent CLD find Class............"+name);
} catch( ClassNotFoundException cnfe )
{
try
{
String rname = name;
rname = rname.replace ('.', '/') + ".class";//重要,必须还原为目录格式
System.out.println("myClassLoaderByURL Msg: Option jar Lib:"+_url.toExternalForm());
System.out.println("myClassLoaderByURL Msg: find class name:"+rname);
URLClassLoader ucld = new URLClassLoader(new URL[]{_url},myClassLoaderByURL.class.getClassLoader());
InputStream is = ucld.getResourceAsStream(rname);
if(is==null)
{
throw new ClassNotFoundException(" Can't Find Specified Class:"+name);
}
int size=is.available();
byte[] b = new byte[size];
is.read(b,0,size);
is.close();
speClass = defineClass(name,b,0,b.length);
}catch(Exception ioe)
{
ioe.printStackTrace();
throw new ClassNotFoundException();
}
}catch(Exception ce)
{
ce.printStackTrace();
throw new ClassNotFoundException();
}
System.out.println("myClassLoaderByURL Msg: ok,load success");
return speClass;
}
public Class loadClass( String pClassName, boolean pResolve ) throws ClassNotFoundException {
System.out.println("myClassLoaderByURL Msg:loadClass(), resolve: " + pResolve );
Class lClass = findLoadedClass( pClassName );
lClass = findClass( pClassName );
//ResolveClass.
try {
if( pResolve ) {
System.out.println("myClassLoaderByURL Msg:resolve class: " + lClass );
resolveClass( lClass );
}
} catch (Error e) {
e.printStackTrace();
throw e;
}
return lClass;
}
public Class loadClass( String pClassName) throws ClassNotFoundException
{
Class t =loadClass(pClassName,true);
return t;
}
private boolean checkJar(String jarfile)
{
System.out.println("myClassLoaderByURL Msg: checkJar:"+jarfile);
File jarf = new File(jarfile);
try
{
System.out.println(jarf.getCanonicalFile());
}catch(Exception ee)
{
}
return jarf.exists();
}
}
编写自己的Boot Wrapper类:
import java.io.*;
import java.util.zip.*;
import java.util.jar.*;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Method;
public class testClassLoaderByURL extends java.lang.Thread
{
static myClassLoaderByURL Mydefault;
static String JarLibraryPath="";
static String executedClass = "";
public static void main(String[] args) throws Exception
{
if(args.length <2)
{
System.out.println("Syntax testClassLoaderByURL /path/xx.jar Executable class name(include main method");
System.out.println("Example: java testClassLoaderByURL c:\\temp\\test.jar org.sun.go");
System.exit(0);
}
JarLibraryPath=args[0];
executedClass = args[1];
//here can verified parameter
System.out.println("Jar is:"+JarLibraryPath+", Class is:"+executedClass);
System.out.println("Begin Load");
Mydefault = new myClassLoaderByURL(JarLibraryPath,testClassLoader.class.getClassLoader());
myClassLoaderByURL.defaultld = Mydefault;
Thread.currentThread().setContextClassLoader(Mydefault);
Class cls = Mydefault.loadClass(executedClass);//重要.
Method main = cls.getMethod("main", new Class[]{String[].class});
main.invoke(cls, new Object[]{args});
}
}
Javascript 变量范围问题.
function test1()
{
for (i=0;i<3 br="" i=""> {
print("test1:"+i);
}
}
function test2()
{
for (i=0;i<3 br="" i=""> {
print("test2:"+i);
test1();
}
}
test2();
得到结果,出乎人的意料:3>3>
test2:0
test1:0
test1:1
test1:2
修改函数test1 如下:
function test1()
{
for (var i=0;i<3 br="" i=""> {
print("test1:"+i);
}
}3>
得到结果,符合预期:
test2:0
test1:0
test1:1
test1:2
test2:1
test1:0
test1:1
test1:2
test2:2
test1:0
test1:1
test1:2
这是因为Javascript的变量范围 scope是函数级别的,并没有其他语言级别的local概念。
所以为了避免递归或者嵌套查询问题,最好在变脸名前宣布var,或者使用Ecmascript 1.7
里的let关键词来避免这种情况。
关于可靠性的数序模型
| 可靠性特征量 | R(t) | F(t) | f(t) | λ(t) |
| R(t)(可靠度) | - | 1-F(t) | ||
| F(t)(累积失效率) | 1-R(t) | - | ||
| f(t)(概率密度) | - | |||
| λ(t)(失效率) | - |
Android Eclipse Project JNI 指引
需要首先安装JDK,Eclipse,cygwin环境或者mingw环境,下载Android SDK,ndk.
http://code.google.com/p/awesomeguy/wiki/JNITutorial#JNI_Sources
JNI目录必须在项目最顶级目录下建立。所有的文件都要放在这里,
包括Android.mk文件,Android.mk文件内容看上述指引内复制后修改为自己实际目录即可。
.c/.cc文件。
头文件,可以借用javah来生成,如图设置Eclipse的位置工具即可。
如此则可以运行该外部工具生成对应的.h头文件。
编写完自己的c/cc文件后需要到终端运行NDK 提供的命令行工具来编译生成库。
项目的根目录及包含jni的该层目录。
运行ndk-build即可完成编译工作。看ndk-build编译图.
ndk-build编译图.
星期二, 三月 26, 2013
如何避免 field symbol has not yet been assigned 小技巧tips
所以必须用赋值操作 = 或者move, <fs2>=<fs1>. 或者move <fs1> to <fs2>来进行copy value的操作。
但是这两个语句往往就会出现 field symbol has not yet been assigned.
所以根据<fs2>的类型来做一下申明即可避免这种错误。
Data: ref1 like <fs2>的数据类型,如果是个internal table的 符号,
field-symbols <fs2> like line of so_table.
那么就需要以下声明:
data: lc_dref1 like line of so_table.
ASSIGN lc_dref1 TO <fs2>.
然后再使用 <fs2> = <fs1>. 就无问题了。
可以使用 UNASSIGN <fs2> 来释放。
星期二, 三月 19, 2013
星期一, 三月 18, 2013
如何在ant里获取时间字符串,然后用于其他task
1.得到当前时间格式化字符串 TODAY_UK
<tstamp>
<format property="TODAY_UK" pattern="yyyyMMdd_HHmmsssss" locale="cn,CN"/>
</tstamp>
2.删除符合某个规则所有文件。
<delete verbose="true" failonerror="false">
<fileset dir="." includes="Bonus*.jar"/>
</delete>
3.新建包含时间字符串的文件。
<jar destfile="Bonus${TODAY_UK}.jar" filesetmanifest="skip">
<zipfileset dir="." prefix="bonus" >
<exclude name="sqlite*.*"/>
<include name="*.c"/>
<include name="*.cpp"/>
<include name="*.h"/>
<include name="*.xml"/>
<include name="*.xrc"/>
<include name="*.rc"/>
<include name="*.bat"/>
<include name="*.sql"/>
</zipfileset>
星期三, 三月 13, 2013
Google V8 在 Window 7和mingw 环境下编译通过
一般通过修改相关配置文件的命令行选项可以纠正之。
其次,出现 undefined reference to vtable for XXXX的link错误提示,vtable是c++用来处理虚函数编译器算法,可以参看
Virtual method table - Wikipedia, the free encyclopedia。
所以出现这种提示,2种可能,
1.虚函数方法没有实现,只是在.h头部文件中声明而已。
2.实现虚函数的.cc文件没有放到link命令行内。
1.首先下载最新的V8 Javascript源代码(必须首先安装上svn工具)
svn checkout http://v8.googlecode.com/svn/trunk/ v82.进入v8 目录 cd v8,然后准备新的Google v8 编译管理工具 gyp
注意: http://v8.googlecode.com/svn/branches/ 这个目录下是用于Google浏览器的稳定版本,可以下载最大数字的版本编译svn checkout http://v8.googlecode.com/svn/branches/3.16/ v8_316 。
svn co http://gyp.googlecode.com/svn/trunk build/gyp
然后运行:
python build\gyp_v8 --generator-output="out" -Dv8_target_arch=ia32
注意:如果是使用msvc可以告之gyp使用的visual studio express 版本:
python build\gyp_v8 -G msvs_version=2010 --generator-output="out" -Dv8_target_arch=ia32
最后开始编译并声称v8.dll和libv8.a文件
如果最后link出现了vtable 错误,就寻找包含的方法在那个.cc文件,编译并手动生成v8.dll和libv8.a
g++ -o obj\release\marking-thread.o -c -fno-rtti -fno-exceptions -DWIN32 -Wall -Werror -W -Wno-unused-parameter -Woverloaded-virtual -Wnon-virtual-dtor -pedantic -Wno-long-long -Wno-pedantic-ms-format -m32 -O3 -fomit-frame-pointer -fdata-sections -ffunction-sections -DWIN32 -DV8_TARGET_ARCH_IA32 -DENABLE_DEBUGGER_SUPPORT -Isrc src\marking-thread.cc
还必须要下载 第三方的包,
在v8 或v8_316目录下:
1.svn co http://src.chromium.org/svn/trunk/deps/third_party/cygwin@66844 third_party/cygwin
2.svn co http://src.chromium.org/svn/trunk/tools/third_party/python_26@89111 third_party/python_26
这两个包用来msvc用来生成snapshot.lib的过程中,必须的。
至此即可用v8 Engine嵌入自己的程序来使用了。
星期六, 三月 09, 2013
v8 Javascript嵌入Javascript代码注意。
const char * jscode=
"ball=['1','2','3','4'];\n"
"result=''\n"
"function test(){ for(i=0;i<ball.length;i++){result=result+ball[i];}return result;}"
" ts= test();"
;
注意如果不加入换行\n,则每行代码结束必须加入分号;,来表示一行代码结束
星期五, 三月 08, 2013
Window 7 Mingw 环境编译 Google V8 Javascript 步骤
http://code.google.com/p/v8/wiki/Source
svn checkout http://v8.googlecode.com/svn/trunk/ v8
这个代码根据说明是更新频率是按周来的,实际我发现一天之内就可能有多个版本。
所以不得不寻找稳定的版本,最后找到一下地址:
http://v8.googlecode.com/svn/branches/
得到最新的稳定版本到 v8_39目录下
svn checkout http://v8.googlecode.com/svn/branches/3.9/ v8_39
然后进入v8_39目录用以下命令成功编译.
d:\Python26\Scripts\scons.py mode=release os=win32 toolchain=gcc importenv=PATH library=shared snapshot=on I_know_I_should_build_with_GYP=yes
1.
编译的环境,首先需要安装mingw环境,我使用的tdm gcc 4.7.1 的
http://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm-gcc-4.7.1-2.exe/download
2.安装python,我使用的python for window 2.6版本并且要安装scons 模块.
下载展开到一个目录后,运行 python setup.py 就将scons 安装到了python 安装目录的Scirpts目录下。
after source code download,use follow command line .
all is ok.
星期二, 三月 05, 2013
It is time say good by mingw.
之间的互操作越来越困难了,C时代的mingw的得心应手,在Visual C++的复杂,和import library的机制下,需要花费越来越多的精力了。
不同的C++生成DLL之间的使用,终于成为一个不可能的任务了。
终于决定gcc为主,msvc用作window平台工具了。
星期一, 二月 25, 2013
delopy web app to Google appengine
D:\appengine-java-sdk-1.7.5\bin\appcfg.cmd update war -A szpureweb
星期日, 一月 27, 2013
如何在Eclipse 中实现Build number的功能
如图
1.首先输入当前Project的ant build.xml文件,和一个保存编译计数器的properties文件。
加入新的target用name 来指定:
2.给项目加入Ant Builder ,项目属性里寻找Builder 然后加入Ant Builder,然后将其用
up按钮移动到最顶部,让其第一个被调用.
3.要加入refresh的功能和默认的target,这个参考如上和如下两个图就知道了.
星期二, 一月 22, 2013
星期三, 一月 16, 2013
abap 执行本地SQL Native SQL
insert into test(code) valeus(
:pl_wa-Entity_Code,
);
endexec.
if sy-subrc ne 0.
write:/,'fail!'.
endif.
commit work.
星期一, 一月 14, 2013
Dynamic load properties file by Absolulated directory way.
通过getResourceAsStream方式载入的文件被Cache.
public InputStream getpro()
{
String propersetupfile="com/resources/controlSetup.properties";
String dir=this.getServlet().getServletContext().getRealPath("/");
System.out.println("real directory:"+dir);
String pdir=dir+"WEB-INF/classes/"+propersetupfile;
System.out.println("properties file in:"+pdir);
InputStream fi=null;
try
{
fi=new FileInputStream(pdir);
}catch(Exception e)
{
e.printStackTrace();
fi= this.getClass()
.getClassLoader()
.getResourceAsStream(propersetupfile);
}
return fi;
// return this.getClass()
// .getClassLoader()
// .getResourceAsStream(propersetupfile);
}
Java读取某个目录下的制定properteis tips
ResourceBundle rb = ResourceBundle.getBundle(fileName, new java.util.Locale(""));
在test目录下有2个文件 app.properties和app_en.properties
如果想读取 app.properties 则只要给一个空的Locale("")即可。
同样,想读取app_en.properties需要用 Locale("en")
Java计算tips,如何从一个数字字符串中得到字符整数转换。
int x = (character – '0') * 20;
星期四, 一月 10, 2013
abap 如何获得Internal table的定义字段,用RTTS
*& Report Z_GET_TABLE_COLUMNNAME
*& get component structure by RTTS.(Runtime Type Servcies)
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT Z_GET_TABLE_COLUMNNAME.
TYPES: BEGIN OF typ_itab,
sex(20) TYPE c,
tigh TYPE i,
salary TYPE p LENGTH 12 DECIMALS 3,
name type string,
END OF typ_itab.
DATA: i_itab TYPE TABLE OF typ_itab , "if has with header line,exceptin happend
l_tabledescr_ref TYPE REF TO cl_abap_tabledescr,
l_descr_ref TYPE REF TO cl_abap_structdescr,
wa_table TYPE abap_compdescr.
l_tabledescr_ref ?= cl_abap_typedescr=>describe_by_data( i_itab ).
l_descr_ref ?= l_tabledescr_ref->get_table_line_type( ).
LOOP AT l_descr_ref->components INTO wa_table .
WRITE: / wa_table-name,wa_table-type_kind,
wa_table-length,wa_table-decimals.
ENDLOOP.
cl_salv_table 获得 所有internal table 定义field name
USING alv_table TYPE REF TO cl_salv_table.
DATA: lo_columns TYPE REF TO cl_salv_columns.
DATA: lo_column TYPE REF TO cl_salv_column.
"add field authority check
DATA: alv_fieldcat TYPE slis_fieldcat_alv.
DATA: cx_exc_ref TYPE REF TO cx_root.
DATA error_text TYPE c.
DATA:
lta_column_ref TYPE salv_t_column_ref
.
FIELD-SYMBOLS:
<lfs_column> LIKE LINE OF lta_column_ref
lo_columns = alv_table->get_columns( ).
lta_column_ref = lo_columns->get( ).
LOOP AT lta_column_ref ASSIGNING <lfs_column> .
lo_column = <lfs_column>-r_column.
write <lfs_column>-columnname.
ENDLOOP.
星期三, 一月 02, 2013
Volatile: why prevent compiler reorder code
解释链接:
http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#volatile
星期三, 十一月 28, 2012
Android,不再为线程烦恼
不再为线程烦恼
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Bitmap b = loadImageFromNetwork();
mImageView.setImageBitmap(b);
}
}).start();
}new Thread(new Runnable() {
public void run() {
final Bitmap b = loadImageFromNetwork();
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(b);
}
});
}
}).start();
}
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask {
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
- 你可以用类型,泛型或这参数化参数制定处理的值或任务最后的值
- 方法 doInBackground() 自动在工人线程中执行
- onPreExecute(), onPostExecute() 和 onProgressUpdate() 被UI线程调用
- doInBackground() 返回的值被送往方法 onPostExecute()
- 你可以在UI 线程内任何时间调用 publishProgress() 来执行 onProgressUpdate()
- 你可以在任何线程内任何时间放弃任务
Fwd: 如何在Android 4 使用optins menu
Google is happy to bid farewell to the Menu button, and they don’t even want you calling that funny little three-dot button the “menu.”
Rather, Google wants us to think of the menu at an “action overflow.”
When there isn’t enough space in the Action Bar for all the icons you might need in an app, the action overflow handles it.
编写兼容的程序从2.x-4.x ,在Eclipse ADT 里编译的时候就必须要选择高版本LEVEL API,
但在发布的时候,AndroidManifest.xml,要删除掉 android:targetSdkVersion="16" ,而只保留 android:minSdkVersion="10"
android 2.3.3 level api,则在Android 4.X 系统下,options menu出现在了系统导航条上的三点菜单里。
注意,options menu->action bar ->system navigation bar -> overflow menu 几个概念的演化。
如图

星期四, 十一月 22, 2012
Fwd: git Cannot pull into a repository with state: MERGING 解决
编辑后,使用add to index ,然后再commit,即可解决这个问题
星期四, 十一月 15, 2012
Android 4.2 root 后adb shell ,su 发现read-only file system.如何解决
mount 发现/system / 两个文件卷的路径,一般是第一列为物理路径,
第二列为系统卷,重新remount为可读写,
1.mount -o remount,rw rootfs
2.mount -o remount,rw /dev/block/platform/omap/omap_hsmmc.0/by-name/system
3.现在可以复制东西到/etc/了
原因是Android要在/ 卷和/system卷之间做很多的配置文件同步的比如,/etc/hosts,就会在/system/etc 下同步的。
正则表达式发现不包含某个特定字符串的表达
而以下pattern则无法处理包含ExcludeString的独立行
^(.(?!stringToExclude))*$
星期四, 十一月 08, 2012
正则表达式的例子
更加精简的pattern.
grep -P "\d{7}.(\d\d).{7}"
用来搜索同一个文本内的如下格式的数据:
2012083:04-09-14-15-26-33-04
2012084:02-10-20-26-28-29-14
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888
Re: 正则表达一点例子.
更加精简的pattern.
grep -P "\d{7}.(\d\d).{7}"
用来搜索同一个文本内的如下格式的数据:
2012083:04-09-14-15-26-33-04
2012084:02-10-20-26-28-29-14
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888
grep -P "\d{7}(\s+|:)(\d\d[\s+-]?){7}"
用来搜索同一个文本内的如下格式的数据:
2012083:04-09-14-15-26-33-04
2012084:02-10-20-26-28-29-14
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888
正则表达一点例子.
用来搜索同一个文本内的如下格式的数据:
2012083:04-09-14-15-26-33-04
2012084:02-10-20-26-28-29-14
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888
星期三, 十一月 07, 2012
规则表达一些概念
贪婪(greedy)
一般默认情况下,正则的量词是贪婪的,也就是“尽可能多地匹配”,符号通常是:* + ? {num,num}
举个例子说,比如"a+"这个正则,对于"aaaaaab"这个字符串,它就会匹配到6个a,这是最常见的情况
勉强(reluctant)[也叫懒惰(lazy)]
勉强与贪婪正好是相对的,它“尽可能少地匹配”,它会用最小的努力,去完成(应付)正则的匹配要求,所以很懒。符号是贪婪量词后面加上问号:*? +? ?? {num,num}?
回到前面的那个例子,如果正则换成了“勉强”的,"a+?",对于目标字符串还是"aaaaaab",那么这个正则就只会匹配一个a,因为"+"量词的意思是“一个或一个以上”,匹配一个a就能满足要求,它就不再去尝试了(不像贪婪的"a+"那样,不遗余力将所有符合要求的都匹配了)
“勉强”的量词在实际的使用中也是比较常见的,例如引号配对之类的问题,例如有这么一个字符串,"A says, 'bb'.",想要用正则将单引号里面的bb匹配出来,可以用正则'.*',这种情况下默认的贪婪量词也是能适用的,但如果这个字符串变成了 "A says, 'bb', and C says, 'ddddd'.",如果还是用'.*',由于贪婪,它匹配到的并不是你想要的'bb', and C says, 'ddddd',中间的.(点元字符)代表任何字符,当然也包括单引号,所以'.*'会将两个单引号中间的所有内容都匹配,不管是不是含有单引号。显然这不是我们想要的结果。如果用“勉强”的量词,'.*?',加一个问号,那么它就会尽最小的能力去完成,只要下一个字符是单引号,.*?就停止尝试,结果它就只会匹配'bb',不会将后面的, and ....那些都纳进来。
侵占(possessive)
这个用得很少,侵占量词在很多语言的正则中都没有被支持,它的符号是在贪婪量词后面加上一个加号:*+ ++ ?+ {num,num}+
侵占量词有个特点,它前面的子表达式作为一个整体,不记录回溯点(关于回溯方面的问题,建议你去看一看《精通正则表达式》这本书,讲得比较详细),通常是可以利用这个特点,对正则的匹配效率进行优化。其实它是和固化分组等价的,不支持侵占量词的语言中可以用固化分组的形式来代替。固化分组的写法:(?>)
有个例子可以帮助理解侵占量词的特点,但这个例子并不实用:
对于字符串"abbbbbbc",如果用普通的(贪婪)正则"a.*c"来匹配的话,它可以匹配整个字符串,因为.*可以匹配任何数量的任何字符,它是可以匹配bbbbbbc的,但它会发现正则后面还有一个c需要匹配,否则不能匹配成功,所以.*就“退回”一个字符c,使得整个正则能够成功匹配(回溯),所以贪婪即使是“贪”,它还是会顾全大局,退回一些必要的字符。
换侵占量词就不同了,"a.*+c"是一个侵占式的,它并不能成功匹配,因为.*+太霸道,占着最后的那个c不肯放回(不回溯),所以a.*+前面就已经匹配了整个字符串abbbbbbc,a.*+正则后面的c发现没有字符可以匹配了,前面的又不肯吐出一个来,所以整个正则以匹配失败结束。(这个"a.*+c"可以用固化分组改写成:"a(?>.*)c")
星期二, 十一月 06, 2012
Android 2.3.3 以上通用Notification 生成 代码.使用NotificationCompat.Builder
//The IDs must be unique only within the namespace of your .apk's package nam
//这个ID只需要在你自己的app 空间里唯一即可,不是整个系统唯一
public void setupNotification(Context ctx)
{
Intent notificationIntent = new Intent(ctx, StartGateActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
0, notificationIntent,
0);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(100);
Resources res = ctx.getResources();
CharSequence text = res.getString(R.string.nf_text);
CharSequence contentTitle = res.getString(R.string.nf_title);
CharSequence ticket = res.getString(R.string.nf_ticket);
NotificationCompat.Builder builder= new NotificationCompat.Builder(ctx);
builder.setContentTitle(contentTitle)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true) //this flag notification record can't be clear
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setTicker(ticket)
.setContentIntent(contentIntent);
Notification nf = builder.build();
nm.notify(100, nf);
}
星期一, 十一月 05, 2012
如何在app中3.0 API LEVEL 11后加入notification.
{
Intent notificationIntent = new Intent(ctx, LifeDemo.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("Demo Show")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Demo Show 1")
.setContentText("Demo Show2");
Notification n = builder.build();
nm.notify(1, n);
}
星期四, 十一月 01, 2012
如何给手机刷CMW Mod
fastboot boot recovery-clockwork-touch-6.0.1.4-maguro.img
#永久刷新
fastboot flash recovery recovery-clockwork-touch-6.0.1.4-maguro.img
adb reboot
发布一个apk的准备工作.
1.keytool -genkey -v -keystore debug.keystore -alias wxkdebugkey -keyalg RSA-validity 14000
跟随提示回答问题.
2.Jarsigner -verbose -keystore debug.keystore yourapk.apk wxkdebugkey
3.jarsigner -verify yourapk.apk
4.zipalign -v 4 yourapk.apk lastname.apk
5.start a avd
tools\android.bat list avd
Available Android Virtual Devices:
Name: AVD233
Path: d:\temp\.android\avd\AVD233.avd
Target: Android 2.3.3 (API level 10)
ABI: armeabi
Skin: WVGA800
---------
Name: avd411
Path: d:\temp\.android\avd\avd411.avd
Target: Android 4.1 (API level 16)
ABI: armeabi-v7a
Skin: WVGA800
Sdcard: 512M
Snapshot: true
emulator -avd AVD233 启动
5.adb devices
adb -e install lastname.apk
Android 产生key
JDK 7.0 Display MD5 key
keytool -v -list -alias wxkdebugkey -keystore my-release-key.keystore
星期二, 十月 23, 2012
比如屏幕旋转90度,Configuration Changes
Configuration Changes
If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.
Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).
This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.
In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.
| 1 2 3 4 5 6 7 | <activity android:name=".SomeActivity" android:label="@string/app_name" android:configChanges="orientation"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter></activity> |
The "configChanges" attribute tells Android Platform that, some of the config changes will be handled by the activity by themselves. Platform does not need to do any special handling for these.
Android Configuration change.
In Android, a configuration change causes the current activity to go away and be recreated.
The application itself keeps on running, but it has the opportunity to change
how the activity is displayed in response to the configuration change.
use onSaveInstanceState(Bundle) to keep state or other runtime data.
root Jelly Bean 4.1.2
首先必须安装了Android sdk和对应手机的USB驱动。
android sdk: http://developer.android.com/sdk/index.html
需要一些工具
1.download cmw(ClockworkMod) recover 工具.
http://www.clockworkmod.com/rommanager
查找对应的设备后面的文件。
Galaxy Nexus 就要下载:
http://download2.clockworkmod.com/recoveries/recovery-clockwork-touch-6.0.1.0-maguro.img
2. download superuser packagae
http://androidsu.com/superuser/
下载 Superuser-3.1.3-arm-signed.zip
3.连接手机USB,设置为debug模式
用adb push Superuser-3.1.3-arm-signed.zip /sdcard/Superuser-3.1.3-arm-
传送到手机上
4.临时进入cmw recover 并安装superuser 包
进入window 命令行状态 cmd,进入android sdk安装目录下的android-sdk-windows\platform-tools目录.
adb reboot bootloader
fastboot boot recovery-clockwork-touch-6.0.1.0-maguro.img
等待3秒出现,安装CMW 安装界面,如果是touch版本,则用手势滚动屏幕,选择安装
select install zip from sdcard.
否则用音量上下键移动,电源键确认来选择。
安装完成后,用adb reboot 启动手机即可。
星期四, 九月 20, 2012
Fwd: OSWORKFLOW on JBOSS 4.22 Config.
http://java.net/downloads/osworkflow/
2.将osworkflow-2.8.0-example.war展开到jboss 422/server/default/deploy/osworkflow-2.8.0-example.war 目录下。
3.在MYSQL 上建立一个osworkflow的database,并运行osworkflow-2.8.0\src\etc\deployment\jdbc
\mysql.sql,建立其需要使用的表格.
4.配置JBOSS 数据源
在jboss 422/server/default/deploy/mysql-ds.xml里加入数据源:
<!-- add for osworkflow datasorce -->
<local-tx-datasource>
<jndi-name>osworkflow</jndi-name>
<connection-url>jdbc:mysql://192.1.1.100:3306/osworkflow?autoReconnect=true&useUnicode=TRUE&characterEncoding=utf-8</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password></password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<min-pool-size>5</min-pool-size>
<max-pool-size>10</max-pool-size>
<idle-timeout-minutes>1</idle-timeout-minutes>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
5.进入jboss 422/server/default/deploy/osworkflow-2.8.0-example.war/WEB-INF/classes下修改配置,加入数据源说明.
注意Linux table name 有大小写的问题。
osuser.xml:
<opensymphony-user>
<provider class="com.opensymphony.user.provider.jdbc.JDBCAccessProvider">
<property name="user.table">OS_USER</property>
<property name="group.table">OS_GROUP</property>
<property name="membership.table">OS_MEMBERSHIP</property>
<property name="user.name" >username</property>
<property name="user.password">passwordhash</property>
<property name="group.name">groupname</property>
<property name="membership.userName" >username</property>
<property name="membership.groupName">groupname</property>
<property name="datasource">java:/osworkflow</property>
</provider>
<provider class="com.opensymphony.user.provider.jdbc.JDBCCredentialsProvider">
<property name="user.table">OS_USER</property>
<property name="group.table">OS_GROUP</property>
<property name="membership.table">OS_MEMBERSHIP</property>
<property name="user.name" >username</property>
<property name="user.password">passwordhash</property>
<property name="group.name">groupname</property>
<property name="membership.userName" >username</property>
<property name="membership.groupName">groupname</property>
<property name="datasource">java:/osworkflow</property>
</provider>
<provider class="com.opensymphony.user.provider.jdbc.JDBCProfileProvider">
<property name="user.table">OS_USER</property>
<property name="group.table">OS_GROUP</property>
<property name="membership.table">OS_MEMBERSHIP</property>
<property name="user.name" >username</property>
<property name="user.password">passwordhash</property>
<property name="group.name">groupname</property>
<property name="membership.userName" >username</property>
<property name="membership.groupName">groupname</property>
<property name="datasource">java:/osworkflow</property>
</provider>
<authenticator class="com.opensymphony.user.authenticator.SmartAuthenticator" />
</opensymphony-user>
osworkflow.xml:
<osworkflow>
<!--persistence class="com.opensymphony.workflow.spi.memory.MemoryWorkflowStore"/-->
<persistence class="com.opensymphony.workflow.spi.jdbc.MySQLWorkflowStore">
<property key="datasource" value="java:/osworkflow"/>
<property key="entry.sequence"
value="SELECT max(id)+1 FROM OS_WFENTRY"/>
<property key="entry.table" value="OS_WFENTRY"/>
<property key="entry.id" value="ID"/>
<property key="entry.name" value="NAME"/>
<property key="entry.state" value="STATE"/>
<property key="step.sequence" value="SELECT max(ID)+1 FROM OS_STEPIDS"/>
<property key="step.sequence.increment" value="INSERT INTO OS_STEPIDS (ID) values (null)"/>
<property key="step.sequence.retrieve" value="SELECT max(ID) FROM OS_STEPIDS"/>
<property key="entry.sequence.increment" value="INSERT INTO OS_ENTRYIDS (ID) values (null)"/>
<property key="entry.sequence.retrieve" value="SELECT max(ID) FROM OS_ENTRYIDS"/>
<property key="history.table" value="OS_HISTORYSTEP"/>
<property key="current.table" value="OS_CURRENTSTEP"/>
<property key="historyPrev.table" value="OS_HISTORYSTEP_PREV"/>
<property key="currentPrev.table" value="OS_CURRENTSTEP_PREV"/>
<property key="step.id" value="ID"/>
<property key="step.entryId" value="ENTRY_ID"/>
<property key="step.stepId" value="STEP_ID"/>
<property key="step.actionId" value="ACTION_ID"/>
<property key="step.owner" value="OWNER"/>
<property key="step.caller" value="CALLER"/>
<property key="step.startDate" value="START_DATE"/>
<property key="step.finishDate" value="FINISH_DATE"/>
<property key="step.dueDate" value="DUE_DATE"/>
<property key="step.status" value="STATUS"/>
<property key="step.previousId" value="PREVIOUS_ID"/>
</persistence>
<factory class="com.opensymphony.workflow.loader.XMLWorkflowFactory">
<property key="resource" value="workflows.xml" />
</factory>
</osworkflow>
propertyset.xml:
<propertysets>
<propertyset name="jdbc" class="com.opensymphony.module.propertyset.database.JDBCPropertySet">
<arg name="table.name" value="OS_PROPERTYENTRY"/>
<arg name="col.globalKey" value="GLOBAL_KEY"/>
<arg name="col.itemKey" value="ITEM_KEY"/>
<arg name="col.itemType" value="ITEM_TYPE"/>
<arg name="col.string" value="STRING_VALUE"/>
<arg name="col.date" value="DATE_VALUE"/>
<arg name="col.data" value="DATA_VALUE"/>
<arg name="col.float" value="FLOAT_VALUE"/>
<arg name="col.number" value="NUMBER_VALUE"/>
<arg name="datasource" value="java:/osworkflow"/>
<arg name="" value=""/>
</propertyset>
</propertysets>
至此配置完成,启动JBOSS,从浏览器输入:
http://192.168.1.1:8080/osworkflow-2.8.0-example/
即可查看.
星期二, 九月 18, 2012
Android如何确认一个Intent是否存在呢?
/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
*
* @param context The application's environment.
* @param action The Intent action to check for availability.
*
* @return True if an Intent with the specified action can be sent and
* responded to, false otherwise.
* Example:isIntentAvailable(getApplicationContext(),Intent.ACTION_WEB_SEARCH)
*/
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
星期四, 九月 13, 2012
Android动态改变语言并自动刷新菜单.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String languageToLoad = "zh";
locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
//getBaseContext().getResources().getDisplayMetrics()
Log.v("log:","local is changed!");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
}
星期二, 九月 11, 2012
Andorid xml is binary format in .apk package
只有一个例外就是 res/raw下的文件是原始文件,没有做任何处理。
Activity的getString和Activity.getResource().getString的区别是什么呢?
localized string from the application's package's default string table
Resource的getString说明如下:
Returns the string value associated with a particular resource ID. It will be stripped of any styled text information
但是查看源代码,发现activity的getString如下:
public final String getString(int resId) {
return getResources().getString(resId);
}
其实是一回事。真不明白为什么说明差异如此之大。
Android classpath access external resource.
Resources.getSystem().getString(android.R.string.somecommonstuff)
星期一, 八月 27, 2012
如何禁止wxWidgets 2.9.x Debug信息
在wx/debug.h 里设置
wxDEBUG_LEVEL=0
然后从新编译所有的库文件,再编译应用代码。
REM 清除所有的库文件
mingw32-make -f makefile.gcc SHARED=0 UNICODE=1 BUILD=release MONOLITHIC=0 USE_STC=0 USE_PROPGRID=1 USE_GUI=1 clean
REM 重新编译所有库
mingw32-make -f makefile.gcc SHARED=0 UNICODE=1 BUILD=release MONOLITHIC=0 USE_STC=0 USE_PROPGRID=1 USE_GUI=1
禁止 wxWidgets 2.9 调试信息弹出
定义wxDEBUG_LEVEL=0 在 IMPLEMENT_APP() 代码前。
星期五, 八月 10, 2012
Dynamic load css definition file.
load: /*static*/ function (url_, /*optional*/ media_) {
// We are preventing loading a file already loaded
var _links = document.getElementsByTagName("link");
if (_links.length > 0 && _links["href"] == url_) return;
// Optional parameters check
var _media = media_ === undefined || media_ === null ? "all" : media_;
var _elstyle = document.createElement("link");
_elstyle.setAttribute("rel", "stylesheet");
_elstyle.setAttribute("type", "text/css");
_elstyle.setAttribute("media", _media);
_elstyle.setAttribute("href", url_);
var _head = document.getElementsByTagName("head")[0];
_head.element.appendChild(_elstyle);
}
};
星期三, 七月 25, 2012
如何用SVG显示汉字。只需要注意HTML需要用UTF-8格式保存即可.
<body>
<p><canvas id="canvas" style="border:2px solid black;" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var data = "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" +
"<foreignObject width='100%' height='100%'>" +
"<div xmlns='http://www.w3.org/1999/xhtml' style='font-size:40px;color:red'>" +
"<em>I</em> like <span style='color:white; text-shadow:0 0 2px blue;'>网络的上面</span>" +
"</div>" +
"</foreignObject>" +
"</svg>";
var svg = new (self.BlobBuilder || self.MozBlobBuilder || self.WebKitBlobBuilder);
var DOMURL = self.URL || self.webkitURL || self;
var img = new Image();
svg.append(data);
var url = DOMURL.createObjectURL(svg.getBlob("image/svg+xml;charset=utf-8"));
img.onload = function() {
ctx.drawImage(img, 0, 0);
DOMURL.revokeObjectURL(url);
};
img.src = url;
</script>
</body>
</html>
星期一, 七月 16, 2012
tcpdump debug http traffic and dns query.
windump -i \Device\NPF_{CDF6DEBB-8C8E-49EF-91D5-CF6A69F1FE81} -n port 80 or port 443 or port 53
adb shell tcpdump -i wlan0 -n port 80 or port 443 or port 53
星期五, 六月 08, 2012
Android memory usage
App:Application
VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
星期二, 四月 03, 2012
POI 3.7如何实现cell的填充颜色。
必须在setCellValue之后,再调用setCellStyle的语句。
顺序不能错误,否则效果不如意。
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);
如何在JSP里实现Streaming content,流失内容,让内容逐渐显示
2.在out.println一些之后使用out.flush().
这样在浏览器内就显示出来效果了,逐渐显示,而不是全部一下显示了。













