站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题Microsoft.Net 的Java实现

Microsoft.Net 的Java实现

摘要: Microsoft.Net 的Java实现
<table cellSpacing=0 cellPadding=0 border=0 class="zh114" align="right"> <tr> <td > </td> </tr> </table>
  众所周知,微软公司正在全力打造.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;

<table width="96%"> <tr> <td background="http:///images/dian.gif" height="3"></td> </tr> </table>

↑返回目录
前一篇: 重用java.util.Observable类
后一篇: 用java开发Email工具之发送邮件