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之創(chuàng)建DataObject

          初學(xué)SDO ,在網(wǎng)上下載SDO的Sample就調(diào)試,結(jié)果走了不少彎路,就是出現(xiàn)IllegalArgumentException,不知是出現(xiàn) 什么問題,心想我也是照著例子搞的怎么就是通不過呢?于是乎跟蹤tuscany的源碼,發(fā)現(xiàn)Type為空。為什么呢?可能是在運(yùn)行這前要把你想要創(chuàng)建的 DataObject的命名空間與類型的名字在文件中得注冊。于是又仔細(xì)跟蹤看到了這么一段: 

              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文件拷到項(xiàng)目下。再運(yùn)行通過,高興呀,哈哈。下面我把例子貼出,可能也有網(wǎng)友跟我一樣吧,也希望其它初學(xué)SDO的網(wǎng)友別走我彎路。

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

              
          /**
               * 創(chuàng)建類型范圍的上下文
               * 
               * 
          @return
               
          */
              
          private HelperContext createScopeForType() {

                  
          return SDOUtil.createHelperContext();
              }

              
          /**
               * 從XML計(jì)劃文件中加載類型。
               * 
               * 
          @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();
                  }
              }

              
          /**
               * 組裝數(shù)據(jù)圖
               * 
               * 
          @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 閱讀(1067) 評論(0)  編輯  收藏 所屬分類: SDOSOA


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(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
          主站蜘蛛池模板: 双鸭山市| 彰武县| 永寿县| 鞍山市| 建瓯市| 博客| 邓州市| 犍为县| 博湖县| 申扎县| 秦皇岛市| 浮山县| 奉新县| 云安县| 香格里拉县| 达尔| 清水河县| 太和县| 平顶山市| 华亭县| 化隆| 牟定县| 万山特区| 阳东县| 岱山县| 正蓝旗| 香格里拉县| 盐源县| 尤溪县| 镇赉县| 察隅县| 舟山市| 屏山县| 河北省| 上饶县| 麻江县| 五家渠市| 曲阜市| 吴忠市| 南靖县| 乐安县|