package net.xoetrope.debug.selfhealing;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import net.xoetrope.xui.XProject;
/**
* Add the datasource.xml files if they are missing when required by an
* application
* <p>Copyright (c) Xoetrope Ltd., 2002-2007</p>
* <p>License: see license.txt</p>
*/
public class DataSourceClassMissing implements RepairOperation
{
/**
* Attempt to fix an error when running a XUI application
* @param project the current project
* @param sourceObj the instance of the class from which the error handler was
* invoked, or the object being repaired
* @param t throwable the exception that was trapped, or null
*/
public boolean fixError( XProject project, Object sourceObj, Throwable t )
{
final XProject theProject = project;
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
int rc = JOptionPane.showConfirmDialog( null, "The XDataSourceClass entry is missing from the startup file, do you want to add it now?",
"Correct the startup properties",
JOptionPane.YES_NO_OPTION );
if ( rc == JOptionPane.YES_OPTION ) {
theProject.setStartupParam( "XDataSourceClass", "net.xoetrope.optional.data.XOptionalDataSource" );
InputStream is = theProject.getBufferedInputStream( theProject.getStartupFile());
Properties startSettings = new Properties();
try {
startSettings.load( is );
startSettings.put( "XDataSourceClass", "net.xoetrope.optional.data.XOptionalDataSource" );
is.close();
URL startupFile = theProject.findResource( theProject.getStartupFile());
File outputFile = new File( startupFile.getFile());
BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( outputFile.toString() ));
startSettings.store( bos, "XUI startup properties, automatically modified by XUI at " + new java.util.Date().toString());
bos.flush();
bos.close();
}
catch ( Exception e ) {
e.printStackTrace();
JOptionPane.showMessageDialog( null, "An error occured while writing the modified startup file. Please add the following line to your startup file:"+
"\nXDataSourceClass=net.xoetrope.optional.data.XOptionalDataSource",
"Unable to repair the startup file", JOptionPane.ERROR_MESSAGE );
return;
}
JOptionPane.showMessageDialog( null, "Please restart your application. The following line was add to your startup file:"+
"\nXDataSourceClass=net.xoetrope.optional.data.XOptionalDataSource",
"Startup file modified", JOptionPane.INFORMATION_MESSAGE );
}
}
});
return false;
}
}