gembin

OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

HBase, Hadoop, ZooKeeper, Cassandra

Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

About Me

 

SDO之创建DataObject

初学SDO ,在网上下载SDO的Sample就调试,结果走了不少弯路,就是出现IllegalArgumentException,不知是出现 什么问题,心想我也是照着例子搞的怎么就是通不过呢?于是乎跟踪tuscany的源码,发现Type为空。为什么呢?可能是在运行这前要把你想要创建的 DataObject的命名空间与类型的名字在文件中得注册。于是又仔细跟踪看到了这么一段: 

    XSDHelper xsdHelper = scope.getXSDHelper();
    InputStream is 
= null;
    
try {
         URL url 
= getClass().getResource("/"+fileName);
          is 
= url.openStream();
          xsdHelper.define(is, url.toString());
     } 
catch (Exception e) {
           somethingUnexpectedHasHappened(e);
     } 
finally {
       
try {
         is.close();
       } 
catch (Exception e) {
         somethingUnexpectedHasHappened(e);
       }
   }

猛然明白了。所以把例子中的xsd文件拷到项目下。再运行通过,高兴呀,哈哈。下面我把例子贴出,可能也有网友跟我一样吧,也希望其它初学SDO的网友别走我弯路。

public class SdoDataObject {
    
private static final String COMPANY_GENERATED_XML = "companyGenerated.xml";

    
/**
     * 创建类型范围的上下文
     * 
     * 
@return
     
*/
    
private HelperContext createScopeForType() {

        
return SDOUtil.createHelperContext();
    }

    
/**
     * 从XML计划文件中加载类型。
     * 
     * 
@param filename
     
*/
    
private void loadTypesFromXMLSchemaFile(HelperContext scope, String filename) {
        XSDHelper xsdHelper 
= scope.getXSDHelper();

        URL url 
= getClass().getResource("/" + filename);
        InputStream is 
= null;
        
try {
            is 
= url.openStream();
            xsdHelper.define(is, url.toString());
            is.close();
        } 
catch (IOException e) {
            e.printStackTrace();
        }
    }

    
/**
     * 组装数据图
     * 
     * 
@param company
     
*/
    
private void populateDataGraph(DataObject company) {
        company.setString(
"name""aaaa");
        company.setString(
"employeeOfTheMonth""D001");

        DataObject depts 
= company.createDataObject("departments");

        depts.setString(
"name""Advanced Technologies");
        depts.setString(
"location""NY");
        depts.setString(
"number""123");

        System.out.println(
"Creating an employee: John Jones");
        DataObject johnJones 
= depts.createDataObject("employees");
        johnJones.setString(
"name""John Jones");

        johnJones.setString(
"SN""E0001");

        System.out.println(
"Creating an employee: Jane Doe");
        DataObject janeDoe 
= depts.createDataObject("employees");
        janeDoe.setString(
"name""Jane Doe");
        janeDoe.setString(
"SN""E0003");

        System.out.println(
"Creating a manager: Fred Bloggs");
        DataObject fVarone 
= depts.createDataObject("employees");
        fVarone.setString(
"name""Fred Bloggs");
        fVarone.setString(
"SN""E0004");
        fVarone.setString(
"manager""true");
        System.out.println(
"DataObject creation completed");
        System.out.println();
        System.out.println(
"create DataObject susseccfully");
    }

    
public void run() {
        HelperContext scope 
= createScopeForType();
        loadTypesFromXMLSchemaFile(scope, 
"company.xsd");
        
        DataObject company 
= scope.getDataFactory().create("company.xsd",
                
"CompanyType");
        populateDataGraph(company);

        
try {
            FileOutputStream fos 
= new FileOutputStream(COMPANY_GENERATED_XML);
            
try {
                scope.getXMLHelper().save(company, 
"company.xsd""company",
                        fos);
            } 
catch (IOException e) {
                e.printStackTrace();
            }
            String xml 
= scope.getXMLHelper().save(company, "company.xsd",
                    
"company");
            System.out.println(xml);
        } 
catch (FileNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    
public static void main(String[] args) {
        SdoDataObject sdoDataObject 
= new SdoDataObject();
        sdoDataObject.run();
    }

}

posted on 2008-04-27 22:15 gembin 阅读(769) 评论(0)  编辑  收藏 所属分类: SDOSOA


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(6)

随笔分类(440)

随笔档案(378)

文章档案(6)

新闻档案(1)

相册

收藏夹(9)

Adobe

Android

AS3

Blog-Links

Build

Design Pattern

Eclipse

Favorite Links

Flickr

Game Dev

HBase

Identity Management

IT resources

JEE

Language

OpenID

OSGi

SOA

Version Control

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

free counters