I'm using Aspose to handle PDFs and Word documents. Each time I'm about to do something with a document, I make sure to call this:
Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
pdfLicense.SetLicense("Aspose.Total.lic");
Aspose.Words.License wordLicense = new Aspose.Words.License();
wordLicense.SetLicense("Aspose.Total.lic");
The pdfLicense
and wordLicense
variables are never used anywhere, yet Aspose correctly recognises that I do have a valid license. How does this happen? Are the licenses being held in a secret singleton somewhere? If so, does this mean they last for the lifetime of the thread?
As this is being used in a web application, if I run the above code when the application starts up, can I then safely use Aspose throughout my application without worrying about the licensing?
At the moment I'd being more paranoid and running that code at the start of every method that uses Aspose. This works fine - my license is recognised correctly - but it's a bit too "programming-by-coincidence" for me to feel comfortable 开发者_如何学运维about it.
(I'm using C# with ASP.NET 3.5, if that makes any difference.)
If you read the product documentation, you will see this line:
You need to set a license before performing any operations with documents. It is only required to set a license once per application (or process).
Hence it is process-centric.
In the Java version of Aspose you can check if the license was set by calling
License.isLicenseSet();
which returns a boolean. Notice that this is a static method.
I tried creating a Spring bean that would do this (as shown below), but it did not work. Spring seemed to want to call License.setLicense(Reader) instead of License.setLicense(String). The error I get is Failed to convert property value of type 'java.lang.String' to required type 'java.io.Reader' for property 'license'.
<bean id="asposeLicense" class="com.aspose.cells.License">
<property name="license" value="Aspose.Cells.lic" />
</bean>
I did however get this more generic (Java) solution to work:
web.xml:
<!-- does things needing doing when application starts or stops -->
<listener>
<listener-class>
com.xyz.listener.ApplicationStartupListener
</listener-class>
</listener>
ApplicationStartupListener.java (new class):
package com.xyz.listener;
import java.io.InputStream;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.aspose.cells.License;
public class ApplicationStartupListener implements ServletContextListener {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("Initializing application context...");
try {
// set license for Aspose.Cells (the Excel API)
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("excel/Aspose.Cells.lic");
License license = new License();
license.setLicense(inputStream);
logger.info("Aspose.Cells license set? " + License.isLicenseSet());
} catch (Exception e) {
logger.error("Error encountered trying to set Aspose.Cells license!", e);
}
logger.info("Application context initialized");
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
精彩评论