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 閱讀(1066) 評論(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
          主站蜘蛛池模板: 文登市| 桐柏县| 石渠县| 桐乡市| 保亭| 开远市| 永吉县| 横峰县| 华亭县| 孟连| 英山县| 澳门| 余干县| 金秀| 日土县| 罗源县| 五家渠市| 南陵县| 渝中区| 白水县| 伊金霍洛旗| 扶风县| 湾仔区| 新疆| 永平县| 恩施市| 达尔| 台东县| 耒阳市| 房山区| 宜黄县| 视频| 平和县| 老河口市| 台州市| 环江| 惠安县| 乡城县| 原平市| 大洼县| 溆浦县|