Microsoft.Net 的Java实现

2002-12-20 9:41:55【作者】 畅享网 【进入论坛】
广告

Microsoft.Net 的Java实现

李文军  

众所周知,微软公司正在全力打造.Net,并准备将它作为向其他公司进攻的砝码,来保证其在业界的技术领先地位。 其实,微软公司在许多领域都处于领导地位,一些优秀的产品让其他公司难望其项颈,如操作系统,办公软件等基于Windows平台的一些优秀的应用软件(如:IE)。但在美国,许多大公司非常反感微软一手包办的作风,也对微软的不放心,对其产品的安全性的担忧,所以大部分都用Unix 和linux等非Windows平台,许多服务器也是用的非Windows平台,或是用自己开发的操作系统。但Windows的方便性和界面友好性及众多的工具也是其他平台难以比拟的(应该没什么争议的),而Unix和Linux平台似乎只适合一些专家。能不能找到一个桥梁将Windows产品同其他平台联系起来呢?目前有www.stryon.com公司正在实现这一点,开发了iNet,将Microsoft.Net转换成java代码来实现跨平台。

例如用Visual Studio.Net开发了一个Web Service程序:

testClient.asmx:

<%@ WebService Language="C#" Class="testClient" %>


using System;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Web.Services.Description;

using System.Xml;

using System.Xml.Schema;

using System.Xml.Serialization;

using System.Data;


public class testClient : WebService {


[WebMethod()]

public int testInt(int a,int b){

return a+b;

}


[WebMethod()]

public struct1 testStruct(struct1 a){

return a;

}


[WebMethod()]

public int[] testIntArr(int[] a){

return a;

}


[WebMethod()]

public struct1[] testStrArr(struct1[] a){

return a;

}


[WebMethod()]

public struct1 testStructAndArr(struct1 a,struct1[] b){

return a;

}


[WebMethod()]

public struct1[][] testStrManyArr(struct1[][] a){

return a;

}

}


public class struct1:parent{


public int i=1;

public string j="ok";

public struct2 s2;

}

public class parent{


public String p;

}


public class struct2{


public String sField;

}


我们可以用il2java 工具转换成java代码(il2java工具可以在www.stryon.com 网站上下载,包括在iNet产品中),如:il2java http://localhost/testClient/testClient.asmx c:\temp,运行这个命令,将在c盘temp目录下产生 testClient.java 和testClient_Info.java:

testClient.java:


import system.*;

import system.Reflection.*;

import system.Web.Services.*;


public class testClient extends WebService{


public int testInt(int a, int b){

return a+b;

}


public struct1 testStruct(struct1 a){

return a;

}


public int[] testIntArr(int[] a){

return a;

}


public struct1[] testStrArr(struct1[] a){

return a;

}


public struct1 testStructAndArr(struct1 a, struct1[] b){

return a;

}


public struct1[][] testStrManyArr(struct1[][] a){

return a;

}


public testClient(){

super();


}

}

用来指明Web Service中有哪些Web方法,以便被客户调用;


testClient_Info.java:


import system.*;

import system.Reflection.*;

import system.Web.Services.*;


public class testClient_Info implements IMetaData{


public void fillType(Type t){

long value = TypeAttributes.AnsiClass.value__ | TypeAttributes.AutoLayout.value__ | TypeAttributes.BeforeFieldInit.value__ | TypeAttributes.Class.value__ | TypeAttributes.Public.value__;

TypeAttributes attributes = new TypeAttributes(value);

t.set_Attributes(attributes);

}


public FieldInfo[] GetFieldsImpl(Type t){

return new FieldInfo[0];

}


public ConstructorInfo[] GetConstructorsImpl(Type t){

long value = 0;

ConstructorInfo ctor = null;

ParameterInfo param = null;

MethodAttributes attributes = null;

ParameterAttributes paramAttrs = null;

java.util.Vector ctorVec = new java.util.Vector();


// public testClient();

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__ | MethodAttributes.RTSpecialName.value__ | MethodAttributes.SpecialName.value__;

attributes = new MethodAttributes(value);

ctor = new ConstructorInfo(t);

ctorVec.addElement(ctor);

ctor.set_Attributes(attributes);

ctor.set_Name("testClient");

ctor.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);


Object[] objs = ctorVec.toArray();

ctorVec = null;

ConstructorInfo[] ctors = new ConstructorInfo[objs.length];

java.lang.System.arraycopy(objs, 0, ctors, 0, objs.length);

return ctors;

}


public MethodInfo[] GetMethodsImpl(Type t){

long value = 0;

MethodInfo method = null;

ParameterInfo param = null;

MethodAttributes attributes = null;

ParameterAttributes paramAttrs = null;

java.util.Vector mdVec = new java.util.Vector();


// public int testInt(int a, int b);

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__;

attributes = new MethodAttributes(value);

method = new MethodInfo(t);

mdVec.addElement(method);

method.set_Attributes(attributes);

method.set_Name("testInt");

method.set_ReturnType("System.Int32");

method.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("a");

param.set_ParamType("System.Int32");

method.addParameterInfo(param);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("b");

param.set_ParamType("System.Int32");

method.addParameterInfo(param);


// public struct1 testStruct(struct1 a);

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__;

attributes = new MethodAttributes(value);

method = new MethodInfo(t);

mdVec.addElement(method);

method.set_Attributes(attributes);

method.set_Name("testStruct");

method.set_ReturnType("struct1");

method.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("a");

param.set_ParamType("struct1");

method.addParameterInfo(param);


// public int[] testIntArr(int[] a);

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__;

attributes = new MethodAttributes(value);

method = new MethodInfo(t);

mdVec.addElement(method);

method.set_Attributes(attributes);

method.set_Name("testIntArr");

method.set_ReturnType("System.Int32[]");

method.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("a");

param.set_ParamType("System.Int32[]");

method.addParameterInfo(param);


// public struct1[] testStrArr(struct1[] a);

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__;

attributes = new MethodAttributes(value);

method = new MethodInfo(t);

mdVec.addElement(method);

method.set_Attributes(attributes);

method.set_Name("testStrArr");

method.set_ReturnType("struct1[]");

method.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("a");

param.set_ParamType("struct1[]");

method.addParameterInfo(param);


// public struct1 testStructAndArr(struct1 a, struct1[] b);

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__;

attributes = new MethodAttributes(value);

method = new MethodInfo(t);

mdVec.addElement(method);

method.set_Attributes(attributes);

method.set_Name("testStructAndArr");

method.set_ReturnType("struct1");

method.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("a");

param.set_ParamType("struct1");

method.addParameterInfo(param);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("b");

param.set_ParamType("struct1[]");

method.addParameterInfo(param);


// public struct1[][] testStrManyArr(struct1[][] a);

value = MethodAttributes.HideBySig.value__ | MethodAttributes.Public.value__ | MethodAttributes.ReuseSlot.value__;

attributes = new MethodAttributes(value);

method = new MethodInfo(t);

mdVec.addElement(method);

method.set_Attributes(attributes);

method.set_Name("testStrManyArr");

method.set_ReturnType("struct1[][]");

method.set_BindingFlags(BindingFlags.Public.value__ | BindingFlags.Instance.value__);

value = 0;

paramAttrs = new ParameterAttributes(value);

param = new ParameterInfo(method);

param.set_Attributes(paramAttrs);

param.set_Name("a");

param.set_ParamType("struct1[][]");

method.addParameterInfo(param);


Object[] objs = mdVec.toArray();

mdVec = null;

MethodInfo[] methods = new MethodInfo[objs.length];

java.lang.System.arraycopy(objs, 0, methods, 0, objs.length);

return methods;

}


public EventInfo[] GetEventImpl(Type t){

return new EventInfo[0];

}


public PropertyInfo[] GetPropertiesImpl(Type t){

return new PropertyInfo[0];

}


public Object[] GetAttrsForType(){

Object[] attrObjArr = {};

return attrObjArr;

}


public Object[] GetAttrsForFieldInfo(String name){

return new Object[0];

}


public Object[] GetAttrsForCtorInfo(String str){

return new Object[0];

}


public Object[] GetAttrsForMethodInfo(String str){

if(str.equals("testInt&System.Int32&System.Int32")){

WebMethodAttribute attrObj0 = new WebMethodAttribute();

Object[] attrObjArr = {attrObj0};

return attrObjArr;

}

if(str.equals("testStruct&struct1")){

WebMethodAttribute attrObj0 = new WebMethodAttribute();

Object[] attrObjArr = {attrObj0};

return attrObjArr;

}

if(str.equals("testIntArr&System.Int32[]")){

WebMethodAttribute attrObj0 = new WebMethodAttribute();

Object[] attrObjArr = {attrObj0};

return attrObjArr;

}

if(str.equals("testStrArr&struct1[]")){

WebMethodAttribute attrObj0 = new WebMethodAttribute();

Object[] attrObjArr = {attrObj0};

return attrObjArr;

}

if(str.equals("testStructAndArr&struct1&struct1[]")){

WebMethodAttribute attrObj0 = new WebMethodAttribute();

Object[] attrObjArr = {attrObj0};

return attrObjArr;

}

if(str.equals("testStrManyArr&struct1[][]")){

WebMethodAttribute attrObj0 = new WebMethodAttribute();

Object[] attrObjArr = {attrObj0};

return attrObjArr;

}

return new Object[0];

}


public Object[] GetAttrsForReturn(String str){

return new Object[0];

}


public Object[] GetAttrsForParameterInfo(String str, int index){

return new Object[0];

}


public Object[] GetAttrsForEventInfo(String name){

return new Object[0];

}


public Object[] GetAttrsForPropertyInfo(String name){

return new Object[0];

}


public Assembly get_Assembly(){

return Assembly.Load("muiepsyi");

}


public Module get_Module(){

return Module.Load("muiepsyi");

}

}

用来保存Web Service中的一些有用的信息,如:Field 域,Property域,方法的一些信息(参数,用户定制属性等),构造器等。Il2java工具产生的一Info文件对应Web Service中的Type。

如果是Tomcat4x服务器,那么可以在server.xml中配置testClient的虚拟目录,如:<Context path="/testClient"

docBase="c:\temp">

</Context>,也可以用iNet中的自动配置工具去配置。(iNet支持多个服务器,如:IIS,Apache,WebSphere,Bea WebLogical,Tomcat,Orcal9i等,实现真正意义上的系统兼容)。配置好以后,我们就可以运行了,我们可以发现速度并不比微软慢(因为Microsoft.Net每次都要编译成IL代码才能运行,而iNet在il2java 的时候已经产生了class,所以运行时不占用编译时间)。

但是,微软一个重要的技术XML是非常好的,完全用java去做性能上可能要差一点,因为java的速度本身是要慢,而且Apache公司的JAXP的解释器性能存在瓶颈(Apache公司自己也承认这一点),我想iNet在XML方面可能存在一些问题(在www.stryon.com的网站上也可看到有说明)。

不管怎么说,我觉得iNet这个产品非常的新颖,有创意,应该大有市场。

本文原载于计算机世界

如果您希望与本文章的作者或其所在机构,进一步交流,请联系:畅享网 姜小姐
jill.jiang@amteam.org | 021-51096826-112 | 在线联系
老孙的IT运维管理之道[原创]用户的BSM用户的IT业务管..

从企业实际的IT运营角度来看,BSM是推动IT与业务融合,实现、改善WCNG司IT管理和治理的最佳实践之一。

吕建伟 专栏和CIO问答软件项目实施管理

现实中很少能按照正规流程来的,所以只能把流程中的各个环节拆开,个个击破,以后就可以见招拆招了。

ITIL实施:CIO时刻准备着

千军易得,一将难求,要推进ITIL实施,CIO扮演的角色不容忽视。吹响集结号,CIO出击的时刻已经来到。

节能与优化IT 企业CIO过冬良策

当前金融危机的影响还在继续漫延,很多企业都在苦寻过冬的良策,在这种情况下,节能与优化技术与产品无疑成为CIO们关注的首要对象,本次选题就是针对节能与优化IT来为CIO们提供过冬的良……