2012-07-31 57 views
0

我想導出的文件,但在收到以下異常:類轉換異常錯誤:試圖寫入到文件

An unexpected error occurred: java.lang.Exception: sailpoint.tools.GeneralException: The application script threw an exception: java.lang.ClassCastException: Cannot cast sailpoint.tools.xml.PersistentArrayList to java.lang.String BSF info: Export File - Abc at line: 0 column: columnNo

我的代碼如下:

import java.io.FileWriter; 
    import java.io.File; 
    import java.io.BufferedWriter; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.*; 

    String appname = "Abc"; 
    String path = "//home/exportfile//"; 
    String filename = path+"ApplicationExport-"+appname+".txt"; 
    String ret = "false"; 

    QueryOptions ops = new QueryOptions(); 
    Filter [] filters = new Filter[1]; 
    filters[0] = Filter.eq("application.name", appname); 
    ops.add(filters); 

    List props = new ArrayList(); 
    props.add("identity.name"); 

    //Do search 
    Iterator it = context.search(Link.class, ops, props); 

    //Build file and export header row 
    BufferedWriter out = new BufferedWriter(new FileWriter(filename)); 
    out.write("IdentityName,UserName,WorkforceID,Organization"); 
    out.newLine();   

    //Iterate Search Results 
    if (it!=null) 
    {        
      while (it.hasNext()) { 
        //Get link and create object 
        Object [] record = it.next(); 
        String identityName = (String) record[0]; 
        Identity user = (Identity) context.getObject(Identity.class, identityName); 

        //Get Identity attributes for export 
        String workforceid = (String) user.getAttribute("workforceID");     

        //Get application attributes for export 
        String userid=""; 
         String org=""; 
        List links = user.getLinks(); 
        if (links!=null) 
        { 
          Iterator lit = links.iterator(); 
          while (lit.hasNext()) 
          { 
            Link l = lit.next(); 
            String lname = l.getApplicationName(); 
            if (lname.equalsIgnoreCase(appname)) 
            { 
               userid = (String) l.getAttribute("User Name"); 
               org= (String) l.getAttribute("Organization"); 
            } 
          } 
        }        

        //Output file 
        out.write(identityName+","+userid+","+workforceid+","+org);        
        out.newLine();                   
        out.flush(); 
      }      
      ret="true"; 
    } 
    //Close file and return 
    out.close(); 
    return ret;  

在運行上面的代碼我得到了強制異常錯誤:因爲Organization屬性是Multivalued.ie:在該屬性中可以有多個值。

任何幫助更正此代碼將不勝感激。

回答

1

它說得很清楚,你正在得到一個sailpoint.tools.xml.PersistentArrayList類型的對象並試圖將它轉換爲一個字符串。

從你的問題我理解這個問題是在這裏:

org= (String) l.getAttribute("Organization"); 

所以,你需要將其更改爲:

sailpoint.tools.xml.PersistentArrayList orgList = (sailpoint.tools.xml.PersistentArrayList) l.getAttribute("Organization"); 

,然後提取從該列表中正確的值。

+0

更改上述值沒有幫助.. 你推薦使用List或ArrayList的那個屬性嗎?我對Java.so非常陌生,不知道該在哪裏更改此代碼? – user1565893 2012-07-31 14:05:45

+0

你得到同樣的錯誤? – Tomer 2012-07-31 14:06:14

+0

代碼無誤運行,但它沒有在「組織」列中寫任何內容。 – user1565893 2012-07-31 14:14:04