openoffice-api mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steele, Raymond" <raymond.ste...@lmco.com>
Subject RE: EXTERNAL: Re: DisposedException
Date Thu, 06 Mar 2014 18:05:35 GMT
I was able to overcome this by making the following changes. Basically, I used XNameAcess to
access the sheet and created a new object each time that I needed a new sheet within the workbook.
I am not sure why it works, but it does now.


Now, in a different area of code, I am getting DisposedExceptions again. I have a for loop
that uses loadComponentFromURL to load, export as PDF, and close the document each  iteration.
 I do a desktop.terminate when the loop completes and get the XMultiCOmponentFactory and XComponentLoader
before the loop executes.


  public class Load {
              String master_track_dir = System.getenv("PLN_MASTER_TRK");
              String restore_track = master_track_dir + "/RAYMOND/restore_track.xml";
              String master_track = master_track_dir + "/RAYMOND/track.xml";

       public Load() {

       }

       public static main(String[] args) {
              try {
                     Load block = new Load();
                     XComponentContext context = Bootstarp.bootstrap();
                     if(context == null) {
                           System.err.println("ERROR: Could not bootstrap default Office");
                           System.exit(1);
                     }

                     XMultiComponentFactory component_factory = context.getServiceManager();
                     try {
                           Object desktop = component_factory.createInstanceWithContext("com.sun.star.frame.Desktop",
context);
                           XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class,
desktop);

                           PropertyValue loadProps= new PropertyValue[1];
                           loadProps[0] = new PropertyValue();
                           loadProps[0].Name = "Hidden";
                           loadProps[0].Value = Boolean.FALSE;

                           String odsFile = System.getenv("PLN_DAT_GCFILES") + "/Track.ods";
                           new File(odsFile).delete();
                           DocumentHandler otsHandler = new DocumentHandler();
                           String otsFile = "/tmp/Track.ots";
                           String otsUrl = otsHandler.convertFilePathToURL(otsFile);
                           String odsUrl = otsHandler.convertFilePathToURL(odsFile);

                           //Load .ots file, opens "Untitled 1.ods"
                           XComponent otsComponent = XComponentLoader.loadComponent.loadComponentFromURL(otsUrl,
"_blank", 0, loadProps);
                           //export "Untitled 1" as /tmp/Track.ods file
                           block.exportDocumentAs(otsComponent, odsFile, DocumentHAndler.DocumentSaveFormat.SCALC,
Boolean.TRUE, "1");
                           //close "Untitled 1.ods"
                           block.closeDocument(otsComponent);
                           //Open /tmp/Track.ods
                           XComponent odsComponent = XComponentLoader.loadComponentFromURL(odsUrl,
"_blank", 0, loadProps);

                           String track = null;
                           if(new File(block.restore_track).exists()){
                                  track = block.restore_track;
                           } else {
                                  track = block.master_track;
                           }

                           XSpreadsheetDocument doc = UnoRuntime.queryInterface(XspreadsheetDocument.class,
odsComponent);
                           XSpreadsheets sheets = doc.getSheets();

                          // Object sheetObj = null;
                          // XSpreadsheet sheet = null;
                          // XCellRange cells = null;
                          // XCellRange range = null;
                          // XCellRangeData data = null;
                          // Object[][] data_obj = null;

                                                                XNameAcess sheetNA = UnoRuntime.queryInterface(XspreadsheetDocument.class,
sheets);
                                                                Object sheetObj1 = sheetNA.getByName("Dest
Data");
XSpreadsheet sheet1 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheetObj1);
                            //Populate worksheet
                           //sheetObj = sheets.getByName("Dest Data");
                           //sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj1);
                           XCellRange cells1 = UnoRuntime.queryInterface(XCellRange.class,
sheet1);
                           XCellRange range1 = cells1.getCellRangeByName("A2:E100");
                           XCellRangeData data1 = UnoRuntime.queryInterface(XCellRangeData.class,
range1);
                           Object[][] data_obj1 = data1.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data1.setDataArray(data_obj1);

XNameAcess sheetNA2 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheets);
                                                                Object sheetObj2 = sheetNA2.getByName("Emer
Data");
XSpreadsheet sheet2 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheetObj2);
                           //Populate worksheet
                           //sheetObj = sheets.getByName("Emer Data");
                           //sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           XCellRange cells2 = UnoRuntime.queryInterface(XCellRange.class,
sheet2);
                           XCellRange range2 = cells2.getCellRangeByName("A2:F17");
                           XCellRangeData data2 = UnoRuntime.queryInterface(XCellRangeData.class,
range2);
Object[][] data_obj2 = data2.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data2.setDataArray(data_obj2);

                           //Populate worksheet
XNameAcess sheetNA3 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheets);
                                                                Object sheetObj3 = sheetNA3.getByName("Oper
Data");
XSpreadsheet sheet3 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheetObj3);
                           //sheetObj = sheets.getByName("Oper Data");
                           //**************** com.sun.star.lang.DisposedException occurs here.************************//
                       //sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           XCellRange cells3 = UnoRuntime.queryInterface(XCellRange.class,
sheet3);
                           XCellRange range3 = cells3.getCellRangeByName("A2:E11");
                           XCellRangeData data3 = UnoRuntime.queryInterface(XCellRangeData.class,
range3);
Object[][] data_obj3 = data3.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data3.setDataArray(data_obj3);

//Populate worksheet
XNameAcess sheetNA4 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheets);
                                                                Object sheetObj4 = sheetNA4.getByName("Param
Data");
XSpreadsheet sheet4 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheetObj4);
                           //sheetObj = sheets.getByName("Param Data");
                           //sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           XCellRange cells4 = UnoRuntime.queryInterface(XCellRange.class,
sheet4);
                           XCellRange range4 = cells.getCellRangeByName("A2:AW2);
                           XCellRangeData data4 = UnoRuntime.queryInterface(XCellRangeData.class,
range4);
Object[][] data_obj4 = data4.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data4.setDataArray(data_obj4);

//Populate worksheet
XNameAcess sheetNA5 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheets);
                                                                Object sheetObj5 = sheetNA5.getByName("Point
Data");
XSpreadsheet sheet5 = UnoRuntime.queryInterface(XspreadsheetDocument.class, sheetObj5);

                           //sheetObj = sheets.getByName("Point Data");
                           //sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           XCellRange cells5 = UnoRuntime.queryInterface(XCellRange.class,
sheet5);
                           XCellRange range5 = cells5.getCellRangeByName("A2:AJ151");
                           XCellRangeData data5 = UnoRuntime.queryInterface(XCellRangeData.class,
range5);
Object[][] data_obj5 = data5.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data5.setDataArray(data_obj5);

                     } catch(com.sun.star.uno.Exception ex) {
                           System.err.println(ex.getMessage());
                     } finally {
                           System.exit(1);
                     }


              }
       }

       public void closeDocument(XComponent document) {
              XModel model = UnoRuntime.queryInterface(XModel.class, document);
              if(model != null) {
                     XCloseable closeable = UnoRuntime.queryInterface(XCloseable.class, model);
                     if(closeable != null) {
                           try {
                                  closeable.close(false);
                           } catch (CloseVetoException ex) {
                                  System.out.println(ex.getMessage());
                           }
                     }
              } else {
                     XComponent disposeable = UnoRuntime.queryInterface(XCompnent.class, model);
                     disposeable.dispose();
              }
       }

       public void ExportDocumentAs(XComponent doc, String save_file, DocumentHandler.DocumentSaveFormat
format, boolean overwrite, String page_range) {
              if((save_file == null) || (save_file.trim().length()) == 0) {
                     return;
              }

              if(!overwrite) {
                     File sfile = new File(save_file);
                     if(sfile.exists()) {
                           throw new Exception("File exists");
                     }
              }

              DocumentHandler = new DocumentHandler();

              String file_url = handler.convertFIlePAthToUrl(save_file);
              XStoreable store = UnoRuntime.queryInterface(XStoreable.class, doc);

              PropertyValue filter_data= new PropertyValue[1];
              loadProps[0] = new PropertyValue();
              loadProps[0].Name = "PageRange";
              loadProps[0].Value = page_range;

              PropertyValue loadProps= new PropertyValue[2];
              loadProps[0] = new PropertyValue();
              loadProps[0].Name = "FilterName";
              loadProps[0].Value = format.getFormatCode;
              loadProps[1] = new PropertyValue();
              loadProps[1].Name = "FilterData";
              loadProps[1].Value = filter_data;

              store.storeToUrl(file_url, loadProps);


       }
}


From: Andre Fischer [mailto:awf.aoo@gmail.com]
Sent: Thursday, March 06, 2014 1:24 AM
To: Steele, Raymond; Andre Fischer; api@openoffice.apache.org
Subject: Re: EXTERNAL: Re: DisposedException

On 28.02.2014 20:35, Steele, Raymond wrote:
I am still having issues. I've typed (there may be typos) out most of the code. The following
code will fail during runtime on the line bolded below. Can anyone identify why? Any help
would be great!!!

Sorry for the late reply, I am still not scanning api@ every day and my thunderbird did not
update the count of new mails automatically.

I don't see any obvious error in your Java code.  I also looked into the C++ implementation
of ScTableSheetObj and did not find anything obvious either.  The only thing that I can do
is to debug the C++ side of the problem, but only if you can send me a running version of
your extension, stripped down to the minimum to reproduce this bug.

The only alternative to that (if you can not provide the extension) then we have to rely on
collecting more data:

- add an XEventListener to sheets and possibly to doc right after the
         XSpreadsheets sheets = doc.getSheets();
line and see if the disposing() method of the listener is called.

- when the DisposedException is thrown, then look at its Source member (should be sheetObj).
 I don't expect any surprises there but the check doesn't take long.

I am sorry that I can not be of more help.

-Andre



Basically, I am just trying to populate an .ods file's worksheets with data.


       public class Load {
              String master_track_dir = System.getenv("PLN_MASTER_TRK");
              String restore_track = master_track_dir + "/RAYMOND/restore_track.xml";
              String master_track = master_track_dir + "/RAYMOND/track.xml";

       public Load() {

       }

       public static main(String[] args) {
              try {
                     Load block = new Load();
                     XComponentContext context = Bootstarp.bootstrap();
                     if(context == null) {
                           System.err.println("ERROR: Could not bootstrap default Office");
                           System.exit(1);
                     }

                     XMultiComponentFactory component_factory = context.getServiceManager();
                     try {
                           Object desktop = component_factory.createInstanceWithContext("com.sun.star.frame.Desktop",
context);
                           XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class,
desktop);

                           PropertyValue loadProps= new PropertyValue[1];
                           loadProps[0] = new PropertyValue();
                           loadProps[0].Name = "Hidden";
                           loadProps[0].Value = Boolean.FALSE;

                           String odsFile = System.getenv("PLN_DAT_GCFILES") + "/Track.ods";
                           new File(odsFile).delete();
                           DocumentHandler otsHandler = new DocumentHandler();
                           String otsFile = "/tmp/Track.ots";
                           String otsUrl = otsHandler.convertFilePathToURL(otsFile);
                           String odsUrl = otsHandler.convertFilePathToURL(odsFile);

                           //Load .ots file, opens "Untitled 1.ods"
                           XComponent otsComponent = XComponentLoader.loadComponent.loadComponentFromURL(otsUrl,
"_blank", 0, loadProps);
                           //export "Untitled 1" as /tmp/Track.ods file
                           block.exportDocumentAs(otsComponent, odsFile, DocumentHAndler.DocumentSaveFormat.SCALC,
Boolean.TRUE, "1");
                           //close "Untitled 1.ods"
                           block.closeDocument(otsComponent);
                           //Open /tmp/Track.ods
                           XComponent odsComponent = XComponentLoader.loadComponentFromURL(odsUrl,
"_blank", 0, loadProps);

                           String track = null;
                           if(new File(block.restore_track).exists()){
                                  track = block.restore_track;
                           } else {
                                  track = block.master_track;
                           }

                           XSpreadsheetDocument doc = UnoRuntime.queryInterface(XspreadsheetDocument.class,
odsComponent);
                           XSpreadsheets sheets = doc.getSheets();

                           Object sheetObj = null;
                           XSpreadsheet sheet = null;
                           XCellRange cells = null;
                           XCellRange range = null;
                           XCellRangeData data = null;
                           Object[][] data_obj = null;

                           //Populate worksheet
                           sheetObj = sheets.getByName("Dest Data");
                           sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           cells = UnoRuntime.queryInterface(XCellRange.class, sheet);
                           range = cells.getCellRangeByName("A2:E100");
                           data = UnoRuntime.queryInterface(XCellRangeData.class, range);
                           data_obj = data.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data.setDataArray(data_obj);

                           //Populate worksheet
                           sheetObj = sheets.getByName("Emer Data");
                           sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           cells = UnoRuntime.queryInterface(XCellRange.class, sheet);
                           range = cells.getCellRangeByName("A2:F17");
                           data = UnoRuntime.queryInterface(XCellRangeData.class, range);
                           data_obj = data.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data.setDataArray(data_obj);

                           //Populate worksheet
                           sheetObj = sheets.getByName("Oper Data");
                           //**************** com.sun.star.lang.DisposedException occurs here.************************//
                     sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           cells = UnoRuntime.queryInterface(XCellRange.class, sheet);
                           range = cells.getCellRangeByName("A2:E11");
                           data = UnoRuntime.queryInterface(XCellRangeData.class, range);
                           data_obj = data.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data.setDataArray(data_obj);

//Populate worksheet
                           sheetObj = sheets.getByName("Param Data");
                           sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           cells = UnoRuntime.queryInterface(XCellRange.class, sheet);
                           range = cells.getCellRangeByName("A2:AW2");
                           data = UnoRuntime.queryInterface(XCellRangeData.class, range);
                           data_obj = data.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data.setDataArray(data_obj);

//Populate worksheet
                           sheetObj = sheets.getByName("Point Data");
                           sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
                           cells = UnoRuntime.queryInterface(XCellRange.class, sheet);
                           range = cells.getCellRangeByName("A2:AJ151");
                           data = UnoRuntime.queryInterface(XCellRangeData.class, range);
                           data_obj = data.getDataArray();
                           //Populate data_obj here..... This part omitted for clarity an
need to manually type.
                           data.setDataArray(data_obj);

                     } catch(com.sun.star.uno.Exception ex) {
                           System.err.println(ex.getMessage());
                     } finally {
                           System.exit(1);
                     }


              }
       }

       public void closeDocument(XComponent document) {
              XModel model = UnoRuntime.queryInterface(XModel.class, document);
              if(model != null) {
                     XCloseable closeable = UnoRuntime.queryInterface(XCloseable.class, model);
                     if(closeable != null) {
                           try {
                                  closeable.close(false);
                           } catch (CloseVetoException ex) {
                                  System.out.println(ex.getMessage());
                           }
                     }
              } else {
                     XComponent disposeable = UnoRuntime.queryInterface(XCompnent.class, model);
                     disposeable.dispose();
              }
       }

       public void ExportDocumentAs(XComponent doc, String save_file, DocumentHandler.DocumentSaveFormat
format, boolean overwrite, String page_range) {
              if((save_file == null) || (save_file.trim().length()) == 0) {
                     return;
              }

              if(!overwrite) {
                     File sfile = new File(save_file);
                     if(sfile.exists()) {
                           throw new Exception("File exists");
                     }
              }

              DocumentHandler = new DocumentHandler();

              String file_url = handler.convertFIlePAthToUrl(save_file);
              XStoreable store = UnoRuntime.queryInterface(XStoreable.class, doc);

              PropertyValue filter_data= new PropertyValue[1];
              loadProps[0] = new PropertyValue();
              loadProps[0].Name = "PageRange";
              loadProps[0].Value = page_range;

              PropertyValue loadProps= new PropertyValue[2];
              loadProps[0] = new PropertyValue();
              loadProps[0].Name = "FilterName";
              loadProps[0].Value = format.getFormatCode;
              loadProps[1] = new PropertyValue();
              loadProps[1].Name = "FilterData";
              loadProps[1].Value = filter_data;

              store.storeToUrl(file_url, loadProps);


       }
}

From: Andre Fischer [mailto:awf.aoo@gmail.com]
Sent: Wednesday, February 26, 2014 3:15 AM
To: Steele, Raymond; Andre Fischer; api@openoffice.apache.org<mailto:api@openoffice.apache.org>
Subject: Re: EXTERNAL: Re: DisposedException

On 26.02.2014 00:25, Steele, Raymond wrote:

Thanks,



I am not explicitly calling the dispose method .

I would expect that dispose is called by a closing frame or something like that.




Here is an overview of what I am doing.



1.       opening a XSpreadsheetDocument (called temp_doc)  by calling after creating an object
using  XMultiComponentFactory::createInstanceWithContext.

a.       The document is a .ots file which opens as "Untitled 1".

2.       XComponent  temp_comp = UnoRuntime.queryInterface(XComponent.class, temp_doc);

3.       XStoreable store = UnoRuntime.queryInterface(XStoreable.class, temp_comp);

a.        exporting "Unititled 1",  to an "ODF Spreadsheet", "StarOffice XML (Calc)" format
by setting the PropertyValue FilterName property.  Once the properties are set, I use XStoreable::stopToURL
to save "Untitled 1" to "RawData.ods"

4.       Then I close the document:

a.       XModel model = UnoRuntime.queryInterface(XModel.class, document);

b.      If model not null then, XCloseable closeable = UnoRuntime.queryInterface(XCloseable.class,
model);

                                                               i.      closeable.close();
(This is always the case that happens)

c.       If model null then,  XComponent disposable = UnoRuntime.queryInterface(Xcomponent.class,
 model);

                                                               i.      disposable.dispose();
(My code never gets into this condition)

5.       I then immediately open the file I exported in step three above by creating a new
XSpreadsheetDocument (called doc), using the same methods defined in step one above,  but
when I do a

a.       document = UnoRuntime.queryInterface(XpreadsheetDocument.class, xCalcComponent);

I receive: com.sun.star.lang.DisposedExcpetion

                at com.sun.star.lib.uno.environments.remote.JobQueue.removeJob(JobQueue.java:
210)





Is it possible that xCalcComponent still belongs to temp_doc?  That would explain why it is
disposed.
Otherwise, xCalcComponent should not be disposed directly after loading.  Even if the loading
failed, it should not be created in the first place and be NULL (well, an empty Reference,
that contains NULL).  I would ask for any exceptions during the second loading, but unfortunately
OpenOffice throws a lot of exceptions that don't notify any errors but are used for normal
communication (don't ask)

-Andre




Hopefully, this provides some context. Basically, I am opening an .ots file, saving(exporting)
it as an .ods file and closing it. Then  I open the .ods file so that I can populate the cells
with data, but right after loadComponentFromURL, I try to query the XComponent returned from
loadComponentFromURL to get the XSpreadsheetDocument.  That is when I get the DisposedException.





-----Original Message-----
From: Andre Fischer [mailto:awf.aoo@gmail.com]
Sent: Tuesday, February 25, 2014 1:26 AM
To: Steele, Raymond; api@openoffice.apache.org<mailto:api@openoffice.apache.org>
Cc: awf.aoo@gmail.com<mailto:awf.aoo@gmail.com>
Subject: Re: EXTERNAL: Re: DisposedException



On 24.02.2014 20:56, Steele, Raymond wrote:

> Thanks for responding. How can I determine what called dispose()?



If you want to know who calls dispose() then you have to place a breakpoint in the dispose()
method of xCalcComponent.



If you want to know when dispose() is called then register a com::sun::star::lang::XEventListener
at the xCalcComponent object. When it is disposed, it will call disposing(...) at the listener.

I am just not sure if it calls disposing() when the object is already disposed when the listener
is registered.  Probably not.



>   This is working in version AOO 3.3. Has something changed?



Without knowing the context this can easily be a timing problem in a multi-threaded environment:
in AOO 3.3 some operation took longer resulting in the dispose() call made a few milliseconds
later (or anything to this effect.)



-Andre



>

> -----Original Message-----

> From: Andre Fischer [mailto:awf.aoo@gmail.com]

> Sent: Monday, February 24, 2014 3:54 AM

> To: api@openoffice.apache.org<mailto:api@openoffice.apache.org>

> Subject: EXTERNAL: Re: DisposedException

>

> On 21.02.2014 19:20, Steele, Raymond wrote:

>> While running my application using AOO 4.0.1 on Solaris 11,  a star.lang.DisposedException
is thrown  inconsistently (sometimes it works) when I use UnoRuntime.queryInterface.

>>

>> The line of code causing the issue:

>>

>> XSpreadsheetDocument  document =

>> UnoRuntime.queryInterface(XSpreadsheetDocument.class,

>> xCalcCompinent);

> The DisposedException means that the throwing object has been marked by its owner for
destruction in the near future.  Any access after that results in the DisposedException being
thrown.

>

> That means, that the line above is executed after xCalcCompinent has received a dispose()
call.

>

> -Andre

>

>> This works perfectly when I use AOO 3.3, but not since upgrading to 4.0.1.

>>

>> Any help would be appreciated.

>>

>> Thanks,

>> Raymond

>>

>>

>

> ---------------------------------------------------------------------

> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org<mailto:api-unsubscribe@openoffice.apache.org>

> For additional commands, e-mail: api-help@openoffice.apache.org<mailto:api-help@openoffice.apache.org>

>





Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message