2006-05-27

DWR是个好东西

DWR

如果你有个POJO写成下面的格式:

public class DwrMagic
{
public String reply()
{
return "oh, dwr magic !";
}
}

你需要通过javascript访问到这个方法并展示在页面中,容易么?DWR告诉你,相当容易:
DwrMagic.reply(function(replycontent){alert(replycontent);}

Woo~ Cool~

下面讲一下具体实施办法:
下载一个dwr.jar放到WEB-INF/lib目录下,然后修改web.xml,添加个servlet:
不贴代码了,用blogger的编辑器编辑的时候有些麻烦,它自动处理代码,即使你不愿意它处理,看dwr上的getstart吧
然后再创建个dwr.xml放在web.xml同目录下,并把自己创建的类的信息写进来:
不贴代码了,用blogger的编辑器编辑的时候有些麻烦,它自动处理代码,即使你不愿意它处理,看dwr上的getstart吧
然后建立个testdwr.html文件:
把这几个script文件连接进来:
/dwr/dwr/interface/Demo.js
/dwr/dwr/engine.js
然后再写个javascript脚本函数:

function getData(str)
{
alert(str);
}
function getReply()
{
Demo.reply(getData);
}

然后随便找个地方调用getReply()就可以看到效果了,非常cool~

2006-05-25

代用户与server交互

Cookie怎么保存

JDK里没有参考实现,只有一个CookieHandler,而且是全局的,也就是说在一个JVM内你只能代表一个用户,后来找到个JCookie,不过操作起来挺麻烦的,后来感觉好像还和IE有关联,因为我没开IE的时候它就运行出错,开着就一切OK =_=#!

最后找到一个很好的:HttpClient,是apache的,早找到就好了,T_T,挺好用的,而且还带了些sample,常用的功能基本都能展示出来,暂时先不贴代码....

2006-05-24

建立ssl连接并通过BASIC认证

贴段代码了,没啥好说的哦

public void testHttpsConnection() throws Exception
{
URL url = new URL("https://www.blogger.com/atom");
X509TrustManager xtm = new X509TrustManager()
{
public void checkClientTrusted(X509Certificate chain[], String authType)throws CertificateException{}
public void checkServerTrusted(X509Certificate chain[], String authType)throws CertificateException{}
public X509Certificate[] getAcceptedIssuers()
{
X509Certificate[] cert = {};
return cert;
}
};
TrustManager mytm[] = {xtm};
SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null,mytm, null );
SSLSocketFactory sf = ctx.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(sf);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
String usernamePassword = "username" + ":" + "password";
String encoding = new BASE64Encoder().encode(usernamePassword.getBytes());
conn.setRequestProperty("Authorization", "BASIC " + encoding);
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IoUtil.transfer(in, out);
System.out.println(new String(out.toByteArray()));
}

把用户名和密码改掉了,嘿。只要你注册一个blogger的账号就可以顺利获得结果 :)

还有,blogger的atom API在

2006-05-23

从群里偷偷瞄来的,收藏下~

Re: SSLHandshakeException: could not find trusted certificate
Sep 2, 2002 6:40 AM (reply 1 of 12)



if you work in windows in control panel you have java plugin -
there is column certificates then you click on secure site and import public key.

in unix execute ControlPanel from your java direcotry and makes the same.
it could help.

l33t0n3
Posts:5
Registered: 9/3/02 Re: SSLHandshakeException: could not find trusted certificate
Sep 3, 2002 9:31 AM (reply 2 of 12)



There are two ways to fix your problem. The first is add the certificate(s) of the target site to your TrustStore.

In the JSSE, the TrustStore object is a Keystore file which contains the public key(s) and any Root keys for a server certificate. You can add the key to your truststore by using keytool. (read JSSE documentation http://java.sun.com/products/jsse/doc/guide/API_users_guide.html to learn more about keytool):

In order to do this you will need to get the servers public (not private) key and any root certificates and then use the keytool.

Unfortunately, most of the time this is way too much of a pain in the butt to do for every ssl connection. For this reason, you can actually create your own TrustManager implementaiton and assign it to the SSLSocketFactory. The SSLSocketFactory can either be used by you directly, or you can let the java.net.URL connection factory handle the fetching and reading of sockets for you. I highly recommend using java.net.URL because it will load the https handler and you won't have to worry about writing the http requests properly.

First, you must create an implementation of the TrustManager factory. I recommend that for your first go you just trust everything (i.e. the isServerTrusted(X509Certificate[] Servers) method should always return true).

Make sure you have specified your provider (system property or dynamically as shown (this is jdk1.3 w/ JSSE 1.0.3, with jdk 1.4 it s bit different)...

java.security.Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());

get your ssl context...
ctx = SSLContext.getInstance("TLS");

create an array of 1 object containing your TrustManager...
TrustManager[] _trustm = {new your.BogusX509TrustManager()};

And then tell the context to init using your trust manager...
ctx.init(null, _trustm, null);

make that your default socket factory for ssl connections...
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

Now your SocketFactory (and TrustManager) will be used for any new https connection you open in this instance. Since you set isServerTrusted to always return true, you will trust all certificates. You can also go a bit farther and validate certificates and store certificates in your TrustStore...

2006-05-22

dom4j解析的时候乱码

link: "java中文乱码解决方案和经验"

用DOM4J读节点数据的时候老出现中文乱码,我开始以为是DOM4J的问题,然后就查,最后看到一个解决方案,感觉不太爽,然后就接着找,后来不知道怎么就发现原来错误不在DOM4J。

主要是我读文件后把它存成byte[]格式的了,然后直接
byte[] gdpBytes = retrieveBytes();
String text = new String(gdpBytes);
Document doc = DocumentHelper.parseText(text);


这样是不对滴,就在第二行出的问题,这个时候还需要作一下编码声明处理:
String text = new String(gdpBytes,"utf-8");

这样就OK了。还有几个连接,虽然没用到,但是以后可能有用:
1 2

2006-05-19

hibernate中的binary

在mapping文件中声明了一项用来存储文件的属性“bytes”:





实际上是把bytes映射成数据库表中的varchar,然后看了一下mysql的文档,varchar和binary是一样的属性,但是为什么是varchar而不直接binary就不明白了。但是varchar显然不够用于存储文件,执行存储的时候报错说
Data truncation: Data too long for column 'bytes' at row 1
后来就把它改成varbinary,顺便把最大值设为55535,然后就通过了。

没错,不是65535,如果那样的话会报错说需要把字段类型转换为BLOB。

用的是hibernate2.1, mysqlconnector5, mysql5.0

2006-05-18

瞎忙活了

鼓捣了半天,虽然没明白怎么回事,但是还是出了结果了。

就是想通过hbm.xml产生.sql文件,目的就是产生数据库表。然后就使劲地看build.xml文件,愣没看出来啥错,但是编译就是不通过,后来把build.properties改了一下,还是有错,然后把我添加到buildpath的一些包啊什么的去掉,然后就一切OK了。

见鬼

2006-05-16

Unbound classpath variable

在线教程: "Unbound classpath variable"

是变量没定义或定义的不对,可以在java Build Path里更改或增加variable。

swimlane

swimlane: "A swimlane therefore has one assignment and all tasks that reference a swimlane should not specify an assignment."

如果在一个任务中声明了swimlane,就表示你也把需要做的任务也移交给它了,所以不能再在task声明里另声明assignment.

现在才明白,wfe是把所有的操作(人工&自动)都分门别类交给swimlane来处理,这样,只要把swimlane声明好了,操作也就固定了,剩下的问题就是这些swimlane的执行顺序是什么样的?

当然,还有一个decision,需要写script或代理等,这里应该可以和规则引擎联系起来吧。

Integer.parseInt()

在org.jbpm.webapp.tag.ProcessImageTag看到这个代码:
result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
看着不怎么舒服,其实有个更简洁的途径:
result[0] = Integer.parseInt(node.attribute("x").getValue());
这个是好久以前就知道的,现在看到代码就突然想起来了,先记一下,嘿

2006-05-11

jbpm里的Authorization

Chapter 17. Security:"Authorization is optional, and there is no authorization implementation yet. "
找了半天,原来授权部分还没实现,只有个identity包,是实现认证部分的,记得在jbpm.cfg.xml里有个服务好像叫"authentication",得去看看那个starter-kit里是如何进行授权的了,应该是很简陋的那种吧,比如执行某个task的时候,预先查下数据库,看有没定义这个用户的可以执行,行就通过(本来就应该是这么回事儿嘛,有什么简陋的丫):

Token 的概念

Token
Token这个概念,现在有点糊涂了,按胡工说的,就是标识流程所处的状态,而我理解的是流程当前流到节点的标识,嗯,差不多的概念。。。
哦,找到了,在javadoc里:
represents one path of execution and maintains a pointer to a node in the ProcessDefinition. Most common way to get a hold of the token objects is with ProcessInstance.getRootToken() or ProcessInstance.findToken(String).
意思就是说一个token代表一条执行路径而且维护一个指针用来标识一个节点(就是我所理解的那层意思,不过我没理解到“代表一条执行路径”).这么说的话token的在流程定义的时候就已经确定了,按照图论......

2006-05-10

知道怎么访问hsqldb的数据文件了

首先下载hsqldb.然后把要查看的数据文件(总共五个)copy到hsqldb_home/demo目录下,然后运行runManager.bat,然后在出现的选择框里选stand alone模式,在url里象这么写:
jdbc:hsqldb:file:d:/java/hsqldb/demo/localDB
其中的"d:/java/hsqldb/demo/localDB"就是文件的全路径.最后把用户名密码输入就OK了.

stand alone 模式的意思是hsqldb运行在应用程序所再JVM内,这样可以使性能得到最优化,因为数据传输不再需要通过网络.但是这也限制了它不能通过外部程序访问.

2006-05-09

将JBPM数据库改为MySQL

其实挺简单的,不知道开始的时候不行,后来看自己代码太乱整理了一下然后就好了,奇怪了 @_@ :

环境就是jbpm的GPD, 新建立个工程后会自动产生一些结构性的文件和目录,把src/config.files下的hibernate.cfg.xml作下修改,大致就类似这个:
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/jbpm
hibernate.connection.username=root
hibernate.connection.password=rootpassword
然后就是设置数据库了,得先找到ddl的脚本,我查了半天,最后终于在jbpm-starter-kit-3.1里找到了,是在/jbpm-db/build/mysql/目录下面的,有个mysql.create.sql,执行它,打开MySQL看就会看到一大堆的表,嗯,这部分工作就算完成了.
然后就是写TestCase,在这之前随便用Designer作个工作流图.

public class JayTestProcess extends TestCase
{
public void testSimplePersistence()
{
JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
ProcessDefinition definition = ProcessDefinition.parseXmlResource("qqq.par/processdefinition.xml");

//do deploy process definition to database :
try
{
jbpmContext.deployProcessDefinition(definition);
}finally
{
jbpmContext.close();
}
}
}

回去再看一下MySQL里面就有数据了,大概就是这么回事吧.还有个问题没解决就是在执行过程中会有WARN 说
22:16:21,638 [main] WARN EhCacheProvider : Could not find configuration [org.jbpm.graph.def.Node]; using defaults.
等等一大堆的找不到configuration,不知道它是不是在找用户自定义的configuration?

.sql脚本文件的执行

shell> mysql -h host -u user -p <>

Enter password: ********

哦,挺简单的嘛,以前怎么就不想去查捏

Task的访问权限

user就是单一用户,而actor是指角色。
Task的执行通过assignment来实现:
public interface AssignmentHandler extends Serializable
{
void assign( Assignable assignable, ExecutionContext executionContext );
}

其中的Assignable就是拥有此任务执行权限的人或角色的列表:
public interface Assignable
{
public void setActorId(String actorId);
public void setPooledActors(String[] pooledActors);
}
你可以通过二个函数来声明这个task是只某个人能执行还是一些角色都可以执行。